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
24 struct _pthread_cleanup_buffer
;
27 #include "wine/port.h"
29 #define _GNU_SOURCE /* we may need to override some GNU extensions */
43 /* Currently this probably works only for glibc2,
44 * which checks for the presence of double-underscore-prepended
45 * pthread primitives, and use them if available.
46 * If they are not available, the libc defaults to
47 * non-threadsafe operation (not good). */
49 #if (defined(__GLIBC__) || defined(__FreeBSD__)) && !defined(HAVE_NPTL)
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))
67 static pid_t (*libc_fork
)(void);
68 static int (*libc_sigaction
)(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
);
70 void PTHREAD_init_done(void)
73 if (!libc_fork
) libc_fork
= dlsym( RTLD_NEXT
, "fork" );
74 if (!libc_sigaction
) libc_sigaction
= dlsym( RTLD_NEXT
, "sigaction" );
78 /* NOTE: This is a truly extremely incredibly ugly hack!
79 * But it does seem to work... */
81 /* assume that pthread_mutex_t has room for at least one pointer,
82 * and hope that the users of pthread_mutex_t considers it opaque
83 * (never checks what's in it)
84 * also: assume that static initializer sets pointer to NULL
87 CRITICAL_SECTION
*critsect
;
90 /* see wine_mutex above for comments */
95 typedef struct _wine_cleanup
{
96 void (*routine
)(void *);
100 typedef const void *key_data
;
103 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
105 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
107 void __pthread_initialize(void)
111 struct pthread_thread_init
{
112 void* (*start_routine
)(void*);
116 static DWORD CALLBACK
pthread_thread_start(LPVOID data
)
118 struct pthread_thread_init init
= *(struct pthread_thread_init
*)data
;
119 HeapFree(GetProcessHeap(),0,data
);
120 return (DWORD
)init
.start_routine(init
.arg
);
123 int pthread_create(pthread_t
* thread
, const pthread_attr_t
* attr
, void*
124 (*start_routine
)(void *), void* arg
)
127 struct pthread_thread_init
* idata
= HeapAlloc(GetProcessHeap(), 0,
128 sizeof(struct pthread_thread_init
));
130 idata
->start_routine
= start_routine
;
132 hThread
= CreateThread( NULL
, 0, pthread_thread_start
, idata
, 0,
136 CloseHandle(hThread
);
139 HeapFree(GetProcessHeap(),0,idata
); /* free idata struct on failure */
146 int pthread_cancel(pthread_t thread
)
148 HANDLE hThread
= OpenThread(THREAD_ALL_ACCESS
, FALSE
, (DWORD
)thread
);
150 if(!TerminateThread(hThread
, 0))
152 CloseHandle(hThread
);
153 return EINVAL
; /* return error */
156 CloseHandle(hThread
);
158 return 0; /* return success */
161 int pthread_join(pthread_t thread
, void **value_ptr
)
163 HANDLE hThread
= OpenThread(THREAD_ALL_ACCESS
, FALSE
, (DWORD
)thread
);
165 WaitForSingleObject(hThread
, INFINITE
);
166 if(!GetExitCodeThread(hThread
, (LPDWORD
)value_ptr
))
168 CloseHandle(hThread
);
169 return EINVAL
; /* FIXME: make this more correctly match */
170 } /* windows errors */
172 CloseHandle(hThread
);
176 /*FIXME: not sure what to do with this one... */
177 int pthread_detach(pthread_t thread
)
179 P_OUTPUT("FIXME:pthread_detach\n");
183 /* FIXME: we have no equivalents in win32 for the policys */
184 /* so just keep this as a stub */
185 int pthread_attr_setschedpolicy(pthread_attr_t
*attr
, int policy
)
187 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
191 /* FIXME: no win32 equivalent for scope */
192 int pthread_attr_setscope(pthread_attr_t
*attr
, int scope
)
194 P_OUTPUT("FIXME:pthread_attr_setscope\n");
195 return 0; /* return success */
198 /* FIXME: no win32 equivalent for schedule param */
199 int pthread_attr_setschedparam(pthread_attr_t
*attr
,
200 const struct sched_param
*param
)
202 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
203 return 0; /* return success */
206 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
208 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
211 memcpy(&once_now
,&the_once
,sizeof(once_now
));
212 if (InterlockedCompareExchange((LONG
*)once_control
, once_now
+1, once_now
) == once_now
)
216 strong_alias(__pthread_once
, pthread_once
);
218 void __pthread_kill_other_threads_np(void)
220 /* we don't need to do anything here */
222 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
226 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
228 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT("atfork_section");
229 typedef void (*atfork_handler
)();
230 static atfork_handler atfork_prepare
[MAX_ATFORK
];
231 static atfork_handler atfork_parent
[MAX_ATFORK
];
232 static atfork_handler atfork_child
[MAX_ATFORK
];
233 static int atfork_count
;
235 int __pthread_atfork(void (*prepare
)(void),
236 void (*parent
)(void),
239 if (init_done
) EnterCriticalSection( &atfork_section
);
240 assert( atfork_count
< MAX_ATFORK
);
241 atfork_prepare
[atfork_count
] = prepare
;
242 atfork_parent
[atfork_count
] = parent
;
243 atfork_child
[atfork_count
] = child
;
245 if (init_done
) LeaveCriticalSection( &atfork_section
);
248 strong_alias(__pthread_atfork
, pthread_atfork
);
257 libc_fork
= dlsym( RTLD_NEXT
, "fork" );
260 EnterCriticalSection( &atfork_section
);
261 /* prepare handlers are called in reverse insertion order */
262 for (i
= atfork_count
- 1; i
>= 0; i
--) if (atfork_prepare
[i
]) atfork_prepare
[i
]();
263 if (!(pid
= libc_fork()))
265 InitializeCriticalSection( &atfork_section
);
266 for (i
= 0; i
< atfork_count
; i
++) if (atfork_child
[i
]) atfork_child
[i
]();
270 for (i
= 0; i
< atfork_count
; i
++) if (atfork_parent
[i
]) atfork_parent
[i
]();
271 LeaveCriticalSection( &atfork_section
);
275 strong_alias(__fork
, fork
);
277 /***** MUTEXES *****/
279 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
280 const pthread_mutexattr_t
*mutexattr
)
282 /* glibc has a tendency to initialize mutexes very often, even
283 in situations where they are not really used later on.
285 As for us, initializing a mutex is very expensive, we postpone
286 the real initialization until the time the mutex is first used. */
288 ((wine_mutex
)mutex
)->critsect
= NULL
;
291 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
293 static void mutex_real_init( pthread_mutex_t
*mutex
)
295 CRITICAL_SECTION
*critsect
= HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION
));
296 InitializeCriticalSection(critsect
);
298 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
299 /* too late, some other thread already did it */
300 DeleteCriticalSection(critsect
);
301 HeapFree(GetProcessHeap(), 0, critsect
);
305 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
307 if (!init_done
) return 0;
308 if (!((wine_mutex
)mutex
)->critsect
)
309 mutex_real_init( mutex
);
311 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
314 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
316 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
318 if (!init_done
) return 0;
319 if (!((wine_mutex
)mutex
)->critsect
)
320 mutex_real_init( mutex
);
322 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
328 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
330 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
332 if (!((wine_mutex
)mutex
)->critsect
) return 0;
333 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
336 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
338 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
340 if (!((wine_mutex
)mutex
)->critsect
) return 0;
341 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
342 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
345 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
346 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
349 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
350 HeapFree(GetProcessHeap(), 0, ((wine_mutex
)mutex
)->critsect
);
353 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
356 /***** MUTEX ATTRIBUTES *****/
357 /* just dummies, since critical sections are always recursive */
359 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
363 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
365 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
369 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
371 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
375 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
377 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
379 *kind
= PTHREAD_MUTEX_RECURSIVE
;
382 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
384 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
388 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
390 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
392 *kind
= PTHREAD_MUTEX_RECURSIVE
;
395 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
398 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
400 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
402 static LONG keycnt
= FIRST_KEY
;
403 *key
= InterlockedExchangeAdd(&keycnt
, 1);
406 strong_alias(__pthread_key_create
, pthread_key_create
);
408 int __pthread_key_delete(pthread_key_t key
)
412 strong_alias(__pthread_key_delete
, pthread_key_delete
);
414 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
416 TEB
*teb
= NtCurrentTeb();
417 if (!teb
->pthread_data
) {
418 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
420 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
423 strong_alias(__pthread_setspecific
, pthread_setspecific
);
425 void *__pthread_getspecific(pthread_key_t key
)
427 TEB
*teb
= NtCurrentTeb();
428 if (!teb
) return NULL
;
429 if (!teb
->pthread_data
) return NULL
;
430 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
432 strong_alias(__pthread_getspecific
, pthread_getspecific
);
435 /***** "EXCEPTION" FRAMES *****/
436 /* not implemented right now */
438 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
440 ((wine_cleanup
)buffer
)->routine
= routine
;
441 ((wine_cleanup
)buffer
)->arg
= arg
;
444 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
446 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
449 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
451 _pthread_cleanup_push(buffer
, routine
, arg
);
454 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
456 _pthread_cleanup_pop(buffer
, execute
);
460 /***** CONDITIONS *****/
461 /* not implemented right now */
463 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
465 P_OUTPUT("FIXME:pthread_cond_init\n");
469 int pthread_cond_destroy(pthread_cond_t
*cond
)
471 P_OUTPUT("FIXME:pthread_cond_destroy\n");
475 int pthread_cond_signal(pthread_cond_t
*cond
)
477 P_OUTPUT("FIXME:pthread_cond_signal\n");
481 int pthread_cond_broadcast(pthread_cond_t
*cond
)
483 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
487 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
489 P_OUTPUT("FIXME:pthread_cond_wait\n");
493 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
495 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
499 /**** CONDITION ATTRIBUTES *****/
500 /* not implemented right now */
502 int pthread_condattr_init(pthread_condattr_t
*attr
)
507 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
512 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
513 /***** READ-WRITE LOCKS *****/
515 static void rwlock_real_init(pthread_rwlock_t
*rwlock
)
517 RTL_RWLOCK
*lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK
));
518 RtlInitializeResource(lock
);
520 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock
)rwlock
)->lock
),lock
,NULL
) != NULL
) {
521 /* too late, some other thread already did it */
522 RtlDeleteResource(lock
);
523 HeapFree(GetProcessHeap(), 0, lock
);
527 int __pthread_rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*rwlock_attr
)
529 ((wine_rwlock
)rwlock
)->lock
= NULL
;
532 strong_alias(__pthread_rwlock_init
, pthread_rwlock_init
);
534 int __pthread_rwlock_destroy(pthread_rwlock_t
*rwlock
)
536 if (!((wine_rwlock
)rwlock
)->lock
) return 0;
537 RtlDeleteResource(((wine_rwlock
)rwlock
)->lock
);
538 HeapFree(GetProcessHeap(), 0, ((wine_rwlock
)rwlock
)->lock
);
541 strong_alias(__pthread_rwlock_destroy
, pthread_rwlock_destroy
);
543 int __pthread_rwlock_rdlock(pthread_rwlock_t
*rwlock
)
545 if (!init_done
) return 0;
546 if (!((wine_rwlock
)rwlock
)->lock
)
547 rwlock_real_init( rwlock
);
550 if (RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, TRUE
))
553 strong_alias(__pthread_rwlock_rdlock
, pthread_rwlock_rdlock
);
555 int __pthread_rwlock_tryrdlock(pthread_rwlock_t
*rwlock
)
557 if (!init_done
) return 0;
558 if (!((wine_rwlock
)rwlock
)->lock
)
559 rwlock_real_init( rwlock
);
561 if (!RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, FALSE
)) {
567 strong_alias(__pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
);
569 int __pthread_rwlock_wrlock(pthread_rwlock_t
*rwlock
)
571 if (!init_done
) return 0;
572 if (!((wine_rwlock
)rwlock
)->lock
)
573 rwlock_real_init( rwlock
);
576 if (RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, TRUE
))
579 strong_alias(__pthread_rwlock_wrlock
, pthread_rwlock_wrlock
);
581 int __pthread_rwlock_trywrlock(pthread_rwlock_t
*rwlock
)
583 if (!init_done
) return 0;
584 if (!((wine_rwlock
)rwlock
)->lock
)
585 rwlock_real_init( rwlock
);
587 if (!RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, FALSE
)) {
593 strong_alias(__pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
);
595 int __pthread_rwlock_unlock(pthread_rwlock_t
*rwlock
)
597 if (!((wine_rwlock
)rwlock
)->lock
) return 0;
598 RtlReleaseResource( ((wine_rwlock
)rwlock
)->lock
);
601 strong_alias(__pthread_rwlock_unlock
, pthread_rwlock_unlock
);
603 /**** READ-WRITE LOCK ATTRIBUTES *****/
604 /* not implemented right now */
606 int pthread_rwlockattr_init(pthread_rwlockattr_t
*attr
)
611 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
615 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
617 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
623 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
627 #endif /* glibc 2.2 */
631 pthread_t
pthread_self(void)
633 return (pthread_t
)GetCurrentThreadId();
636 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
638 return (DWORD
)thread1
== (DWORD
)thread2
;
641 void pthread_exit(void *retval
)
643 /* FIXME: pthread cleanup */
644 ExitThread((DWORD
)retval
);
647 int pthread_setcanceltype(int type
, int *oldtype
)
649 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
653 /***** ANTI-OVERRIDES *****/
654 /* pthreads tries to override these, point them back to libc */
656 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
660 libc_sigaction
= dlsym( RTLD_NEXT
, "sigaction" );
661 assert( libc_sigaction
);
663 return libc_sigaction(signum
, act
, oldact
);
666 #else /* __GLIBC__ || __FREEBSD__ */
668 void PTHREAD_init_done(void)
672 #endif /* __GLIBC__ || __FREEBSD__ */