Fix signed/unsigned comparison warnings.
[wine] / loader / kthread.c
1 /*
2  * pthread emulation based on kernel threads
3  *
4  * We can't use pthreads directly, so why not let libcs
5  * that want pthreads use Wine's own threading instead...
6  *
7  * Copyright 1999 Ove Kåven
8  * Copyright 2003 Alexandre Julliard
9  *
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.
14  *
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.
19  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 struct _pthread_cleanup_buffer;
29
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
70
71 #include "wine/library.h"
72 #include "wine/pthread.h"
73
74 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
75
76 #define PSTR(str) __ASM_NAME(#str)
77
78 static struct wine_pthread_functions funcs;
79
80 /* thread descriptor */
81
82 #define FIRST_KEY 0
83 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
84 #define MAX_TSD  16
85
86 struct pthread_descr_struct
87 {
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 */
96 };
97
98 typedef struct pthread_descr_struct *pthread_descr;
99
100 static struct pthread_descr_struct initial_descr;
101
102 pthread_descr __pthread_thread_self(void)
103 {
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;
109 }
110
111 static int (*libc_uselocale)(int set);
112 static int *libc_multiple_threads;
113
114 /***********************************************************************
115  *           __errno_location/__error/__errno/___errno/__thr_errno
116  *
117  * Get the per-thread errno location.
118  */
119 int *__errno_location(void)                            /* Linux */
120 {
121     pthread_descr descr = __pthread_thread_self();
122     return &descr->thread_errno;
123 }
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 */
128
129 /***********************************************************************
130  *           __h_errno_location
131  *
132  * Get the per-thread h_errno location.
133  */
134 int *__h_errno_location(void)
135 {
136     pthread_descr descr = __pthread_thread_self();
137     return &descr->thread_h_errno;
138 }
139
140 struct __res_state *__res_state(void)
141 {
142     pthread_descr descr = __pthread_thread_self();
143     return &descr->res_state;
144 }
145
146 static inline void writejump( const char *symbol, void *dest )
147 {
148 #if defined(__GLIBC__) && defined(__i386__)
149     unsigned char *addr = wine_dlsym( RTLD_NEXT, symbol, NULL, 0 );
150
151     if (!addr) return;
152
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);
158
159 # ifdef HAVE_VALGRIND_MEMCHECK_H
160     VALGRIND_DISCARD_TRANSLATIONS( addr, 5 );
161 # endif
162 #endif  /* __GLIBC__ && __i386__ */
163 }
164
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 LONG next_temp_stack;  /* next temp stack to use */
170
171 /***********************************************************************
172  *           get_temp_stack
173  *
174  * Get a temporary stack address to run the thread exit code on.
175  */
176 inline static char *get_temp_stack(void)
177 {
178     unsigned int next = interlocked_xchg_add( &next_temp_stack, 1 );
179     return temp_stacks[next % NB_TEMP_STACKS] + TEMP_STACK_SIZE;
180 }
181
182
183 /***********************************************************************
184  *           cleanup_thread
185  *
186  * Cleanup the remains of a thread. Runs on a temporary stack.
187  */
188 static void cleanup_thread( void *ptr )
189 {
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     munmap( info.stack_base, info.stack_size );
194     munmap( info.teb_base, info.teb_size );
195 #ifdef HAVE__LWP_CREATE
196     _lwp_exit();
197 #endif
198     _exit( info.exit_status );
199 }
200
201
202 /***********************************************************************
203  *           wine_pthread_init_process
204  *
205  * Initialization for a newly created process.
206  */
207 void wine_pthread_init_process( const struct wine_pthread_functions *functions )
208 {
209     memcpy( &funcs, functions, min(functions->size,sizeof(funcs)) );
210     funcs.ptr_set_thread_data( &initial_descr );
211 }
212
213
214 /***********************************************************************
215  *           wine_pthread_init_thread
216  *
217  * Initialization for a newly created thread.
218  */
219 void wine_pthread_init_thread( struct wine_pthread_thread_info *info )
220 {
221     struct pthread_descr_struct *descr;
222
223     if (funcs.ptr_set_thread_data)
224     {
225         descr = calloc( 1, sizeof(*descr) );
226         funcs.ptr_set_thread_data( descr );
227         if (libc_multiple_threads) *libc_multiple_threads = 1;
228     }
229     else  /* first thread */
230     {
231         descr = &initial_descr;
232         writejump( "__errno_location", __errno_location );
233         writejump( "__h_errno_location", __h_errno_location );
234         writejump( "__res_state", __res_state );
235     }
236     descr->cancel_state = PTHREAD_CANCEL_ENABLE;
237     descr->cancel_type  = PTHREAD_CANCEL_ASYNCHRONOUS;
238     if (libc_uselocale) libc_uselocale( -1 /*LC_GLOBAL_LOCALE*/ );
239 }
240
241
242 /***********************************************************************
243  *           wine_pthread_create_thread
244  */
245 int wine_pthread_create_thread( struct wine_pthread_thread_info *info )
246 {
247     if (!info->stack_base)
248     {
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;
252     }
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)
259     {
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), "g" (SYS_rfork), "g" (RFPROC | RFMEM | RFTHREAD)
276             : "eax", "edx");
277         return 0;
278     }
279 #elif defined(HAVE__LWP_CREATE)
280     {
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;
287     }
288 #endif
289     return -1;
290 }
291
292
293 /***********************************************************************
294  *           wine_pthread_init_current_teb
295  *
296  * Set the current TEB for a new thread.
297  */
298 void wine_pthread_init_current_teb( struct wine_pthread_thread_info *info )
299 {
300 #ifdef __i386__
301     /* On the i386, the current thread is in the %fs register */
302     LDT_ENTRY fs_entry;
303
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(__powerpc__)
309     /* On PowerPC, the current TEB is in the gpr13 register */
310 # ifdef __APPLE__
311     __asm__ __volatile__("mr r13, %0" : : "r" (info->teb_base));
312 # else
313     __asm__ __volatile__("mr 2, %0" : : "r" (info->teb_base));
314 # endif
315 #elif defined(HAVE__LWP_CREATE)
316     /* On non-i386 Solaris, we use the LWP private pointer */
317     _lwp_setprivate( info->teb_base );
318 #endif
319
320     /* set pid and tid */
321     info->pid = getpid();
322 #ifdef HAVE__LWP_SELF
323     info->tid = _lwp_self();
324 #else
325     info->tid = -1;
326 #endif
327 }
328
329
330 /***********************************************************************
331  *           wine_pthread_get_current_teb
332  */
333 void *wine_pthread_get_current_teb(void)
334 {
335     void *ret;
336
337 #ifdef __i386__
338     __asm__( ".byte 0x64\n\tmovl 0x18,%0" : "=r" (ret) );
339 #elif defined(HAVE__LWP_CREATE)
340     ret = _lwp_getprivate();
341 #elif defined(__powerpc__)
342 # ifdef __APPLE__
343     __asm__( "mr %0,r13" : "=r" (ret) );
344 # else
345     __asm__( "mr %0,2" : "=r" (ret) );
346 # endif
347 #else
348 # error wine_pthread_get_current_teb not defined for this architecture
349 #endif  /* __i386__ */
350
351     return ret;
352 }
353
354
355 /***********************************************************************
356  *           wine_pthread_exit_thread
357  */
358 void wine_pthread_exit_thread( struct wine_pthread_thread_info *info )
359 {
360     wine_switch_to_stack( cleanup_thread, info, get_temp_stack() );
361 }
362
363
364 /***********************************************************************
365  *           wine_pthread_abort_thread
366  */
367 void wine_pthread_abort_thread( int status )
368 {
369 #ifdef HAVE__LWP_CREATE
370     _lwp_exit();
371 #endif
372     _exit( status );
373 }
374
375
376 /* Currently this probably works only for glibc2,
377  * which checks for the presence of double-underscore-prepended
378  * pthread primitives, and use them if available.
379  * If they are not available, the libc defaults to
380  * non-threadsafe operation (not good). */
381
382 #if defined(__GLIBC__) || defined(__FreeBSD__)
383
384 /* adapt as necessary (a construct like this is used in glibc sources) */
385 #define strong_alias(orig, alias) \
386  asm(".globl " PSTR(alias) "\n" \
387      "\t.set " PSTR(alias) "," PSTR(orig))
388
389 struct fork_block;
390
391 /* pthread functions redirection */
392
393 struct pthread_functions
394 {
395   pid_t (*ptr_pthread_fork) (struct fork_block *);
396   int (*ptr_pthread_attr_destroy) (pthread_attr_t *);
397   int (*ptr___pthread_attr_init_2_0) (pthread_attr_t *);
398   int (*ptr___pthread_attr_init_2_1) (pthread_attr_t *);
399   int (*ptr_pthread_attr_getdetachstate) (const pthread_attr_t *, int *);
400   int (*ptr_pthread_attr_setdetachstate) (pthread_attr_t *, int);
401   int (*ptr_pthread_attr_getinheritsched) (const pthread_attr_t *, int *);
402   int (*ptr_pthread_attr_setinheritsched) (pthread_attr_t *, int);
403   int (*ptr_pthread_attr_getschedparam) (const pthread_attr_t *, struct sched_param *);
404   int (*ptr_pthread_attr_setschedparam) (pthread_attr_t *, const struct sched_param *);
405   int (*ptr_pthread_attr_getschedpolicy) (const pthread_attr_t *, int *);
406   int (*ptr_pthread_attr_setschedpolicy) (pthread_attr_t *, int);
407   int (*ptr_pthread_attr_getscope) (const pthread_attr_t *, int *);
408   int (*ptr_pthread_attr_setscope) (pthread_attr_t *, int);
409   int (*ptr_pthread_condattr_destroy) (pthread_condattr_t *);
410   int (*ptr_pthread_condattr_init) (pthread_condattr_t *);
411   int (*ptr___pthread_cond_broadcast) (pthread_cond_t *);
412   int (*ptr___pthread_cond_destroy) (pthread_cond_t *);
413   int (*ptr___pthread_cond_init) (pthread_cond_t *, const pthread_condattr_t *);
414   int (*ptr___pthread_cond_signal) (pthread_cond_t *);
415   int (*ptr___pthread_cond_wait) (pthread_cond_t *, pthread_mutex_t *);
416   int (*ptr_pthread_equal) (pthread_t, pthread_t);
417   void (*ptr___pthread_exit) (void *);
418   int (*ptr_pthread_getschedparam) (pthread_t, int *, struct sched_param *);
419   int (*ptr_pthread_setschedparam) (pthread_t, int, const struct sched_param *);
420   int (*ptr_pthread_mutex_destroy) (pthread_mutex_t *);
421   int (*ptr_pthread_mutex_init) (pthread_mutex_t *, const pthread_mutexattr_t *);
422   int (*ptr_pthread_mutex_lock) (pthread_mutex_t *);
423   int (*ptr_pthread_mutex_trylock) (pthread_mutex_t *);
424   int (*ptr_pthread_mutex_unlock) (pthread_mutex_t *);
425   pthread_t (*ptr_pthread_self) (void);
426   int (*ptr_pthread_setcancelstate) (int, int *);
427   int (*ptr_pthread_setcanceltype) (int, int *);
428   void (*ptr_pthread_do_exit) (void *retval, char *currentframe);
429   void (*ptr_pthread_cleanup_upto) (jmp_buf target, char *targetframe);
430   pthread_descr (*ptr_pthread_thread_self) (void);
431   int (*ptr_pthread_internal_tsd_set) (int key, const void *pointer);
432   void * (*ptr_pthread_internal_tsd_get) (int key);
433   void ** __attribute__ ((__const__)) (*ptr_pthread_internal_tsd_address) (int key);
434   int (*ptr_pthread_sigaction) (int sig, const struct sigaction * act, struct sigaction *oact);
435   int (*ptr_pthread_sigwait) (const sigset_t *set, int *sig);
436   int (*ptr_pthread_raise) (int sig);
437   int (*ptr___pthread_cond_timedwait) (pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
438   void (*ptr__pthread_cleanup_push) (struct _pthread_cleanup_buffer * buffer, void (*routine)(void *), void * arg);
439   void (*ptr__pthread_cleanup_pop) (struct _pthread_cleanup_buffer * buffer, int execute);
440 };
441
442 static pid_t (*libc_fork)(void);
443 static int (*libc_sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact);
444 static int *(*libc_pthread_init)( const struct pthread_functions *funcs );
445
446 static struct pthread_functions libc_pthread_functions;
447
448 strong_alias(__pthread_thread_self, pthread_thread_self);
449
450 /* redefine this to prevent libpthread from overriding our function pointers */
451 int *__libc_pthread_init( const struct pthread_functions *funcs )
452 {
453     return libc_multiple_threads;
454 }
455
456 typedef struct _wine_cleanup {
457   void (*routine)(void *);
458   void *arg;
459 } *wine_cleanup;
460
461 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
462         (*start_routine)(void *), void* arg)
463 {
464     assert( funcs.ptr_pthread_create );
465     return funcs.ptr_pthread_create( thread, attr, start_routine, arg );
466 }
467
468 int pthread_cancel(pthread_t thread)
469 {
470     assert( funcs.ptr_pthread_cancel );
471     return funcs.ptr_pthread_cancel( thread );
472 }
473
474 int pthread_join(pthread_t thread, void **value_ptr)
475 {
476     assert( funcs.ptr_pthread_join );
477     return funcs.ptr_pthread_join( thread, value_ptr );
478 }
479
480 int pthread_detach(pthread_t thread)
481 {
482     assert( funcs.ptr_pthread_detach );
483     return funcs.ptr_pthread_detach( thread );
484 }
485
486 /* FIXME: we have no equivalents in win32 for the policys */
487 /* so just keep this as a stub */
488 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
489 {
490   P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
491   return 0;
492 }
493
494 /* FIXME: no win32 equivalent for scope */
495 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
496 {
497   P_OUTPUT("FIXME:pthread_attr_setscope\n");
498   return 0; /* return success */
499 }
500
501 /* FIXME: no win32 equivalent for schedule param */
502 int pthread_attr_setschedparam(pthread_attr_t *attr,
503     const struct sched_param *param)
504 {
505   P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
506   return 0; /* return success */
507 }
508
509 /* FIXME */
510 int pthread_attr_setstack(pthread_attr_t *attr, void *addr, size_t size)
511 {
512   return 0; /* return success */
513 }
514
515 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
516 {
517     static pthread_once_t the_once = PTHREAD_ONCE_INIT;
518     long once_now;
519
520     memcpy(&once_now,&the_once,sizeof(once_now));
521     if (interlocked_cmpxchg((long*)once_control, once_now+1, once_now) == once_now)
522         (*init_routine)();
523     return 0;
524 }
525 strong_alias(__pthread_once, pthread_once);
526
527 void __pthread_kill_other_threads_np(void)
528 {
529     /* we don't need to do anything here */
530 }
531 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
532
533 /***** atfork *****/
534
535 #define MAX_ATFORK 8  /* libc doesn't need that many anyway */
536
537 static pthread_mutex_t atfork_mutex = PTHREAD_MUTEX_INITIALIZER;
538
539 typedef void (*atfork_handler)();
540 static atfork_handler atfork_prepare[MAX_ATFORK];
541 static atfork_handler atfork_parent[MAX_ATFORK];
542 static atfork_handler atfork_child[MAX_ATFORK];
543 static int atfork_count;
544
545 int __pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
546 {
547     pthread_mutex_lock( &atfork_mutex );
548     assert( atfork_count < MAX_ATFORK );
549     atfork_prepare[atfork_count] = prepare;
550     atfork_parent[atfork_count] = parent;
551     atfork_child[atfork_count] = child;
552     atfork_count++;
553     pthread_mutex_unlock( &atfork_mutex );
554     return 0;
555 }
556 strong_alias(__pthread_atfork, pthread_atfork);
557
558 pid_t __fork(void)
559 {
560     pid_t pid;
561     int i;
562
563     if (!libc_fork)
564     {
565         libc_fork = wine_dlsym( RTLD_NEXT, "fork", NULL, 0 );
566         assert( libc_fork );
567     }
568     pthread_mutex_lock( &atfork_mutex );
569     /* prepare handlers are called in reverse insertion order */
570     for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
571     if (!(pid = libc_fork()))
572     {
573         pthread_mutex_init( &atfork_mutex, NULL );
574         for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
575     }
576     else
577     {
578         for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
579         pthread_mutex_unlock( &atfork_mutex );
580     }
581     return pid;
582 }
583 strong_alias(__fork, fork);
584
585 /***** MUTEXES *****/
586
587 int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
588 {
589     if (!funcs.ptr_pthread_mutex_init) return 0;
590     return funcs.ptr_pthread_mutex_init( mutex, mutexattr );
591 }
592 strong_alias(__pthread_mutex_init, pthread_mutex_init);
593
594 int __pthread_mutex_lock(pthread_mutex_t *mutex)
595 {
596     if (!funcs.ptr_pthread_mutex_lock) return 0;
597     return funcs.ptr_pthread_mutex_lock( mutex );
598 }
599 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
600
601 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
602 {
603     if (!funcs.ptr_pthread_mutex_trylock) return 0;
604     return funcs.ptr_pthread_mutex_trylock( mutex );
605 }
606 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
607
608 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
609 {
610     if (!funcs.ptr_pthread_mutex_unlock) return 0;
611     return funcs.ptr_pthread_mutex_unlock( mutex );
612 }
613 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
614
615 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
616 {
617     if (!funcs.ptr_pthread_mutex_destroy) return 0;
618     return funcs.ptr_pthread_mutex_destroy( mutex );
619 }
620 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
621
622
623 /***** MUTEX ATTRIBUTES *****/
624 /* just dummies, since critical sections are always recursive */
625
626 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
627 {
628   return 0;
629 }
630 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
631
632 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
633 {
634   return 0;
635 }
636 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
637
638 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
639 {
640   return 0;
641 }
642 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
643
644 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
645 {
646   *kind = PTHREAD_MUTEX_RECURSIVE;
647   return 0;
648 }
649 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
650
651 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
652 {
653   return 0;
654 }
655 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
656
657 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
658 {
659   *kind = PTHREAD_MUTEX_RECURSIVE;
660   return 0;
661 }
662 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
663
664
665 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
666
667 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
668 {
669     static long keycnt = FIRST_KEY;
670     *key = interlocked_xchg_add(&keycnt, 1);
671     return 0;
672 }
673 strong_alias(__pthread_key_create, pthread_key_create);
674
675 int __pthread_key_delete(pthread_key_t key)
676 {
677   return 0;
678 }
679 strong_alias(__pthread_key_delete, pthread_key_delete);
680
681 int __pthread_setspecific(pthread_key_t key, const void *pointer)
682 {
683     pthread_descr descr = __pthread_thread_self();
684     descr->key_data[key] = pointer;
685     return 0;
686 }
687 strong_alias(__pthread_setspecific, pthread_setspecific);
688
689 void *__pthread_getspecific(pthread_key_t key)
690 {
691     pthread_descr descr = __pthread_thread_self();
692     return (void *)descr->key_data[key];
693 }
694 strong_alias(__pthread_getspecific, pthread_getspecific);
695
696 static int pthread_internal_tsd_set( int key, const void *pointer )
697 {
698     pthread_descr descr = __pthread_thread_self();
699     descr->tsd_data[key] = pointer;
700     return 0;
701 }
702 int (*__libc_internal_tsd_set)(int, const void *) = pthread_internal_tsd_set;
703
704 static void *pthread_internal_tsd_get( int key )
705 {
706     pthread_descr descr = __pthread_thread_self();
707     return (void *)descr->tsd_data[key];
708 }
709 void* (*__libc_internal_tsd_get)(int) = pthread_internal_tsd_get;
710
711 static void ** __attribute__((const)) pthread_internal_tsd_address( int key )
712 {
713     pthread_descr descr = __pthread_thread_self();
714     return (void **)&descr->tsd_data[key];
715 }
716 void** (*__libc_internal_tsd_address)(int) = pthread_internal_tsd_address;
717
718 /***** "EXCEPTION" FRAMES *****/
719 /* not implemented right now */
720
721 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
722 {
723   ((wine_cleanup)buffer)->routine = routine;
724   ((wine_cleanup)buffer)->arg = arg;
725 }
726
727 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
728 {
729   if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
730 }
731
732 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
733 {
734   _pthread_cleanup_push(buffer, routine, arg);
735 }
736
737 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
738 {
739   _pthread_cleanup_pop(buffer, execute);
740 }
741
742 void __pthread_cleanup_upto(jmp_buf target, char *frame)
743 {
744     /* FIXME */
745 }
746
747 /***** CONDITIONS *****/
748
749 int __pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
750 {
751     if (!funcs.ptr_pthread_cond_init) return 0;
752     return funcs.ptr_pthread_cond_init(cond, cond_attr);
753 }
754 strong_alias(__pthread_cond_init, pthread_cond_init);
755
756 int __pthread_cond_destroy(pthread_cond_t *cond)
757 {
758     if (!funcs.ptr_pthread_cond_destroy) return 0;
759     return funcs.ptr_pthread_cond_destroy(cond);
760 }
761 strong_alias(__pthread_cond_destroy, pthread_cond_destroy);
762
763 int __pthread_cond_signal(pthread_cond_t *cond)
764 {
765     if (!funcs.ptr_pthread_cond_signal) return 0;
766     return funcs.ptr_pthread_cond_signal(cond);
767 }
768 strong_alias(__pthread_cond_signal, pthread_cond_signal);
769
770 int __pthread_cond_broadcast(pthread_cond_t *cond)
771 {
772     if (!funcs.ptr_pthread_cond_broadcast) return 0;
773     return funcs.ptr_pthread_cond_broadcast(cond);
774 }
775 strong_alias(__pthread_cond_broadcast, pthread_cond_broadcast);
776
777 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
778 {
779     if (!funcs.ptr_pthread_cond_wait) return 0;
780     return funcs.ptr_pthread_cond_wait(cond, mutex);
781 }
782 strong_alias(__pthread_cond_wait, pthread_cond_wait);
783
784 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
785 {
786     if (!funcs.ptr_pthread_cond_timedwait) return 0;
787     return funcs.ptr_pthread_cond_timedwait(cond, mutex, abstime);
788 }
789 strong_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
790
791 /**** CONDITION ATTRIBUTES *****/
792 /* not implemented right now */
793
794 int pthread_condattr_init(pthread_condattr_t *attr)
795 {
796   return 0;
797 }
798
799 int pthread_condattr_destroy(pthread_condattr_t *attr)
800 {
801   return 0;
802 }
803
804 /***** READ-WRITE LOCKS *****/
805
806 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
807 {
808     assert( funcs.ptr_pthread_rwlock_init );
809     return funcs.ptr_pthread_rwlock_init( rwlock, rwlock_attr );
810 }
811 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
812
813 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
814 {
815     assert( funcs.ptr_pthread_rwlock_destroy );
816     return funcs.ptr_pthread_rwlock_destroy( rwlock );
817 }
818 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
819
820 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
821 {
822     if (!funcs.ptr_pthread_rwlock_rdlock) return 0;
823     return funcs.ptr_pthread_rwlock_rdlock( rwlock );
824 }
825 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
826
827 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
828 {
829     assert( funcs.ptr_pthread_rwlock_tryrdlock );
830     return funcs.ptr_pthread_rwlock_tryrdlock( rwlock );
831 }
832 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
833
834 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
835 {
836     assert( funcs.ptr_pthread_rwlock_wrlock );
837     return funcs.ptr_pthread_rwlock_wrlock( rwlock );
838 }
839 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
840
841 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
842 {
843     assert( funcs.ptr_pthread_rwlock_trywrlock );
844     return funcs.ptr_pthread_rwlock_trywrlock( rwlock );
845 }
846 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
847
848 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
849 {
850     if (!funcs.ptr_pthread_rwlock_unlock) return 0;
851     return funcs.ptr_pthread_rwlock_unlock( rwlock );
852 }
853 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
854
855 /**** READ-WRITE LOCK ATTRIBUTES *****/
856 /* not implemented right now */
857
858 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
859 {
860   return 0;
861 }
862
863 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
864 {
865   return 0;
866 }
867 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
868
869 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
870 {
871   *pref = 0;
872   return 0;
873 }
874
875 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
876 {
877   return 0;
878 }
879
880 /***** MISC *****/
881
882 pthread_t pthread_self(void)
883 {
884     assert( funcs.ptr_pthread_self );
885     return funcs.ptr_pthread_self();
886 }
887
888 int pthread_equal(pthread_t thread1, pthread_t thread2)
889 {
890     assert( funcs.ptr_pthread_equal );
891     return funcs.ptr_pthread_equal( thread1, thread2 );
892 }
893
894 void __pthread_do_exit(void *retval, char *currentframe)
895 {
896     assert( funcs.ptr_pthread_exit );
897     return funcs.ptr_pthread_exit( retval, currentframe );
898 }
899
900 void __pthread_exit(void *retval)
901 {
902     __pthread_do_exit( retval, NULL );
903 }
904 strong_alias(__pthread_exit, pthread_exit);
905
906 int pthread_setcancelstate(int state, int *oldstate)
907 {
908     pthread_descr descr = __pthread_thread_self();
909     if (oldstate) *oldstate = descr->cancel_state;
910     descr->cancel_state = state;
911     return 0;
912 }
913
914 int pthread_setcanceltype(int type, int *oldtype)
915 {
916     pthread_descr descr = __pthread_thread_self();
917     if (oldtype) *oldtype = descr->cancel_type;
918     descr->cancel_type = type;
919     return 0;
920 }
921
922 /***** ANTI-OVERRIDES *****/
923 /* pthreads tries to override these, point them back to libc */
924
925 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
926 {
927     if (!libc_sigaction)
928     {
929         libc_sigaction = wine_dlsym( RTLD_NEXT, "sigaction", NULL, 0 );
930         assert( libc_sigaction );
931     }
932     return libc_sigaction(signum, act, oldact);
933 }
934
935 void __pthread_initialize(void)
936 {
937     static int done;
938
939     if (!done)
940     {
941         done = 1;
942         libc_fork = wine_dlsym( RTLD_NEXT, "fork", NULL, 0 );
943         libc_sigaction = wine_dlsym( RTLD_NEXT, "sigaction", NULL, 0 );
944         libc_uselocale = wine_dlsym( RTLD_DEFAULT, "uselocale", NULL, 0 );
945         libc_pthread_init = wine_dlsym( RTLD_NEXT, "__libc_pthread_init", NULL, 0 );
946         if (libc_pthread_init) libc_multiple_threads = libc_pthread_init( &libc_pthread_functions );
947     }
948 }
949 DECL_GLOBAL_CONSTRUCTOR(init) { __pthread_initialize(); }
950
951 static struct pthread_functions libc_pthread_functions =
952 {
953     NULL,                          /* ptr_pthread_fork */
954     NULL, /* FIXME */              /* ptr_pthread_attr_destroy */
955     NULL, /* FIXME */              /* ptr___pthread_attr_init_2_0 */
956     NULL, /* FIXME */              /* ptr___pthread_attr_init_2_1 */
957     NULL, /* FIXME */              /* ptr_pthread_attr_getdetachstate */
958     NULL, /* FIXME */              /* ptr_pthread_attr_setdetachstate */
959     NULL, /* FIXME */              /* ptr_pthread_attr_getinheritsched */
960     NULL, /* FIXME */              /* ptr_pthread_attr_setinheritsched */
961     NULL, /* FIXME */              /* ptr_pthread_attr_getschedparam */
962     pthread_attr_setschedparam,    /* ptr_pthread_attr_setschedparam */
963     NULL, /* FIXME */              /* ptr_pthread_attr_getschedpolicy */
964     NULL, /* FIXME */              /* ptr_pthread_attr_setschedpolicy */
965     NULL, /* FIXME */              /* ptr_pthread_attr_getscope */
966     NULL, /* FIXME */              /* ptr_pthread_attr_setscope */
967     pthread_condattr_destroy,      /* ptr_pthread_condattr_destroy */
968     pthread_condattr_init,         /* ptr_pthread_condattr_init */
969     __pthread_cond_broadcast,      /* ptr___pthread_cond_broadcast */
970     __pthread_cond_destroy,        /* ptr___pthread_cond_destroy */
971     __pthread_cond_init,           /* ptr___pthread_cond_init */
972     __pthread_cond_signal,         /* ptr___pthread_cond_signal */
973     __pthread_cond_wait,           /* ptr___pthread_cond_wait */
974     pthread_equal,                 /* ptr_pthread_equal */
975     __pthread_exit,                /* ptr___pthread_exit */
976     NULL, /* FIXME */              /* ptr_pthread_getschedparam */
977     NULL, /* FIXME */              /* ptr_pthread_setschedparam */
978     __pthread_mutex_destroy,       /* ptr_pthread_mutex_destroy */
979     __pthread_mutex_init,          /* ptr_pthread_mutex_init */
980     __pthread_mutex_lock,          /* ptr_pthread_mutex_lock */
981     __pthread_mutex_trylock,       /* ptr_pthread_mutex_trylock */
982     __pthread_mutex_unlock,        /* ptr_pthread_mutex_unlock */
983     pthread_self,                  /* ptr_pthread_self */
984     pthread_setcancelstate,        /* ptr_pthread_setcancelstate */
985     pthread_setcanceltype,         /* ptr_pthread_setcanceltype */
986     __pthread_do_exit,             /* ptr_pthread_do_exit */
987     __pthread_cleanup_upto,        /* ptr_pthread_cleanup_upto */
988     __pthread_thread_self,         /* ptr_pthread_thread_self */
989     pthread_internal_tsd_set,      /* ptr_pthread_internal_tsd_set */
990     pthread_internal_tsd_get,      /* ptr_pthread_internal_tsd_get */
991     pthread_internal_tsd_address,  /* ptr_pthread_internal_tsd_address */
992     NULL,                          /* ptr_pthread_sigaction */
993     NULL,                          /* ptr_pthread_sigwait */
994     NULL,                          /* ptr_pthread_raise */
995     __pthread_cond_timedwait,      /* ptr___pthread_cond_timedwait */
996     _pthread_cleanup_push,         /* ptr__pthread_cleanup_push */
997     _pthread_cleanup_pop           /* ptr__pthread_cleanup_pop */
998 };
999
1000 #endif /* __GLIBC__ || __FREEBSD__ */