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
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
25 void PTHREAD_init_done(void)
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__)
40 #ifdef NEED_UNDERSCORE_PREFIX
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__)
57 #define jump_alias(orig, alias) \
58 asm(".globl " PSTR(alias) "\n" \
59 "\t.type " PSTR(alias) ",@function\n" \
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
70 #define LIBC_FORK __fork
71 #define PTHREAD_FORK fork
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
89 CRITICAL_SECTION
*critsect
;
92 /* see wine_mutex above for comments */
97 typedef struct _wine_cleanup
{
98 void (*routine
)(void *);
102 typedef const void *key_data
;
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
)
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
);
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),
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
;
151 if (init_done
) LeaveCriticalSection( &atfork_section
);
154 strong_alias(__pthread_atfork
, pthread_atfork
);
156 pid_t
PTHREAD_FORK(void)
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
]();
171 for (i
= 0; i
< atfork_count
; i
++) if (atfork_parent
[i
]) atfork_parent
[i
]();
172 LeaveCriticalSection( &atfork_section
);
177 strong_alias(PTHREAD_FORK
, fork
);
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
;
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
);
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
)) {
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
);
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 */
248 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
249 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
252 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
253 HeapFree(GetProcessHeap(), 0, ((wine_mutex
)mutex
)->critsect
);
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
)
266 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
268 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
272 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
274 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
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
;
285 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
287 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
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
;
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);
309 strong_alias(__pthread_key_create
, pthread_key_create
);
311 int __pthread_key_delete(pthread_key_t key
)
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
;
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");
372 int pthread_cond_destroy(pthread_cond_t
*cond
)
374 P_OUTPUT("FIXME:pthread_cond_destroy\n");
378 int pthread_cond_signal(pthread_cond_t
*cond
)
380 P_OUTPUT("FIXME:pthread_cond_signal\n");
384 int pthread_cond_broadcast(pthread_cond_t
*cond
)
386 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
390 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
392 P_OUTPUT("FIXME:pthread_cond_wait\n");
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");
402 /**** CONDITION ATTRIBUTES *****/
403 /* not implemented right now */
405 int pthread_condattr_init(pthread_condattr_t
*attr
)
410 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
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
;
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
);
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
);
453 if (RtlAcquireResourceShared(((wine_rwlock
)rwlock
)->lock
, TRUE
))
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
)) {
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
);
479 if (RtlAcquireResourceExclusive(((wine_rwlock
)rwlock
)->lock
, TRUE
))
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
)) {
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
);
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
)
514 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
518 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
520 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
526 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
530 #endif /* glibc 2.2 */
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
;
556 /***** ANTI-OVERRIDES *****/
557 /* pthreads tries to override these, point them back to libc */
560 jump_alias(LIBC_SIGACTION
, sigaction
);
562 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
564 return LIBC_SIGACTION(signum
, act
, oldact
);
568 #endif /* __GLIBC__ */