ntdll: Set a proper HEAP_MIN_DATA_SIZE on 64-bit platforms.
[wine/wine-kai.git] / loader / kthread.c
blob577311b9566f26d2a293a5d7eb6cc8d9a686aba4
1 /*
2 * pthread emulation based on kernel threads
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
8 * Copyright 2003 Alexandre Julliard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 struct _pthread_cleanup_buffer;
30 #include <assert.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <setjmp.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <string.h>
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #ifdef HAVE_SYS_MMAN_H
44 #include <sys/mman.h>
45 #endif
46 #ifdef HAVE_NETINET_IN_H
47 # include <netinet/in.h>
48 #endif
49 #ifdef HAVE_ARPA_NAMESER_H
50 # include <arpa/nameser.h>
51 #endif
52 #ifdef HAVE_RESOLV_H
53 # include <resolv.h>
54 #endif
55 #ifdef HAVE_VALGRIND_MEMCHECK_H
56 #include <valgrind/memcheck.h>
57 #endif
58 #ifdef HAVE_SYS_SYSCALL_H
59 # include <sys/syscall.h>
60 #endif
61 #ifdef HAVE_SYS_LWP_H
62 # include <sys/lwp.h>
63 #endif
64 #ifdef HAVE_UCONTEXT_H
65 # include <ucontext.h>
66 #endif
67 #ifdef HAVE_SCHED_H
68 #include <sched.h>
69 #endif
71 #include "wine/library.h"
72 #include "wine/pthread.h"
74 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
76 #define PSTR(str) __ASM_NAME(#str)
78 static struct wine_pthread_callbacks funcs;
80 /* thread descriptor */
82 #define FIRST_KEY 0
83 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
84 #define MAX_TSD 16
86 struct pthread_descr_struct
88 char dummy[2048];
89 int thread_errno;
90 int thread_h_errno;
91 int cancel_state;
92 int cancel_type;
93 struct __res_state res_state;
94 const void *key_data[MAX_KEYS]; /* for normal pthread keys */
95 const void *tsd_data[MAX_TSD]; /* for libc internal tsd variables */
98 typedef struct pthread_descr_struct *pthread_descr;
100 static struct pthread_descr_struct initial_descr;
102 pthread_descr __pthread_thread_self(void)
104 struct pthread_descr_struct *descr;
105 if (!funcs.ptr_get_thread_data) return &initial_descr;
106 descr = funcs.ptr_get_thread_data();
107 if (!descr) return &initial_descr;
108 return descr;
111 static int (*libc_uselocale)(int set);
112 static int *libc_multiple_threads;
114 /***********************************************************************
115 * __errno_location/__error/__errno/___errno/__thr_errno
117 * Get the per-thread errno location.
119 int *__errno_location(void) /* Linux */
121 pthread_descr descr = __pthread_thread_self();
122 return &descr->thread_errno;
124 int *__error(void) { return __errno_location(); } /* FreeBSD */
125 int *__errno(void) { return __errno_location(); } /* NetBSD */
126 int *___errno(void) { return __errno_location(); } /* Solaris */
127 int *__thr_errno(void) { return __errno_location(); } /* UnixWare */
129 /***********************************************************************
130 * __h_errno_location
132 * Get the per-thread h_errno location.
134 int *__h_errno_location(void)
136 pthread_descr descr = __pthread_thread_self();
137 return &descr->thread_h_errno;
140 struct __res_state *__res_state(void)
142 pthread_descr descr = __pthread_thread_self();
143 return &descr->res_state;
146 static inline void writejump( const char *symbol, void *dest )
148 #if defined(__GLIBC__) && defined(__i386__)
149 unsigned char *addr = wine_dlsym( RTLD_NEXT, symbol, NULL, 0 );
151 if (!addr) return;
153 /* write a relative jump at the function address */
154 mprotect((void*)((unsigned int)addr & ~(getpagesize()-1)), 5, PROT_READ|PROT_EXEC|PROT_WRITE);
155 addr[0] = 0xe9;
156 *(int *)(addr+1) = (unsigned char *)dest - (addr + 5);
157 mprotect((void*)((unsigned int)addr & ~(getpagesize()-1)), 5, PROT_READ|PROT_EXEC);
159 # ifdef HAVE_VALGRIND_MEMCHECK_H
160 VALGRIND_DISCARD_TRANSLATIONS( addr, 5 );
161 # endif
162 #endif /* __GLIBC__ && __i386__ */
165 /* temporary stacks used on thread exit */
166 #define TEMP_STACK_SIZE 1024
167 #define NB_TEMP_STACKS 8
168 static char temp_stacks[NB_TEMP_STACKS][TEMP_STACK_SIZE];
169 static int next_temp_stack; /* next temp stack to use */
171 /***********************************************************************
172 * get_temp_stack
174 * Get a temporary stack address to run the thread exit code on.
176 inline static char *get_temp_stack(void)
178 unsigned int next = interlocked_xchg_add( &next_temp_stack, 1 );
179 return temp_stacks[next % NB_TEMP_STACKS] + TEMP_STACK_SIZE;
183 /***********************************************************************
184 * cleanup_thread
186 * Cleanup the remains of a thread. Runs on a temporary stack.
188 static void cleanup_thread( void *ptr )
190 /* copy the info structure since it is on the stack we will free */
191 struct wine_pthread_thread_info info = *(struct wine_pthread_thread_info *)ptr;
192 wine_ldt_free_fs( info.teb_sel );
193 if (info.stack_size) munmap( info.stack_base, info.stack_size );
194 if (info.teb_size) munmap( info.teb_base, info.teb_size );
195 #ifdef HAVE__LWP_CREATE
196 _lwp_exit();
197 #endif
198 _exit( info.exit_status );
202 /***********************************************************************
203 * init_process
205 * Initialization for a newly created process.
207 static void init_process( const struct wine_pthread_callbacks *callbacks, size_t size )
209 memcpy( &funcs, callbacks, min( size, sizeof(funcs) ));
210 funcs.ptr_set_thread_data( &initial_descr );
214 /***********************************************************************
215 * init_thread
217 * Initialization for a newly created thread.
219 static void init_thread( struct wine_pthread_thread_info *info )
221 struct pthread_descr_struct *descr;
223 if (funcs.ptr_set_thread_data)
225 descr = calloc( 1, sizeof(*descr) );
226 funcs.ptr_set_thread_data( descr );
227 if (libc_multiple_threads) *libc_multiple_threads = 1;
229 else /* first thread */
231 descr = &initial_descr;
232 writejump( "__errno_location", __errno_location );
233 writejump( "__h_errno_location", __h_errno_location );
234 writejump( "__res_state", __res_state );
236 descr->cancel_state = PTHREAD_CANCEL_ENABLE;
237 descr->cancel_type = PTHREAD_CANCEL_ASYNCHRONOUS;
238 if (libc_uselocale) libc_uselocale( -1 /*LC_GLOBAL_LOCALE*/ );
242 /***********************************************************************
243 * create_thread
245 static int create_thread( struct wine_pthread_thread_info *info )
247 if (!info->stack_base)
249 info->stack_base = wine_anon_mmap( NULL, info->stack_size,
250 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
251 if (info->stack_base == (void *)-1) return -1;
253 #ifdef HAVE_CLONE
254 if (clone( (int (*)(void *))info->entry, (char *)info->stack_base + info->stack_size,
255 CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD, info ) < 0)
256 return -1;
257 return 0;
258 #elif defined(HAVE_RFORK)
260 void **sp = (void **)((char *)info->stack_base + info->stack_size);
261 *--sp = info;
262 *--sp = 0;
263 *--sp = info->entry;
264 __asm__ __volatile__(
265 "pushl %2;\n\t" /* flags */
266 "pushl $0;\n\t" /* 0 ? */
267 "movl %1,%%eax;\n\t" /* SYS_rfork */
268 ".byte 0x9a; .long 0; .word 7;\n\t" /* lcall 7:0... FreeBSD syscall */
269 "cmpl $0, %%edx;\n\t"
270 "je 1f;\n\t"
271 "movl %0,%%esp;\n\t" /* child -> new thread */
272 "ret;\n"
273 "1:\n\t" /* parent -> caller thread */
274 "addl $8,%%esp" :
275 : "r" (sp), "r" (SYS_rfork), "r" (RFPROC | RFMEM | RFTHREAD)
276 : "eax", "edx");
277 return 0;
279 #elif defined(HAVE__LWP_CREATE)
281 ucontext_t context;
282 _lwp_makecontext( &context, (void(*)(void *))info->entry, info,
283 NULL, info->stack_base, info->stack_size );
284 if ( _lwp_create( &context, 0, NULL ) )
285 return -1;
286 return 0;
288 #endif
289 return -1;
293 /***********************************************************************
294 * init_current_teb
296 * Set the current TEB for a new thread.
298 static void init_current_teb( struct wine_pthread_thread_info *info )
300 #ifdef __i386__
301 /* On the i386, the current thread is in the %fs register */
302 LDT_ENTRY fs_entry;
304 wine_ldt_set_base( &fs_entry, info->teb_base );
305 wine_ldt_set_limit( &fs_entry, info->teb_size - 1 );
306 wine_ldt_set_flags( &fs_entry, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
307 wine_ldt_init_fs( info->teb_sel, &fs_entry );
308 #elif defined(HAVE__LWP_CREATE)
309 /* On non-i386 Solaris, we use the LWP private pointer */
310 _lwp_setprivate( info->teb_base );
311 #elif defined(__powerpc__)
312 /* On PowerPC, the current TEB is in the gpr13 register */
313 # ifdef __APPLE__
314 __asm__ __volatile__("mr r13, %0" : : "r" (info->teb_base));
315 # else
316 __asm__ __volatile__("mr 2, %0" : : "r" (info->teb_base));
317 # endif
318 #elif defined(__ALPHA__)
319 /* FIXME: On Alpha, the current TEB is not accessible to user-space */
320 /* __asm__ __volatile__();*/
321 #else
322 # error You must implement init_current_teb for your platform
323 #endif
325 /* set pid and tid */
326 info->pid = getpid();
327 #ifdef HAVE__LWP_SELF
328 info->tid = _lwp_self();
329 #else
330 info->tid = -1;
331 #endif
335 /***********************************************************************
336 * get_current_teb
338 static void *get_current_teb(void)
340 void *ret;
342 #ifdef __i386__
343 __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
344 #elif defined(HAVE__LWP_CREATE)
345 ret = _lwp_getprivate();
346 #elif defined(__powerpc__)
347 # ifdef __APPLE__
348 __asm__( "mr %0,r13" : "=r" (ret) );
349 # else
350 __asm__( "mr %0,2" : "=r" (ret) );
351 # endif
352 #elif defined(__ALPHA__)
353 /* 0x00ab is the PAL opcode for rdteb */
354 __asm__( "lda $30,8($30)\n\t"
355 "stq $0,0($30)\n\t"
356 "call_pal 0x00ab\n\t"
357 "mov $0,%0\n\t"
358 "ldq $0,0($30)\n\t"
359 "lda $30,-8($30)" : "=r" (ret) );
360 #else
361 # error get_current_teb not defined for this architecture
362 #endif /* __i386__ */
364 return ret;
368 /***********************************************************************
369 * exit_thread
371 static void DECLSPEC_NORETURN exit_thread( struct wine_pthread_thread_info *info )
373 wine_switch_to_stack( cleanup_thread, info, get_temp_stack() );
377 /***********************************************************************
378 * abort_thread
380 static void DECLSPEC_NORETURN abort_thread( long status )
382 #ifdef HAVE__LWP_CREATE
383 _lwp_exit();
384 #endif
385 _exit( status );
389 /***********************************************************************
390 * pthread_functions
392 const struct wine_pthread_functions pthread_functions =
394 init_process,
395 init_thread,
396 create_thread,
397 init_current_teb,
398 get_current_teb,
399 exit_thread,
400 abort_thread,
401 sigprocmask
405 /* Currently this probably works only for glibc2,
406 * which checks for the presence of double-underscore-prepended
407 * pthread primitives, and use them if available.
408 * If they are not available, the libc defaults to
409 * non-threadsafe operation (not good). */
411 #if defined(HAVE_PTHREAD_H) && (defined(__GLIBC__) || defined(__FreeBSD__))
413 /* adapt as necessary (a construct like this is used in glibc sources) */
414 #define strong_alias(orig, alias) \
415 asm(".globl " PSTR(alias) "\n" \
416 "\t.set " PSTR(alias) "," PSTR(orig))
418 struct fork_block;
420 /* pthread functions redirection */
422 struct pthread_functions
424 pid_t (*ptr_pthread_fork) (struct fork_block *);
425 int (*ptr_pthread_attr_destroy) (pthread_attr_t *);
426 int (*ptr___pthread_attr_init_2_0) (pthread_attr_t *);
427 int (*ptr___pthread_attr_init_2_1) (pthread_attr_t *);
428 int (*ptr_pthread_attr_getdetachstate) (const pthread_attr_t *, int *);
429 int (*ptr_pthread_attr_setdetachstate) (pthread_attr_t *, int);
430 int (*ptr_pthread_attr_getinheritsched) (const pthread_attr_t *, int *);
431 int (*ptr_pthread_attr_setinheritsched) (pthread_attr_t *, int);
432 int (*ptr_pthread_attr_getschedparam) (const pthread_attr_t *, struct sched_param *);
433 int (*ptr_pthread_attr_setschedparam) (pthread_attr_t *, const struct sched_param *);
434 int (*ptr_pthread_attr_getschedpolicy) (const pthread_attr_t *, int *);
435 int (*ptr_pthread_attr_setschedpolicy) (pthread_attr_t *, int);
436 int (*ptr_pthread_attr_getscope) (const pthread_attr_t *, int *);
437 int (*ptr_pthread_attr_setscope) (pthread_attr_t *, int);
438 int (*ptr_pthread_condattr_destroy) (pthread_condattr_t *);
439 int (*ptr_pthread_condattr_init) (pthread_condattr_t *);
440 int (*ptr___pthread_cond_broadcast) (pthread_cond_t *);
441 int (*ptr___pthread_cond_destroy) (pthread_cond_t *);
442 int (*ptr___pthread_cond_init) (pthread_cond_t *, const pthread_condattr_t *);
443 int (*ptr___pthread_cond_signal) (pthread_cond_t *);
444 int (*ptr___pthread_cond_wait) (pthread_cond_t *, pthread_mutex_t *);
445 int (*ptr_pthread_equal) (pthread_t, pthread_t);
446 void (*ptr___pthread_exit) (void *);
447 int (*ptr_pthread_getschedparam) (pthread_t, int *, struct sched_param *);
448 int (*ptr_pthread_setschedparam) (pthread_t, int, const struct sched_param *);
449 int (*ptr_pthread_mutex_destroy) (pthread_mutex_t *);
450 int (*ptr_pthread_mutex_init) (pthread_mutex_t *, const pthread_mutexattr_t *);
451 int (*ptr_pthread_mutex_lock) (pthread_mutex_t *);
452 int (*ptr_pthread_mutex_trylock) (pthread_mutex_t *);
453 int (*ptr_pthread_mutex_unlock) (pthread_mutex_t *);
454 pthread_t (*ptr_pthread_self) (void);
455 int (*ptr_pthread_setcancelstate) (int, int *);
456 int (*ptr_pthread_setcanceltype) (int, int *);
457 void (*ptr_pthread_do_exit) (void *retval, char *currentframe);
458 void (*ptr_pthread_cleanup_upto) (jmp_buf target, char *targetframe);
459 pthread_descr (*ptr_pthread_thread_self) (void);
460 int (*ptr_pthread_internal_tsd_set) (int key, const void *pointer);
461 void * (*ptr_pthread_internal_tsd_get) (int key);
462 void ** __attribute__ ((__const__)) (*ptr_pthread_internal_tsd_address) (int key);
463 int (*ptr_pthread_sigaction) (int sig, const struct sigaction * act, struct sigaction *oact);
464 int (*ptr_pthread_sigwait) (const sigset_t *set, int *sig);
465 int (*ptr_pthread_raise) (int sig);
466 int (*ptr___pthread_cond_timedwait) (pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
467 void (*ptr__pthread_cleanup_push) (struct _pthread_cleanup_buffer * buffer, void (*routine)(void *), void * arg);
468 void (*ptr__pthread_cleanup_pop) (struct _pthread_cleanup_buffer * buffer, int execute);
471 static pid_t (*libc_fork)(void);
472 static int (*libc_sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact);
473 static int *(*libc_pthread_init)( const struct pthread_functions *funcs );
475 static struct pthread_functions libc_pthread_functions;
477 strong_alias(__pthread_thread_self, pthread_thread_self);
479 /* redefine this to prevent libpthread from overriding our function pointers */
480 int *__libc_pthread_init( const struct pthread_functions *funcs )
482 return libc_multiple_threads;
485 typedef struct _wine_cleanup {
486 void (*routine)(void *);
487 void *arg;
488 } *wine_cleanup;
490 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
491 (*start_routine)(void *), void* arg)
493 assert( funcs.ptr_pthread_create );
494 return funcs.ptr_pthread_create( thread, attr, start_routine, arg );
497 int pthread_cancel(pthread_t thread)
499 assert( funcs.ptr_pthread_cancel );
500 return funcs.ptr_pthread_cancel( thread );
503 int pthread_join(pthread_t thread, void **value_ptr)
505 assert( funcs.ptr_pthread_join );
506 return funcs.ptr_pthread_join( thread, value_ptr );
509 int pthread_detach(pthread_t thread)
511 assert( funcs.ptr_pthread_detach );
512 return funcs.ptr_pthread_detach( thread );
515 /* FIXME: we have no equivalents in win32 for the policys */
516 /* so just keep this as a stub */
517 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
519 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
520 return 0;
523 /* FIXME: no win32 equivalent for scope */
524 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
526 P_OUTPUT("FIXME:pthread_attr_setscope\n");
527 return 0; /* return success */
530 /* FIXME: no win32 equivalent for schedule param */
531 int pthread_attr_setschedparam(pthread_attr_t *attr,
532 const struct sched_param *param)
534 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
535 return 0; /* return success */
538 /* FIXME */
539 int pthread_attr_setstack(pthread_attr_t *attr, void *addr, size_t size)
541 return 0; /* return success */
544 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
546 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
547 int once_now;
549 memcpy(&once_now,&the_once,sizeof(once_now));
550 if (interlocked_cmpxchg((int*)once_control, once_now+1, once_now) == once_now)
551 (*init_routine)();
552 return 0;
554 strong_alias(__pthread_once, pthread_once);
556 void __pthread_kill_other_threads_np(void)
558 /* we don't need to do anything here */
560 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
562 /***** atfork *****/
564 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
566 static pthread_mutex_t atfork_mutex = PTHREAD_MUTEX_INITIALIZER;
568 typedef void (*atfork_handler)(void);
569 static atfork_handler atfork_prepare[MAX_ATFORK];
570 static atfork_handler atfork_parent[MAX_ATFORK];
571 static atfork_handler atfork_child[MAX_ATFORK];
572 static int atfork_count;
574 int __pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
576 pthread_mutex_lock( &atfork_mutex );
577 assert( atfork_count < MAX_ATFORK );
578 atfork_prepare[atfork_count] = prepare;
579 atfork_parent[atfork_count] = parent;
580 atfork_child[atfork_count] = child;
581 atfork_count++;
582 pthread_mutex_unlock( &atfork_mutex );
583 return 0;
585 strong_alias(__pthread_atfork, pthread_atfork);
587 pid_t __fork(void)
589 pid_t pid;
590 int i;
592 if (!libc_fork)
594 libc_fork = wine_dlsym( RTLD_NEXT, "fork", NULL, 0 );
595 assert( libc_fork );
597 pthread_mutex_lock( &atfork_mutex );
598 /* prepare handlers are called in reverse insertion order */
599 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
600 if (!(pid = libc_fork()))
602 pthread_mutex_init( &atfork_mutex, NULL );
603 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
605 else
607 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
608 pthread_mutex_unlock( &atfork_mutex );
610 return pid;
612 strong_alias(__fork, fork);
614 /***** MUTEXES *****/
616 int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
618 if (!funcs.ptr_pthread_mutex_init) return 0;
619 return funcs.ptr_pthread_mutex_init( mutex, mutexattr );
621 strong_alias(__pthread_mutex_init, pthread_mutex_init);
623 int __pthread_mutex_lock(pthread_mutex_t *mutex)
625 if (!funcs.ptr_pthread_mutex_lock) return 0;
626 return funcs.ptr_pthread_mutex_lock( mutex );
628 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
630 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
632 if (!funcs.ptr_pthread_mutex_trylock) return 0;
633 return funcs.ptr_pthread_mutex_trylock( mutex );
635 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
637 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
639 if (!funcs.ptr_pthread_mutex_unlock) return 0;
640 return funcs.ptr_pthread_mutex_unlock( mutex );
642 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
644 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
646 if (!funcs.ptr_pthread_mutex_destroy) return 0;
647 return funcs.ptr_pthread_mutex_destroy( mutex );
649 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
652 /***** MUTEX ATTRIBUTES *****/
653 /* just dummies, since critical sections are always recursive */
655 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
657 return 0;
659 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
661 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
663 return 0;
665 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
667 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
669 return 0;
671 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
673 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
675 *kind = PTHREAD_MUTEX_RECURSIVE;
676 return 0;
678 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
680 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
682 return 0;
684 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
686 int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
688 *kind = PTHREAD_MUTEX_RECURSIVE;
689 return 0;
691 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
694 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
696 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
698 static int keycnt = FIRST_KEY;
699 *key = interlocked_xchg_add(&keycnt, 1);
700 return 0;
702 strong_alias(__pthread_key_create, pthread_key_create);
704 int __pthread_key_delete(pthread_key_t key)
706 return 0;
708 strong_alias(__pthread_key_delete, pthread_key_delete);
710 int __pthread_setspecific(pthread_key_t key, const void *pointer)
712 pthread_descr descr = __pthread_thread_self();
713 descr->key_data[key] = pointer;
714 return 0;
716 strong_alias(__pthread_setspecific, pthread_setspecific);
718 void *__pthread_getspecific(pthread_key_t key)
720 pthread_descr descr = __pthread_thread_self();
721 return (void *)descr->key_data[key];
723 strong_alias(__pthread_getspecific, pthread_getspecific);
725 static int pthread_internal_tsd_set( int key, const void *pointer )
727 pthread_descr descr = __pthread_thread_self();
728 descr->tsd_data[key] = pointer;
729 return 0;
731 int (*__libc_internal_tsd_set)(int, const void *) = pthread_internal_tsd_set;
733 static void *pthread_internal_tsd_get( int key )
735 pthread_descr descr = __pthread_thread_self();
736 return (void *)descr->tsd_data[key];
738 void* (*__libc_internal_tsd_get)(int) = pthread_internal_tsd_get;
740 static void ** __attribute__((const)) pthread_internal_tsd_address( int key )
742 pthread_descr descr = __pthread_thread_self();
743 return (void **)&descr->tsd_data[key];
745 void** (*__libc_internal_tsd_address)(int) = pthread_internal_tsd_address;
747 /***** "EXCEPTION" FRAMES *****/
748 /* not implemented right now */
750 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
752 ((wine_cleanup)buffer)->routine = routine;
753 ((wine_cleanup)buffer)->arg = arg;
756 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
758 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
761 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
763 _pthread_cleanup_push(buffer, routine, arg);
766 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
768 _pthread_cleanup_pop(buffer, execute);
771 void __pthread_cleanup_upto(jmp_buf target, char *frame)
773 /* FIXME */
776 /***** CONDITIONS *****/
778 int __pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
780 if (!funcs.ptr_pthread_cond_init) return 0;
781 return funcs.ptr_pthread_cond_init(cond, cond_attr);
783 strong_alias(__pthread_cond_init, pthread_cond_init);
785 int __pthread_cond_destroy(pthread_cond_t *cond)
787 if (!funcs.ptr_pthread_cond_destroy) return 0;
788 return funcs.ptr_pthread_cond_destroy(cond);
790 strong_alias(__pthread_cond_destroy, pthread_cond_destroy);
792 int __pthread_cond_signal(pthread_cond_t *cond)
794 if (!funcs.ptr_pthread_cond_signal) return 0;
795 return funcs.ptr_pthread_cond_signal(cond);
797 strong_alias(__pthread_cond_signal, pthread_cond_signal);
799 int __pthread_cond_broadcast(pthread_cond_t *cond)
801 if (!funcs.ptr_pthread_cond_broadcast) return 0;
802 return funcs.ptr_pthread_cond_broadcast(cond);
804 strong_alias(__pthread_cond_broadcast, pthread_cond_broadcast);
806 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
808 if (!funcs.ptr_pthread_cond_wait) return 0;
809 return funcs.ptr_pthread_cond_wait(cond, mutex);
811 strong_alias(__pthread_cond_wait, pthread_cond_wait);
813 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
815 if (!funcs.ptr_pthread_cond_timedwait) return 0;
816 return funcs.ptr_pthread_cond_timedwait(cond, mutex, abstime);
818 strong_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
820 /**** CONDITION ATTRIBUTES *****/
821 /* not implemented right now */
823 int pthread_condattr_init(pthread_condattr_t *attr)
825 return 0;
828 int pthread_condattr_destroy(pthread_condattr_t *attr)
830 return 0;
833 /***** READ-WRITE LOCKS *****/
835 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
837 assert( funcs.ptr_pthread_rwlock_init );
838 return funcs.ptr_pthread_rwlock_init( rwlock, rwlock_attr );
840 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
842 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
844 assert( funcs.ptr_pthread_rwlock_destroy );
845 return funcs.ptr_pthread_rwlock_destroy( rwlock );
847 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
849 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
851 if (!funcs.ptr_pthread_rwlock_rdlock) return 0;
852 return funcs.ptr_pthread_rwlock_rdlock( rwlock );
854 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
856 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
858 assert( funcs.ptr_pthread_rwlock_tryrdlock );
859 return funcs.ptr_pthread_rwlock_tryrdlock( rwlock );
861 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
863 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
865 assert( funcs.ptr_pthread_rwlock_wrlock );
866 return funcs.ptr_pthread_rwlock_wrlock( rwlock );
868 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
870 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
872 assert( funcs.ptr_pthread_rwlock_trywrlock );
873 return funcs.ptr_pthread_rwlock_trywrlock( rwlock );
875 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
877 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
879 if (!funcs.ptr_pthread_rwlock_unlock) return 0;
880 return funcs.ptr_pthread_rwlock_unlock( rwlock );
882 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
884 /**** READ-WRITE LOCK ATTRIBUTES *****/
885 /* not implemented right now */
887 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
889 return 0;
892 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
894 return 0;
896 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
898 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
900 *pref = 0;
901 return 0;
904 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
906 return 0;
909 /***** MISC *****/
911 pthread_t pthread_self(void)
913 assert( funcs.ptr_pthread_self );
914 return funcs.ptr_pthread_self();
917 int pthread_equal(pthread_t thread1, pthread_t thread2)
919 assert( funcs.ptr_pthread_equal );
920 return funcs.ptr_pthread_equal( thread1, thread2 );
923 void __pthread_do_exit(void *retval, char *currentframe)
925 assert( funcs.ptr_pthread_exit );
926 return funcs.ptr_pthread_exit( retval, currentframe );
929 void __pthread_exit(void *retval)
931 __pthread_do_exit( retval, NULL );
933 strong_alias(__pthread_exit, pthread_exit);
935 int pthread_setcancelstate(int state, int *oldstate)
937 pthread_descr descr = __pthread_thread_self();
938 if (oldstate) *oldstate = descr->cancel_state;
939 descr->cancel_state = state;
940 return 0;
943 int pthread_setcanceltype(int type, int *oldtype)
945 pthread_descr descr = __pthread_thread_self();
946 if (oldtype) *oldtype = descr->cancel_type;
947 descr->cancel_type = type;
948 return 0;
951 /***** ANTI-OVERRIDES *****/
952 /* pthreads tries to override these, point them back to libc */
954 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
956 if (!libc_sigaction)
958 libc_sigaction = wine_dlsym( RTLD_NEXT, "sigaction", NULL, 0 );
959 assert( libc_sigaction );
961 return libc_sigaction(signum, act, oldact);
964 void __pthread_initialize(void)
966 static int done;
968 if (!done)
970 done = 1;
971 libc_fork = wine_dlsym( RTLD_NEXT, "fork", NULL, 0 );
972 libc_sigaction = wine_dlsym( RTLD_NEXT, "sigaction", NULL, 0 );
973 libc_uselocale = wine_dlsym( RTLD_DEFAULT, "uselocale", NULL, 0 );
974 libc_pthread_init = wine_dlsym( RTLD_NEXT, "__libc_pthread_init", NULL, 0 );
975 if (libc_pthread_init) libc_multiple_threads = libc_pthread_init( &libc_pthread_functions );
979 #ifdef __GNUC__
980 static void init(void) __attribute__((constructor));
981 static void init(void)
983 __pthread_initialize();
985 #endif
987 static struct pthread_functions libc_pthread_functions =
989 NULL, /* ptr_pthread_fork */
990 NULL, /* FIXME */ /* ptr_pthread_attr_destroy */
991 NULL, /* FIXME */ /* ptr___pthread_attr_init_2_0 */
992 NULL, /* FIXME */ /* ptr___pthread_attr_init_2_1 */
993 NULL, /* FIXME */ /* ptr_pthread_attr_getdetachstate */
994 NULL, /* FIXME */ /* ptr_pthread_attr_setdetachstate */
995 NULL, /* FIXME */ /* ptr_pthread_attr_getinheritsched */
996 NULL, /* FIXME */ /* ptr_pthread_attr_setinheritsched */
997 NULL, /* FIXME */ /* ptr_pthread_attr_getschedparam */
998 pthread_attr_setschedparam, /* ptr_pthread_attr_setschedparam */
999 NULL, /* FIXME */ /* ptr_pthread_attr_getschedpolicy */
1000 NULL, /* FIXME */ /* ptr_pthread_attr_setschedpolicy */
1001 NULL, /* FIXME */ /* ptr_pthread_attr_getscope */
1002 NULL, /* FIXME */ /* ptr_pthread_attr_setscope */
1003 pthread_condattr_destroy, /* ptr_pthread_condattr_destroy */
1004 pthread_condattr_init, /* ptr_pthread_condattr_init */
1005 __pthread_cond_broadcast, /* ptr___pthread_cond_broadcast */
1006 __pthread_cond_destroy, /* ptr___pthread_cond_destroy */
1007 __pthread_cond_init, /* ptr___pthread_cond_init */
1008 __pthread_cond_signal, /* ptr___pthread_cond_signal */
1009 __pthread_cond_wait, /* ptr___pthread_cond_wait */
1010 pthread_equal, /* ptr_pthread_equal */
1011 __pthread_exit, /* ptr___pthread_exit */
1012 NULL, /* FIXME */ /* ptr_pthread_getschedparam */
1013 NULL, /* FIXME */ /* ptr_pthread_setschedparam */
1014 __pthread_mutex_destroy, /* ptr_pthread_mutex_destroy */
1015 __pthread_mutex_init, /* ptr_pthread_mutex_init */
1016 __pthread_mutex_lock, /* ptr_pthread_mutex_lock */
1017 __pthread_mutex_trylock, /* ptr_pthread_mutex_trylock */
1018 __pthread_mutex_unlock, /* ptr_pthread_mutex_unlock */
1019 pthread_self, /* ptr_pthread_self */
1020 pthread_setcancelstate, /* ptr_pthread_setcancelstate */
1021 pthread_setcanceltype, /* ptr_pthread_setcanceltype */
1022 __pthread_do_exit, /* ptr_pthread_do_exit */
1023 __pthread_cleanup_upto, /* ptr_pthread_cleanup_upto */
1024 __pthread_thread_self, /* ptr_pthread_thread_self */
1025 pthread_internal_tsd_set, /* ptr_pthread_internal_tsd_set */
1026 pthread_internal_tsd_get, /* ptr_pthread_internal_tsd_get */
1027 pthread_internal_tsd_address, /* ptr_pthread_internal_tsd_address */
1028 NULL, /* ptr_pthread_sigaction */
1029 NULL, /* ptr_pthread_sigwait */
1030 NULL, /* ptr_pthread_raise */
1031 __pthread_cond_timedwait, /* ptr___pthread_cond_timedwait */
1032 _pthread_cleanup_push, /* ptr__pthread_cleanup_push */
1033 _pthread_cleanup_pop /* ptr__pthread_cleanup_pop */
1036 #endif /* HAVE_PTHREAD_H && (__GLIBC__ || __FREEBSD__) */