Support default output name (a.out); some configure scripts check for
[wine/multimedia.git] / scheduler / pthread.c
blobbc5cf813f2b615310c6bdeefc58ac4241a7850af
1 /*
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 #include "config.h"
25 #include "wine/port.h"
27 #define _GNU_SOURCE /* we may need to override some GNU extensions */
29 #include <assert.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <string.h>
37 #include "winbase.h"
38 #include "thread.h"
39 #include "winternl.h"
41 /* Currently this probably works only for glibc2,
42 * which checks for the presence of double-underscore-prepended
43 * pthread primitives, and use them if available.
44 * If they are not available, the libc defaults to
45 * non-threadsafe operation (not good). */
47 #if defined(__GLIBC__) || defined(__FreeBSD__)
49 #ifndef __USE_UNIX98
50 #define __USE_UNIX98
51 #endif
53 #include <pthread.h>
54 #include <signal.h>
56 #define PSTR(str) __ASM_NAME(#str)
58 /* adapt as necessary (a construct like this is used in glibc sources) */
59 #define strong_alias(orig, alias) \
60 asm(".globl " PSTR(alias) "\n" \
61 "\t.set " PSTR(alias) "," PSTR(orig))
63 static int init_done;
65 static pid_t (*libc_fork)(void);
66 static int (*libc_sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact);
68 void PTHREAD_init_done(void)
70 init_done = 1;
71 if (!libc_fork) libc_fork = dlsym( RTLD_NEXT, "fork" );
72 if (!libc_sigaction) libc_sigaction = dlsym( RTLD_NEXT, "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)
82 * also: assume that static initializer sets pointer to NULL
84 typedef struct {
85 CRITICAL_SECTION *critsect;
86 } *wine_mutex;
88 /* see wine_mutex above for comments */
89 typedef struct {
90 RTL_RWLOCK *lock;
91 } *wine_rwlock;
93 typedef struct _wine_cleanup {
94 void (*routine)(void *);
95 void *arg;
96 } *wine_cleanup;
98 typedef const void *key_data;
100 #define FIRST_KEY 0
101 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
103 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
105 void __pthread_initialize(void)
109 struct pthread_thread_init {
110 void* (*start_routine)(void*);
111 void* arg;
114 static DWORD CALLBACK pthread_thread_start(LPVOID data)
116 struct pthread_thread_init init = *(struct pthread_thread_init*)data;
117 HeapFree(GetProcessHeap(),0,data);
118 return (DWORD)init.start_routine(init.arg);
121 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
122 (*start_routine)(void *), void* arg)
124 HANDLE hThread;
125 struct pthread_thread_init* idata = HeapAlloc(GetProcessHeap(), 0,
126 sizeof(struct pthread_thread_init));
128 idata->start_routine = start_routine;
129 idata->arg = arg;
130 hThread = CreateThread(NULL, 0, pthread_thread_start, idata, 0, thread);
132 if(hThread)
133 CloseHandle(hThread);
134 else
136 HeapFree(GetProcessHeap(),0,idata); /* free idata struct on failure */
137 return EAGAIN;
140 return 0;
143 int pthread_cancel(pthread_t thread)
145 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
147 if(!TerminateThread(hThread, 0))
149 CloseHandle(hThread);
150 return EINVAL; /* return error */
153 CloseHandle(hThread);
155 return 0; /* return success */
158 int pthread_join(pthread_t thread, void **value_ptr)
160 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
162 WaitForSingleObject(hThread, INFINITE);
163 if(!GetExitCodeThread(hThread, (LPDWORD)value_ptr))
165 CloseHandle(hThread);
166 return EINVAL; /* FIXME: make this more correctly match */
167 } /* windows errors */
169 CloseHandle(hThread);
170 return 0;
173 /*FIXME: not sure what to do with this one... */
174 int pthread_detach(pthread_t thread)
176 P_OUTPUT("FIXME:pthread_detach\n");
177 return 0;
180 /* FIXME: we have no equivalents in win32 for the policys */
181 /* so just keep this as a stub */
182 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
184 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
185 return 0;
188 /* FIXME: no win32 equivalent for scope */
189 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
191 P_OUTPUT("FIXME:pthread_attr_setscope\n");
192 return 0; /* return success */
195 /* FIXME: no win32 equivalent for schedule param */
196 int pthread_attr_setschedparam(pthread_attr_t *attr,
197 const struct sched_param *param)
199 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
200 return 0; /* return success */
203 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
205 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
206 LONG once_now;
208 memcpy(&once_now,&the_once,sizeof(once_now));
209 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
210 (*init_routine)();
211 return 0;
213 strong_alias(__pthread_once, pthread_once);
215 void __pthread_kill_other_threads_np(void)
217 /* we don't need to do anything here */
219 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
221 /***** atfork *****/
223 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
225 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
226 typedef void (*atfork_handler)();
227 static atfork_handler atfork_prepare[MAX_ATFORK];
228 static atfork_handler atfork_parent[MAX_ATFORK];
229 static atfork_handler atfork_child[MAX_ATFORK];
230 static int atfork_count;
232 int __pthread_atfork(void (*prepare)(void),
233 void (*parent)(void),
234 void (*child)(void))
236 if (init_done) EnterCriticalSection( &atfork_section );
237 assert( atfork_count < MAX_ATFORK );
238 atfork_prepare[atfork_count] = prepare;
239 atfork_parent[atfork_count] = parent;
240 atfork_child[atfork_count] = child;
241 atfork_count++;
242 if (init_done) LeaveCriticalSection( &atfork_section );
243 return 0;
245 strong_alias(__pthread_atfork, pthread_atfork);
247 pid_t __fork(void)
249 pid_t pid;
250 int i;
252 if (!libc_fork)
254 libc_fork = dlsym( RTLD_NEXT, "fork" );
255 assert( libc_fork );
257 EnterCriticalSection( &atfork_section );
258 /* prepare handlers are called in reverse insertion order */
259 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
260 if (!(pid = libc_fork()))
262 InitializeCriticalSection( &atfork_section );
263 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
265 else
267 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
268 LeaveCriticalSection( &atfork_section );
270 return pid;
272 strong_alias(__fork, fork);
274 /***** MUTEXES *****/
276 int __pthread_mutex_init(pthread_mutex_t *mutex,
277 const pthread_mutexattr_t *mutexattr)
279 /* glibc has a tendency to initialize mutexes very often, even
280 in situations where they are not really used later on.
282 As for us, initializing a mutex is very expensive, we postpone
283 the real initialization until the time the mutex is first used. */
285 ((wine_mutex)mutex)->critsect = NULL;
286 return 0;
288 strong_alias(__pthread_mutex_init, pthread_mutex_init);
290 static void mutex_real_init( pthread_mutex_t *mutex )
292 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
293 InitializeCriticalSection(critsect);
295 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
296 /* too late, some other thread already did it */
297 DeleteCriticalSection(critsect);
298 HeapFree(GetProcessHeap(), 0, critsect);
302 int __pthread_mutex_lock(pthread_mutex_t *mutex)
304 if (!init_done) return 0;
305 if (!((wine_mutex)mutex)->critsect)
306 mutex_real_init( mutex );
308 EnterCriticalSection(((wine_mutex)mutex)->critsect);
309 return 0;
311 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
313 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
315 if (!init_done) return 0;
316 if (!((wine_mutex)mutex)->critsect)
317 mutex_real_init( mutex );
319 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
320 errno = EBUSY;
321 return -1;
323 return 0;
325 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
327 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
329 if (!((wine_mutex)mutex)->critsect) return 0;
330 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
331 return 0;
333 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
335 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
337 if (!((wine_mutex)mutex)->critsect) return 0;
338 if (((wine_mutex)mutex)->critsect->RecursionCount) {
339 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
340 return EBUSY;
341 #else
342 while (((wine_mutex)mutex)->critsect->RecursionCount)
343 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
344 #endif
346 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
347 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
348 return 0;
350 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
353 /***** MUTEX ATTRIBUTES *****/
354 /* just dummies, since critical sections are always recursive */
356 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
358 return 0;
360 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
362 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
364 return 0;
366 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
368 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
370 return 0;
372 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
374 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
376 *kind = PTHREAD_MUTEX_RECURSIVE;
377 return 0;
379 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
381 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
383 return 0;
385 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
387 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
389 *kind = PTHREAD_MUTEX_RECURSIVE;
390 return 0;
392 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
395 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
397 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
399 static LONG keycnt = FIRST_KEY;
400 *key = InterlockedExchangeAdd(&keycnt, 1);
401 return 0;
403 strong_alias(__pthread_key_create, pthread_key_create);
405 int __pthread_key_delete(pthread_key_t key)
407 return 0;
409 strong_alias(__pthread_key_delete, pthread_key_delete);
411 int __pthread_setspecific(pthread_key_t key, const void *pointer)
413 TEB *teb = NtCurrentTeb();
414 if (!teb->pthread_data) {
415 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
417 ((key_data*)(teb->pthread_data))[key] = pointer;
418 return 0;
420 strong_alias(__pthread_setspecific, pthread_setspecific);
422 void *__pthread_getspecific(pthread_key_t key)
424 TEB *teb = NtCurrentTeb();
425 if (!teb) return NULL;
426 if (!teb->pthread_data) return NULL;
427 return (void *)(((key_data*)(teb->pthread_data))[key]);
429 strong_alias(__pthread_getspecific, pthread_getspecific);
432 /***** "EXCEPTION" FRAMES *****/
433 /* not implemented right now */
435 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
437 ((wine_cleanup)buffer)->routine = routine;
438 ((wine_cleanup)buffer)->arg = arg;
441 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
443 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
446 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
448 _pthread_cleanup_push(buffer, routine, arg);
451 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
453 _pthread_cleanup_pop(buffer, execute);
457 /***** CONDITIONS *****/
458 /* not implemented right now */
460 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
462 P_OUTPUT("FIXME:pthread_cond_init\n");
463 return 0;
466 int pthread_cond_destroy(pthread_cond_t *cond)
468 P_OUTPUT("FIXME:pthread_cond_destroy\n");
469 return 0;
472 int pthread_cond_signal(pthread_cond_t *cond)
474 P_OUTPUT("FIXME:pthread_cond_signal\n");
475 return 0;
478 int pthread_cond_broadcast(pthread_cond_t *cond)
480 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
481 return 0;
484 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
486 P_OUTPUT("FIXME:pthread_cond_wait\n");
487 return 0;
490 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
492 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
493 return 0;
496 /**** CONDITION ATTRIBUTES *****/
497 /* not implemented right now */
499 int pthread_condattr_init(pthread_condattr_t *attr)
501 return 0;
504 int pthread_condattr_destroy(pthread_condattr_t *attr)
506 return 0;
509 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
510 /***** READ-WRITE LOCKS *****/
512 static void rwlock_real_init(pthread_rwlock_t *rwlock)
514 RTL_RWLOCK *lock = HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK));
515 RtlInitializeResource(lock);
517 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock)rwlock)->lock),lock,NULL) != NULL) {
518 /* too late, some other thread already did it */
519 RtlDeleteResource(lock);
520 HeapFree(GetProcessHeap(), 0, lock);
524 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
526 ((wine_rwlock)rwlock)->lock = NULL;
527 return 0;
529 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
531 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
533 if (!((wine_rwlock)rwlock)->lock) return 0;
534 RtlDeleteResource(((wine_rwlock)rwlock)->lock);
535 HeapFree(GetProcessHeap(), 0, ((wine_rwlock)rwlock)->lock);
536 return 0;
538 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
540 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
542 if (!init_done) return 0;
543 if (!((wine_rwlock)rwlock)->lock)
544 rwlock_real_init( rwlock );
546 while(TRUE)
547 if (RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, TRUE))
548 return 0;
550 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
552 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
554 if (!init_done) return 0;
555 if (!((wine_rwlock)rwlock)->lock)
556 rwlock_real_init( rwlock );
558 if (!RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, FALSE)) {
559 errno = EBUSY;
560 return -1;
562 return 0;
564 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
566 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
568 if (!init_done) return 0;
569 if (!((wine_rwlock)rwlock)->lock)
570 rwlock_real_init( rwlock );
572 while(TRUE)
573 if (RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, TRUE))
574 return 0;
576 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
578 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
580 if (!init_done) return 0;
581 if (!((wine_rwlock)rwlock)->lock)
582 rwlock_real_init( rwlock );
584 if (!RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, FALSE)) {
585 errno = EBUSY;
586 return -1;
588 return 0;
590 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
592 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
594 if (!((wine_rwlock)rwlock)->lock) return 0;
595 RtlReleaseResource( ((wine_rwlock)rwlock)->lock );
596 return 0;
598 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
600 /**** READ-WRITE LOCK ATTRIBUTES *****/
601 /* not implemented right now */
603 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
605 return 0;
608 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
610 return 0;
612 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
614 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
616 *pref = 0;
617 return 0;
620 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
622 return 0;
624 #endif /* glibc 2.2 */
626 /***** MISC *****/
628 pthread_t pthread_self(void)
630 return (pthread_t)GetCurrentThreadId();
633 int pthread_equal(pthread_t thread1, pthread_t thread2)
635 return (DWORD)thread1 == (DWORD)thread2;
638 void pthread_exit(void *retval)
640 /* FIXME: pthread cleanup */
641 ExitThread((DWORD)retval);
644 int pthread_setcanceltype(int type, int *oldtype)
646 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
647 return 0;
650 /***** ANTI-OVERRIDES *****/
651 /* pthreads tries to override these, point them back to libc */
653 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
655 if (!libc_sigaction)
657 libc_sigaction = dlsym( RTLD_NEXT, "sigaction" );
658 assert( libc_sigaction );
660 return libc_sigaction(signum, act, oldact);
663 #else /* __GLIBC__ */
665 void PTHREAD_init_done(void)
669 #endif /* __GLIBC__ */