2 * pthread emulation for re-entrant libcs
4 * We can't use pthreads directly, so why not let libcs
5 * that want pthreads use Wine's own threading instead...
7 * Copyright 1999 Ove Kåven
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
24 void PTHREAD_init_done(void)
29 /* Currently this probably works only for glibc2,
30 * which checks for the presence of double-underscore-prepended
31 * pthread primitives, and use them if available.
32 * If they are not available, the libc defaults to
33 * non-threadsafe operation (not good). */
35 #if defined(__GLIBC__)
39 #ifdef NEED_UNDERSCORE_PREFIX
45 #define PSTR(str) PREFIX #str
47 /* adapt as necessary (a construct like this is used in glibc sources) */
48 #define strong_alias(orig, alias) \
49 asm(".globl " PSTR(alias) "\n" \
50 "\t.set " PSTR(alias) "," PSTR(orig))
52 /* strong_alias does not work on external symbols (.o format limitation?),
53 * so for those, we need to use the pogo stick */
54 #if defined(__i386__) && !defined(__PIC__)
56 #define jump_alias(orig, alias) \
57 asm(".globl " PSTR(alias) "\n" \
58 "\t.type " PSTR(alias) ",@function\n" \
63 /* get necessary libc symbols */
64 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
65 #define LIBC_FORK __libc_fork
66 #define PTHREAD_FORK __fork
69 #define LIBC_FORK __fork
70 #define PTHREAD_FORK fork
72 extern pid_t LIBC_FORK(void);
74 #define LIBC_SIGACTION __sigaction
76 /* NOTE: This is a truly extremely incredibly ugly hack!
77 * But it does seem to work... */
79 /* assume that pthread_mutex_t has room for at least one pointer,
80 * and hope that the users of pthread_mutex_t considers it opaque
81 * (never checks what's in it) */
83 CRITICAL_SECTION *critsect;
86 typedef struct _wine_cleanup {
87 void (*routine)(void *);
91 typedef const void *key_data;
94 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
96 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
98 void __pthread_initialize(void)
102 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
104 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
105 LONG once_now = *(LONG *)&the_once;
107 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
111 strong_alias(__pthread_once, pthread_once);
113 void __pthread_kill_other_threads_np(void)
115 /* we don't need to do anything here */
117 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
121 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
123 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
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),
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;
140 if (init_done) LeaveCriticalSection( &atfork_section );
143 strong_alias(__pthread_atfork, pthread_atfork);
145 pid_t PTHREAD_FORK(void)
150 EnterCriticalSection( &atfork_section );
151 /* prepare handlers are called in reverse insertion order */
152 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
153 if (!(pid = LIBC_FORK()))
155 InitializeCriticalSection( &atfork_section );
156 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
160 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
161 LeaveCriticalSection( &atfork_section );
166 strong_alias(PTHREAD_FORK, fork);
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;
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 (InterlockedCompareExchangePointer((void**)&(((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);
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)) {
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);
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 */
237 while (((wine_mutex)mutex)->critsect->RecursionCount)
238 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
241 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
242 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
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)
255 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
257 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
261 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
263 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
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;
274 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
276 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
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;
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);
298 strong_alias(__pthread_key_create, pthread_key_create);
300 int __pthread_key_delete(pthread_key_t key)
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;
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");
361 int pthread_cond_destroy(pthread_cond_t *cond)
363 P_OUTPUT("FIXME:pthread_cond_destroy\n");
367 int pthread_cond_signal(pthread_cond_t *cond)
369 P_OUTPUT("FIXME:pthread_cond_signal\n");
373 int pthread_cond_broadcast(pthread_cond_t *cond)
375 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
379 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
381 P_OUTPUT("FIXME:pthread_cond_wait\n");
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");
391 /**** CONDITION ATTRIBUTES *****/
392 /* not implemented right now */
394 int pthread_condattr_init(pthread_condattr_t *attr)
399 int pthread_condattr_destroy(pthread_condattr_t *attr)
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");
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");
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");
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");
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");
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");
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");
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)
465 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
469 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
471 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
477 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
481 #endif /* glibc 2.2 */
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;
507 /***** ANTI-OVERRIDES *****/
508 /* pthreads tries to override these, point them back to libc */
511 jump_alias(LIBC_SIGACTION, sigaction);
513 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
515 return LIBC_SIGACTION(signum, act, oldact);
519 #endif /* __GLIBC__ */