Removed the code duplication.
[wine.git] / scheduler / pthread.c
blob706813b8c6bf951cf189aa212f989fcb07103c07
1 /*
2 * pthread emulation for re-entrant libcs
4 * We can't use pthreads directly, so why not let libcs
5 * that wants pthreads use Wine's own threading instead...
7 * Copyright 1999 Ove Kåven
8 */
10 #include "config.h"
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
13 #include <assert.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <unistd.h>
18 #include "winbase.h"
19 #include "heap.h"
20 #include "thread.h"
22 /* Currently this probably works only for glibc2,
23 * which checks for the presence of double-underscore-prepended
24 * pthread primitives, and use them if available.
25 * If they are not available, the libc defaults to
26 * non-threadsafe operation (not good). */
28 #if defined(__GLIBC__)
29 #include <pthread.h>
30 #include <signal.h>
32 #ifdef NEED_UNDERSCORE_PREFIX
33 # define PREFIX "_"
34 #else
35 # define PREFIX
36 #endif
38 #define PSTR(str) PREFIX #str
40 /* adapt as necessary (a construct like this is used in glibc sources) */
41 #define strong_alias(orig, alias) \
42 asm(".globl " PSTR(alias) "\n" \
43 "\t.set " PSTR(alias) "," PSTR(orig))
45 /* strong_alias does not work on external symbols (.o format limitation?),
46 * so for those, we need to use the pogo stick */
47 #if defined(__i386__) && !defined(__PIC__)
48 /* FIXME: PIC */
49 #define jump_alias(orig, alias) \
50 asm(".globl " PSTR(alias) "\n" \
51 "\t.type " PSTR(alias) ",@function\n" \
52 PSTR(alias) ":\n" \
53 "\tjmp " PSTR(orig))
54 #endif
56 /* get necessary libc symbols */
57 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
58 #define LIBC_FORK __libc_fork
59 #define PTHREAD_FORK __fork
60 #define ALIAS_FORK
61 #else
62 #define LIBC_FORK __fork
63 #define PTHREAD_FORK fork
64 #endif
65 extern pid_t LIBC_FORK(void);
67 #define LIBC_SIGACTION __sigaction
69 /* NOTE: This is a truly extremely incredibly ugly hack!
70 * But it does seem to work... */
72 /* assume that pthread_mutex_t has room for at least one pointer,
73 * and hope that the users of pthread_mutex_t considers it opaque
74 * (never checks what's in it) */
75 typedef struct {
76 CRITICAL_SECTION *critsect;
77 } *wine_mutex;
79 typedef struct _wine_cleanup {
80 void (*routine)(void *);
81 void *arg;
82 } *wine_cleanup;
84 typedef const void *key_data;
86 #define FIRST_KEY 0
87 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
89 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
91 void __pthread_initialize(void)
95 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
97 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
98 LONG once_now = *(LONG *)&the_once;
100 if (InterlockedCompareExchange((PVOID*)once_control, (PVOID)(once_now+1), (PVOID)once_now) == (PVOID)once_now)
101 (*init_routine)();
102 return 0;
104 strong_alias(__pthread_once, pthread_once);
106 void __pthread_kill_other_threads_np(void)
108 /* FIXME: this is supposed to be preparation for exec() */
109 if (SystemHeap) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
111 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
113 /***** atfork *****/
115 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
117 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT;
118 typedef void (*atfork_handler)();
119 static atfork_handler atfork_prepare[MAX_ATFORK];
120 static atfork_handler atfork_parent[MAX_ATFORK];
121 static atfork_handler atfork_child[MAX_ATFORK];
122 static int atfork_count;
124 int __pthread_atfork(void (*prepare)(void),
125 void (*parent)(void),
126 void (*child)(void))
128 if (SystemHeap) EnterCriticalSection( &atfork_section );
129 assert( atfork_count < MAX_ATFORK );
130 atfork_prepare[atfork_count] = prepare;
131 atfork_parent[atfork_count] = parent;
132 atfork_child[atfork_count] = child;
133 atfork_count++;
134 if (SystemHeap) LeaveCriticalSection( &atfork_section );
135 return 0;
137 strong_alias(__pthread_atfork, pthread_atfork);
139 pid_t PTHREAD_FORK(void)
141 pid_t pid;
142 int i;
144 EnterCriticalSection( &atfork_section );
145 /* prepare handlers are called in reverse insertion order */
146 for (i = atfork_count - 1; i >= 0; i--) atfork_prepare[i]();
147 if (!(pid = LIBC_FORK()))
149 InitializeCriticalSection( &atfork_section );
150 for (i = 0; i < atfork_count; i++) atfork_child[i]();
152 else
154 for (i = 0; i < atfork_count; i++) atfork_parent[i]();
155 LeaveCriticalSection( &atfork_section );
157 return pid;
159 #ifdef ALIAS_FORK
160 strong_alias(PTHREAD_FORK, fork);
161 #endif
163 /***** MUTEXES *****/
165 int __pthread_mutex_init(pthread_mutex_t *mutex,
166 const pthread_mutexattr_t *mutexattr)
168 /* glibc has a tendency to initialize mutexes very often, even
169 in situations where they are not really used later on.
171 As for us, initializing a mutex is very expensive, we postpone
172 the real initialization until the time the mutex is first used. */
174 ((wine_mutex)mutex)->critsect = NULL;
175 return 0;
177 strong_alias(__pthread_mutex_init, pthread_mutex_init);
179 static void mutex_real_init( pthread_mutex_t *mutex )
181 CRITICAL_SECTION *critsect = HeapAlloc(SystemHeap, 0, sizeof(CRITICAL_SECTION));
182 InitializeCriticalSection(critsect);
184 if (InterlockedCompareExchange((PVOID*)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
185 /* too late, some other thread already did it */
186 DeleteCriticalSection(critsect);
187 HeapFree(SystemHeap, 0, critsect);
191 int __pthread_mutex_lock(pthread_mutex_t *mutex)
193 if (!SystemHeap) return 0;
194 if (!((wine_mutex)mutex)->critsect)
195 mutex_real_init( mutex );
197 EnterCriticalSection(((wine_mutex)mutex)->critsect);
198 return 0;
200 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
202 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
204 if (!SystemHeap) return 0;
205 if (!((wine_mutex)mutex)->critsect)
206 mutex_real_init( mutex );
208 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
209 errno = EBUSY;
210 return -1;
212 return 0;
214 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
216 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
218 if (!((wine_mutex)mutex)->critsect) return 0;
219 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
220 return 0;
222 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
224 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
226 if (!((wine_mutex)mutex)->critsect) return 0;
227 if (((wine_mutex)mutex)->critsect->RecursionCount) {
228 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
229 return EBUSY;
230 #else
231 while (((wine_mutex)mutex)->critsect->RecursionCount)
232 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
233 #endif
235 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
236 HeapFree(SystemHeap, 0, ((wine_mutex)mutex)->critsect);
237 return 0;
239 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
242 /***** MUTEX ATTRIBUTES *****/
243 /* just dummies, since critical sections are always recursive */
245 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
247 return 0;
249 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
251 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
253 return 0;
255 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
257 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
259 return 0;
261 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
263 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
265 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
266 return 0;
268 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
270 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
272 return 0;
274 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
276 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
278 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
279 return 0;
281 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
284 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
286 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
288 static LONG keycnt = FIRST_KEY;
289 *key = InterlockedExchangeAdd(&keycnt, 1);
290 return 0;
292 strong_alias(__pthread_key_create, pthread_key_create);
294 int __pthread_key_delete(pthread_key_t key)
296 return 0;
298 strong_alias(__pthread_key_delete, pthread_key_delete);
300 int __pthread_setspecific(pthread_key_t key, const void *pointer)
302 TEB *teb = NtCurrentTeb();
303 if (!teb->pthread_data) {
304 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
306 ((key_data*)(teb->pthread_data))[key] = pointer;
307 return 0;
309 strong_alias(__pthread_setspecific, pthread_setspecific);
311 void *__pthread_getspecific(pthread_key_t key)
313 TEB *teb = NtCurrentTeb();
314 if (!teb) return NULL;
315 if (!teb->pthread_data) return NULL;
316 return (void *)(((key_data*)(teb->pthread_data))[key]);
318 strong_alias(__pthread_getspecific, pthread_getspecific);
321 /***** "EXCEPTION" FRAMES *****/
322 /* not implemented right now */
324 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
326 ((wine_cleanup)buffer)->routine = routine;
327 ((wine_cleanup)buffer)->arg = arg;
330 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
332 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
335 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
337 _pthread_cleanup_push(buffer, routine, arg);
340 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
342 _pthread_cleanup_pop(buffer, execute);
346 /***** CONDITIONS *****/
347 /* not implemented right now */
349 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
351 P_OUTPUT("FIXME:pthread_cond_init\n");
352 return 0;
355 int pthread_cond_destroy(pthread_cond_t *cond)
357 P_OUTPUT("FIXME:pthread_cond_destroy\n");
358 return 0;
361 int pthread_cond_signal(pthread_cond_t *cond)
363 P_OUTPUT("FIXME:pthread_cond_signal\n");
364 return 0;
367 int pthread_cond_broadcast(pthread_cond_t *cond)
369 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
370 return 0;
373 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
375 P_OUTPUT("FIXME:pthread_cond_wait\n");
376 return 0;
379 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
381 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
382 return 0;
385 /**** CONDITION ATTRIBUTES *****/
386 /* not implemented right now */
388 int pthread_condattr_init(pthread_condattr_t *attr)
390 return 0;
393 int pthread_condattr_destroy(pthread_condattr_t *attr)
395 return 0;
398 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
399 /***** READ-WRITE LOCKS *****/
400 /* not implemented right now */
402 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
404 P_OUTPUT("FIXME:pthread_rwlock_init\n");
405 return 0;
407 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
409 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
411 P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
412 return 0;
414 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
416 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
418 P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
419 return 0;
421 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
423 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
425 P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
426 return 0;
428 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
430 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
432 P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
433 return 0;
435 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
437 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
439 P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
440 return 0;
442 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
444 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
446 P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
447 return 0;
449 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
451 /**** READ-WRITE LOCK ATTRIBUTES *****/
452 /* not implemented right now */
454 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
456 return 0;
459 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
461 return 0;
463 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
465 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
467 *pref = 0;
468 return 0;
471 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
473 return 0;
475 #endif /* glibc 2.2 */
477 /***** MISC *****/
479 pthread_t pthread_self(void)
481 return (pthread_t)GetCurrentThreadId();
484 int pthread_equal(pthread_t thread1, pthread_t thread2)
486 return (DWORD)thread1 == (DWORD)thread2;
489 void pthread_exit(void *retval)
491 /* FIXME: pthread cleanup */
492 ExitThread((DWORD)retval);
495 int pthread_setcanceltype(int type, int *oldtype)
497 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
498 return 0;
501 /***** ANTI-OVERRIDES *****/
502 /* pthreads tries to override these, point them back to libc */
504 #ifdef jump_alias
505 jump_alias(LIBC_SIGACTION, sigaction);
506 #else
507 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
509 return LIBC_SIGACTION(signum, act, oldact);
511 #endif
513 #endif /* __GLIBC__ */