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
|
---|
13 | extern "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 | */
|
---|
31 | typedef struct {
|
---|
32 | #ifdef WINDOWS
|
---|
33 | DWORD key;
|
---|
34 | #else
|
---|
35 | pthread_key_t key;
|
---|
36 | #endif
|
---|
37 | int initMagic;
|
---|
38 | } CRtsd;
|
---|
39 |
|
---|
40 |
|
---|
41 | extern DECLEXPORT(void) crInitTSD(CRtsd *tsd);
|
---|
42 | extern DECLEXPORT(void) crInitTSDF(CRtsd *tsd, void (*destructor)(void *));
|
---|
43 | extern DECLEXPORT(void) crFreeTSD(CRtsd *tsd);
|
---|
44 | extern DECLEXPORT(void) crSetTSD(CRtsd *tsd, void *ptr);
|
---|
45 | extern DECLEXPORT(void *) crGetTSD(CRtsd *tsd);
|
---|
46 | extern DECLEXPORT(unsigned long) crThreadID(void);
|
---|
47 |
|
---|
48 |
|
---|
49 | /* Mutex datatype */
|
---|
50 | #ifdef WINDOWS
|
---|
51 | typedef CRITICAL_SECTION CRmutex;
|
---|
52 | #else
|
---|
53 | typedef pthread_mutex_t CRmutex;
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | extern DECLEXPORT(void) crInitMutex(CRmutex *mutex);
|
---|
57 | extern DECLEXPORT(void) crFreeMutex(CRmutex *mutex);
|
---|
58 | extern DECLEXPORT(void) crLockMutex(CRmutex *mutex);
|
---|
59 | extern DECLEXPORT(void) crUnlockMutex(CRmutex *mutex);
|
---|
60 |
|
---|
61 |
|
---|
62 | /* Condition variable datatype */
|
---|
63 | #ifdef WINDOWS
|
---|
64 | typedef int CRcondition;
|
---|
65 | #else
|
---|
66 | typedef pthread_cond_t CRcondition;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | extern DECLEXPORT(void) crInitCondition(CRcondition *cond);
|
---|
70 | extern DECLEXPORT(void) crFreeCondition(CRcondition *cond);
|
---|
71 | extern DECLEXPORT(void) crWaitCondition(CRcondition *cond, CRmutex *mutex);
|
---|
72 | extern DECLEXPORT(void) crSignalCondition(CRcondition *cond);
|
---|
73 |
|
---|
74 |
|
---|
75 | /* Barrier datatype */
|
---|
76 | typedef 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 |
|
---|
87 | extern DECLEXPORT(void) crInitBarrier(CRbarrier *b, unsigned int count);
|
---|
88 | extern DECLEXPORT(void) crFreeBarrier(CRbarrier *b);
|
---|
89 | extern 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 |
|
---|
99 | extern DECLEXPORT(void) crInitSemaphore(CRsemaphore *s, unsigned int count);
|
---|
100 | extern DECLEXPORT(void) crWaitSemaphore(CRsemaphore *s);
|
---|
101 | extern DECLEXPORT(void) crSignalSemaphore(CRsemaphore *s);
|
---|
102 |
|
---|
103 | typedef DECLCALLBACK(void) FNCRTSDREFDTOR(void*);
|
---|
104 | typedef FNCRTSDREFDTOR *PFNCRTSDREFDTOR;
|
---|
105 |
|
---|
106 | typedef 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 */
|
---|