Put console documentation in sync with current console status.
[wine/wine-kai.git] / scheduler / pthread.c
blobdba503ce27f15ba737b230b185cc20a254eb4aff
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #define _GNU_SOURCE /* we may need to override some GNU extensions */
29 #include <assert.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <string.h>
37 #include "winbase.h"
38 #include "thread.h"
39 #include "winternl.h"
41 /* Currently this probably works only for glibc2,
42 * which checks for the presence of double-underscore-prepended
43 * pthread primitives, and use them if available.
44 * If they are not available, the libc defaults to
45 * non-threadsafe operation (not good). */
47 #if defined(__GLIBC__) || defined(__FreeBSD__)
49 #ifndef __USE_UNIX98
50 #define __USE_UNIX98
51 #endif
53 #include <pthread.h>
54 #include <signal.h>
56 #define PSTR(str) __ASM_NAME(#str)
58 /* adapt as necessary (a construct like this is used in glibc sources) */
59 #define strong_alias(orig, alias) \
60 asm(".globl " PSTR(alias) "\n" \
61 "\t.set " PSTR(alias) "," PSTR(orig))
63 static int init_done;
65 static pid_t (*libc_fork)(void);
66 static int (*libc_sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact);
68 void PTHREAD_init_done(void)
70 init_done = 1;
71 if (!libc_fork) libc_fork = dlsym( RTLD_NEXT, "fork" );
72 if (!libc_sigaction) libc_sigaction = dlsym( RTLD_NEXT, "sigaction" );
76 /* NOTE: This is a truly extremely incredibly ugly hack!
77 * But it does seem to work... */
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 * also: assume that static initializer sets pointer to NULL
84 typedef struct {
85 CRITICAL_SECTION *critsect;
86 } *wine_mutex;
88 /* see wine_mutex above for comments */
89 typedef struct {
90 RTL_RWLOCK *lock;
91 } *wine_rwlock;
93 typedef struct _wine_cleanup {
94 void (*routine)(void *);
95 void *arg;
96 } *wine_cleanup;
98 typedef const void *key_data;
100 #define FIRST_KEY 0
101 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
103 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
105 void __pthread_initialize(void)
109 struct pthread_thread_init {
110 void* (*start_routine)(void*);
111 void* arg;
114 static DWORD CALLBACK pthread_thread_start(LPVOID data)
116 struct pthread_thread_init init = *(struct pthread_thread_init*)data;
117 HeapFree(GetProcessHeap(),0,data);
118 return (DWORD)init.start_routine(init.arg);
121 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void*
122 (*start_routine)(void *), void* arg)
124 HANDLE hThread;
125 struct pthread_thread_init* idata = HeapAlloc(GetProcessHeap(), 0,
126 sizeof(struct pthread_thread_init));
128 idata->start_routine = start_routine;
129 idata->arg = arg;
130 hThread = CreateThread( NULL, 0, pthread_thread_start, idata, 0,
131 (LPDWORD)thread);
133 if(hThread)
134 CloseHandle(hThread);
135 else
137 HeapFree(GetProcessHeap(),0,idata); /* free idata struct on failure */
138 return EAGAIN;
141 return 0;
144 int pthread_cancel(pthread_t thread)
146 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD)thread);
148 if(!TerminateThread(hThread, 0))
150 CloseHandle(hThread);
151 return EINVAL; /* return error */
154 CloseHandle(hThread);
156 return 0; /* return success */
159 int pthread_join(pthread_t thread, void **value_ptr)
161 HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD)thread);
163 WaitForSingleObject(hThread, INFINITE);
164 if(!GetExitCodeThread(hThread, (LPDWORD)value_ptr))
166 CloseHandle(hThread);
167 return EINVAL; /* FIXME: make this more correctly match */
168 } /* windows errors */
170 CloseHandle(hThread);
171 return 0;
174 /*FIXME: not sure what to do with this one... */
175 int pthread_detach(pthread_t thread)
177 P_OUTPUT("FIXME:pthread_detach\n");
178 return 0;
181 /* FIXME: we have no equivalents in win32 for the policys */
182 /* so just keep this as a stub */
183 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
185 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
186 return 0;
189 /* FIXME: no win32 equivalent for scope */
190 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
192 P_OUTPUT("FIXME:pthread_attr_setscope\n");
193 return 0; /* return success */
196 /* FIXME: no win32 equivalent for schedule param */
197 int pthread_attr_setschedparam(pthread_attr_t *attr,
198 const struct sched_param *param)
200 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
201 return 0; /* return success */
204 int __pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
206 static pthread_once_t the_once = PTHREAD_ONCE_INIT;
207 LONG once_now;
209 memcpy(&once_now,&the_once,sizeof(once_now));
210 if (InterlockedCompareExchange((LONG*)once_control, once_now+1, once_now) == once_now)
211 (*init_routine)();
212 return 0;
214 strong_alias(__pthread_once, pthread_once);
216 void __pthread_kill_other_threads_np(void)
218 /* we don't need to do anything here */
220 strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np);
222 /***** atfork *****/
224 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
226 static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT("atfork_section");
227 typedef void (*atfork_handler)();
228 static atfork_handler atfork_prepare[MAX_ATFORK];
229 static atfork_handler atfork_parent[MAX_ATFORK];
230 static atfork_handler atfork_child[MAX_ATFORK];
231 static int atfork_count;
233 int __pthread_atfork(void (*prepare)(void),
234 void (*parent)(void),
235 void (*child)(void))
237 if (init_done) EnterCriticalSection( &atfork_section );
238 assert( atfork_count < MAX_ATFORK );
239 atfork_prepare[atfork_count] = prepare;
240 atfork_parent[atfork_count] = parent;
241 atfork_child[atfork_count] = child;
242 atfork_count++;
243 if (init_done) LeaveCriticalSection( &atfork_section );
244 return 0;
246 strong_alias(__pthread_atfork, pthread_atfork);
248 pid_t __fork(void)
250 pid_t pid;
251 int i;
253 if (!libc_fork)
255 libc_fork = dlsym( RTLD_NEXT, "fork" );
256 assert( libc_fork );
258 EnterCriticalSection( &atfork_section );
259 /* prepare handlers are called in reverse insertion order */
260 for (i = atfork_count - 1; i >= 0; i--) if (atfork_prepare[i]) atfork_prepare[i]();
261 if (!(pid = libc_fork()))
263 InitializeCriticalSection( &atfork_section );
264 for (i = 0; i < atfork_count; i++) if (atfork_child[i]) atfork_child[i]();
266 else
268 for (i = 0; i < atfork_count; i++) if (atfork_parent[i]) atfork_parent[i]();
269 LeaveCriticalSection( &atfork_section );
271 return pid;
273 strong_alias(__fork, fork);
275 /***** MUTEXES *****/
277 int __pthread_mutex_init(pthread_mutex_t *mutex,
278 const pthread_mutexattr_t *mutexattr)
280 /* glibc has a tendency to initialize mutexes very often, even
281 in situations where they are not really used later on.
283 As for us, initializing a mutex is very expensive, we postpone
284 the real initialization until the time the mutex is first used. */
286 ((wine_mutex)mutex)->critsect = NULL;
287 return 0;
289 strong_alias(__pthread_mutex_init, pthread_mutex_init);
291 static void mutex_real_init( pthread_mutex_t *mutex )
293 CRITICAL_SECTION *critsect = HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION));
294 InitializeCriticalSection(critsect);
296 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex)mutex)->critsect),critsect,NULL) != NULL) {
297 /* too late, some other thread already did it */
298 DeleteCriticalSection(critsect);
299 HeapFree(GetProcessHeap(), 0, critsect);
303 int __pthread_mutex_lock(pthread_mutex_t *mutex)
305 if (!init_done) return 0;
306 if (!((wine_mutex)mutex)->critsect)
307 mutex_real_init( mutex );
309 EnterCriticalSection(((wine_mutex)mutex)->critsect);
310 return 0;
312 strong_alias(__pthread_mutex_lock, pthread_mutex_lock);
314 int __pthread_mutex_trylock(pthread_mutex_t *mutex)
316 if (!init_done) return 0;
317 if (!((wine_mutex)mutex)->critsect)
318 mutex_real_init( mutex );
320 if (!TryEnterCriticalSection(((wine_mutex)mutex)->critsect)) {
321 errno = EBUSY;
322 return -1;
324 return 0;
326 strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
328 int __pthread_mutex_unlock(pthread_mutex_t *mutex)
330 if (!((wine_mutex)mutex)->critsect) return 0;
331 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
332 return 0;
334 strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
336 int __pthread_mutex_destroy(pthread_mutex_t *mutex)
338 if (!((wine_mutex)mutex)->critsect) return 0;
339 if (((wine_mutex)mutex)->critsect->RecursionCount) {
340 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
341 return EBUSY;
342 #else
343 while (((wine_mutex)mutex)->critsect->RecursionCount)
344 LeaveCriticalSection(((wine_mutex)mutex)->critsect);
345 #endif
347 DeleteCriticalSection(((wine_mutex)mutex)->critsect);
348 HeapFree(GetProcessHeap(), 0, ((wine_mutex)mutex)->critsect);
349 return 0;
351 strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy);
354 /***** MUTEX ATTRIBUTES *****/
355 /* just dummies, since critical sections are always recursive */
357 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
359 return 0;
361 strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init);
363 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
365 return 0;
367 strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy);
369 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
371 return 0;
373 strong_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
375 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind)
377 *kind = PTHREAD_MUTEX_RECURSIVE;
378 return 0;
380 strong_alias(__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
382 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
384 return 0;
386 strong_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype);
388 int __pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *kind)
390 *kind = PTHREAD_MUTEX_RECURSIVE;
391 return 0;
393 strong_alias(__pthread_mutexattr_gettype, pthread_mutexattr_gettype);
396 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
398 int __pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
400 static LONG keycnt = FIRST_KEY;
401 *key = InterlockedExchangeAdd(&keycnt, 1);
402 return 0;
404 strong_alias(__pthread_key_create, pthread_key_create);
406 int __pthread_key_delete(pthread_key_t key)
408 return 0;
410 strong_alias(__pthread_key_delete, pthread_key_delete);
412 int __pthread_setspecific(pthread_key_t key, const void *pointer)
414 TEB *teb = NtCurrentTeb();
415 if (!teb->pthread_data) {
416 teb->pthread_data = calloc(MAX_KEYS,sizeof(key_data));
418 ((key_data*)(teb->pthread_data))[key] = pointer;
419 return 0;
421 strong_alias(__pthread_setspecific, pthread_setspecific);
423 void *__pthread_getspecific(pthread_key_t key)
425 TEB *teb = NtCurrentTeb();
426 if (!teb) return NULL;
427 if (!teb->pthread_data) return NULL;
428 return (void *)(((key_data*)(teb->pthread_data))[key]);
430 strong_alias(__pthread_getspecific, pthread_getspecific);
433 /***** "EXCEPTION" FRAMES *****/
434 /* not implemented right now */
436 void _pthread_cleanup_push(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
438 ((wine_cleanup)buffer)->routine = routine;
439 ((wine_cleanup)buffer)->arg = arg;
442 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer *buffer, int execute)
444 if (execute) (*(((wine_cleanup)buffer)->routine))(((wine_cleanup)buffer)->arg);
447 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *buffer, void (*routine)(void *), void *arg)
449 _pthread_cleanup_push(buffer, routine, arg);
452 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *buffer, int execute)
454 _pthread_cleanup_pop(buffer, execute);
458 /***** CONDITIONS *****/
459 /* not implemented right now */
461 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
463 P_OUTPUT("FIXME:pthread_cond_init\n");
464 return 0;
467 int pthread_cond_destroy(pthread_cond_t *cond)
469 P_OUTPUT("FIXME:pthread_cond_destroy\n");
470 return 0;
473 int pthread_cond_signal(pthread_cond_t *cond)
475 P_OUTPUT("FIXME:pthread_cond_signal\n");
476 return 0;
479 int pthread_cond_broadcast(pthread_cond_t *cond)
481 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
482 return 0;
485 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
487 P_OUTPUT("FIXME:pthread_cond_wait\n");
488 return 0;
491 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
493 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
494 return 0;
497 /**** CONDITION ATTRIBUTES *****/
498 /* not implemented right now */
500 int pthread_condattr_init(pthread_condattr_t *attr)
502 return 0;
505 int pthread_condattr_destroy(pthread_condattr_t *attr)
507 return 0;
510 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
511 /***** READ-WRITE LOCKS *****/
513 static void rwlock_real_init(pthread_rwlock_t *rwlock)
515 RTL_RWLOCK *lock = HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK));
516 RtlInitializeResource(lock);
518 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock)rwlock)->lock),lock,NULL) != NULL) {
519 /* too late, some other thread already did it */
520 RtlDeleteResource(lock);
521 HeapFree(GetProcessHeap(), 0, lock);
525 int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *rwlock_attr)
527 ((wine_rwlock)rwlock)->lock = NULL;
528 return 0;
530 strong_alias(__pthread_rwlock_init, pthread_rwlock_init);
532 int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
534 if (!((wine_rwlock)rwlock)->lock) return 0;
535 RtlDeleteResource(((wine_rwlock)rwlock)->lock);
536 HeapFree(GetProcessHeap(), 0, ((wine_rwlock)rwlock)->lock);
537 return 0;
539 strong_alias(__pthread_rwlock_destroy, pthread_rwlock_destroy);
541 int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
543 if (!init_done) return 0;
544 if (!((wine_rwlock)rwlock)->lock)
545 rwlock_real_init( rwlock );
547 while(TRUE)
548 if (RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, TRUE))
549 return 0;
551 strong_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
553 int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
555 if (!init_done) return 0;
556 if (!((wine_rwlock)rwlock)->lock)
557 rwlock_real_init( rwlock );
559 if (!RtlAcquireResourceShared(((wine_rwlock)rwlock)->lock, FALSE)) {
560 errno = EBUSY;
561 return -1;
563 return 0;
565 strong_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
567 int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
569 if (!init_done) return 0;
570 if (!((wine_rwlock)rwlock)->lock)
571 rwlock_real_init( rwlock );
573 while(TRUE)
574 if (RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, TRUE))
575 return 0;
577 strong_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
579 int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
581 if (!init_done) return 0;
582 if (!((wine_rwlock)rwlock)->lock)
583 rwlock_real_init( rwlock );
585 if (!RtlAcquireResourceExclusive(((wine_rwlock)rwlock)->lock, FALSE)) {
586 errno = EBUSY;
587 return -1;
589 return 0;
591 strong_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
593 int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
595 if (!((wine_rwlock)rwlock)->lock) return 0;
596 RtlReleaseResource( ((wine_rwlock)rwlock)->lock );
597 return 0;
599 strong_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
601 /**** READ-WRITE LOCK ATTRIBUTES *****/
602 /* not implemented right now */
604 int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
606 return 0;
609 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
611 return 0;
613 strong_alias(__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
615 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *pref)
617 *pref = 0;
618 return 0;
621 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref)
623 return 0;
625 #endif /* glibc 2.2 */
627 /***** MISC *****/
629 pthread_t pthread_self(void)
631 return (pthread_t)GetCurrentThreadId();
634 int pthread_equal(pthread_t thread1, pthread_t thread2)
636 return (DWORD)thread1 == (DWORD)thread2;
639 void pthread_exit(void *retval)
641 /* FIXME: pthread cleanup */
642 ExitThread((DWORD)retval);
645 int pthread_setcanceltype(int type, int *oldtype)
647 if (oldtype) *oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
648 return 0;
651 /***** ANTI-OVERRIDES *****/
652 /* pthreads tries to override these, point them back to libc */
654 int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
656 if (!libc_sigaction)
658 libc_sigaction = dlsym( RTLD_NEXT, "sigaction" );
659 assert( libc_sigaction );
661 return libc_sigaction(signum, act, oldact);
664 #else /* __GLIBC__ || __FREEBSD__ */
666 void PTHREAD_init_done(void)
670 #endif /* __GLIBC__ || __FREEBSD__ */