Some dumb fixes.
[wine] / scheduler / pthread.c
1 /*
2  * pthread emulation for re-entrant libcs
3  *
4  * We can't use pthreads directly, so why not let libcs
5  * that wants pthreads use Wine's own threading instead...
6  *
7  * Copyright 1999 Ove Kåven
8  */
9
10 #include "config.h"
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
12
13 #include <assert.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "winbase.h"
20 #include "thread.h"
21
22 static int init_done;
23
24 void PTHREAD_init_done(void)
25 {
26     init_done = 1;
27 }
28
29 /* Currently this probably works only for glibc2,
30  * which checks for the presence of double-underscore-prepended
31  * pthread primitives, and use them if available.
32  * If they are not available, the libc defaults to
33  * non-threadsafe operation (not good). */
34
35 #if defined(__GLIBC__)
36 #include <pthread.h>
37 #include <signal.h>
38
39 #ifdef NEED_UNDERSCORE_PREFIX
40 # define PREFIX "_"
41 #else
42 # define PREFIX
43 #endif
44
45 #define PSTR(str) PREFIX #str
46
47 /* adapt as necessary (a construct like this is used in glibc sources) */
48 #define strong_alias(orig, alias) \
49  asm(".globl " PSTR(alias) "\n" \
50      "\t.set " PSTR(alias) "," PSTR(orig))
51
52 /* strong_alias does not work on external symbols (.o format limitation?),
53  * so for those, we need to use the pogo stick */
54 #if defined(__i386__) && !defined(__PIC__)
55 /* FIXME: PIC */
56 #define jump_alias(orig, alias) \
57  asm(".globl " PSTR(alias) "\n" \
58      "\t.type " PSTR(alias) ",@function\n" \
59      PSTR(alias) ":\n" \
60      "\tjmp " PSTR(orig))
61 #endif
62
63 /* get necessary libc symbols */
64 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
65 #define LIBC_FORK __libc_fork
66 #define PTHREAD_FORK __fork
67 #define ALIAS_FORK
68 #else
69 #define LIBC_FORK __fork
70 #define PTHREAD_FORK fork
71 #endif
72 extern pid_t LIBC_FORK(void);
73
74 #define LIBC_SIGACTION __sigaction
75
76 /* NOTE: This is a truly extremely incredibly ugly hack!
77  * But it does seem to work... */
78
79 /* assume that pthread_mutex_t has room for at least one pointer,
80  * and hope that the users of pthread_mutex_t considers it opaque
81  * (never checks what's in it) */
82 typedef struct {
83   CRITICAL_SECTION *critsect;
84 } *wine_mutex;
85
86 typedef struct _wine_cleanup {
87   void (*routine)(void *);
88   void *arg;
89 } *wine_cleanup;
90
91 typedef const void *key_data;
92
93 #define FIRST_KEY 0
94 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
95
96 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
97
98 void __pthread_initialize(void)
99 {
100 }
101
102 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
103 {
104   static pthread_once_t the_once = PTHREAD_ONCE_INIT;
105   LONG once_now = *(LONG *)&the_once;
106
107   if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
108     (*init_routine)();
109   return 0;
110 }
111 strong_alias(__pthread_once, pthread_once);
112
113 void __pthread_kill_other_threads_np(void)
114 {
115   /* FIXME: this is supposed to be preparation for exec() */
116   if (init_done) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
117 }
118 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
119
120 /***** atfork *****/
121
122 #define MAX_ATFORK 8  /* libc doesn't need that many anyway */
123
124 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
125 typedef void (*atfork_handler)();
126 static atfork_handler atfork_prepare[MAX_ATFORK];
127 static atfork_handler atfork_parent[MAX_ATFORK];
128 static atfork_handler atfork_child[MAX_ATFORK];
129 static int atfork_count;
130
131 int __pthread_atfork(void (*prepare)(void),
132                      void (*parent)(void),
133                      void (*child)(void))
134 {
135     if (init_done) EnterCriticalSection( &atfork_section );
136     assert( atfork_count < MAX_ATFORK );
137     atfork_prepare[atfork_count] = prepare;
138     atfork_parent[atfork_count] = parent;
139     atfork_child[atfork_count] = child;
140     atfork_count++;
141     if (init_done) LeaveCriticalSection( &atfork_section );
142     return 0;
143 }
144 strong_alias(__pthread_atfork, pthread_atfork);
145
146 pid_t PTHREAD_FORK(void)
147 {
148     pid_t pid;
149     int i;
150
151     EnterCriticalSection( &atfork_section );
152     /* prepare handlers are called in reverse insertion order */
153     for (i = atfork_count - 1; i >= 0; i--) atfork_prepare[i]();
154     if (!(pid = LIBC_FORK()))
155     {
156         InitializeCriticalSection( &atfork_section );
157         for (i = 0; i < atfork_count; i++) atfork_child[i]();
158     }
159     else
160     {
161         for (i = 0; i < atfork_count; i++) atfork_parent[i]();
162         LeaveCriticalSection( &atfork_section );
163     }
164     return pid;
165 }
166 #ifdef ALIAS_FORK
167 strong_alias(PTHREAD_FORK, fork);
168 #endif
169
170 /***** MUTEXES *****/
171
172 int __pthread_mutex_init(pthread_mutex_t *mutex,
173                         const pthread_mutexattr_t *mutexattr)
174 {
175   /* glibc has a tendency to initialize mutexes very often, even
176      in situations where they are not really used later on.
177
178      As for us, initializing a mutex is very expensive, we postpone
179      the real initialization until the time the mutex is first used. */
180
181   ((wine_mutex)mutex)->critsect = NULL;
182   return 0;
183 }
184 strong_alias(__pthread_mutex_init, pthread_mutex_init);
185
186 static void mutex_real_init( pthread_mutex_t *mutex )
187 {
188   CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
189   InitializeCriticalSection(critsect);
190
191   if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
192     /* too late, some other thread already did it */
193     DeleteCriticalSection(critsect);
194     HeapFree(GetProcessHeap(), 0, critsect);
195   }
196 }
197
198 int __pthread_mutex_lock(pthread_mutex_t *mutex)
199 {
200   if (!init_done) return 0;
201   if (!((wine_mutex)mutex)->critsect) 
202     mutex_real_init( mutex );
203
204   EnterCriticalSection(((wine_mutex)mutex)->critsect);
205   return 0;
206 }
207 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
208
209 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
210 {
211   if (!init_done) return 0;
212   if (!((wine_mutex)mutex)->critsect) 
213     mutex_real_init( mutex );
214
215   if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
216     errno = EBUSY;
217     return -1;
218   }
219   return 0;
220 }
221 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
222
223 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
224 {
225   if (!((wine_mutex)mutex)->critsect) return 0;
226   LeaveCriticalSection(((wine_mutex)mutex)->critsect);
227   return 0;
228 }
229 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
230
231 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
232 {
233   if (!((wine_mutex)mutex)->critsect) return 0;
234   if (((wine_mutex)mutex)->critsect->RecursionCount) {
235 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
236     return EBUSY;
237 #else
238     while (((wine_mutex)mutex)->critsect->RecursionCount)
239       LeaveCriticalSection(((wine_mutex)mutex)->critsect);
240 #endif
241   }
242   DeleteCriticalSection(((wine_mutex)mutex)->critsect);
243   HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
244   return 0;
245 }
246 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
247
248
249 /***** MUTEX ATTRIBUTES *****/
250 /* just dummies, since critical sections are always recursive */
251
252 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
253 {
254   return 0;
255 }
256 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
257
258 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
259 {
260   return 0;
261 }
262 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
263
264 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
265 {
266   return 0;
267 }
268 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
269
270 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
271 {
272   *kind = PTHREAD_MUTEX_RECURSIVE_NP;
273   return 0;
274 }
275 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
276
277 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
278 {
279   return 0;
280 }
281 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
282
283 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
284 {
285   *kind = PTHREAD_MUTEX_RECURSIVE_NP;
286   return 0;
287 }
288 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
289
290
291 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
292
293 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
294 {
295   static LONG keycnt = FIRST_KEY;
296   *key = InterlockedExchangeAdd(&keycnt, 1);
297   return 0;
298 }
299 strong_alias(__pthread_key_create, pthread_key_create);
300
301 int __pthread_key_delete(pthread_key_t key)
302 {
303   return 0;
304 }
305 strong_alias(__pthread_key_delete, pthread_key_delete);
306
307 int __pthread_setspecific(pthread_key_t key, const void *pointer)
308 {
309   TEB *teb = NtCurrentTeb();
310   if (!teb->pthread_data) {
311     teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
312   }
313   ((key_data*)(teb->pthread_data))[key] = pointer;
314   return 0;
315 }
316 strong_alias(__pthread_setspecific, pthread_setspecific);
317
318 void *__pthread_getspecific(pthread_key_t key)
319 {
320   TEB *teb = NtCurrentTeb();
321   if (!teb) return NULL;
322   if (!teb->pthread_data) return NULL;
323   return (void *)(((key_data*)(teb->pthread_data))[key]);
324 }
325 strong_alias(__pthread_getspecific, pthread_getspecific);
326
327
328 /***** "EXCEPTION" FRAMES *****/
329 /* not implemented right now */
330
331 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
332 {
333   ((wine_cleanup)buffer)->routine = routine;
334   ((wine_cleanup)buffer)->arg = arg;
335 }
336
337 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
338 {
339   if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
340 }
341
342 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
343 {
344   _pthread_cleanup_push(buffer, routine, arg);
345 }
346
347 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
348 {
349   _pthread_cleanup_pop(buffer, execute);
350 }
351
352
353 /***** CONDITIONS *****/
354 /* not implemented right now */
355
356 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
357 {
358   P_OUTPUT("FIXME:pthread_cond_init\n");
359   return 0;
360 }
361
362 int pthread_cond_destroy(pthread_cond_t *cond)
363 {
364   P_OUTPUT("FIXME:pthread_cond_destroy\n");
365   return 0;
366 }
367
368 int pthread_cond_signal(pthread_cond_t *cond)
369 {
370   P_OUTPUT("FIXME:pthread_cond_signal\n");
371   return 0;
372 }
373
374 int pthread_cond_broadcast(pthread_cond_t *cond)
375 {
376   P_OUTPUT("FIXME:pthread_cond_broadcast\n");
377   return 0;
378 }
379
380 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
381 {
382   P_OUTPUT("FIXME:pthread_cond_wait\n");
383   return 0;
384 }
385
386 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
387 {
388   P_OUTPUT("FIXME:pthread_cond_timedwait\n");
389   return 0;
390 }
391
392 /**** CONDITION ATTRIBUTES *****/
393 /* not implemented right now */
394
395 int pthread_condattr_init(pthread_condattr_t *attr)
396 {
397   return 0;
398 }
399
400 int pthread_condattr_destroy(pthread_condattr_t *attr)
401 {
402   return 0;
403 }
404
405 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
406 /***** READ-WRITE LOCKS *****/
407 /* not implemented right now */
408
409 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
410 {
411   P_OUTPUT("FIXME:pthread_rwlock_init\n");
412   return 0;
413 }
414 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
415
416 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
417 {
418   P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
419   return 0;
420 }
421 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
422
423 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
424 {
425   P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
426   return 0;
427 }
428 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
429
430 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
431 {
432   P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
433   return 0;
434 }
435 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
436
437 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
438 {
439   P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
440   return 0;
441 }
442 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
443
444 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
445 {
446   P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
447   return 0;
448 }
449 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
450
451 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
452 {
453   P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
454   return 0;
455 }
456 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
457
458 /**** READ-WRITE LOCK ATTRIBUTES *****/
459 /* not implemented right now */
460
461 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
462 {
463   return 0;
464 }
465
466 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
467 {
468   return 0;
469 }
470 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
471
472 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
473 {
474   *pref = 0;
475   return 0;
476 }
477
478 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
479 {
480   return 0;
481 }
482 #endif /* glibc 2.2 */
483
484 /***** MISC *****/
485
486 pthread_t pthread_self(void)
487 {
488   return (pthread_t)GetCurrentThreadId();
489 }
490
491 int pthread_equal(pthread_t thread1, pthread_t thread2)
492 {
493   return (DWORD)thread1 == (DWORD)thread2;
494 }
495
496 void pthread_exit(void *retval)
497 {
498   /* FIXME: pthread cleanup */
499   ExitThread((DWORD)retval);
500 }
501
502 int pthread_setcanceltype(int type, int *oldtype)
503 {
504   if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
505   return 0;
506 }
507
508 /***** ANTI-OVERRIDES *****/
509 /* pthreads tries to override these, point them back to libc */
510
511 #ifdef jump_alias
512 jump_alias(LIBC_SIGACTION, sigaction);
513 #else
514 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
515 {
516   return LIBC_SIGACTION(signum, act, oldact);
517 }
518 #endif
519
520 #endif /* __GLIBC__ */