summaryrefslogtreecommitdiffstats
path: root/src/VBox/GuestHost/OpenGL/util/threads.c
blob: 5502fd7c573edaf6cdcd3fe948bcf38d25242648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Copyright (c) 2001, Stanford University
 * All rights reserved.
 *
 * See the file LICENSE.txt for information on redistributing this software.
 */
    
#include <stdio.h>
#include "cr_threads.h"
#include "cr_error.h"

/* perror() messages */
#define INIT_TSD_ERROR "InitTSD: failed to allocate key"
#define FREE_TSD_ERROR "FreeTSD: failed to destroy key"
#define SET_TSD_ERROR "InitTSD: thread failed to set thread specific data"
#define GET_TSD_ERROR "InitTSD: failed to get thread specific data"

/* Magic number to determine if a CRtsd has been initialized */
#define INIT_MAGIC 0xff8adc98


/* Initialize a CRtsd */
void crInitTSDF(CRtsd *tsd, void (*destructor)(void *))
{
#ifdef WINDOWS
    tsd->key = TlsAlloc();
    if (tsd->key == 0xffffffff) {
        crError("crInitTSD failed!");
    }
    (void) destructor;
#else
    if (pthread_key_create(&tsd->key, destructor) != 0)
        crDebug("crServer: failed to allocate TLS key.");
    else
        crDebug("crServer: TLS key %d allocated.", tsd->key);
#endif
    tsd->initMagic = INIT_MAGIC;
}


void crInitTSD(CRtsd *tsd)
{
    crInitTSDF(tsd, NULL);
}


void crFreeTSD(CRtsd *tsd)
{
#ifdef WINDOWS
    /* Windows returns true on success, 0 on failure */
    if (TlsFree(tsd->key) == 0) {
        crError("crFreeTSD failed!");
    }
#else
    if (pthread_key_delete(tsd->key) != 0)
        crDebug("crServer: failed to delete TLS key %d.", tsd->key);
    else
        crDebug("crServer: TLS key %d deleted.", tsd->key);
#endif
    tsd->initMagic = 0x0;
}


/* Set thread-specific data */
void crSetTSD(CRtsd *tsd, void *ptr)
{
    if (tsd->initMagic != (int) INIT_MAGIC) {
        /* initialize this CRtsd */
        crInitTSD(tsd);
    }
#ifdef WINDOWS
    if (TlsSetValue(tsd->key, ptr) == 0) {
        crError("crSetTSD failed!");
    }
#else
    if (pthread_setspecific(tsd->key, ptr) != 0) {
        crError("crSetTSD failed!");
    }
#endif
}


/* Get thread-specific data */
void *crGetTSD(CRtsd *tsd)
{
#ifdef WINDOWS
    void * value;
    DWORD  err;
    LPVOID lpMsgBuf;
#endif
    if (tsd->initMagic != (int) INIT_MAGIC) {
        crInitTSD(tsd);
    }
#ifdef WINDOWS
    value = TlsGetValue(tsd->key);
    if (!value) {
        err = GetLastError();
        if ( err != ERROR_SUCCESS ) {
            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                err,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) &lpMsgBuf,
                0, NULL );
            crError("crGetTSD failed with %d: %s", err, lpMsgBuf);
            LocalFree(lpMsgBuf);
        }
    }
    return value;
#else
    return pthread_getspecific(tsd->key);
#endif
}



/* Return ID of calling thread */
unsigned long crThreadID(void)
{
#ifdef WINDOWS
    return (unsigned long) GetCurrentThreadId();
#else
    return (unsigned long) pthread_self();
#endif
}



void crInitMutex(CRmutex *mutex)
{
#ifdef WINDOWS
    InitializeCriticalSection(mutex);
#else
    pthread_mutexattr_t mta;
    int rc;

    rc = pthread_mutexattr_init(&mta);
    CRASSERT(!rc);
    rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
    CRASSERT(!rc);
    rc = pthread_mutex_init(mutex, &mta);
    CRASSERT(!rc);
    pthread_mutexattr_destroy(&mta);
#endif
}


void crFreeMutex(CRmutex *mutex)
{
#ifdef WINDOWS
    DeleteCriticalSection(mutex);
#else
    pthread_mutex_destroy(mutex);
#endif
}


void crLockMutex(CRmutex *mutex)
{
#ifdef WINDOWS
    EnterCriticalSection(mutex);
#else
    pthread_mutex_lock(mutex);
#endif
}


void crUnlockMutex(CRmutex *mutex)
{
#ifdef WINDOWS
    LeaveCriticalSection(mutex);
#else
    pthread_mutex_unlock(mutex);
#endif
}


void crInitCondition(CRcondition *cond)
{
#ifdef WINDOWS
    /* XXX fix me */
    (void) cond;
#else
    int err = pthread_cond_init(cond, NULL);
    if (err) {
        crError("crInitCondition failed");
    }
#endif
}


void crFreeCondition(CRcondition *cond)
{
#ifdef WINDOWS
    /* XXX fix me */
    (void) cond;
#else
    int err = pthread_cond_destroy(cond);
    if (err) {
        crError("crFreeCondition error (threads waiting on the condition?)");
    }
#endif
}

/**
 * We're basically just wrapping the pthread condition var interface.
 * See the man page for pthread_cond_wait to learn about the mutex parameter.
 */
void crWaitCondition(CRcondition *cond, CRmutex *mutex)
{
#ifdef WINDOWS
    /* XXX fix me */
    (void) cond;
    (void) mutex;
#else
    pthread_cond_wait(cond, mutex);
#endif
}


void crSignalCondition(CRcondition *cond)
{
#ifdef WINDOWS
    /* XXX fix me */
    (void) cond;
#else
    pthread_cond_signal(cond);
#endif
}


void crInitBarrier(CRbarrier *b, unsigned int count)
{
#ifdef WINDOWS
    unsigned int i;
    for (i = 0; i < count; i++)
        b->hEvents[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
#else
    b->count = count;
    b->waiting = 0;
    pthread_cond_init( &(b->cond), NULL );
    pthread_mutex_init( &(b->mutex), NULL );
#endif
}


void crFreeBarrier(CRbarrier *b)
{
    /* XXX anything to do? */
    (void)b;
}


void crWaitBarrier(CRbarrier *b)
{
#ifdef WINDOWS
    DWORD dwEvent
        = WaitForMultipleObjects( b->count, b->hEvents, FALSE, INFINITE );
    (void)dwEvent;
#else
    pthread_mutex_lock( &(b->mutex) );
    b->waiting++;
    if (b->waiting < b->count) {
        pthread_cond_wait( &(b->cond), &(b->mutex) );
    }
    else {
        pthread_cond_broadcast( &(b->cond) );
        b->waiting = 0;
    }
    pthread_mutex_unlock( &(b->mutex) );
#endif
}


void crInitSemaphore(CRsemaphore *s, unsigned int count)
{
#ifdef WINDOWS
    (void) s; (void) count;
    crWarning("CRsemaphore functions not implemented on Windows");
#else
    sem_init(s, 0, count);
#endif
}


void crWaitSemaphore(CRsemaphore *s)
{
#ifdef WINDOWS
    /* to do */
    (void) s;
#else
    sem_wait(s);
#endif
}


void crSignalSemaphore(CRsemaphore *s)
{
#ifdef WINDOWS
    /* to do */
    (void) s;
#else
    sem_post(s);
#endif
}