Release 20000430.
[wine/multimedia.git] / scheduler / pthread.c
bloba394e9f3385615c90e92dcc7510e99d2050bdae4
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"
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <unistd.h>
16 #include "winbase.h"
17 #include "heap.h"
18 #include "thread.h"
20 /* Currently this probably works only for glibc2,
21 * which checks for the presence of double-underscore-prepended
22 * pthread primitives, and use them if available.
23 * If they are not available, the libc defaults to
24 * non-threadsafe operation (not good). */
26 #if defined(__GLIBC__)
27 #include <pthread.h>
28 #include <signal.h>
30 #ifdef NEED_UNDERSCORE_PREFIX
31 # define PREFIX "_"
32 #else
33 # define PREFIX
34 #endif
36 #define PSTR(str) PREFIX #str
38 /* adapt as necessary (a construct like this is used in glibc sources) */
39 #define strong_alias(orig, alias) \
40 asm(".globl " PSTR(alias) "\n\t.set " PSTR(alias) "," PSTR(orig))
42 /* strong_alias does not work on external symbols (.o format limitation?),
43 * so for those, we need to use the pogo stick */
44 #ifdef __i386__
45 #define jump_alias(orig, alias) \
46 asm(".globl " PSTR(alias) "\n\t" PSTR(alias) ":\n\tjmp " PSTR(orig))
47 #endif
49 /* get necessary libc symbols */
50 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
51 #define LIBC_FORK __libc_fork
52 #define PTHREAD_FORK __fork
53 #define ALIAS_FORK
54 #else
55 #define LIBC_FORK __fork
56 #define PTHREAD_FORK fork
57 #endif
58 extern pid_t LIBC_FORK(void);
60 #define LIBC_SIGACTION __sigaction
62 /* NOTE: This is a truly extremely incredibly ugly hack!
63 * But it does seem to work... */
65 /* assume that pthread_mutex_t has room for at least one pointer,
66 * and hope that the users of pthread_mutex_t considers it opaque
67 * (never checks what's in it) */
68 typedef struct {
69 CRITICAL_SECTION *critsect;
70 } *wine_mutex;
72 typedef struct _wine_cleanup {
73 void (*routine)(void *);
74 void *arg;
75 } *wine_cleanup;
77 typedef const void *key_data;
79 #define FIRST_KEY 0
80 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
82 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
84 void __pthread_initialize(void)
88 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
90 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
91 LONG once_now = *(LONG *)&the_once;
93 if (InterlockedCompareExchange((PVOID*)once_control, (PVOID)(once_now+1), (PVOID)once_now) == (PVOID)once_now)
94 (*init_routine)();
95 return 0;
97 strong_alias(__pthread_once, pthread_once);
99 void __pthread_kill_other_threads_np(void)
101 /* FIXME: this is supposed to be preparation for exec() */
102 if (SystemHeap) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
104 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
106 int __pthread_atfork(void (*prepare)(void),
107 void (*parent)(void),
108 void (*child)(void))
110 P_OUTPUT("fixme:pthread_atfork\n");
111 return 0;
113 strong_alias(__pthread_atfork, pthread_atfork);
115 pid_t PTHREAD_FORK(void)
117 pid_t pid;
118 /* call prepare handlers */
119 pid = LIBC_FORK();
120 if (pid == 0) {
121 /* call child handlers */
122 } else {
123 /* call parent handlers */
125 return pid;
127 #ifdef ALIAS_FORK
128 strong_alias(PTHREAD_FORK, fork);
129 #endif
131 /***** MUTEXES *****/
133 int __pthread_mutex_init(pthread_mutex_t *mutex,
134 const pthread_mutexattr_t *mutexattr)
136 /* glibc has a tendency to initialize mutexes very often, even
137 in situations where they are not really used later on.
139 As for us, initializing a mutex is very expensive, we postpone
140 the real initialization until the time the mutex is first used. */
142 ((wine_mutex)mutex)->critsect = NULL;
143 return 0;
145 strong_alias(__pthread_mutex_init, pthread_mutex_init);
147 static void mutex_real_init( pthread_mutex_t *mutex )
149 CRITICAL_SECTION *critsect = HeapAlloc(SystemHeap, 0, sizeof(CRITICAL_SECTION));
150 InitializeCriticalSection(critsect);
152 if (InterlockedCompareExchange((PVOID*)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
153 /* too late, some other thread already did it */
154 DeleteCriticalSection(critsect);
155 HeapFree(SystemHeap, 0, critsect);
159 int __pthread_mutex_lock(pthread_mutex_t *mutex)
161 if (!SystemHeap) return 0;
162 if (!((wine_mutex)mutex)->critsect)
163 mutex_real_init( mutex );
165 EnterCriticalSection(((wine_mutex)mutex)->critsect);
166 return 0;
168 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
170 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
172 if (!SystemHeap) return 0;
173 if (!((wine_mutex)mutex)->critsect)
174 mutex_real_init( mutex );
176 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
177 errno = EBUSY;
178 return -1;
180 return 0;
182 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
184 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
186 if (!((wine_mutex)mutex)->critsect) return 0;
187 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
188 return 0;
190 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
192 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
194 if (!((wine_mutex)mutex)->critsect) return 0;
195 if (((wine_mutex)mutex)->critsect->RecursionCount) {
196 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
197 return EBUSY;
198 #else
199 while (((wine_mutex)mutex)->critsect->RecursionCount)
200 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
201 #endif
203 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
204 HeapFree(SystemHeap, 0, ((wine_mutex)mutex)->critsect);
205 return 0;
207 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
210 /***** MUTEX ATTRIBUTES *****/
211 /* just dummies, since critical sections are always recursive */
213 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
215 return 0;
217 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
219 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
221 return 0;
223 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
225 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
227 return 0;
229 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
231 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
233 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
234 return 0;
236 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
238 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
240 return 0;
242 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
244 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
246 *kind = PTHREAD_MUTEX_RECURSIVE_NP;
247 return 0;
249 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
252 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
254 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
256 static LONG keycnt = FIRST_KEY;
257 *key = InterlockedExchangeAdd(&keycnt, 1);
258 return 0;
260 strong_alias(__pthread_key_create, pthread_key_create);
262 int __pthread_key_delete(pthread_key_t key)
264 return 0;
266 strong_alias(__pthread_key_delete, pthread_key_delete);
268 int __pthread_setspecific(pthread_key_t key, const void *pointer)
270 TEB *teb = NtCurrentTeb();
271 if (!teb->pthread_data) {
272 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
274 ((key_data*)(teb->pthread_data))[key] = pointer;
275 return 0;
277 strong_alias(__pthread_setspecific, pthread_setspecific);
279 void *__pthread_getspecific(pthread_key_t key)
281 TEB *teb = NtCurrentTeb();
282 if (!teb) return NULL;
283 if (!teb->pthread_data) return NULL;
284 return (void *)(((key_data*)(teb->pthread_data))[key]);
286 strong_alias(__pthread_getspecific, pthread_getspecific);
289 /***** "EXCEPTION" FRAMES *****/
290 /* not implemented right now */
292 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
294 ((wine_cleanup)buffer)->routine = routine;
295 ((wine_cleanup)buffer)->arg = arg;
298 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
300 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
303 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
305 _pthread_cleanup_push(buffer, routine, arg);
308 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
310 _pthread_cleanup_pop(buffer, execute);
314 /***** CONDITIONS *****/
315 /* not implemented right now */
317 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
319 P_OUTPUT("FIXME:pthread_cond_init\n");
320 return 0;
323 int pthread_cond_destroy(pthread_cond_t *cond)
325 P_OUTPUT("FIXME:pthread_cond_destroy\n");
326 return 0;
329 int pthread_cond_signal(pthread_cond_t *cond)
331 P_OUTPUT("FIXME:pthread_cond_signal\n");
332 return 0;
335 int pthread_cond_broadcast(pthread_cond_t *cond)
337 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
338 return 0;
341 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
343 P_OUTPUT("FIXME:pthread_cond_wait\n");
344 return 0;
347 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
349 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
350 return 0;
353 /**** CONDITION ATTRIBUTES *****/
354 /* not implemented right now */
356 int pthread_condattr_init(pthread_condattr_t *attr)
358 return 0;
361 int pthread_condattr_destroy(pthread_condattr_t *attr)
363 return 0;
366 /***** MISC *****/
368 pthread_t pthread_self(void)
370 return (pthread_t)GetCurrentThreadId();
373 int pthread_equal(pthread_t thread1, pthread_t thread2)
375 return (DWORD)thread1 == (DWORD)thread2;
378 void pthread_exit(void *retval)
380 /* FIXME: pthread cleanup */
381 ExitThread((DWORD)retval);
384 int pthread_setcanceltype(int type, int *oldtype)
386 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
387 return 0;
390 /***** ANTI-OVERRIDES *****/
391 /* pthreads tries to override these, point them back to libc */
393 #ifdef jump_alias
394 jump_alias(LIBC_SIGACTION, sigaction);
395 #else
396 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
398 return LIBC_SIGACTION(signum, act, oldact);
400 #endif
402 #endif /* __GLIBC__ */