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
11 #define _GNU_SOURCE /* we may need to override some GNU extensions */
22 /* Currently this probably works only for glibc2,
23 * which checks for the presence of double-underscore-prepended
24 * pthread primitives, and use them if available.
25 * If they are not available, the libc defaults to
26 * non-threadsafe operation (not good). */
28 #if defined(__GLIBC__)
32 #ifdef NEED_UNDERSCORE_PREFIX
38 #define PSTR(str) PREFIX #str
40 /* adapt as necessary (a construct like this is used in glibc sources) */
41 #define strong_alias(orig, alias) \
42 asm(".globl " PSTR(alias) "\n" \
43 "\t.set " PSTR(alias) "," PSTR(orig))
45 /* strong_alias does not work on external symbols (.o format limitation?),
46 * so for those, we need to use the pogo stick */
47 #if defined(__i386__) && !defined(__PIC__)
49 #define jump_alias(orig, alias) \
50 asm(".globl " PSTR(alias) "\n" \
51 "\t.type " PSTR(alias) ",@function\n" \
56 /* get necessary libc symbols */
57 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
58 #define LIBC_FORK __libc_fork
59 #define PTHREAD_FORK __fork
62 #define LIBC_FORK __fork
63 #define PTHREAD_FORK fork
65 extern pid_t
LIBC_FORK(void);
67 #define LIBC_SIGACTION __sigaction
69 /* NOTE: This is a truly extremely incredibly ugly hack!
70 * But it does seem to work... */
72 /* assume that pthread_mutex_t has room for at least one pointer,
73 * and hope that the users of pthread_mutex_t considers it opaque
74 * (never checks what's in it) */
76 CRITICAL_SECTION
*critsect
;
79 typedef struct _wine_cleanup
{
80 void (*routine
)(void *);
84 typedef const void *key_data
;
87 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
89 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
91 void __pthread_initialize(void)
95 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
97 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
98 LONG once_now
= *(LONG
*)&the_once
;
100 if (InterlockedCompareExchange((PVOID
*)once_control
, (PVOID
)(once_now
+1), (PVOID
)once_now
) == (PVOID
)once_now
)
104 strong_alias(__pthread_once
, pthread_once
);
106 void __pthread_kill_other_threads_np(void)
108 /* FIXME: this is supposed to be preparation for exec() */
109 if (SystemHeap
) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
111 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
115 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
117 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT
;
118 typedef void (*atfork_handler
)();
119 static atfork_handler atfork_prepare
[MAX_ATFORK
];
120 static atfork_handler atfork_parent
[MAX_ATFORK
];
121 static atfork_handler atfork_child
[MAX_ATFORK
];
122 static int atfork_count
;
124 int __pthread_atfork(void (*prepare
)(void),
125 void (*parent
)(void),
128 if (SystemHeap
) EnterCriticalSection( &atfork_section
);
129 assert( atfork_count
< MAX_ATFORK
);
130 atfork_prepare
[atfork_count
] = prepare
;
131 atfork_parent
[atfork_count
] = parent
;
132 atfork_child
[atfork_count
] = child
;
134 if (SystemHeap
) LeaveCriticalSection( &atfork_section
);
137 strong_alias(__pthread_atfork
, pthread_atfork
);
139 pid_t
PTHREAD_FORK(void)
144 EnterCriticalSection( &atfork_section
);
145 /* prepare handlers are called in reverse insertion order */
146 for (i
= atfork_count
- 1; i
>= 0; i
--) atfork_prepare
[i
]();
147 if (!(pid
= LIBC_FORK()))
149 InitializeCriticalSection( &atfork_section
);
150 for (i
= 0; i
< atfork_count
; i
++) atfork_child
[i
]();
154 for (i
= 0; i
< atfork_count
; i
++) atfork_parent
[i
]();
155 LeaveCriticalSection( &atfork_section
);
160 strong_alias(PTHREAD_FORK
, fork
);
163 /***** MUTEXES *****/
165 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
166 const pthread_mutexattr_t
*mutexattr
)
168 /* glibc has a tendency to initialize mutexes very often, even
169 in situations where they are not really used later on.
171 As for us, initializing a mutex is very expensive, we postpone
172 the real initialization until the time the mutex is first used. */
174 ((wine_mutex
)mutex
)->critsect
= NULL
;
177 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
179 static void mutex_real_init( pthread_mutex_t
*mutex
)
181 CRITICAL_SECTION
*critsect
= HeapAlloc(SystemHeap
, 0, sizeof(CRITICAL_SECTION
));
182 InitializeCriticalSection(critsect
);
184 if (InterlockedCompareExchange((PVOID
*)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
185 /* too late, some other thread already did it */
186 DeleteCriticalSection(critsect
);
187 HeapFree(SystemHeap
, 0, critsect
);
191 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
193 if (!SystemHeap
) return 0;
194 if (!((wine_mutex
)mutex
)->critsect
)
195 mutex_real_init( mutex
);
197 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
200 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
202 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
204 if (!SystemHeap
) return 0;
205 if (!((wine_mutex
)mutex
)->critsect
)
206 mutex_real_init( mutex
);
208 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
214 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
216 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
218 if (!((wine_mutex
)mutex
)->critsect
) return 0;
219 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
222 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
224 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
226 if (!((wine_mutex
)mutex
)->critsect
) return 0;
227 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
228 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
231 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
232 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
235 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
236 HeapFree(SystemHeap
, 0, ((wine_mutex
)mutex
)->critsect
);
239 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
242 /***** MUTEX ATTRIBUTES *****/
243 /* just dummies, since critical sections are always recursive */
245 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
249 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
251 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
255 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
257 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
261 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
263 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
265 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
268 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
270 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
274 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
276 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
278 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
281 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
284 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
286 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
288 static LONG keycnt
= FIRST_KEY
;
289 *key
= InterlockedExchangeAdd(&keycnt
, 1);
292 strong_alias(__pthread_key_create
, pthread_key_create
);
294 int __pthread_key_delete(pthread_key_t key
)
298 strong_alias(__pthread_key_delete
, pthread_key_delete
);
300 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
302 TEB
*teb
= NtCurrentTeb();
303 if (!teb
->pthread_data
) {
304 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
306 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
309 strong_alias(__pthread_setspecific
, pthread_setspecific
);
311 void *__pthread_getspecific(pthread_key_t key
)
313 TEB
*teb
= NtCurrentTeb();
314 if (!teb
) return NULL
;
315 if (!teb
->pthread_data
) return NULL
;
316 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
318 strong_alias(__pthread_getspecific
, pthread_getspecific
);
321 /***** "EXCEPTION" FRAMES *****/
322 /* not implemented right now */
324 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
326 ((wine_cleanup
)buffer
)->routine
= routine
;
327 ((wine_cleanup
)buffer
)->arg
= arg
;
330 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
332 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
335 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
337 _pthread_cleanup_push(buffer
, routine
, arg
);
340 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
342 _pthread_cleanup_pop(buffer
, execute
);
346 /***** CONDITIONS *****/
347 /* not implemented right now */
349 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
351 P_OUTPUT("FIXME:pthread_cond_init\n");
355 int pthread_cond_destroy(pthread_cond_t
*cond
)
357 P_OUTPUT("FIXME:pthread_cond_destroy\n");
361 int pthread_cond_signal(pthread_cond_t
*cond
)
363 P_OUTPUT("FIXME:pthread_cond_signal\n");
367 int pthread_cond_broadcast(pthread_cond_t
*cond
)
369 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
373 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
375 P_OUTPUT("FIXME:pthread_cond_wait\n");
379 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
381 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
385 /**** CONDITION ATTRIBUTES *****/
386 /* not implemented right now */
388 int pthread_condattr_init(pthread_condattr_t
*attr
)
393 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
398 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
399 /***** READ-WRITE LOCKS *****/
400 /* not implemented right now */
402 int __pthread_rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*rwlock_attr
)
404 P_OUTPUT("FIXME:pthread_rwlock_init\n");
407 strong_alias(__pthread_rwlock_init
, pthread_rwlock_init
);
409 int __pthread_rwlock_destroy(pthread_rwlock_t
*rwlock
)
411 P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
414 strong_alias(__pthread_rwlock_destroy
, pthread_rwlock_destroy
);
416 int __pthread_rwlock_rdlock(pthread_rwlock_t
*rwlock
)
418 P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
421 strong_alias(__pthread_rwlock_rdlock
, pthread_rwlock_rdlock
);
423 int __pthread_rwlock_tryrdlock(pthread_rwlock_t
*rwlock
)
425 P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
428 strong_alias(__pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
);
430 int __pthread_rwlock_wrlock(pthread_rwlock_t
*rwlock
)
432 P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
435 strong_alias(__pthread_rwlock_wrlock
, pthread_rwlock_wrlock
);
437 int __pthread_rwlock_trywrlock(pthread_rwlock_t
*rwlock
)
439 P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
442 strong_alias(__pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
);
444 int __pthread_rwlock_unlock(pthread_rwlock_t
*rwlock
)
446 P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
449 strong_alias(__pthread_rwlock_unlock
, pthread_rwlock_unlock
);
451 /**** READ-WRITE LOCK ATTRIBUTES *****/
452 /* not implemented right now */
454 int pthread_rwlockattr_init(pthread_rwlockattr_t
*attr
)
459 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
463 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
465 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
471 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
475 #endif /* glibc 2.2 */
479 pthread_t
pthread_self(void)
481 return (pthread_t
)GetCurrentThreadId();
484 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
486 return (DWORD
)thread1
== (DWORD
)thread2
;
489 void pthread_exit(void *retval
)
491 /* FIXME: pthread cleanup */
492 ExitThread((DWORD
)retval
);
495 int pthread_setcanceltype(int type
, int *oldtype
)
497 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
501 /***** ANTI-OVERRIDES *****/
502 /* pthreads tries to override these, point them back to libc */
505 jump_alias(LIBC_SIGACTION
, sigaction
);
507 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
509 return LIBC_SIGACTION(signum
, act
, oldact
);
513 #endif /* __GLIBC__ */