VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/threads.c@ 18326

Last change on this file since 18326 was 18326, checked in by vboxsync, 16 years ago

crOpenGL: tabs to spaces

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