- fix wrong hexadecimal GetLastError() output
[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 want 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     /* we don't need to do anything here */
116 }
117 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
118
119 /***** atfork *****/
120
121 #define MAX_ATFORK 8  /* libc doesn't need that many anyway */
122
123 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
124 typedef void (*atfork_handler)();
125 static atfork_handler atfork_prepare[MAX_ATFORK];
126 static atfork_handler atfork_parent[MAX_ATFORK];
127 static atfork_handler atfork_child[MAX_ATFORK];
128 static int atfork_count;
129
130 int __pthread_atfork(void (*prepare)(void),
131                      void (*parent)(void),
132                      void (*child)(void))
133 {
134     if (init_done) EnterCriticalSection( &atfork_section );
135     assert( atfork_count < MAX_ATFORK );
136     atfork_prepare[atfork_count] = prepare;
137     atfork_parent[atfork_count] = parent;
138     atfork_child[atfork_count] = child;
139     atfork_count++;
140     if (init_done) LeaveCriticalSection( &atfork_section );
141     return 0;
142 }
143 strong_alias(__pthread_atfork, pthread_atfork);
144
145 pid_t PTHREAD_FORK(void)
146 {
147     pid_t pid;
148     int i;
149
150     EnterCriticalSection( &atfork_section );
151     /* prepare handlers are called in reverse insertion order */
152     for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
153     if (!(pid = LIBC_FORK()))
154     {
155         InitializeCriticalSection( &atfork_section );
156         for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
157     }
158     else
159     {
160         for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
161         LeaveCriticalSection( &atfork_section );
162     }
163     return pid;
164 }
165 #ifdef ALIAS_FORK
166 strong_alias(PTHREAD_FORK, fork);
167 #endif
168
169 /***** MUTEXES *****/
170
171 int __pthread_mutex_init(pthread_mutex_t *mutex,
172                         const pthread_mutexattr_t *mutexattr)
173 {
174   /* glibc has a tendency to initialize mutexes very often, even
175      in situations where they are not really used later on.
176
177      As for us, initializing a mutex is very expensive, we postpone
178      the real initialization until the time the mutex is first used. */
179
180   ((wine_mutex)mutex)->critsect = NULL;
181   return 0;
182 }
183 strong_alias(__pthread_mutex_init, pthread_mutex_init);
184
185 static void mutex_real_init( pthread_mutex_t *mutex )
186 {
187   CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
188   InitializeCriticalSection(critsect);
189
190   if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
191     /* too late, some other thread already did it */
192     DeleteCriticalSection(critsect);
193     HeapFree(GetProcessHeap(), 0, critsect);
194   }
195 }
196
197 int __pthread_mutex_lock(pthread_mutex_t *mutex)
198 {
199   if (!init_done) return 0;
200   if (!((wine_mutex)mutex)->critsect) 
201     mutex_real_init( mutex );
202
203   EnterCriticalSection(((wine_mutex)mutex)->critsect);
204   return 0;
205 }
206 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
207
208 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
209 {
210   if (!init_done) return 0;
211   if (!((wine_mutex)mutex)->critsect) 
212     mutex_real_init( mutex );
213
214   if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
215     errno = EBUSY;
216     return -1;
217   }
218   return 0;
219 }
220 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
221
222 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
223 {
224   if (!((wine_mutex)mutex)->critsect) return 0;
225   LeaveCriticalSection(((wine_mutex)mutex)->critsect);
226   return 0;
227 }
228 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
229
230 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
231 {
232   if (!((wine_mutex)mutex)->critsect) return 0;
233   if (((wine_mutex)mutex)->critsect->RecursionCount) {
234 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
235     return EBUSY;
236 #else
237     while (((wine_mutex)mutex)->critsect->RecursionCount)
238       LeaveCriticalSection(((wine_mutex)mutex)->critsect);
239 #endif
240   }
241   DeleteCriticalSection(((wine_mutex)mutex)->critsect);
242   HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
243   return 0;
244 }
245 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
246
247
248 /***** MUTEX ATTRIBUTES *****/
249 /* just dummies, since critical sections are always recursive */
250
251 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
252 {
253   return 0;
254 }
255 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
256
257 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
258 {
259   return 0;
260 }
261 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
262
263 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
264 {
265   return 0;
266 }
267 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
268
269 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
270 {
271   *kind = PTHREAD_MUTEX_RECURSIVE_NP;
272   return 0;
273 }
274 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
275
276 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
277 {
278   return 0;
279 }
280 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
281
282 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
283 {
284   *kind = PTHREAD_MUTEX_RECURSIVE_NP;
285   return 0;
286 }
287 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
288
289
290 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
291
292 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
293 {
294   static LONG keycnt = FIRST_KEY;
295   *key = InterlockedExchangeAdd(&keycnt, 1);
296   return 0;
297 }
298 strong_alias(__pthread_key_create, pthread_key_create);
299
300 int __pthread_key_delete(pthread_key_t key)
301 {
302   return 0;
303 }
304 strong_alias(__pthread_key_delete, pthread_key_delete);
305
306 int __pthread_setspecific(pthread_key_t key, const void *pointer)
307 {
308   TEB *teb = NtCurrentTeb();
309   if (!teb->pthread_data) {
310     teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
311   }
312   ((key_data*)(teb->pthread_data))[key] = pointer;
313   return 0;
314 }
315 strong_alias(__pthread_setspecific, pthread_setspecific);
316
317 void *__pthread_getspecific(pthread_key_t key)
318 {
319   TEB *teb = NtCurrentTeb();
320   if (!teb) return NULL;
321   if (!teb->pthread_data) return NULL;
322   return (void *)(((key_data*)(teb->pthread_data))[key]);
323 }
324 strong_alias(__pthread_getspecific, pthread_getspecific);
325
326
327 /***** "EXCEPTION" FRAMES *****/
328 /* not implemented right now */
329
330 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
331 {
332   ((wine_cleanup)buffer)->routine = routine;
333   ((wine_cleanup)buffer)->arg = arg;
334 }
335
336 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
337 {
338   if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
339 }
340
341 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
342 {
343   _pthread_cleanup_push(buffer, routine, arg);
344 }
345
346 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
347 {
348   _pthread_cleanup_pop(buffer, execute);
349 }
350
351
352 /***** CONDITIONS *****/
353 /* not implemented right now */
354
355 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
356 {
357   P_OUTPUT("FIXME:pthread_cond_init\n");
358   return 0;
359 }
360
361 int pthread_cond_destroy(pthread_cond_t *cond)
362 {
363   P_OUTPUT("FIXME:pthread_cond_destroy\n");
364   return 0;
365 }
366
367 int pthread_cond_signal(pthread_cond_t *cond)
368 {
369   P_OUTPUT("FIXME:pthread_cond_signal\n");
370   return 0;
371 }
372
373 int pthread_cond_broadcast(pthread_cond_t *cond)
374 {
375   P_OUTPUT("FIXME:pthread_cond_broadcast\n");
376   return 0;
377 }
378
379 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
380 {
381   P_OUTPUT("FIXME:pthread_cond_wait\n");
382   return 0;
383 }
384
385 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
386 {
387   P_OUTPUT("FIXME:pthread_cond_timedwait\n");
388   return 0;
389 }
390
391 /**** CONDITION ATTRIBUTES *****/
392 /* not implemented right now */
393
394 int pthread_condattr_init(pthread_condattr_t *attr)
395 {
396   return 0;
397 }
398
399 int pthread_condattr_destroy(pthread_condattr_t *attr)
400 {
401   return 0;
402 }
403
404 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
405 /***** READ-WRITE LOCKS *****/
406 /* not implemented right now */
407
408 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
409 {
410   P_OUTPUT("FIXME:pthread_rwlock_init\n");
411   return 0;
412 }
413 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
414
415 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
416 {
417   P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
418   return 0;
419 }
420 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
421
422 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
423 {
424   P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
425   return 0;
426 }
427 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
428
429 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
430 {
431   P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
432   return 0;
433 }
434 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
435
436 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
437 {
438   P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
439   return 0;
440 }
441 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
442
443 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
444 {
445   P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
446   return 0;
447 }
448 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
449
450 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
451 {
452   P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
453   return 0;
454 }
455 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
456
457 /**** READ-WRITE LOCK ATTRIBUTES *****/
458 /* not implemented right now */
459
460 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
461 {
462   return 0;
463 }
464
465 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
466 {
467   return 0;
468 }
469 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
470
471 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
472 {
473   *pref = 0;
474   return 0;
475 }
476
477 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
478 {
479   return 0;
480 }
481 #endif /* glibc 2.2 */
482
483 /***** MISC *****/
484
485 pthread_t pthread_self(void)
486 {
487   return (pthread_t)GetCurrentThreadId();
488 }
489
490 int pthread_equal(pthread_t thread1, pthread_t thread2)
491 {
492   return (DWORD)thread1 == (DWORD)thread2;
493 }
494
495 void pthread_exit(void *retval)
496 {
497   /* FIXME: pthread cleanup */
498   ExitThread((DWORD)retval);
499 }
500
501 int pthread_setcanceltype(int type, int *oldtype)
502 {
503   if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
504   return 0;
505 }
506
507 /***** ANTI-OVERRIDES *****/
508 /* pthreads tries to override these, point them back to libc */
509
510 #ifdef jump_alias
511 jump_alias(LIBC_SIGACTION, sigaction);
512 #else
513 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
514 {
515   return LIBC_SIGACTION(signum, act, oldact);
516 }
517 #endif
518
519 #endif /* __GLIBC__ */