Moved PTHREAD_init_done out of the #ifdef.
[wine/multimedia.git] / scheduler / pthread.c
blobfb1755586d2834be20fc79c7b1a5c256775a401e
1 /*
2 * pthread emulation for re-entrant libcs
4 * We can't use pthreads directly, so why not let libcs
5 * that wants pthreads use Wine's own threading instead...
7 * Copyright 1999 Ove Kåven
8 */
10 #include "config.h"
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
13 #include <assert.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <unistd.h>
18 #include "winbase.h"
19 #include "thread.h"
21 static int init_done;
23 void PTHREAD_init_done(void)
25 init_done = 1;
28 /* Currently this probably works only for glibc2,
29 * which checks for the presence of double-underscore-prepended
30 * pthread primitives, and use them if available.
31 * If they are not available, the libc defaults to
32 * non-threadsafe operation (not good). */
34 #if defined(__GLIBC__)
35 #include <pthread.h>
36 #include <signal.h>
38 #ifdef NEED_UNDERSCORE_PREFIX
39 # define PREFIX "_"
40 #else
41 # define PREFIX
42 #endif
44 #define PSTR(str) PREFIX #str
46 /* adapt as necessary (a construct like this is used in glibc sources) */
47 #define strong_alias(orig, alias) \
48 asm(".globl " PSTR(alias) "\n" \
49 "\t.set " PSTR(alias) "," PSTR(orig))
51 /* strong_alias does not work on external symbols (.o format limitation?),
52 * so for those, we need to use the pogo stick */
53 #if defined(__i386__) && !defined(__PIC__)
54 /* FIXME: PIC */
55 #define jump_alias(orig, alias) \
56 asm(".globl " PSTR(alias) "\n" \
57 "\t.type " PSTR(alias) ",@function\n" \
58 PSTR(alias) ":\n" \
59 "\tjmp " PSTR(orig))
60 #endif
62 /* get necessary libc symbols */
63 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
64 #define LIBC_FORK __libc_fork
65 #define PTHREAD_FORK __fork
66 #define ALIAS_FORK
67 #else
68 #define LIBC_FORK __fork
69 #define PTHREAD_FORK fork
70 #endif
71 extern pid_t LIBC_FORK(void);
73 #define LIBC_SIGACTION __sigaction
75 /* NOTE: This is a truly extremely incredibly ugly hack!
76 * But it does seem to work... */
78 /* assume that pthread_mutex_t has room for at least one pointer,
79 * and hope that the users of pthread_mutex_t considers it opaque
80 * (never checks what's in it) */
81 typedef struct {
82 CRITICAL_SECTION *critsect;
83 } *wine_mutex;
85 typedef struct _wine_cleanup {
86 void (*routine)(void *);
87 void *arg;
88 } *wine_cleanup;
90 typedef const void *key_data;
92 #define FIRST_KEY 0
93 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
95 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
97 void __pthread_initialize(void)
101 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
103 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
104 LONG once_now = *(LONG *)&the_once;
106 if (InterlockedCompareExchange((PVOID*)once_control, (PVOID)(once_now+1), (PVOID)once_now) == (PVOID)once_now)
107 (*init_routine)();
108 return 0;
110 strong_alias(__pthread_once, pthread_once);
112 void __pthread_kill_other_threads_np(void)
114 /* FIXME: this is supposed to be preparation for exec() */
115 if (init_done) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
117 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
119 /***** atfork *****/
121 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
123 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT;
124 typedef void (*atfork_handler)();
125 static atfork_handler atfork_prepare[MAX_ATFORK];
126 static atfork_handler atfork_parent[MAX_ATFORK];
127 static atfork_handler atfork_child[MAX_ATFORK];
128 static int atfork_count;
130 int __pthread_atfork(void (*prepare)(void),
131 void (*parent)(void),
132 void (*child)(void))
134 if (init_done) EnterCriticalSection( &atfork_section );
135 assert( atfork_count < MAX_ATFORK );
136 atfork_prepare[atfork_count] = prepare;
137 atfork_parent[atfork_count] = parent;
138 atfork_child[atfork_count] = child;
139 atfork_count++;
140 if (init_done) LeaveCriticalSection( &atfork_section );
141 return 0;
143 strong_alias(__pthread_atfork, pthread_atfork);
145 pid_t PTHREAD_FORK(void)
147 pid_t pid;
148 int i;
150 EnterCriticalSection( &atfork_section );
151 /* prepare handlers are called in reverse insertion order */
152 for (i = atfork_count - 1; i >= 0; i--) atfork_prepare[i]();
153 if (!(pid = LIBC_FORK()))
155 InitializeCriticalSection( &atfork_section );
156 for (i = 0; i < atfork_count; i++) atfork_child[i]();
158 else
160 for (i = 0; i < atfork_count; i++) atfork_parent[i]();
161 LeaveCriticalSection( &atfork_section );
163 return pid;
165 #ifdef ALIAS_FORK
166 strong_alias(PTHREAD_FORK, fork);
167 #endif
169 /***** MUTEXES *****/
171 int __pthread_mutex_init(pthread_mutex_t *mutex,
172 const pthread_mutexattr_t *mutexattr)
174 /* glibc has a tendency to initialize mutexes very often, even
175 in situations where they are not really used later on.
177 As for us, initializing a mutex is very expensive, we postpone
178 the real initialization until the time the mutex is first used. */
180 ((wine_mutex)mutex)->critsect = NULL;
181 return 0;
183 strong_alias(__pthread_mutex_init, pthread_mutex_init);
185 static void mutex_real_init( pthread_mutex_t *mutex )
187 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
188 InitializeCriticalSection(critsect);
190 if (InterlockedCompareExchange((PVOID*)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
191 /* too late, some other thread already did it */
192 DeleteCriticalSection(critsect);
193 HeapFree(GetProcessHeap(), 0, critsect);
197 int __pthread_mutex_lock(pthread_mutex_t *mutex)
199 if (!init_done) return 0;
200 if (!((wine_mutex)mutex)->critsect)
201 mutex_real_init( mutex );
203 EnterCriticalSection(((wine_mutex)mutex)->critsect);
204 return 0;
206 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
208 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
210 if (!init_done) return 0;
211 if (!((wine_mutex)mutex)->critsect)
212 mutex_real_init( mutex );
214 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
215 errno = EBUSY;
216 return -1;
218 return 0;
220 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
222 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
224 if (!((wine_mutex)mutex)->critsect) return 0;
225 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
226 return 0;
228 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
230 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
232 if (!((wine_mutex)mutex)->critsect) return 0;
233 if (((wine_mutex)mutex)->critsect->RecursionCount) {
234 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
235 return EBUSY;
236 #else
237 while (((wine_mutex)mutex)->critsect->RecursionCount)
238 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
239 #endif
241 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
242 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
243 return 0;
245 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
248 /***** MUTEX ATTRIBUTES *****/
249 /* just dummies, since critical sections are always recursive */
251 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
253 return 0;
255 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
257 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
259 return 0;
261 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
263 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
265 return 0;
267 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
269 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
271 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
272 return 0;
274 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
276 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
278 return 0;
280 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
282 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
284 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
285 return 0;
287 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
290 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
292 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
294 static LONG keycnt = FIRST_KEY;
295 *key = InterlockedExchangeAdd(&keycnt, 1);
296 return 0;
298 strong_alias(__pthread_key_create, pthread_key_create);
300 int __pthread_key_delete(pthread_key_t key)
302 return 0;
304 strong_alias(__pthread_key_delete, pthread_key_delete);
306 int __pthread_setspecific(pthread_key_t key, const void *pointer)
308 TEB *teb = NtCurrentTeb();
309 if (!teb->pthread_data) {
310 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
312 ((key_data*)(teb->pthread_data))[key] = pointer;
313 return 0;
315 strong_alias(__pthread_setspecific, pthread_setspecific);
317 void *__pthread_getspecific(pthread_key_t key)
319 TEB *teb = NtCurrentTeb();
320 if (!teb) return NULL;
321 if (!teb->pthread_data) return NULL;
322 return (void *)(((key_data*)(teb->pthread_data))[key]);
324 strong_alias(__pthread_getspecific, pthread_getspecific);
327 /***** "EXCEPTION" FRAMES *****/
328 /* not implemented right now */
330 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
332 ((wine_cleanup)buffer)->routine = routine;
333 ((wine_cleanup)buffer)->arg = arg;
336 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
338 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
341 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
343 _pthread_cleanup_push(buffer, routine, arg);
346 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
348 _pthread_cleanup_pop(buffer, execute);
352 /***** CONDITIONS *****/
353 /* not implemented right now */
355 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
357 P_OUTPUT("FIXME:pthread_cond_init\n");
358 return 0;
361 int pthread_cond_destroy(pthread_cond_t *cond)
363 P_OUTPUT("FIXME:pthread_cond_destroy\n");
364 return 0;
367 int pthread_cond_signal(pthread_cond_t *cond)
369 P_OUTPUT("FIXME:pthread_cond_signal\n");
370 return 0;
373 int pthread_cond_broadcast(pthread_cond_t *cond)
375 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
376 return 0;
379 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
381 P_OUTPUT("FIXME:pthread_cond_wait\n");
382 return 0;
385 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
387 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
388 return 0;
391 /**** CONDITION ATTRIBUTES *****/
392 /* not implemented right now */
394 int pthread_condattr_init(pthread_condattr_t *attr)
396 return 0;
399 int pthread_condattr_destroy(pthread_condattr_t *attr)
401 return 0;
404 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
405 /***** READ-WRITE LOCKS *****/
406 /* not implemented right now */
408 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
410 P_OUTPUT("FIXME:pthread_rwlock_init\n");
411 return 0;
413 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
415 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
417 P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
418 return 0;
420 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
422 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
424 P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
425 return 0;
427 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
429 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
431 P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
432 return 0;
434 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
436 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
438 P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
439 return 0;
441 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
443 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
445 P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
446 return 0;
448 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
450 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
452 P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
453 return 0;
455 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
457 /**** READ-WRITE LOCK ATTRIBUTES *****/
458 /* not implemented right now */
460 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
462 return 0;
465 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
467 return 0;
469 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
471 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
473 *pref = 0;
474 return 0;
477 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
479 return 0;
481 #endif /* glibc 2.2 */
483 /***** MISC *****/
485 pthread_t pthread_self(void)
487 return (pthread_t)GetCurrentThreadId();
490 int pthread_equal(pthread_t thread1, pthread_t thread2)
492 return (DWORD)thread1 == (DWORD)thread2;
495 void pthread_exit(void *retval)
497 /* FIXME: pthread cleanup */
498 ExitThread((DWORD)retval);
501 int pthread_setcanceltype(int type, int *oldtype)
503 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
504 return 0;
507 /***** ANTI-OVERRIDES *****/
508 /* pthreads tries to override these, point them back to libc */
510 #ifdef jump_alias
511 jump_alias(LIBC_SIGACTION, sigaction);
512 #else
513 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
515 return LIBC_SIGACTION(signum, act, oldact);
517 #endif
519 #endif /* __GLIBC__ */