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 */
24 void PTHREAD_init_done(void)
29 /* Currently this probably works only for glibc2,
30 * which checks for the presence of double-underscore-prepended
31 * pthread primitives, and use them if available.
32 * If they are not available, the libc defaults to
33 * non-threadsafe operation (not good). */
35 #if defined(__GLIBC__)
39 #ifdef NEED_UNDERSCORE_PREFIX
45 #define PSTR(str) PREFIX #str
47 /* adapt as necessary (a construct like this is used in glibc sources) */
48 #define strong_alias(orig, alias) \
49 asm(".globl " PSTR(alias) "\n" \
50 "\t.set " PSTR(alias) "," PSTR(orig))
52 /* strong_alias does not work on external symbols (.o format limitation?),
53 * so for those, we need to use the pogo stick */
54 #if defined(__i386__) && !defined(__PIC__)
56 #define jump_alias(orig, alias) \
57 asm(".globl " PSTR(alias) "\n" \
58 "\t.type " PSTR(alias) ",@function\n" \
63 /* get necessary libc symbols */
64 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
65 #define LIBC_FORK __libc_fork
66 #define PTHREAD_FORK __fork
69 #define LIBC_FORK __fork
70 #define PTHREAD_FORK fork
72 extern pid_t
LIBC_FORK(void);
74 #define LIBC_SIGACTION __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) */
83 CRITICAL_SECTION
*critsect
;
86 typedef struct _wine_cleanup
{
87 void (*routine
)(void *);
91 typedef const void *key_data
;
94 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
96 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
98 void __pthread_initialize(void)
102 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
104 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
105 LONG once_now
= *(LONG
*)&the_once
;
107 if (InterlockedCompareExchange((PVOID
*)once_control
, (PVOID
)(once_now
+1), (PVOID
)once_now
) == (PVOID
)once_now
)
111 strong_alias(__pthread_once
, pthread_once
);
113 void __pthread_kill_other_threads_np(void)
115 /* FIXME: this is supposed to be preparation for exec() */
116 if (init_done
) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
118 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
122 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
124 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT
;
125 typedef void (*atfork_handler
)();
126 static atfork_handler atfork_prepare
[MAX_ATFORK
];
127 static atfork_handler atfork_parent
[MAX_ATFORK
];
128 static atfork_handler atfork_child
[MAX_ATFORK
];
129 static int atfork_count
;
131 int __pthread_atfork(void (*prepare
)(void),
132 void (*parent
)(void),
135 if (init_done
) EnterCriticalSection( &atfork_section
);
136 assert( atfork_count
< MAX_ATFORK
);
137 atfork_prepare
[atfork_count
] = prepare
;
138 atfork_parent
[atfork_count
] = parent
;
139 atfork_child
[atfork_count
] = child
;
141 if (init_done
) LeaveCriticalSection( &atfork_section
);
144 strong_alias(__pthread_atfork
, pthread_atfork
);
146 pid_t
PTHREAD_FORK(void)
151 EnterCriticalSection( &atfork_section
);
152 /* prepare handlers are called in reverse insertion order */
153 for (i
= atfork_count
- 1; i
>= 0; i
--) atfork_prepare
[i
]();
154 if (!(pid
= LIBC_FORK()))
156 InitializeCriticalSection( &atfork_section
);
157 for (i
= 0; i
< atfork_count
; i
++) atfork_child
[i
]();
161 for (i
= 0; i
< atfork_count
; i
++) atfork_parent
[i
]();
162 LeaveCriticalSection( &atfork_section
);
167 strong_alias(PTHREAD_FORK
, fork
);
170 /***** MUTEXES *****/
172 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
173 const pthread_mutexattr_t
*mutexattr
)
175 /* glibc has a tendency to initialize mutexes very often, even
176 in situations where they are not really used later on.
178 As for us, initializing a mutex is very expensive, we postpone
179 the real initialization until the time the mutex is first used. */
181 ((wine_mutex
)mutex
)->critsect
= NULL
;
184 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
186 static void mutex_real_init( pthread_mutex_t
*mutex
)
188 CRITICAL_SECTION
*critsect
= HeapAlloc(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION
));
189 InitializeCriticalSection(critsect
);
191 if (InterlockedCompareExchange((PVOID
*)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
192 /* too late, some other thread already did it */
193 DeleteCriticalSection(critsect
);
194 HeapFree(GetProcessHeap(), 0, critsect
);
198 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
200 if (!init_done
) return 0;
201 if (!((wine_mutex
)mutex
)->critsect
)
202 mutex_real_init( mutex
);
204 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
207 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
209 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
211 if (!init_done
) return 0;
212 if (!((wine_mutex
)mutex
)->critsect
)
213 mutex_real_init( mutex
);
215 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
221 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
223 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
225 if (!((wine_mutex
)mutex
)->critsect
) return 0;
226 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
229 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
231 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
233 if (!((wine_mutex
)mutex
)->critsect
) return 0;
234 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
235 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
238 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
239 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
242 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
243 HeapFree(GetProcessHeap(), 0, ((wine_mutex
)mutex
)->critsect
);
246 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
249 /***** MUTEX ATTRIBUTES *****/
250 /* just dummies, since critical sections are always recursive */
252 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
256 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
258 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
262 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
264 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
268 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
270 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
272 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
275 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
277 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
281 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
283 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
285 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
288 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
291 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
293 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
295 static LONG keycnt
= FIRST_KEY
;
296 *key
= InterlockedExchangeAdd(&keycnt
, 1);
299 strong_alias(__pthread_key_create
, pthread_key_create
);
301 int __pthread_key_delete(pthread_key_t key
)
305 strong_alias(__pthread_key_delete
, pthread_key_delete
);
307 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
309 TEB
*teb
= NtCurrentTeb();
310 if (!teb
->pthread_data
) {
311 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
313 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
316 strong_alias(__pthread_setspecific
, pthread_setspecific
);
318 void *__pthread_getspecific(pthread_key_t key
)
320 TEB
*teb
= NtCurrentTeb();
321 if (!teb
) return NULL
;
322 if (!teb
->pthread_data
) return NULL
;
323 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
325 strong_alias(__pthread_getspecific
, pthread_getspecific
);
328 /***** "EXCEPTION" FRAMES *****/
329 /* not implemented right now */
331 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
333 ((wine_cleanup
)buffer
)->routine
= routine
;
334 ((wine_cleanup
)buffer
)->arg
= arg
;
337 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
339 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
342 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
344 _pthread_cleanup_push(buffer
, routine
, arg
);
347 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
349 _pthread_cleanup_pop(buffer
, execute
);
353 /***** CONDITIONS *****/
354 /* not implemented right now */
356 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
358 P_OUTPUT("FIXME:pthread_cond_init\n");
362 int pthread_cond_destroy(pthread_cond_t
*cond
)
364 P_OUTPUT("FIXME:pthread_cond_destroy\n");
368 int pthread_cond_signal(pthread_cond_t
*cond
)
370 P_OUTPUT("FIXME:pthread_cond_signal\n");
374 int pthread_cond_broadcast(pthread_cond_t
*cond
)
376 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
380 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
382 P_OUTPUT("FIXME:pthread_cond_wait\n");
386 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
388 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
392 /**** CONDITION ATTRIBUTES *****/
393 /* not implemented right now */
395 int pthread_condattr_init(pthread_condattr_t
*attr
)
400 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
405 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 2)
406 /***** READ-WRITE LOCKS *****/
407 /* not implemented right now */
409 int __pthread_rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*rwlock_attr
)
411 P_OUTPUT("FIXME:pthread_rwlock_init\n");
414 strong_alias(__pthread_rwlock_init
, pthread_rwlock_init
);
416 int __pthread_rwlock_destroy(pthread_rwlock_t
*rwlock
)
418 P_OUTPUT("FIXME:pthread_rwlock_destroy\n");
421 strong_alias(__pthread_rwlock_destroy
, pthread_rwlock_destroy
);
423 int __pthread_rwlock_rdlock(pthread_rwlock_t
*rwlock
)
425 P_OUTPUT("FIXME:pthread_rwlock_rdlock\n");
428 strong_alias(__pthread_rwlock_rdlock
, pthread_rwlock_rdlock
);
430 int __pthread_rwlock_tryrdlock(pthread_rwlock_t
*rwlock
)
432 P_OUTPUT("FIXME:pthread_rwlock_tryrdlock\n");
435 strong_alias(__pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
);
437 int __pthread_rwlock_wrlock(pthread_rwlock_t
*rwlock
)
439 P_OUTPUT("FIXME:pthread_wrlock_rdlock\n");
442 strong_alias(__pthread_rwlock_wrlock
, pthread_rwlock_wrlock
);
444 int __pthread_rwlock_trywrlock(pthread_rwlock_t
*rwlock
)
446 P_OUTPUT("FIXME:pthread_rwlock_trywrlock\n");
449 strong_alias(__pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
);
451 int __pthread_rwlock_unlock(pthread_rwlock_t
*rwlock
)
453 P_OUTPUT("FIXME:pthread_rwlock_unlock\n");
456 strong_alias(__pthread_rwlock_unlock
, pthread_rwlock_unlock
);
458 /**** READ-WRITE LOCK ATTRIBUTES *****/
459 /* not implemented right now */
461 int pthread_rwlockattr_init(pthread_rwlockattr_t
*attr
)
466 int __pthread_rwlockattr_destroy(pthread_rwlockattr_t
*attr
)
470 strong_alias(__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
);
472 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t
*attr
, int *pref
)
478 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t
*attr
, int pref
)
482 #endif /* glibc 2.2 */
486 pthread_t
pthread_self(void)
488 return (pthread_t
)GetCurrentThreadId();
491 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
493 return (DWORD
)thread1
== (DWORD
)thread2
;
496 void pthread_exit(void *retval
)
498 /* FIXME: pthread cleanup */
499 ExitThread((DWORD
)retval
);
502 int pthread_setcanceltype(int type
, int *oldtype
)
504 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
508 /***** ANTI-OVERRIDES *****/
509 /* pthreads tries to override these, point them back to libc */
512 jump_alias(LIBC_SIGACTION
, sigaction
);
514 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
516 return LIBC_SIGACTION(signum
, act
, oldact
);
520 #endif /* __GLIBC__ */