More Spanish translations.
[wine/multimedia.git] / scheduler / pthread.c
blob6744931b7d93d20f355df696f9b08c8d78426180
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 #ifndef HAVE_NPTL
29 struct _pthread_cleanup_buffer;
31 #define _GNU_SOURCE /* we may need to override some GNU extensions */
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <setjmp.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <string.h>
41 #include <sys/types.h>
42 #if HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 #include <sys/mman.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
50 #endif
51 #ifdef HAVE_RESOLV_H
52 # include <resolv.h>
53 #endif
54 #ifdef HAVE_VALGRIND_MEMCHECK_H
55 #include <valgrind/memcheck.h>
56 #endif
58 #include "winbase.h"
59 #include "thread.h"
60 #include "winternl.h"
62 /* default errno before threading is initialized */
63 static int *default_errno_location(void)
65 static int static_errno;
66 return &static_errno;
69 /* default h_errno before threading is initialized */
70 static int *default_h_errno_location(void)
72 static int static_h_errno;
73 return &static_h_errno;
76 /* errno once threading is working */
77 static int *thread_errno_location(void)
79 return &NtCurrentTeb()->thread_errno;
82 /* h_errno once threading is working */
83 static int *thread_h_errno_location(void)
85 return &NtCurrentTeb()->thread_h_errno;
88 static int* (*errno_location_ptr)(void) = default_errno_location;
89 static int* (*h_errno_location_ptr)(void) = default_h_errno_location;
91 /***********************************************************************
92 * __errno_location/__error/__errno/___errno/__thr_errno
94 * Get the per-thread errno location.
96 int *__errno_location(void) { return errno_location_ptr(); } /* Linux */
97 int *__error(void) { return errno_location_ptr(); } /* FreeBSD */
98 int *__errno(void) { return errno_location_ptr(); } /* NetBSD */
99 int *___errno(void) { return errno_location_ptr(); } /* Solaris */
100 int *__thr_errno(void) { return errno_location_ptr(); } /* UnixWare */
102 /***********************************************************************
103 * __h_errno_location
105 * Get the per-thread h_errno location.
107 int *__h_errno_location(void)
109 return h_errno_location_ptr();
113 /* Currently this probably works only for glibc2,
114 * which checks for the presence of double-underscore-prepended
115 * pthread primitives, and use them if available.
116 * If they are not available, the libc defaults to
117 * non-threadsafe operation (not good). */
119 #if defined(__GLIBC__) || defined(__FreeBSD__)
121 #ifndef __USE_UNIX98
122 #define __USE_UNIX98
123 #endif
125 #include <pthread.h>
126 #include <signal.h>
128 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
130 #define PSTR(str) __ASM_NAME(#str)
132 /* adapt as necessary (a construct like this is used in glibc sources) */
133 #define strong_alias(orig, alias) \
134 asm(".globl " PSTR(alias) "\n" \
135 "\t.set " PSTR(alias) "," PSTR(orig))
137 /* thread descriptor */
139 #define FIRST_KEY 0
140 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
141 #define MAX_TSD 16
143 struct fork_block;
145 struct pthread_descr_struct
147 char dummy[2048];
148 struct __res_state res_state;
149 const void *key_data[MAX_KEYS]; /* for normal pthread keys */
150 const void *tsd_data[MAX_TSD]; /* for libc internal tsd variables */
153 typedef struct pthread_descr_struct *pthread_descr;
155 static struct pthread_descr_struct initial_descr;
157 pthread_descr __pthread_thread_self(void)
159 struct pthread_descr_struct *descr = NtCurrentTeb()->pthread_data;
160 if (!descr) return &initial_descr;
161 return descr;
163 strong_alias(__pthread_thread_self, pthread_thread_self);
165 /* pthread functions redirection */
167 struct pthread_functions
169 pid_t (*ptr_pthread_fork) (struct fork_block *);
170 int (*ptr_pthread_attr_destroy) (pthread_attr_t *);
171 int (*ptr___pthread_attr_init_2_0) (pthread_attr_t *);
172 int (*ptr___pthread_attr_init_2_1) (pthread_attr_t *);
173 int (*ptr_pthread_attr_getdetachstate) (const pthread_attr_t *, int *);
174 int (*ptr_pthread_attr_setdetachstate) (pthread_attr_t *, int);
175 int (*ptr_pthread_attr_getinheritsched) (const pthread_attr_t *, int *);
176 int (*ptr_pthread_attr_setinheritsched) (pthread_attr_t *, int);
177 int (*ptr_pthread_attr_getschedparam) (const pthread_attr_t *, struct sched_param *);
178 int (*ptr_pthread_attr_setschedparam) (pthread_attr_t *, const struct sched_param *);
179 int (*ptr_pthread_attr_getschedpolicy) (const pthread_attr_t *, int *);
180 int (*ptr_pthread_attr_setschedpolicy) (pthread_attr_t *, int);
181 int (*ptr_pthread_attr_getscope) (const pthread_attr_t *, int *);
182 int (*ptr_pthread_attr_setscope) (pthread_attr_t *, int);
183 int (*ptr_pthread_condattr_destroy) (pthread_condattr_t *);
184 int (*ptr_pthread_condattr_init) (pthread_condattr_t *);
185 int (*ptr___pthread_cond_broadcast) (pthread_cond_t *);
186 int (*ptr___pthread_cond_destroy) (pthread_cond_t *);
187 int (*ptr___pthread_cond_init) (pthread_cond_t *, const pthread_condattr_t *);
188 int (*ptr___pthread_cond_signal) (pthread_cond_t *);
189 int (*ptr___pthread_cond_wait) (pthread_cond_t *, pthread_mutex_t *);
190 int (*ptr_pthread_equal) (pthread_t, pthread_t);
191 void (*ptr___pthread_exit) (void *);
192 int (*ptr_pthread_getschedparam) (pthread_t, int *, struct sched_param *);
193 int (*ptr_pthread_setschedparam) (pthread_t, int, const struct sched_param *);
194 int (*ptr_pthread_mutex_destroy) (pthread_mutex_t *);
195 int (*ptr_pthread_mutex_init) (pthread_mutex_t *, const pthread_mutexattr_t *);
196 int (*ptr_pthread_mutex_lock) (pthread_mutex_t *);
197 int (*ptr_pthread_mutex_trylock) (pthread_mutex_t *);
198 int (*ptr_pthread_mutex_unlock) (pthread_mutex_t *);
199 pthread_t (*ptr_pthread_self) (void);
200 int (*ptr_pthread_setcancelstate) (int, int *);
201 int (*ptr_pthread_setcanceltype) (int, int *);
202 void (*ptr_pthread_do_exit) (void *retval, char *currentframe);
203 void (*ptr_pthread_cleanup_upto) (jmp_buf target, char *targetframe);
204 pthread_descr (*ptr_pthread_thread_self) (void);
205 int (*ptr_pthread_internal_tsd_set) (int key, const void *pointer);
206 void * (*ptr_pthread_internal_tsd_get) (int key);
207 void ** __attribute__ ((__const__)) (*ptr_pthread_internal_tsd_address) (int key);
208 int (*ptr_pthread_sigaction) (int sig, const struct sigaction * act, struct sigaction *oact);
209 int (*ptr_pthread_sigwait) (const sigset_t *set, int *sig);
210 int (*ptr_pthread_raise) (int sig);
213 static struct pthread_functions wine_pthread_functions;
214 static int init_done;
216 static pid_t (*libc_fork)(void);
217 static int (*libc_sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact);
218 static int (*libc_uselocale)(int set);
219 static int *(*libc_pthread_init)( const struct pthread_functions *funcs );
220 static int *libc_multiple_threads;
222 void PTHREAD_init_done(void)
224 init_done = 1;
225 if (!libc_fork) libc_fork = dlsym( RTLD_NEXT, "fork" );
226 if (!libc_sigaction) libc_sigaction = dlsym( RTLD_NEXT, "sigaction" );
229 struct __res_state *__res_state(void)
231 pthread_descr descr = __pthread_thread_self();
232 return &descr->res_state;
235 static inline void writejump( const char *symbol, void *dest )
237 #if defined(__GLIBC__) && defined(__i386__)
238 unsigned char *addr = dlsym( RTLD_NEXT, symbol );
240 if (!addr) return;
242 /* write a relative jump at the function address */
243 mprotect((void*)((unsigned int)addr & ~(getpagesize()-1)), 5, PROT_READ|PROT_EXEC|PROT_WRITE);
244 addr[0] = 0xe9;
245 *(int *)(addr+1) = (unsigned char *)dest - (addr + 5);
246 mprotect((void*)((unsigned int)addr & ~(getpagesize()-1)), 5, PROT_READ|PROT_EXEC);
248 # ifdef HAVE_VALGRIND_MEMCHECK_H
249 VALGRIND_DISCARD_TRANSLATIONS( addr, 5 );
250 # endif
251 #endif /* __GLIBC__ && __i386__ */
254 /***********************************************************************
255 * PTHREAD_init_thread
257 * Initialization for a newly created thread.
259 void PTHREAD_init_thread(void)
261 static int first = 1;
263 if (first)
265 first = 0;
266 NtCurrentTeb()->pthread_data = &initial_descr;
267 errno_location_ptr = thread_errno_location;
268 h_errno_location_ptr = thread_h_errno_location;
269 libc_uselocale = dlsym( RTLD_NEXT, "uselocale" );
270 libc_pthread_init = dlsym( RTLD_NEXT, "__libc_pthread_init" );
271 if (libc_pthread_init) libc_multiple_threads = libc_pthread_init( &wine_pthread_functions );
272 writejump( "__errno_location", thread_errno_location );
273 writejump( "__h_errno_location", thread_h_errno_location );
274 writejump( "__res_state", __res_state );
276 else
278 struct pthread_descr_struct *descr = calloc( 1, sizeof(*descr) );
279 NtCurrentTeb()->pthread_data = descr;
280 if (libc_multiple_threads) *libc_multiple_threads = 1;
282 if (libc_uselocale) libc_uselocale( -1 /*LC_GLOBAL_LOCALE*/ );
285 /* redefine this to prevent libpthread from overriding our function pointers */
286 int *__libc_pthread_init( const struct pthread_functions *funcs )
288 return libc_multiple_threads;
291 /* NOTE: This is a truly extremely incredibly ugly hack!
292 * But it does seem to work... */
294 /* assume that pthread_mutex_t has room for at least one pointer,
295 * and hope that the users of pthread_mutex_t considers it opaque
296 * (never checks what's in it)
297 * also: assume that static initializer sets pointer to NULL
299 typedef struct
301 #ifdef __GLIBC__
302 int reserved;
303 #endif
304 CRITICAL_SECTION *critsect;
305 } *wine_mutex;
307 /* see wine_mutex above for comments */
308 typedef struct {
309 RTL_RWLOCK *lock;
310 } *wine_rwlock;
312 typedef struct _wine_cleanup {
313 void (*routine)(void *);
314 void *arg;
315 } *wine_cleanup;
317 void __pthread_initialize(void)
321 struct pthread_thread_init {
322 void* (*start_routine)(void*);
323 void* arg;
326 static DWORD CALLBACK pthread_thread_start(LPVOID data)
328 struct pthread_thread_init init = *(struct pthread_thread_init*)data;
329 HeapFree(GetProcessHeap(),0,data);
330 return (DWORD)init.start_routine(init.arg);
333 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
334 (*start_routine)(void *), void* arg)
336 HANDLE hThread;
337 struct pthread_thread_init* idata = HeapAlloc(GetProcessHeap(), 0,
338 sizeof(struct pthread_thread_init));
340 idata->start_routine = start_routine;
341 idata->arg = arg;
342 hThread = CreateThread( NULL, 0, pthread_thread_start, idata, 0,
343 (LPDWORD)thread);
345 if(hThread)
346 CloseHandle(hThread);
347 else
349 HeapFree(GetProcessHeap(),0,idata); /* free idata struct on failure */
350 return EAGAIN;
353 return 0;
356 int pthread_cancel(pthread_t thread)
358 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD)thread);
360 if(!TerminateThread(hThread, 0))
362 CloseHandle(hThread);
363 return EINVAL; /* return error */
366 CloseHandle(hThread);
368 return 0; /* return success */
371 int pthread_join(pthread_t thread, void **value_ptr)
373 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD)thread);
375 WaitForSingleObject(hThread, INFINITE);
376 if(!GetExitCodeThread(hThread, (LPDWORD)value_ptr))
378 CloseHandle(hThread);
379 return EINVAL; /* FIXME: make this more correctly match */
380 } /* windows errors */
382 CloseHandle(hThread);
383 return 0;
386 /*FIXME: not sure what to do with this one... */
387 int pthread_detach(pthread_t thread)
389 P_OUTPUT("FIXME:pthread_detach\n");
390 return 0;
393 /* FIXME: we have no equivalents in win32 for the policys */
394 /* so just keep this as a stub */
395 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
397 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
398 return 0;
401 /* FIXME: no win32 equivalent for scope */
402 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
404 P_OUTPUT("FIXME:pthread_attr_setscope\n");
405 return 0; /* return success */
408 /* FIXME: no win32 equivalent for schedule param */
409 int pthread_attr_setschedparam(pthread_attr_t *attr,
410 const struct sched_param *param)
412 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
413 return 0; /* return success */
416 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
418 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
419 LONG once_now;
421 memcpy(&once_now,&the_once,sizeof(once_now));
422 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
423 (*init_routine)();
424 return 0;
426 strong_alias(__pthread_once, pthread_once);
428 void __pthread_kill_other_threads_np(void)
430 /* we don't need to do anything here */
432 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
434 /***** atfork *****/
436 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
438 static CRITICAL_SECTION atfork_section;
439 static CRITICAL_SECTION_DEBUG critsect_debug =
441 0, 0, &atfork_section,
442 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
443 0, 0, { 0, (DWORD)(__FILE__ ": atfork_section") }
445 static CRITICAL_SECTION atfork_section = { &critsect_debug, -1, 0, 0, 0, 0 };
447 typedef void (*atfork_handler)();
448 static atfork_handler atfork_prepare[MAX_ATFORK];
449 static atfork_handler atfork_parent[MAX_ATFORK];
450 static atfork_handler atfork_child[MAX_ATFORK];
451 static int atfork_count;
453 int __pthread_atfork(void (*prepare)(void),
454 void (*parent)(void),
455 void (*child)(void))
457 if (init_done) RtlEnterCriticalSection( &atfork_section );
458 assert( atfork_count < MAX_ATFORK );
459 atfork_prepare[atfork_count] = prepare;
460 atfork_parent[atfork_count] = parent;
461 atfork_child[atfork_count] = child;
462 atfork_count++;
463 if (init_done) RtlLeaveCriticalSection( &atfork_section );
464 return 0;
466 strong_alias(__pthread_atfork, pthread_atfork);
468 pid_t __fork(void)
470 pid_t pid;
471 int i;
473 if (!libc_fork)
475 libc_fork = dlsym( RTLD_NEXT, "fork" );
476 assert( libc_fork );
478 RtlEnterCriticalSection( &atfork_section );
479 /* prepare handlers are called in reverse insertion order */
480 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
481 if (!(pid = libc_fork()))
483 RtlInitializeCriticalSection( &atfork_section );
484 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
486 else
488 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
489 RtlLeaveCriticalSection( &atfork_section );
491 return pid;
493 strong_alias(__fork, fork);
495 /***** MUTEXES *****/
497 int __pthread_mutex_init(pthread_mutex_t *mutex,
498 const pthread_mutexattr_t *mutexattr)
500 /* glibc has a tendency to initialize mutexes very often, even
501 in situations where they are not really used later on.
503 As for us, initializing a mutex is very expensive, we postpone
504 the real initialization until the time the mutex is first used. */
506 ((wine_mutex)mutex)->critsect = NULL;
507 return 0;
509 strong_alias(__pthread_mutex_init, pthread_mutex_init);
511 static void mutex_real_init( pthread_mutex_t *mutex )
513 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
514 RtlInitializeCriticalSection(critsect);
516 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
517 /* too late, some other thread already did it */
518 RtlDeleteCriticalSection(critsect);
519 HeapFree(GetProcessHeap(), 0, critsect);
523 int __pthread_mutex_lock(pthread_mutex_t *mutex)
525 if (!init_done) return 0;
526 if (!((wine_mutex)mutex)->critsect)
527 mutex_real_init( mutex );
529 RtlEnterCriticalSection(((wine_mutex)mutex)->critsect);
530 return 0;
532 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
534 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
536 if (!init_done) return 0;
537 if (!((wine_mutex)mutex)->critsect)
538 mutex_real_init( mutex );
540 if (!RtlTryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
541 errno = EBUSY;
542 return -1;
544 return 0;
546 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
548 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
550 if (!((wine_mutex)mutex)->critsect) return 0;
551 RtlLeaveCriticalSection(((wine_mutex)mutex)->critsect);
552 return 0;
554 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
556 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
558 if (!((wine_mutex)mutex)->critsect) return 0;
559 if (((wine_mutex)mutex)->critsect->RecursionCount) {
560 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
561 return EBUSY;
562 #else
563 while (((wine_mutex)mutex)->critsect->RecursionCount)
564 RtlLeaveCriticalSection(((wine_mutex)mutex)->critsect);
565 #endif
567 RtlDeleteCriticalSection(((wine_mutex)mutex)->critsect);
568 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
569 ((wine_mutex)mutex)->critsect = NULL;
570 return 0;
572 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
575 /***** MUTEX ATTRIBUTES *****/
576 /* just dummies, since critical sections are always recursive */
578 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
580 return 0;
582 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
584 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
586 return 0;
588 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
590 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
592 return 0;
594 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
596 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
598 *kind = PTHREAD_MUTEX_RECURSIVE;
599 return 0;
601 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
603 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
605 return 0;
607 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
609 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
611 *kind = PTHREAD_MUTEX_RECURSIVE;
612 return 0;
614 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
617 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
619 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
621 static LONG keycnt = FIRST_KEY;
622 *key = InterlockedExchangeAdd(&keycnt, 1);
623 return 0;
625 strong_alias(__pthread_key_create, pthread_key_create);
627 int __pthread_key_delete(pthread_key_t key)
629 return 0;
631 strong_alias(__pthread_key_delete, pthread_key_delete);
633 int __pthread_setspecific(pthread_key_t key, const void *pointer)
635 pthread_descr descr = __pthread_thread_self();
636 descr->key_data[key] = pointer;
637 return 0;
639 strong_alias(__pthread_setspecific, pthread_setspecific);
641 void *__pthread_getspecific(pthread_key_t key)
643 pthread_descr descr = __pthread_thread_self();
644 return (void *)descr->key_data[key];
646 strong_alias(__pthread_getspecific, pthread_getspecific);
648 /* these are not exported, they are only used in the pthread_functions structure */
650 static int pthread_internal_tsd_set( int key, const void *pointer )
652 pthread_descr descr = __pthread_thread_self();
653 descr->tsd_data[key] = pointer;
654 return 0;
657 static void *pthread_internal_tsd_get( int key )
659 pthread_descr descr = __pthread_thread_self();
660 return (void *)descr->tsd_data[key];
663 static void ** __attribute__((const)) pthread_internal_tsd_address( int key )
665 pthread_descr descr = __pthread_thread_self();
666 return (void **)&descr->tsd_data[key];
669 /***** "EXCEPTION" FRAMES *****/
670 /* not implemented right now */
672 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
674 ((wine_cleanup)buffer)->routine = routine;
675 ((wine_cleanup)buffer)->arg = arg;
678 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
680 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
683 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
685 _pthread_cleanup_push(buffer, routine, arg);
688 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
690 _pthread_cleanup_pop(buffer, execute);
693 void __pthread_cleanup_upto(jmp_buf target, char *frame)
695 /* FIXME */
698 /***** CONDITIONS *****/
699 /* not implemented right now */
701 int __pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
703 P_OUTPUT("FIXME:pthread_cond_init\n");
704 return 0;
706 strong_alias(__pthread_cond_init, pthread_cond_init);
708 int __pthread_cond_destroy(pthread_cond_t *cond)
710 P_OUTPUT("FIXME:pthread_cond_destroy\n");
711 return 0;
713 strong_alias(__pthread_cond_destroy, pthread_cond_destroy);
715 int __pthread_cond_signal(pthread_cond_t *cond)
717 P_OUTPUT("FIXME:pthread_cond_signal\n");
718 return 0;
720 strong_alias(__pthread_cond_signal, pthread_cond_signal);
722 int __pthread_cond_broadcast(pthread_cond_t *cond)
724 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
725 return 0;
727 strong_alias(__pthread_cond_broadcast, pthread_cond_broadcast);
729 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
731 P_OUTPUT("FIXME:pthread_cond_wait\n");
732 return 0;
734 strong_alias(__pthread_cond_wait, pthread_cond_wait);
736 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
738 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
739 return 0;
741 strong_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
743 /**** CONDITION ATTRIBUTES *****/
744 /* not implemented right now */
746 int pthread_condattr_init(pthread_condattr_t *attr)
748 return 0;
751 int pthread_condattr_destroy(pthread_condattr_t *attr)
753 return 0;
756 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
757 /***** READ-WRITE LOCKS *****/
759 static void rwlock_real_init(pthread_rwlock_t *rwlock)
761 RTL_RWLOCK *lock = HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK));
762 RtlInitializeResource(lock);
764 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock)rwlock)->lock),lock,NULL) != NULL) {
765 /* too late, some other thread already did it */
766 RtlDeleteResource(lock);
767 HeapFree(GetProcessHeap(), 0, lock);
771 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
773 ((wine_rwlock)rwlock)->lock = NULL;
774 return 0;
776 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
778 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
780 if (!((wine_rwlock)rwlock)->lock) return 0;
781 RtlDeleteResource(((wine_rwlock)rwlock)->lock);
782 HeapFree(GetProcessHeap(), 0, ((wine_rwlock)rwlock)->lock);
783 return 0;
785 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
787 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
789 if (!init_done) return 0;
790 if (!((wine_rwlock)rwlock)->lock)
791 rwlock_real_init( rwlock );
793 while(TRUE)
794 if (RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, TRUE))
795 return 0;
797 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
799 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
801 if (!init_done) return 0;
802 if (!((wine_rwlock)rwlock)->lock)
803 rwlock_real_init( rwlock );
805 if (!RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, FALSE)) {
806 errno = EBUSY;
807 return -1;
809 return 0;
811 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
813 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
815 if (!init_done) return 0;
816 if (!((wine_rwlock)rwlock)->lock)
817 rwlock_real_init( rwlock );
819 while(TRUE)
820 if (RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, TRUE))
821 return 0;
823 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
825 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
827 if (!init_done) return 0;
828 if (!((wine_rwlock)rwlock)->lock)
829 rwlock_real_init( rwlock );
831 if (!RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, FALSE)) {
832 errno = EBUSY;
833 return -1;
835 return 0;
837 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
839 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
841 if (!((wine_rwlock)rwlock)->lock) return 0;
842 RtlReleaseResource( ((wine_rwlock)rwlock)->lock );
843 return 0;
845 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
847 /**** READ-WRITE LOCK ATTRIBUTES *****/
848 /* not implemented right now */
850 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
852 return 0;
855 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
857 return 0;
859 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
861 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
863 *pref = 0;
864 return 0;
867 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
869 return 0;
871 #endif /* glibc 2.2 */
873 /***** MISC *****/
875 pthread_t pthread_self(void)
877 return (pthread_t)GetCurrentThreadId();
880 int pthread_equal(pthread_t thread1, pthread_t thread2)
882 return (DWORD)thread1 == (DWORD)thread2;
885 void __pthread_do_exit(void *retval, char *currentframe)
887 /* FIXME: pthread cleanup */
888 ExitThread((DWORD)retval);
891 void __pthread_exit(void *retval)
893 __pthread_do_exit( retval, NULL );
895 strong_alias(__pthread_exit, pthread_exit);
897 int pthread_setcanceltype(int type, int *oldtype)
899 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
900 return 0;
903 /***** ANTI-OVERRIDES *****/
904 /* pthreads tries to override these, point them back to libc */
906 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
908 if (!libc_sigaction)
910 libc_sigaction = dlsym( RTLD_NEXT, "sigaction" );
911 assert( libc_sigaction );
913 return libc_sigaction(signum, act, oldact);
916 static struct pthread_functions wine_pthread_functions =
918 NULL, /* ptr_pthread_fork */
919 NULL, /* FIXME */ /* ptr_pthread_attr_destroy */
920 NULL, /* FIXME */ /* ptr___pthread_attr_init_2_0 */
921 NULL, /* FIXME */ /* ptr___pthread_attr_init_2_1 */
922 NULL, /* FIXME */ /* ptr_pthread_attr_getdetachstate */
923 NULL, /* FIXME */ /* ptr_pthread_attr_setdetachstate */
924 NULL, /* FIXME */ /* ptr_pthread_attr_getinheritsched */
925 NULL, /* FIXME */ /* ptr_pthread_attr_setinheritsched */
926 NULL, /* FIXME */ /* ptr_pthread_attr_getschedparam */
927 pthread_attr_setschedparam, /* ptr_pthread_attr_setschedparam */
928 NULL, /* FIXME */ /* ptr_pthread_attr_getschedpolicy) (const pthread_attr_t *, int *); */
929 NULL, /* FIXME */ /* ptr_pthread_attr_setschedpolicy) (pthread_attr_t *, int); */
930 NULL, /* FIXME */ /* ptr_pthread_attr_getscope) (const pthread_attr_t *, int *); */
931 NULL, /* FIXME */ /* ptr_pthread_attr_setscope) (pthread_attr_t *, int); */
932 pthread_condattr_destroy, /* ptr_pthread_condattr_destroy */
933 pthread_condattr_init, /* ptr_pthread_condattr_init */
934 __pthread_cond_broadcast, /* ptr___pthread_cond_broadcast */
935 __pthread_cond_destroy, /* ptr___pthread_cond_destroy */
936 __pthread_cond_init, /* ptr___pthread_cond_init */
937 __pthread_cond_signal, /* ptr___pthread_cond_signal */
938 __pthread_cond_wait, /* ptr___pthread_cond_wait */
939 pthread_equal, /* ptr_pthread_equal */
940 __pthread_exit, /* ptr___pthread_exit */
941 NULL, /* FIXME */ /* ptr_pthread_getschedparam */
942 NULL, /* FIXME */ /* ptr_pthread_setschedparam */
943 __pthread_mutex_destroy, /* ptr_pthread_mutex_destroy */
944 __pthread_mutex_init, /* ptr_pthread_mutex_init */
945 __pthread_mutex_lock, /* ptr_pthread_mutex_lock */
946 __pthread_mutex_trylock, /* ptr_pthread_mutex_trylock */
947 __pthread_mutex_unlock, /* ptr_pthread_mutex_unlock */
948 pthread_self, /* ptr_pthread_self */
949 NULL, /* FIXME */ /* ptr_pthread_setcancelstate */
950 pthread_setcanceltype, /* ptr_pthread_setcanceltype */
951 __pthread_do_exit, /* ptr_pthread_do_exit */
952 __pthread_cleanup_upto, /* ptr_pthread_cleanup_upto */
953 __pthread_thread_self, /* ptr_pthread_thread_self */
954 pthread_internal_tsd_set, /* ptr_pthread_internal_tsd_set */
955 pthread_internal_tsd_get, /* ptr_pthread_internal_tsd_get */
956 pthread_internal_tsd_address, /* ptr_pthread_internal_tsd_address */
957 NULL, /* ptr_pthread_sigaction */
958 NULL, /* ptr_pthread_sigwait */
959 NULL /* ptr_pthread_raise */
962 #endif /* __GLIBC__ || __FREEBSD__ */
964 #else /* HAVE_NPTL */
966 void PTHREAD_init_done(void)
970 #endif /* HAVE_NPTL */