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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
27 #define _GNU_SOURCE /* we may need to override some GNU extensions */
43 void PTHREAD_init_done(void)
48 /* Currently this probably works only for glibc2,
49 * which checks for the presence of double-underscore-prepended
50 * pthread primitives, and use them if available.
51 * If they are not available, the libc defaults to
52 * non-threadsafe operation (not good). */
54 #if defined(__GLIBC__)
58 #define PSTR(str) __ASM_NAME(#str)
60 /* adapt as necessary (a construct like this is used in glibc sources) */
61 #define strong_alias(orig, alias) \
62 asm(".globl " PSTR(alias) "\n" \
63 "\t.set " PSTR(alias) "," PSTR(orig))
65 /* strong_alias does not work on external symbols (.o format limitation?),
66 * so for those, we need to use the pogo stick */
67 #if defined(__i386__) && !defined(__PIC__)
69 #define jump_alias(orig, alias) __ASM_GLOBAL_FUNC( alias, "jmp " PSTR(orig))
72 /* get necessary libc symbols */
73 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
74 #define LIBC_FORK __libc_fork
75 #define PTHREAD_FORK __fork
78 #define LIBC_FORK __fork
79 #define PTHREAD_FORK fork
81 extern pid_t LIBC_FORK(void);
83 #define LIBC_SIGACTION __sigaction
84 extern int LIBC_SIGACTION(int signum,
85 const struct sigaction *act,
86 struct sigaction *oldact);
88 /* NOTE: This is a truly extremely incredibly ugly hack!
89 * But it does seem to work... */
91 /* assume that pthread_mutex_t has room for at least one pointer,
92 * and hope that the users of pthread_mutex_t considers it opaque
93 * (never checks what's in it)
94 * also: assume that static initializer sets pointer to NULL
97 CRITICAL_SECTION *critsect;
100 /* see wine_mutex above for comments */
105 typedef struct _wine_cleanup {
106 void (*routine)(void *);
110 typedef const void *key_data;
113 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
115 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
117 void __pthread_initialize(void)
121 struct pthread_thread_init {
122 void* (*start_routine)(void*);
126 static DWORD CALLBACK pthread_thread_start(LPVOID data)
128 struct pthread_thread_init init = *(struct pthread_thread_init*)data;
129 HeapFree(GetProcessHeap(),0,data);
130 return (DWORD)init.start_routine(init.arg);
133 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
134 (*start_routine)(void *), void* arg)
137 struct pthread_thread_init* idata = HeapAlloc(GetProcessHeap(), 0,
138 sizeof(struct pthread_thread_init));
140 idata->start_routine = start_routine;
142 hThread = CreateThread(NULL, 0, pthread_thread_start, idata, 0, thread);
145 CloseHandle(hThread);
148 HeapFree(GetProcessHeap(),0,idata); /* free idata struct on failure */
155 int pthread_cancel(pthread_t thread)
157 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
159 if(!TerminateThread(hThread, 0))
161 CloseHandle(hThread);
162 return EINVAL; /* return error */
165 CloseHandle(hThread);
167 return 0; /* return success */
170 int pthread_join(pthread_t thread, void **value_ptr)
172 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
174 WaitForSingleObject(hThread, INFINITE);
175 if(!GetExitCodeThread(hThread, (LPDWORD)value_ptr))
177 CloseHandle(hThread);
178 return EINVAL; /* FIXME: make this more correctly match */
179 } /* windows errors */
181 CloseHandle(hThread);
185 /*FIXME: not sure what to do with this one... */
186 int pthread_detach(pthread_t thread)
188 P_OUTPUT("FIXME:pthread_detach\n");
192 /* FIXME: we have no equivalents in win32 for the policys */
193 /* so just keep this as a stub */
194 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
196 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
200 /* FIXME: no win32 equivalent for scope */
201 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
203 P_OUTPUT("FIXME:pthread_attr_setscope\n");
204 return 0; /* return success */
207 /* FIXME: no win32 equivalent for schedule param */
208 int pthread_attr_setschedparam(pthread_attr_t *attr,
209 const struct sched_param *param)
211 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
212 return 0; /* return success */
215 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
217 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
218 LONG once_now = *(LONG *)&the_once;
220 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
224 strong_alias(__pthread_once, pthread_once);
226 void __pthread_kill_other_threads_np(void)
228 /* we don't need to do anything here */
230 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
234 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
236 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
237 typedef void (*atfork_handler)();
238 static atfork_handler atfork_prepare[MAX_ATFORK];
239 static atfork_handler atfork_parent[MAX_ATFORK];
240 static atfork_handler atfork_child[MAX_ATFORK];
241 static int atfork_count;
243 int __pthread_atfork(void (*prepare)(void),
244 void (*parent)(void),
247 if (init_done) EnterCriticalSection( &atfork_section );
248 assert( atfork_count < MAX_ATFORK );
249 atfork_prepare[atfork_count] = prepare;
250 atfork_parent[atfork_count] = parent;
251 atfork_child[atfork_count] = child;
253 if (init_done) LeaveCriticalSection( &atfork_section );
256 strong_alias(__pthread_atfork, pthread_atfork);
258 pid_t PTHREAD_FORK(void)
263 EnterCriticalSection( &atfork_section );
264 /* prepare handlers are called in reverse insertion order */
265 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
266 if (!(pid = LIBC_FORK()))
268 InitializeCriticalSection( &atfork_section );
269 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
273 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
274 LeaveCriticalSection( &atfork_section );
279 strong_alias(PTHREAD_FORK, fork);
282 /***** MUTEXES *****/
284 int __pthread_mutex_init(pthread_mutex_t *mutex,
285 const pthread_mutexattr_t *mutexattr)
287 /* glibc has a tendency to initialize mutexes very often, even
288 in situations where they are not really used later on.
290 As for us, initializing a mutex is very expensive, we postpone
291 the real initialization until the time the mutex is first used. */
293 ((wine_mutex)mutex)->critsect = NULL;
296 strong_alias(__pthread_mutex_init, pthread_mutex_init);
298 static void mutex_real_init( pthread_mutex_t *mutex )
300 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
301 InitializeCriticalSection(critsect);
303 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
304 /* too late, some other thread already did it */
305 DeleteCriticalSection(critsect);
306 HeapFree(GetProcessHeap(), 0, critsect);
310 int __pthread_mutex_lock(pthread_mutex_t *mutex)
312 if (!init_done) return 0;
313 if (!((wine_mutex)mutex)->critsect)
314 mutex_real_init( mutex );
316 EnterCriticalSection(((wine_mutex)mutex)->critsect);
319 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
321 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
323 if (!init_done) return 0;
324 if (!((wine_mutex)mutex)->critsect)
325 mutex_real_init( mutex );
327 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
333 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
335 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
337 if (!((wine_mutex)mutex)->critsect) return 0;
338 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
341 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
343 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
345 if (!((wine_mutex)mutex)->critsect) return 0;
346 if (((wine_mutex)mutex)->critsect->RecursionCount) {
347 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
350 while (((wine_mutex)mutex)->critsect->RecursionCount)
351 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
354 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
355 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
358 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
361 /***** MUTEX ATTRIBUTES *****/
362 /* just dummies, since critical sections are always recursive */
364 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
368 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
370 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
374 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
376 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
380 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
382 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
384 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
387 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
389 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
393 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
395 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
397 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
400 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
403 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
405 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
407 static LONG keycnt = FIRST_KEY;
408 *key = InterlockedExchangeAdd(&keycnt, 1);
411 strong_alias(__pthread_key_create, pthread_key_create);
413 int __pthread_key_delete(pthread_key_t key)
417 strong_alias(__pthread_key_delete, pthread_key_delete);
419 int __pthread_setspecific(pthread_key_t key, const void *pointer)
421 TEB *teb = NtCurrentTeb();
422 if (!teb->pthread_data) {
423 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
425 ((key_data*)(teb->pthread_data))[key] = pointer;
428 strong_alias(__pthread_setspecific, pthread_setspecific);
430 void *__pthread_getspecific(pthread_key_t key)
432 TEB *teb = NtCurrentTeb();
433 if (!teb) return NULL;
434 if (!teb->pthread_data) return NULL;
435 return (void *)(((key_data*)(teb->pthread_data))[key]);
437 strong_alias(__pthread_getspecific, pthread_getspecific);
440 /***** "EXCEPTION" FRAMES *****/
441 /* not implemented right now */
443 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
445 ((wine_cleanup)buffer)->routine = routine;
446 ((wine_cleanup)buffer)->arg = arg;
449 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
451 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
454 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
456 _pthread_cleanup_push(buffer, routine, arg);
459 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
461 _pthread_cleanup_pop(buffer, execute);
465 /***** CONDITIONS *****/
466 /* not implemented right now */
468 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
470 P_OUTPUT("FIXME:pthread_cond_init\n");
474 int pthread_cond_destroy(pthread_cond_t *cond)
476 P_OUTPUT("FIXME:pthread_cond_destroy\n");
480 int pthread_cond_signal(pthread_cond_t *cond)
482 P_OUTPUT("FIXME:pthread_cond_signal\n");
486 int pthread_cond_broadcast(pthread_cond_t *cond)
488 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
492 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
494 P_OUTPUT("FIXME:pthread_cond_wait\n");
498 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
500 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
504 /**** CONDITION ATTRIBUTES *****/
505 /* not implemented right now */
507 int pthread_condattr_init(pthread_condattr_t *attr)
512 int pthread_condattr_destroy(pthread_condattr_t *attr)
517 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
518 /***** READ-WRITE LOCKS *****/
520 static void rwlock_real_init(pthread_rwlock_t *rwlock)
522 RTL_RWLOCK *lock = HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK));
523 RtlInitializeResource(lock);
525 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock)rwlock)->lock),lock,NULL) != NULL) {
526 /* too late, some other thread already did it */
527 RtlDeleteResource(lock);
528 HeapFree(GetProcessHeap(), 0, lock);
532 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
534 ((wine_rwlock)rwlock)->lock = NULL;
537 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
539 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
541 if (!((wine_rwlock)rwlock)->lock) return 0;
542 RtlDeleteResource(((wine_rwlock)rwlock)->lock);
543 HeapFree(GetProcessHeap(), 0, ((wine_rwlock)rwlock)->lock);
546 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
548 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
550 if (!init_done) return 0;
551 if (!((wine_rwlock)rwlock)->lock)
552 rwlock_real_init( rwlock );
555 if (RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, TRUE))
558 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
560 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
562 if (!init_done) return 0;
563 if (!((wine_rwlock)rwlock)->lock)
564 rwlock_real_init( rwlock );
566 if (!RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, FALSE)) {
572 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
574 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
576 if (!init_done) return 0;
577 if (!((wine_rwlock)rwlock)->lock)
578 rwlock_real_init( rwlock );
581 if (RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, TRUE))
584 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
586 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
588 if (!init_done) return 0;
589 if (!((wine_rwlock)rwlock)->lock)
590 rwlock_real_init( rwlock );
592 if (!RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, FALSE)) {
598 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
600 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
602 if (!((wine_rwlock)rwlock)->lock) return 0;
603 RtlReleaseResource( ((wine_rwlock)rwlock)->lock );
606 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
608 /**** READ-WRITE LOCK ATTRIBUTES *****/
609 /* not implemented right now */
611 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
616 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
620 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
622 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
628 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
632 #endif /* glibc 2.2 */
636 pthread_t pthread_self(void)
638 return (pthread_t)GetCurrentThreadId();
641 int pthread_equal(pthread_t thread1, pthread_t thread2)
643 return (DWORD)thread1 == (DWORD)thread2;
646 void pthread_exit(void *retval)
648 /* FIXME: pthread cleanup */
649 ExitThread((DWORD)retval);
652 int pthread_setcanceltype(int type, int *oldtype)
654 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
658 /***** ANTI-OVERRIDES *****/
659 /* pthreads tries to override these, point them back to libc */
662 jump_alias(LIBC_SIGACTION, sigaction);
664 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
666 return LIBC_SIGACTION(signum, act, oldact);
670 #endif /* __GLIBC__ */