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__)
51 #define PSTR(str) __ASM_NAME(#str)
53 /* adapt as necessary (a construct like this is used in glibc sources) */
54 #define strong_alias(orig, alias) \
55 asm(".globl " PSTR(alias) "\n" \
56 "\t.set " PSTR(alias) "," PSTR(orig))
60 static pid_t (*libc_fork
)(void);
61 static int (*libc_sigaction
)(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
);
63 void PTHREAD_init_done(void)
66 if (!libc_fork
) libc_fork
= dlsym( RTLD_NEXT
, "fork" );
67 if (!libc_sigaction
) libc_sigaction
= dlsym( RTLD_NEXT
, "sigaction" );
71 /* NOTE: This is a truly extremely incredibly ugly hack!
72 * But it does seem to work... */
74 /* assume that pthread_mutex_t has room for at least one pointer,
75 * and hope that the users of pthread_mutex_t considers it opaque
76 * (never checks what's in it)
77 * also: assume that static initializer sets pointer to NULL
80 CRITICAL_SECTION
*critsect
;
83 /* see wine_mutex above for comments */
88 typedef struct _wine_cleanup
{
89 void (*routine
)(void *);
93 typedef const void *key_data
;
96 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
98 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
100 void __pthread_initialize(void)
104 struct pthread_thread_init
{
105 void* (*start_routine
)(void*);
109 static DWORD CALLBACK
pthread_thread_start(LPVOID data
)
111 struct pthread_thread_init init
= *(struct pthread_thread_init
*)data
;
112 HeapFree(GetProcessHeap(),0,data
);
113 return (DWORD
)init
.start_routine(init
.arg
);
116 int pthread_create(pthread_t
* thread
, const pthread_attr_t
* attr
, void*
117 (*start_routine
)(void *), void* arg
)
120 struct pthread_thread_init
* idata
= HeapAlloc(GetProcessHeap(), 0,
121 sizeof(struct pthread_thread_init
));
123 idata
->start_routine
= start_routine
;
125 hThread
= CreateThread(NULL
, 0, pthread_thread_start
, idata
, 0, thread
);
128 CloseHandle(hThread
);
131 HeapFree(GetProcessHeap(),0,idata
); /* free idata struct on failure */
138 int pthread_cancel(pthread_t thread
)
140 HANDLE hThread
= OpenThread(THREAD_ALL_ACCESS
, FALSE
, thread
);
142 if(!TerminateThread(hThread
, 0))
144 CloseHandle(hThread
);
145 return EINVAL
; /* return error */
148 CloseHandle(hThread
);
150 return 0; /* return success */
153 int pthread_join(pthread_t thread
, void **value_ptr
)
155 HANDLE hThread
= OpenThread(THREAD_ALL_ACCESS
, FALSE
, thread
);
157 WaitForSingleObject(hThread
, INFINITE
);
158 if(!GetExitCodeThread(hThread
, (LPDWORD
)value_ptr
))
160 CloseHandle(hThread
);
161 return EINVAL
; /* FIXME: make this more correctly match */
162 } /* windows errors */
164 CloseHandle(hThread
);
168 /*FIXME: not sure what to do with this one... */
169 int pthread_detach(pthread_t thread
)
171 P_OUTPUT("FIXME:pthread_detach\n");
175 /* FIXME: we have no equivalents in win32 for the policys */
176 /* so just keep this as a stub */
177 int pthread_attr_setschedpolicy(pthread_attr_t
*attr
, int policy
)
179 P_OUTPUT("FIXME:pthread_attr_setschedpolicy\n");
183 /* FIXME: no win32 equivalent for scope */
184 int pthread_attr_setscope(pthread_attr_t
*attr
, int scope
)
186 P_OUTPUT("FIXME:pthread_attr_setscope\n");
187 return 0; /* return success */
190 /* FIXME: no win32 equivalent for schedule param */
191 int pthread_attr_setschedparam(pthread_attr_t
*attr
,
192 const struct sched_param
*param
)
194 P_OUTPUT("FIXME:pthread_attr_setschedparam\n");
195 return 0; /* return success */
198 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
200 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
203 memcpy(&once_now
,&the_once
,sizeof(once_now
));
204 if (InterlockedCompareExchange((LONG
*)once_control
, once_now
+1, once_now
) == once_now
)
208 strong_alias(__pthread_once
, pthread_once
);
210 void __pthread_kill_other_threads_np(void)
212 /* we don't need to do anything here */
214 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
218 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
220 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT("atfork_section");
221 typedef void (*atfork_handler
)();
222 static atfork_handler atfork_prepare
[MAX_ATFORK
];
223 static atfork_handler atfork_parent
[MAX_ATFORK
];
224 static atfork_handler atfork_child
[MAX_ATFORK
];
225 static int atfork_count
;
227 int __pthread_atfork(void (*prepare
)(void),
228 void (*parent
)(void),
231 if (init_done
) EnterCriticalSection( &atfork_section
);
232 assert( atfork_count
< MAX_ATFORK
);
233 atfork_prepare
[atfork_count
] = prepare
;
234 atfork_parent
[atfork_count
] = parent
;
235 atfork_child
[atfork_count
] = child
;
237 if (init_done
) LeaveCriticalSection( &atfork_section
);
240 strong_alias(__pthread_atfork
, pthread_atfork
);
249 libc_fork
= dlsym( RTLD_NEXT
, "fork" );
252 EnterCriticalSection( &atfork_section
);
253 /* prepare handlers are called in reverse insertion order */
254 for (i
= atfork_count
- 1; i
>= 0; i
--) if (atfork_prepare
[i
]) atfork_prepare
[i
]();
255 if (!(pid
= libc_fork()))
257 InitializeCriticalSection( &atfork_section
);
258 for (i
= 0; i
< atfork_count
; i
++) if (atfork_child
[i
]) atfork_child
[i
]();
262 for (i
= 0; i
< atfork_count
; i
++) if (atfork_parent
[i
]) atfork_parent
[i
]();
263 LeaveCriticalSection( &atfork_section
);
267 strong_alias(__fork
, fork
);
269 /***** MUTEXES *****/
271 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
272 const pthread_mutexattr_t
*mutexattr
)
274 /* glibc has a tendency to initialize mutexes very often, even
275 in situations where they are not really used later on.
277 As for us, initializing a mutex is very expensive, we postpone
278 the real initialization until the time the mutex is first used. */
280 ((wine_mutex
)mutex
)->critsect
= NULL
;
283 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
285 static void mutex_real_init( pthread_mutex_t
*mutex
)
287 CRITICAL_SECTION
*critsect
= HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION
));
288 InitializeCriticalSection(critsect
);
290 if (InterlockedCompareExchangePointer((void**)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
291 /* too late, some other thread already did it */
292 DeleteCriticalSection(critsect
);
293 HeapFree(GetProcessHeap(), 0, critsect
);
297 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
299 if (!init_done
) return 0;
300 if (!((wine_mutex
)mutex
)->critsect
)
301 mutex_real_init( mutex
);
303 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
306 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
308 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
310 if (!init_done
) return 0;
311 if (!((wine_mutex
)mutex
)->critsect
)
312 mutex_real_init( mutex
);
314 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
320 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
322 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
324 if (!((wine_mutex
)mutex
)->critsect
) return 0;
325 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
328 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
330 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
332 if (!((wine_mutex
)mutex
)->critsect
) return 0;
333 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
334 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
337 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
338 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
341 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
342 HeapFree(GetProcessHeap(), 0, ((wine_mutex
)mutex
)->critsect
);
345 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
348 /***** MUTEX ATTRIBUTES *****/
349 /* just dummies, since critical sections are always recursive */
351 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
355 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
357 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
361 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
363 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
367 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
369 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
371 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
374 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
376 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
380 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
382 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
384 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
387 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
390 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
392 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
394 static LONG keycnt
= FIRST_KEY
;
395 *key
= InterlockedExchangeAdd(&keycnt
, 1);
398 strong_alias(__pthread_key_create
, pthread_key_create
);
400 int __pthread_key_delete(pthread_key_t key
)
404 strong_alias(__pthread_key_delete
, pthread_key_delete
);
406 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
408 TEB
*teb
= NtCurrentTeb();
409 if (!teb
->pthread_data
) {
410 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
412 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
415 strong_alias(__pthread_setspecific
, pthread_setspecific
);
417 void *__pthread_getspecific(pthread_key_t key
)
419 TEB
*teb
= NtCurrentTeb();
420 if (!teb
) return NULL
;
421 if (!teb
->pthread_data
) return NULL
;
422 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
424 strong_alias(__pthread_getspecific
, pthread_getspecific
);
427 /***** "EXCEPTION" FRAMES *****/
428 /* not implemented right now */
430 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
432 ((wine_cleanup
)buffer
)->routine
= routine
;
433 ((wine_cleanup
)buffer
)->arg
= arg
;
436 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
438 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
441 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
443 _pthread_cleanup_push(buffer
, routine
, arg
);
446 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
448 _pthread_cleanup_pop(buffer
, execute
);
452 /***** CONDITIONS *****/
453 /* not implemented right now */
455 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
457 P_OUTPUT("FIXME:pthread_cond_init\n");
461 int pthread_cond_destroy(pthread_cond_t
*cond
)
463 P_OUTPUT("FIXME:pthread_cond_destroy\n");
467 int pthread_cond_signal(pthread_cond_t
*cond
)
469 P_OUTPUT("FIXME:pthread_cond_signal\n");
473 int pthread_cond_broadcast(pthread_cond_t
*cond
)
475 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
479 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
481 P_OUTPUT("FIXME:pthread_cond_wait\n");
485 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
487 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
491 /**** CONDITION ATTRIBUTES *****/
492 /* not implemented right now */
494 int pthread_condattr_init(pthread_condattr_t
*attr
)
499 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
504 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
505 /***** READ-WRITE LOCKS *****/
507 static void rwlock_real_init(pthread_rwlock_t
*rwlock
)
509 RTL_RWLOCK
*lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(RTL_RWLOCK
));
510 RtlInitializeResource(lock
);
512 if (InterlockedCompareExchangePointer((void**)&(((wine_rwlock
)rwlock
)->lock
),lock
,NULL
) != NULL
) {
513 /* too late, some other thread already did it */
514 RtlDeleteResource(lock
);
515 HeapFree(GetProcessHeap(), 0, lock
);
519 int __pthread_rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*rwlock_attr
)
521 ((wine_rwlock
)rwlock
)->lock
= NULL
;
524 strong_alias(__pthread_rwlock_init
, pthread_rwlock_init
);
526 int __pthread_rwlock_destroy(pthread_rwlock_t
*rwlock
)
528 if (!((wine_rwlock
)rwlock
)->lock
) return 0;
529 RtlDeleteResource(((wine_rwlock
)rwlock
)->lock
);
530 HeapFree(GetProcessHeap(), 0, ((wine_rwlock
)rwlock
)->lock
);
533 strong_alias(__pthread_rwlock_destroy
, pthread_rwlock_destroy
);
535 int __pthread_rwlock_rdlock(pthread_rwlock_t
*rwlock
)
537 if (!init_done
) return 0;
538 if (!((wine_rwlock
)rwlock
)->lock
)
539 rwlock_real_init( rwlock
);
542 if (RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, TRUE
))
545 strong_alias(__pthread_rwlock_rdlock
, pthread_rwlock_rdlock
);
547 int __pthread_rwlock_tryrdlock(pthread_rwlock_t
*rwlock
)
549 if (!init_done
) return 0;
550 if (!((wine_rwlock
)rwlock
)->lock
)
551 rwlock_real_init( rwlock
);
553 if (!RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, FALSE
)) {
559 strong_alias(__pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
);
561 int __pthread_rwlock_wrlock(pthread_rwlock_t
*rwlock
)
563 if (!init_done
) return 0;
564 if (!((wine_rwlock
)rwlock
)->lock
)
565 rwlock_real_init( rwlock
);
568 if (RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, TRUE
))
571 strong_alias(__pthread_rwlock_wrlock
, pthread_rwlock_wrlock
);
573 int __pthread_rwlock_trywrlock(pthread_rwlock_t
*rwlock
)
575 if (!init_done
) return 0;
576 if (!((wine_rwlock
)rwlock
)->lock
)
577 rwlock_real_init( rwlock
);
579 if (!RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, FALSE
)) {
585 strong_alias(__pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
);
587 int __pthread_rwlock_unlock(pthread_rwlock_t
*rwlock
)
589 if (!((wine_rwlock
)rwlock
)->lock
) return 0;
590 RtlReleaseResource( ((wine_rwlock
)rwlock
)->lock
);
593 strong_alias(__pthread_rwlock_unlock
, pthread_rwlock_unlock
);
595 /**** READ-WRITE LOCK ATTRIBUTES *****/
596 /* not implemented right now */
598 int pthread_rwlockattr_init(pthread_rwlockattr_t
*attr
)
603 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
607 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
609 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
615 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
619 #endif /* glibc 2.2 */
623 pthread_t
pthread_self(void)
625 return (pthread_t
)GetCurrentThreadId();
628 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
630 return (DWORD
)thread1
== (DWORD
)thread2
;
633 void pthread_exit(void *retval
)
635 /* FIXME: pthread cleanup */
636 ExitThread((DWORD
)retval
);
639 int pthread_setcanceltype(int type
, int *oldtype
)
641 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
645 /***** ANTI-OVERRIDES *****/
646 /* pthreads tries to override these, point them back to libc */
648 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
652 libc_sigaction
= dlsym( RTLD_NEXT
, "sigaction" );
653 assert( libc_sigaction
);
655 return libc_sigaction(signum
, act
, oldact
);
658 #else /* __GLIBC__ */
660 void PTHREAD_init_done(void)
664 #endif /* __GLIBC__ */