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