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
20 /* Currently this probably works only for glibc2,
21 * which checks for the presence of double-underscore-prepended
22 * pthread primitives, and use them if available.
23 * If they are not available, the libc defaults to
24 * non-threadsafe operation (not good). */
26 #if defined(__GLIBC__)
30 #ifdef NEED_UNDERSCORE_PREFIX
36 #define PSTR(str) PREFIX #str
38 /* adapt as necessary (a construct like this is used in glibc sources) */
39 #define strong_alias(orig, alias) \
40 asm(".globl " PSTR(alias) "\n\t.set " PSTR(alias) "," PSTR(orig))
42 /* strong_alias does not work on external symbols (.o format limitation?),
43 * so for those, we need to use the pogo stick */
45 #define jump_alias(orig, alias) \
46 asm(".globl " PSTR(alias) "\n\t" PSTR(alias) ":\n\tjmp " PSTR(orig))
49 /* get necessary libc symbols */
50 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
51 #define LIBC_FORK __libc_fork
52 #define PTHREAD_FORK __fork
55 #define LIBC_FORK __fork
56 #define PTHREAD_FORK fork
58 extern pid_t
LIBC_FORK(void);
60 #define LIBC_SIGACTION __sigaction
62 /* NOTE: This is a truly extremely incredibly ugly hack!
63 * But it does seem to work... */
65 /* assume that pthread_mutex_t has room for at least one pointer,
66 * and hope that the users of pthread_mutex_t considers it opaque
67 * (never checks what's in it) */
69 CRITICAL_SECTION
*critsect
;
72 typedef struct _wine_cleanup
{
73 void (*routine
)(void *);
77 typedef const void *key_data
;
80 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
82 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
84 void __pthread_initialize(void)
88 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
90 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
91 LONG once_now
= *(LONG
*)&the_once
;
93 if (InterlockedCompareExchange((PVOID
*)once_control
, (PVOID
)(once_now
+1), (PVOID
)once_now
) == (PVOID
)once_now
)
97 strong_alias(__pthread_once
, pthread_once
);
99 void __pthread_kill_other_threads_np(void)
101 /* FIXME: this is supposed to be preparation for exec() */
102 if (SystemHeap
) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
104 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
106 int __pthread_atfork(void (*prepare
)(void),
107 void (*parent
)(void),
110 P_OUTPUT("fixme:pthread_atfork\n");
113 strong_alias(__pthread_atfork
, pthread_atfork
);
115 pid_t
PTHREAD_FORK(void)
118 /* call prepare handlers */
121 /* call child handlers */
123 /* call parent handlers */
128 strong_alias(PTHREAD_FORK
, fork
);
131 /***** MUTEXES *****/
133 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
134 const pthread_mutexattr_t
*mutexattr
)
136 /* glibc has a tendency to initialize mutexes very often, even
137 in situations where they are not really used later on.
139 As for us, initializing a mutex is very expensive, we postpone
140 the real initialization until the time the mutex is first used. */
142 ((wine_mutex
)mutex
)->critsect
= NULL
;
145 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
147 static void mutex_real_init( pthread_mutex_t
*mutex
)
149 CRITICAL_SECTION
*critsect
= HeapAlloc(SystemHeap
, 0, sizeof(CRITICAL_SECTION
));
150 InitializeCriticalSection(critsect
);
152 if (InterlockedCompareExchange((PVOID
*)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
153 /* too late, some other thread already did it */
154 DeleteCriticalSection(critsect
);
155 HeapFree(SystemHeap
, 0, critsect
);
159 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
161 if (!SystemHeap
) return 0;
162 if (!((wine_mutex
)mutex
)->critsect
)
163 mutex_real_init( mutex
);
165 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
168 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
170 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
172 if (!SystemHeap
) return 0;
173 if (!((wine_mutex
)mutex
)->critsect
)
174 mutex_real_init( mutex
);
176 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
182 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
184 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
186 if (!((wine_mutex
)mutex
)->critsect
) return 0;
187 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
190 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
192 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
194 if (!((wine_mutex
)mutex
)->critsect
) return 0;
195 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
196 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
199 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
200 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
203 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
204 HeapFree(SystemHeap
, 0, ((wine_mutex
)mutex
)->critsect
);
207 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
210 /***** MUTEX ATTRIBUTES *****/
211 /* just dummies, since critical sections are always recursive */
213 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
217 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
219 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
223 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
225 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
229 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
231 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
233 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
236 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
238 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
242 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
244 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
246 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
249 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
252 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
254 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
256 static LONG keycnt
= FIRST_KEY
;
257 *key
= InterlockedExchangeAdd(&keycnt
, 1);
260 strong_alias(__pthread_key_create
, pthread_key_create
);
262 int __pthread_key_delete(pthread_key_t key
)
266 strong_alias(__pthread_key_delete
, pthread_key_delete
);
268 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
270 TEB
*teb
= NtCurrentTeb();
271 if (!teb
->pthread_data
) {
272 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
274 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
277 strong_alias(__pthread_setspecific
, pthread_setspecific
);
279 void *__pthread_getspecific(pthread_key_t key
)
281 TEB
*teb
= NtCurrentTeb();
282 if (!teb
) return NULL
;
283 if (!teb
->pthread_data
) return NULL
;
284 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
286 strong_alias(__pthread_getspecific
, pthread_getspecific
);
289 /***** "EXCEPTION" FRAMES *****/
290 /* not implemented right now */
292 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
294 ((wine_cleanup
)buffer
)->routine
= routine
;
295 ((wine_cleanup
)buffer
)->arg
= arg
;
298 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
300 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
303 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
305 _pthread_cleanup_push(buffer
, routine
, arg
);
308 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
310 _pthread_cleanup_pop(buffer
, execute
);
314 /***** CONDITIONS *****/
315 /* not implemented right now */
317 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
319 P_OUTPUT("FIXME:pthread_cond_init\n");
323 int pthread_cond_destroy(pthread_cond_t
*cond
)
325 P_OUTPUT("FIXME:pthread_cond_destroy\n");
329 int pthread_cond_signal(pthread_cond_t
*cond
)
331 P_OUTPUT("FIXME:pthread_cond_signal\n");
335 int pthread_cond_broadcast(pthread_cond_t
*cond
)
337 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
341 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
343 P_OUTPUT("FIXME:pthread_cond_wait\n");
347 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
349 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
353 /**** CONDITION ATTRIBUTES *****/
354 /* not implemented right now */
356 int pthread_condattr_init(pthread_condattr_t
*attr
)
361 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
368 pthread_t
pthread_self(void)
370 return (pthread_t
)GetCurrentThreadId();
373 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
375 return (DWORD
)thread1
== (DWORD
)thread2
;
378 void pthread_exit(void *retval
)
380 /* FIXME: pthread cleanup */
381 ExitThread((DWORD
)retval
);
384 int pthread_setcanceltype(int type
, int *oldtype
)
386 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
390 /***** ANTI-OVERRIDES *****/
391 /* pthreads tries to override these, point them back to libc */
394 jump_alias(LIBC_SIGACTION
, sigaction
);
396 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
398 return LIBC_SIGACTION(signum
, act
, oldact
);
402 #endif /* __GLIBC__ */