Fix UuidCreate() to not forget MAC address.
[wine/testsucceed.git] / scheduler / pthread.c
bloba87c0d2a8479a5fcb777c2fac8d8de15490dc6c0
1 /*
2 * pthread emulation for re-entrant libcs
4 * We can't use pthreads directly, so why not let libcs
5 * that want 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>
17 #include <string.h>
19 #include "winbase.h"
20 #include "thread.h"
21 #include "ntddk.h"
23 static int init_done;
25 void PTHREAD_init_done(void)
27 init_done = 1;
30 /* Currently this probably works only for glibc2,
31 * which checks for the presence of double-underscore-prepended
32 * pthread primitives, and use them if available.
33 * If they are not available, the libc defaults to
34 * non-threadsafe operation (not good). */
36 #if defined(__GLIBC__)
37 #include <pthread.h>
38 #include <signal.h>
40 #ifdef NEED_UNDERSCORE_PREFIX
41 # define PREFIX "_"
42 #else
43 # define PREFIX
44 #endif
46 #define PSTR(str) PREFIX #str
48 /* adapt as necessary (a construct like this is used in glibc sources) */
49 #define strong_alias(orig, alias) \
50 asm(".globl " PSTR(alias) "\n" \
51 "\t.set " PSTR(alias) "," PSTR(orig))
53 /* strong_alias does not work on external symbols (.o format limitation?),
54 * so for those, we need to use the pogo stick */
55 #if defined(__i386__) && !defined(__PIC__)
56 /* FIXME: PIC */
57 #define jump_alias(orig, alias) \
58 asm(".globl " PSTR(alias) "\n" \
59 "\t.type " PSTR(alias) ",@function\n" \
60 PSTR(alias) ":\n" \
61 "\tjmp " PSTR(orig))
62 #endif
64 /* get necessary libc symbols */
65 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
66 #define LIBC_FORK __libc_fork
67 #define PTHREAD_FORK __fork
68 #define ALIAS_FORK
69 #else
70 #define LIBC_FORK __fork
71 #define PTHREAD_FORK fork
72 #endif
73 extern pid_t LIBC_FORK(void);
75 #define LIBC_SIGACTION __sigaction
76 extern int LIBC_SIGACTION(int signum,
77 const struct sigaction *act,
78 struct sigaction *oldact);
80 /* NOTE: This is a truly extremely incredibly ugly hack!
81 * But it does seem to work... */
83 /* assume that pthread_mutex_t has room for at least one pointer,
84 * and hope that the users of pthread_mutex_t considers it opaque
85 * (never checks what's in it)
86 * also: assume that static initializer sets pointer to NULL
88 typedef struct {
89 CRITICAL_SECTION *critsect;
90 } *wine_mutex;
92 /* see wine_mutex above for comments */
93 typedef struct {
94 RTL_RWLOCK *lock;
95 } *wine_rwlock;
97 typedef struct _wine_cleanup {
98 void (*routine)(void *);
99 void *arg;
100 } *wine_cleanup;
102 typedef const void *key_data;
104 #define FIRST_KEY 0
105 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
107 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
109 void __pthread_initialize(void)
113 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
115 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
116 LONG once_now = *(LONG *)&the_once;
118 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
119 (*init_routine)();
120 return 0;
122 strong_alias(__pthread_once, pthread_once);
124 void __pthread_kill_other_threads_np(void)
126 /* we don't need to do anything here */
128 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
130 /***** atfork *****/
132 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
134 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
135 typedef void (*atfork_handler)();
136 static atfork_handler atfork_prepare[MAX_ATFORK];
137 static atfork_handler atfork_parent[MAX_ATFORK];
138 static atfork_handler atfork_child[MAX_ATFORK];
139 static int atfork_count;
141 int __pthread_atfork(void (*prepare)(void),
142 void (*parent)(void),
143 void (*child)(void))
145 if (init_done) EnterCriticalSection( &atfork_section );
146 assert( atfork_count < MAX_ATFORK );
147 atfork_prepare[atfork_count] = prepare;
148 atfork_parent[atfork_count] = parent;
149 atfork_child[atfork_count] = child;
150 atfork_count++;
151 if (init_done) LeaveCriticalSection( &atfork_section );
152 return 0;
154 strong_alias(__pthread_atfork, pthread_atfork);
156 pid_t PTHREAD_FORK(void)
158 pid_t pid;
159 int i;
161 EnterCriticalSection( &atfork_section );
162 /* prepare handlers are called in reverse insertion order */
163 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
164 if (!(pid = LIBC_FORK()))
166 InitializeCriticalSection( &atfork_section );
167 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
169 else
171 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
172 LeaveCriticalSection( &atfork_section );
174 return pid;
176 #ifdef ALIAS_FORK
177 strong_alias(PTHREAD_FORK, fork);
178 #endif
180 /***** MUTEXES *****/
182 int __pthread_mutex_init(pthread_mutex_t *mutex,
183 const pthread_mutexattr_t *mutexattr)
185 /* glibc has a tendency to initialize mutexes very often, even
186 in situations where they are not really used later on.
188 As for us, initializing a mutex is very expensive, we postpone
189 the real initialization until the time the mutex is first used. */
191 ((wine_mutex)mutex)->critsect = NULL;
192 return 0;
194 strong_alias(__pthread_mutex_init, pthread_mutex_init);
196 static void mutex_real_init( pthread_mutex_t *mutex )
198 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
199 InitializeCriticalSection(critsect);
201 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
202 /* too late, some other thread already did it */
203 DeleteCriticalSection(critsect);
204 HeapFree(GetProcessHeap(), 0, critsect);
208 int __pthread_mutex_lock(pthread_mutex_t *mutex)
210 if (!init_done) return 0;
211 if (!((wine_mutex)mutex)->critsect)
212 mutex_real_init( mutex );
214 EnterCriticalSection(((wine_mutex)mutex)->critsect);
215 return 0;
217 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
219 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
221 if (!init_done) return 0;
222 if (!((wine_mutex)mutex)->critsect)
223 mutex_real_init( mutex );
225 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
226 errno = EBUSY;
227 return -1;
229 return 0;
231 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
233 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
235 if (!((wine_mutex)mutex)->critsect) return 0;
236 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
237 return 0;
239 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
241 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
243 if (!((wine_mutex)mutex)->critsect) return 0;
244 if (((wine_mutex)mutex)->critsect->RecursionCount) {
245 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
246 return EBUSY;
247 #else
248 while (((wine_mutex)mutex)->critsect->RecursionCount)
249 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
250 #endif
252 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
253 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
254 return 0;
256 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
259 /***** MUTEX ATTRIBUTES *****/
260 /* just dummies, since critical sections are always recursive */
262 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
264 return 0;
266 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
268 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
270 return 0;
272 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
274 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
276 return 0;
278 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
280 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
282 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
283 return 0;
285 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
287 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
289 return 0;
291 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
293 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
295 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
296 return 0;
298 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
301 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
303 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
305 static LONG keycnt = FIRST_KEY;
306 *key = InterlockedExchangeAdd(&keycnt, 1);
307 return 0;
309 strong_alias(__pthread_key_create, pthread_key_create);
311 int __pthread_key_delete(pthread_key_t key)
313 return 0;
315 strong_alias(__pthread_key_delete, pthread_key_delete);
317 int __pthread_setspecific(pthread_key_t key, const void *pointer)
319 TEB *teb = NtCurrentTeb();
320 if (!teb->pthread_data) {
321 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
323 ((key_data*)(teb->pthread_data))[key] = pointer;
324 return 0;
326 strong_alias(__pthread_setspecific, pthread_setspecific);
328 void *__pthread_getspecific(pthread_key_t key)
330 TEB *teb = NtCurrentTeb();
331 if (!teb) return NULL;
332 if (!teb->pthread_data) return NULL;
333 return (void *)(((key_data*)(teb->pthread_data))[key]);
335 strong_alias(__pthread_getspecific, pthread_getspecific);
338 /***** "EXCEPTION" FRAMES *****/
339 /* not implemented right now */
341 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
343 ((wine_cleanup)buffer)->routine = routine;
344 ((wine_cleanup)buffer)->arg = arg;
347 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
349 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
352 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
354 _pthread_cleanup_push(buffer, routine, arg);
357 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
359 _pthread_cleanup_pop(buffer, execute);
363 /***** CONDITIONS *****/
364 /* not implemented right now */
366 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
368 P_OUTPUT("FIXME:pthread_cond_init\n");
369 return 0;
372 int pthread_cond_destroy(pthread_cond_t *cond)
374 P_OUTPUT("FIXME:pthread_cond_destroy\n");
375 return 0;
378 int pthread_cond_signal(pthread_cond_t *cond)
380 P_OUTPUT("FIXME:pthread_cond_signal\n");
381 return 0;
384 int pthread_cond_broadcast(pthread_cond_t *cond)
386 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
387 return 0;
390 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
392 P_OUTPUT("FIXME:pthread_cond_wait\n");
393 return 0;
396 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
398 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
399 return 0;
402 /**** CONDITION ATTRIBUTES *****/
403 /* not implemented right now */
405 int pthread_condattr_init(pthread_condattr_t *attr)
407 return 0;
410 int pthread_condattr_destroy(pthread_condattr_t *attr)
412 return 0;
415 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
416 /***** READ-WRITE LOCKS *****/
418 static void rwlock_real_init(pthread_rwlock_t *rwlock)
420 RTL_RWLOCK *lock = HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK));
421 RtlInitializeResource(lock);
423 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock)rwlock)->lock),lock,NULL) != NULL) {
424 /* too late, some other thread already did it */
425 RtlDeleteResource(lock);
426 HeapFree(GetProcessHeap(), 0, lock);
430 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
432 ((wine_rwlock)rwlock)->lock = NULL;
433 return 0;
435 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
437 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
439 if (!((wine_rwlock)rwlock)->lock) return 0;
440 RtlDeleteResource(((wine_rwlock)rwlock)->lock);
441 HeapFree(GetProcessHeap(), 0, ((wine_rwlock)rwlock)->lock);
442 return 0;
444 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
446 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
448 if (!init_done) return 0;
449 if (!((wine_rwlock)rwlock)->lock)
450 rwlock_real_init( rwlock );
452 while(TRUE)
453 if (RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, TRUE))
454 return 0;
456 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
458 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
460 if (!init_done) return 0;
461 if (!((wine_rwlock)rwlock)->lock)
462 rwlock_real_init( rwlock );
464 if (!RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, FALSE)) {
465 errno = EBUSY;
466 return -1;
468 return 0;
470 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
472 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
474 if (!init_done) return 0;
475 if (!((wine_rwlock)rwlock)->lock)
476 rwlock_real_init( rwlock );
478 while(TRUE)
479 if (RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, TRUE))
480 return 0;
482 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
484 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
486 if (!init_done) return 0;
487 if (!((wine_rwlock)rwlock)->lock)
488 rwlock_real_init( rwlock );
490 if (!RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, FALSE)) {
491 errno = EBUSY;
492 return -1;
494 return 0;
496 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
498 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
500 if (!((wine_rwlock)rwlock)->lock) return 0;
501 RtlReleaseResource( ((wine_rwlock)rwlock)->lock );
502 return 0;
504 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
506 /**** READ-WRITE LOCK ATTRIBUTES *****/
507 /* not implemented right now */
509 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
511 return 0;
514 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
516 return 0;
518 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
520 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
522 *pref = 0;
523 return 0;
526 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
528 return 0;
530 #endif /* glibc 2.2 */
532 /***** MISC *****/
534 pthread_t pthread_self(void)
536 return (pthread_t)GetCurrentThreadId();
539 int pthread_equal(pthread_t thread1, pthread_t thread2)
541 return (DWORD)thread1 == (DWORD)thread2;
544 void pthread_exit(void *retval)
546 /* FIXME: pthread cleanup */
547 ExitThread((DWORD)retval);
550 int pthread_setcanceltype(int type, int *oldtype)
552 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
553 return 0;
556 /***** ANTI-OVERRIDES *****/
557 /* pthreads tries to override these, point them back to libc */
559 #ifdef jump_alias
560 jump_alias(LIBC_SIGACTION, sigaction);
561 #else
562 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
564 return LIBC_SIGACTION(signum, act, oldact);
566 #endif
568 #endif /* __GLIBC__ */