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
25 #include "wine/port.h"
27 #define _GNU_SOURCE /* we may need to override some GNU extensions */
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__)
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))
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)
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
85 CRITICAL_SECTION
*critsect
;
88 /* see wine_mutex above for comments */
93 typedef struct _wine_cleanup
{
94 void (*routine
)(void *);
98 typedef const void *key_data
;
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*);
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
)
125 struct pthread_thread_init
* idata
= HeapAlloc(GetProcessHeap(), 0,
126 sizeof(struct pthread_thread_init
));
128 idata
->start_routine
= start_routine
;
130 hThread
= CreateThread( NULL
, 0, pthread_thread_start
, idata
, 0,
134 CloseHandle(hThread
);
137 HeapFree(GetProcessHeap(),0,idata
); /* free idata struct on failure */
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
);
174 /*FIXME: not sure what to do with this one... */
175 int pthread_detach(pthread_t thread
)
177 P_OUTPUT("FIXME:pthread_detach\n");
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");
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
;
209 memcpy(&once_now
,&the_once
,sizeof(once_now
));
210 if (InterlockedCompareExchange((LONG
*)once_control
, once_now
+1, once_now
) == once_now
)
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
);
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),
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
;
243 if (init_done
) LeaveCriticalSection( &atfork_section
);
246 strong_alias(__pthread_atfork
, pthread_atfork
);
255 libc_fork
= dlsym( RTLD_NEXT
, "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
]();
268 for (i
= 0; i
< atfork_count
; i
++) if (atfork_parent
[i
]) atfork_parent
[i
]();
269 LeaveCriticalSection( &atfork_section
);
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
;
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
);
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
)) {
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
);
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 */
343 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
344 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
347 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
348 HeapFree(GetProcessHeap(), 0, ((wine_mutex
)mutex
)->critsect
);
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
)
361 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
363 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
367 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
369 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
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
;
380 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
382 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
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
;
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);
404 strong_alias(__pthread_key_create
, pthread_key_create
);
406 int __pthread_key_delete(pthread_key_t key
)
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
;
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");
467 int pthread_cond_destroy(pthread_cond_t
*cond
)
469 P_OUTPUT("FIXME:pthread_cond_destroy\n");
473 int pthread_cond_signal(pthread_cond_t
*cond
)
475 P_OUTPUT("FIXME:pthread_cond_signal\n");
479 int pthread_cond_broadcast(pthread_cond_t
*cond
)
481 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
485 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
487 P_OUTPUT("FIXME:pthread_cond_wait\n");
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");
497 /**** CONDITION ATTRIBUTES *****/
498 /* not implemented right now */
500 int pthread_condattr_init(pthread_condattr_t
*attr
)
505 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
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
;
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
);
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
);
548 if (RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, TRUE
))
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
)) {
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
);
574 if (RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, TRUE
))
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
)) {
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
);
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
)
609 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
613 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
615 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
621 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
625 #endif /* glibc 2.2 */
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
;
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
)
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__ */