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 | #include <stdio.h>
|
---|
8 | #include "cr_threads.h"
|
---|
9 | #include "cr_error.h"
|
---|
10 |
|
---|
11 | /* perror() messages */
|
---|
12 | #define INIT_TSD_ERROR "InitTSD: failed to allocate key"
|
---|
13 | #define FREE_TSD_ERROR "FreeTSD: failed to destroy key"
|
---|
14 | #define SET_TSD_ERROR "InitTSD: thread failed to set thread specific data"
|
---|
15 | #define GET_TSD_ERROR "InitTSD: failed to get thread specific data"
|
---|
16 |
|
---|
17 | /* Magic number to determine if a CRtsd has been initialized */
|
---|
18 | #define INIT_MAGIC 0xff8adc98
|
---|
19 |
|
---|
20 |
|
---|
21 | /* Initialize a CRtsd */
|
---|
22 | void crInitTSDF(CRtsd *tsd, void (*destructor)(void *))
|
---|
23 | {
|
---|
24 | #ifdef WINDOWS
|
---|
25 | tsd->key = TlsAlloc();
|
---|
26 | if (tsd->key == 0xffffffff) {
|
---|
27 | crError("crInitTSD failed!");
|
---|
28 | }
|
---|
29 | (void) destructor;
|
---|
30 | #else
|
---|
31 | if (pthread_key_create(&tsd->key, destructor) != 0) {
|
---|
32 | perror(INIT_TSD_ERROR);
|
---|
33 | crError("crInitTSD failed!");
|
---|
34 | }
|
---|
35 | #endif
|
---|
36 | tsd->initMagic = INIT_MAGIC;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | void crInitTSD(CRtsd *tsd)
|
---|
41 | {
|
---|
42 | crInitTSDF(tsd, NULL);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | void crFreeTSD(CRtsd *tsd)
|
---|
47 | {
|
---|
48 | #ifdef WINDOWS
|
---|
49 | /* Windows returns true on success, 0 on failure */
|
---|
50 | if (TlsFree(tsd->key) == 0) {
|
---|
51 | crError("crFreeTSD failed!");
|
---|
52 | }
|
---|
53 | #else
|
---|
54 | if (pthread_key_delete(tsd->key) != 0)
|
---|
55 | {
|
---|
56 | // perror(FREE_TSD_ERROR);
|
---|
57 | // crError("crFreeTSD failed!");
|
---|
58 | }
|
---|
59 | #endif
|
---|
60 | tsd->initMagic = 0x0;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /* Set thread-specific data */
|
---|
65 | void crSetTSD(CRtsd *tsd, void *ptr)
|
---|
66 | {
|
---|
67 | if (tsd->initMagic != (int) INIT_MAGIC) {
|
---|
68 | /* initialize this CRtsd */
|
---|
69 | crInitTSD(tsd);
|
---|
70 | }
|
---|
71 | #ifdef WINDOWS
|
---|
72 | if (TlsSetValue(tsd->key, ptr) == 0) {
|
---|
73 | crError("crSetTSD failed!");
|
---|
74 | }
|
---|
75 | #else
|
---|
76 | if (pthread_setspecific(tsd->key, ptr) != 0) {
|
---|
77 | crError("crSetTSD failed!");
|
---|
78 | }
|
---|
79 | #endif
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /* Get thread-specific data */
|
---|
84 | void *crGetTSD(CRtsd *tsd)
|
---|
85 | {
|
---|
86 | #ifdef WINDOWS
|
---|
87 | void * value;
|
---|
88 | DWORD err;
|
---|
89 | LPVOID lpMsgBuf;
|
---|
90 | #endif
|
---|
91 | if (tsd->initMagic != (int) INIT_MAGIC) {
|
---|
92 | crInitTSD(tsd);
|
---|
93 | }
|
---|
94 | #ifdef WINDOWS
|
---|
95 | value = TlsGetValue(tsd->key);
|
---|
96 | if (!value) {
|
---|
97 | err = GetLastError();
|
---|
98 | if ( err != ERROR_SUCCESS ) {
|
---|
99 | FormatMessage(
|
---|
100 | FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
101 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
102 | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
103 | NULL,
|
---|
104 | err,
|
---|
105 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
106 | (LPTSTR) &lpMsgBuf,
|
---|
107 | 0, NULL );
|
---|
108 | crError("crGetTSD failed with %d: %s", err, lpMsgBuf);
|
---|
109 | LocalFree(lpMsgBuf);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return value;
|
---|
113 | #else
|
---|
114 | return pthread_getspecific(tsd->key);
|
---|
115 | #endif
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | /* Return ID of calling thread */
|
---|
121 | unsigned long crThreadID(void)
|
---|
122 | {
|
---|
123 | #ifdef WINDOWS
|
---|
124 | return (unsigned long) GetCurrentThreadId();
|
---|
125 | #else
|
---|
126 | return (unsigned long) pthread_self();
|
---|
127 | #endif
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | void crInitMutex(CRmutex *mutex)
|
---|
133 | {
|
---|
134 | #ifdef WINDOWS
|
---|
135 | InitializeCriticalSection(mutex);
|
---|
136 | #else
|
---|
137 | pthread_mutexattr_t mta;
|
---|
138 | int rc;
|
---|
139 |
|
---|
140 | rc = pthread_mutexattr_init(&mta);
|
---|
141 | CRASSERT(!rc);
|
---|
142 | rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
|
---|
143 | CRASSERT(!rc);
|
---|
144 | rc = pthread_mutex_init(mutex, &mta);
|
---|
145 | CRASSERT(!rc);
|
---|
146 | pthread_mutexattr_destroy(&mta);
|
---|
147 | #endif
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void crFreeMutex(CRmutex *mutex)
|
---|
152 | {
|
---|
153 | #ifdef WINDOWS
|
---|
154 | DeleteCriticalSection(mutex);
|
---|
155 | #else
|
---|
156 | pthread_mutex_destroy(mutex);
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | void crLockMutex(CRmutex *mutex)
|
---|
162 | {
|
---|
163 | #ifdef WINDOWS
|
---|
164 | EnterCriticalSection(mutex);
|
---|
165 | #else
|
---|
166 | pthread_mutex_lock(mutex);
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void crUnlockMutex(CRmutex *mutex)
|
---|
172 | {
|
---|
173 | #ifdef WINDOWS
|
---|
174 | LeaveCriticalSection(mutex);
|
---|
175 | #else
|
---|
176 | pthread_mutex_unlock(mutex);
|
---|
177 | #endif
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | void crInitCondition(CRcondition *cond)
|
---|
182 | {
|
---|
183 | #ifdef WINDOWS
|
---|
184 | /* XXX fix me */
|
---|
185 | (void) cond;
|
---|
186 | #else
|
---|
187 | int err = pthread_cond_init(cond, NULL);
|
---|
188 | if (err) {
|
---|
189 | crError("crInitCondition failed");
|
---|
190 | }
|
---|
191 | #endif
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | void crFreeCondition(CRcondition *cond)
|
---|
196 | {
|
---|
197 | #ifdef WINDOWS
|
---|
198 | /* XXX fix me */
|
---|
199 | (void) cond;
|
---|
200 | #else
|
---|
201 | int err = pthread_cond_destroy(cond);
|
---|
202 | if (err) {
|
---|
203 | crError("crFreeCondition error (threads waiting on the condition?)");
|
---|
204 | }
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * We're basically just wrapping the pthread condition var interface.
|
---|
210 | * See the man page for pthread_cond_wait to learn about the mutex parameter.
|
---|
211 | */
|
---|
212 | void crWaitCondition(CRcondition *cond, CRmutex *mutex)
|
---|
213 | {
|
---|
214 | #ifdef WINDOWS
|
---|
215 | /* XXX fix me */
|
---|
216 | (void) cond;
|
---|
217 | (void) mutex;
|
---|
218 | #else
|
---|
219 | pthread_cond_wait(cond, mutex);
|
---|
220 | #endif
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | void crSignalCondition(CRcondition *cond)
|
---|
225 | {
|
---|
226 | #ifdef WINDOWS
|
---|
227 | /* XXX fix me */
|
---|
228 | (void) cond;
|
---|
229 | #else
|
---|
230 | pthread_cond_signal(cond);
|
---|
231 | #endif
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | void crInitBarrier(CRbarrier *b, unsigned int count)
|
---|
236 | {
|
---|
237 | #ifdef WINDOWS
|
---|
238 | unsigned int i;
|
---|
239 | for (i = 0; i < count; i++)
|
---|
240 | b->hEvents[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
241 | #else
|
---|
242 | b->count = count;
|
---|
243 | b->waiting = 0;
|
---|
244 | pthread_cond_init( &(b->cond), NULL );
|
---|
245 | pthread_mutex_init( &(b->mutex), NULL );
|
---|
246 | #endif
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | void crFreeBarrier(CRbarrier *b)
|
---|
251 | {
|
---|
252 | /* XXX anything to do? */
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | void crWaitBarrier(CRbarrier *b)
|
---|
257 | {
|
---|
258 | #ifdef WINDOWS
|
---|
259 | DWORD dwEvent
|
---|
260 | = WaitForMultipleObjects( b->count, b->hEvents, FALSE, INFINITE );
|
---|
261 | #else
|
---|
262 | pthread_mutex_lock( &(b->mutex) );
|
---|
263 | b->waiting++;
|
---|
264 | if (b->waiting < b->count) {
|
---|
265 | pthread_cond_wait( &(b->cond), &(b->mutex) );
|
---|
266 | }
|
---|
267 | else {
|
---|
268 | pthread_cond_broadcast( &(b->cond) );
|
---|
269 | b->waiting = 0;
|
---|
270 | }
|
---|
271 | pthread_mutex_unlock( &(b->mutex) );
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | void crInitSemaphore(CRsemaphore *s, unsigned int count)
|
---|
277 | {
|
---|
278 | #ifdef WINDOWS
|
---|
279 | crWarning("CRsemaphore functions not implemented on Windows");
|
---|
280 | #else
|
---|
281 | sem_init(s, 0, count);
|
---|
282 | #endif
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | void crWaitSemaphore(CRsemaphore *s)
|
---|
287 | {
|
---|
288 | #ifdef WINDOWS
|
---|
289 | /* to do */
|
---|
290 | #else
|
---|
291 | sem_wait(s);
|
---|
292 | #endif
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | void crSignalSemaphore(CRsemaphore *s)
|
---|
297 | {
|
---|
298 | #ifdef WINDOWS
|
---|
299 | /* to do */
|
---|
300 | #else
|
---|
301 | sem_post(s);
|
---|
302 | #endif
|
---|
303 | }
|
---|
304 |
|
---|