VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/include/cr_threads.h@ 39568

Last change on this file since 39568 was 39568, checked in by vboxsync, 13 years ago

crOpenGL: more threading fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved.
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#ifndef CR_THREADS_H
8#define CR_THREADS_H
9
10#include <iprt/cdefs.h>
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#include "chromium.h"
17#include "cr_bits.h"
18
19#ifdef WINDOWS
20#define WIN32_LEAN_AND_MEAN
21#include <windows.h>
22#else
23#include <pthread.h>
24#include <semaphore.h>
25#endif
26
27#include <iprt/asm.h>
28/*
29 * Handle for Thread-Specific Data
30 */
31typedef struct {
32#ifdef WINDOWS
33 DWORD key;
34#else
35 pthread_key_t key;
36#endif
37 int initMagic;
38} CRtsd;
39
40
41extern DECLEXPORT(void) crInitTSD(CRtsd *tsd);
42extern DECLEXPORT(void) crInitTSDF(CRtsd *tsd, void (*destructor)(void *));
43extern DECLEXPORT(void) crFreeTSD(CRtsd *tsd);
44extern DECLEXPORT(void) crSetTSD(CRtsd *tsd, void *ptr);
45extern DECLEXPORT(void *) crGetTSD(CRtsd *tsd);
46extern DECLEXPORT(unsigned long) crThreadID(void);
47
48
49/* Mutex datatype */
50#ifdef WINDOWS
51typedef CRITICAL_SECTION CRmutex;
52#else
53typedef pthread_mutex_t CRmutex;
54#endif
55
56extern DECLEXPORT(void) crInitMutex(CRmutex *mutex);
57extern DECLEXPORT(void) crFreeMutex(CRmutex *mutex);
58extern DECLEXPORT(void) crLockMutex(CRmutex *mutex);
59extern DECLEXPORT(void) crUnlockMutex(CRmutex *mutex);
60
61
62/* Condition variable datatype */
63#ifdef WINDOWS
64typedef int CRcondition;
65#else
66typedef pthread_cond_t CRcondition;
67#endif
68
69extern DECLEXPORT(void) crInitCondition(CRcondition *cond);
70extern DECLEXPORT(void) crFreeCondition(CRcondition *cond);
71extern DECLEXPORT(void) crWaitCondition(CRcondition *cond, CRmutex *mutex);
72extern DECLEXPORT(void) crSignalCondition(CRcondition *cond);
73
74
75/* Barrier datatype */
76typedef struct {
77 unsigned int count;
78#ifdef WINDOWS
79 HANDLE hEvents[CR_MAX_CONTEXTS];
80#else
81 unsigned int waiting;
82 pthread_cond_t cond;
83 pthread_mutex_t mutex;
84#endif
85} CRbarrier;
86
87extern DECLEXPORT(void) crInitBarrier(CRbarrier *b, unsigned int count);
88extern DECLEXPORT(void) crFreeBarrier(CRbarrier *b);
89extern DECLEXPORT(void) crWaitBarrier(CRbarrier *b);
90
91
92/* Semaphores */
93#ifdef WINDOWS
94 typedef int CRsemaphore;
95#else
96 typedef sem_t CRsemaphore;
97#endif
98
99extern DECLEXPORT(void) crInitSemaphore(CRsemaphore *s, unsigned int count);
100extern DECLEXPORT(void) crWaitSemaphore(CRsemaphore *s);
101extern DECLEXPORT(void) crSignalSemaphore(CRsemaphore *s);
102
103typedef DECLCALLBACK(void) FNCRTSDREFDTOR(void*);
104typedef FNCRTSDREFDTOR *PFNCRTSDREFDTOR;
105
106typedef enum {
107 CRTSDREFDATA_STATE_UNDEFINED = 0,
108 CRTSDREFDATA_STATE_INITIALIZED,
109 CRTSDREFDATA_STATE_TOBE_DESTROYED,
110 CRTSDREFDATA_STATE_DESTROYING,
111 CRTSDREFDATA_STATE_32BIT_HACK = 0x7fffffff
112} CRTSDREFDATA_STATE;
113
114#define CRTSDREFDATA \
115 volatile uint32_t cTsdRefs; \
116 uint32_t enmTsdRefState; \
117 PFNCRTSDREFDTOR pfnTsdRefDtor; \
118
119#define crTSDRefInit(_p, _pfnDtor) do { \
120 (_p)->cTsdRefs = 1; \
121 (_p)->enmTsdRefState = CRTSDREFDATA_STATE_INITIALIZED; \
122 (_p)->pfnTsdRefDtor = (_pfnDtor); \
123 } while (0)
124
125#define crTSDRefIsFunctional(_p) (!!((_p)->enmTsdRefState == CRTSDREFDATA_STATE_INITIALIZED))
126
127#define crTSDRefAddRef(_p) do { \
128 int cRefs = ASMAtomicIncS32(&(_p)->cTsdRefs); \
129 CRASSERT(cRefs > 1 || (_p)->enmTsdRefState == CRTSDREFDATA_STATE_DESTROYING); \
130 } while (0)
131
132#define crTSDRefRelease(_p) do { \
133 int cRefs = ASMAtomicDecS32(&(_p)->cTsdRefs); \
134 CRASSERT(cRefs >= 0); \
135 if (!cRefs && (_p)->enmTsdRefState != CRTSDREFDATA_STATE_DESTROYING /* <- avoid recursion if crTSDRefAddRef/Release is called from dtor */) { \
136 (_p)->enmTsdRefState = CRTSDREFDATA_STATE_DESTROYING; \
137 (_p)->pfnTsdRefDtor((_p)); \
138 } \
139 } while (0)
140
141#define crTSDRefReleaseMarkDestroy(_p) do { \
142 (_p)->enmTsdRefState = CRTSDREFDATA_STATE_TOBE_DESTROYED; \
143 } while (0)
144
145#define crTSDRefGetCurrent(_t, _pTsd) ((_t*) crGetTSD((_pTsd)))
146
147#define crTSDRefSetCurrent(_t, _pTsd, _p) do { \
148 _t * oldCur = crTSDRefGetCurrent(_t, _pTsd); \
149 if (oldCur != (_p)) { \
150 crSetTSD((_pTsd), (_p)); \
151 if (oldCur) { \
152 crTSDRefRelease(oldCur); \
153 } \
154 if ((_p)) { \
155 crTSDRefAddRef((_t*)(_p)); \
156 } \
157 } \
158 } while (0)
159#ifdef __cplusplus
160}
161#endif
162
163#endif /* CR_THREADS_H */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette