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
21 /* Currently this probably works only for glibc2,
22 * which checks for the presence of double-underscore-prepended
23 * pthread primitives, and use them if available.
24 * If they are not available, the libc defaults to
25 * non-threadsafe operation (not good). */
27 #if defined(__GLIBC__)
31 #ifdef NEED_UNDERSCORE_PREFIX
37 #define PSTR(str) PREFIX #str
39 /* adapt as necessary (a construct like this is used in glibc sources) */
40 #define strong_alias(orig, alias) \
41 asm(".globl " PSTR(alias) "\n" \
42 "\t.set " PSTR(alias) "," PSTR(orig))
44 /* strong_alias does not work on external symbols (.o format limitation?),
45 * so for those, we need to use the pogo stick */
46 #if defined(__i386__) && !defined(__PIC__)
48 #define jump_alias(orig, alias) \
49 asm(".globl " PSTR(alias) "\n" \
50 "\t.type " PSTR(alias) ",@function\n" \
55 /* get necessary libc symbols */
56 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
57 #define LIBC_FORK __libc_fork
58 #define PTHREAD_FORK __fork
61 #define LIBC_FORK __fork
62 #define PTHREAD_FORK fork
64 extern pid_t
LIBC_FORK(void);
66 #define LIBC_SIGACTION __sigaction
68 /* NOTE: This is a truly extremely incredibly ugly hack!
69 * But it does seem to work... */
71 /* assume that pthread_mutex_t has room for at least one pointer,
72 * and hope that the users of pthread_mutex_t considers it opaque
73 * (never checks what's in it) */
75 CRITICAL_SECTION
*critsect
;
78 typedef struct _wine_cleanup
{
79 void (*routine
)(void *);
83 typedef const void *key_data
;
86 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
88 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
90 void __pthread_initialize(void)
94 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
96 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
97 LONG once_now
= *(LONG
*)&the_once
;
99 if (InterlockedCompareExchange((PVOID
*)once_control
, (PVOID
)(once_now
+1), (PVOID
)once_now
) == (PVOID
)once_now
)
103 strong_alias(__pthread_once
, pthread_once
);
105 void __pthread_kill_other_threads_np(void)
107 /* FIXME: this is supposed to be preparation for exec() */
108 if (SystemHeap
) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
110 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
114 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
116 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT
;
117 typedef void (*atfork_handler
)();
118 static atfork_handler atfork_prepare
[MAX_ATFORK
];
119 static atfork_handler atfork_parent
[MAX_ATFORK
];
120 static atfork_handler atfork_child
[MAX_ATFORK
];
121 static int atfork_count
;
123 int __pthread_atfork(void (*prepare
)(void),
124 void (*parent
)(void),
127 EnterCriticalSection( &atfork_section
);
128 assert( atfork_count
< MAX_ATFORK
);
129 atfork_prepare
[atfork_count
] = prepare
;
130 atfork_parent
[atfork_count
] = parent
;
131 atfork_child
[atfork_count
] = child
;
133 LeaveCriticalSection( &atfork_section
);
136 strong_alias(__pthread_atfork
, pthread_atfork
);
138 pid_t
PTHREAD_FORK(void)
143 EnterCriticalSection( &atfork_section
);
144 /* prepare handlers are called in reverse insertion order */
145 for (i
= atfork_count
- 1; i
>= 0; i
--) atfork_prepare
[i
]();
146 if (!(pid
= LIBC_FORK()))
148 InitializeCriticalSection( &atfork_section
);
149 for (i
= 0; i
< atfork_count
; i
++) atfork_child
[i
]();
153 for (i
= 0; i
< atfork_count
; i
++) atfork_parent
[i
]();
154 LeaveCriticalSection( &atfork_section
);
159 strong_alias(PTHREAD_FORK
, fork
);
162 /***** MUTEXES *****/
164 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
165 const pthread_mutexattr_t
*mutexattr
)
167 /* glibc has a tendency to initialize mutexes very often, even
168 in situations where they are not really used later on.
170 As for us, initializing a mutex is very expensive, we postpone
171 the real initialization until the time the mutex is first used. */
173 ((wine_mutex
)mutex
)->critsect
= NULL
;
176 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
178 static void mutex_real_init( pthread_mutex_t
*mutex
)
180 CRITICAL_SECTION
*critsect
= HeapAlloc(SystemHeap
, 0, sizeof(CRITICAL_SECTION
));
181 InitializeCriticalSection(critsect
);
183 if (InterlockedCompareExchange((PVOID
*)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
184 /* too late, some other thread already did it */
185 DeleteCriticalSection(critsect
);
186 HeapFree(SystemHeap
, 0, critsect
);
190 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
192 if (!SystemHeap
) return 0;
193 if (!((wine_mutex
)mutex
)->critsect
)
194 mutex_real_init( mutex
);
196 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
199 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
201 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
203 if (!SystemHeap
) return 0;
204 if (!((wine_mutex
)mutex
)->critsect
)
205 mutex_real_init( mutex
);
207 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
213 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
215 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
217 if (!((wine_mutex
)mutex
)->critsect
) return 0;
218 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
221 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
223 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
225 if (!((wine_mutex
)mutex
)->critsect
) return 0;
226 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
227 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
230 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
231 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
234 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
235 HeapFree(SystemHeap
, 0, ((wine_mutex
)mutex
)->critsect
);
238 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
241 /***** MUTEX ATTRIBUTES *****/
242 /* just dummies, since critical sections are always recursive */
244 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
248 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
250 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
254 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
256 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
260 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
262 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
264 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
267 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
269 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
273 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
275 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
277 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
280 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
283 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
285 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
287 static LONG keycnt
= FIRST_KEY
;
288 *key
= InterlockedExchangeAdd(&keycnt
, 1);
291 strong_alias(__pthread_key_create
, pthread_key_create
);
293 int __pthread_key_delete(pthread_key_t key
)
297 strong_alias(__pthread_key_delete
, pthread_key_delete
);
299 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
301 TEB
*teb
= NtCurrentTeb();
302 if (!teb
->pthread_data
) {
303 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
305 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
308 strong_alias(__pthread_setspecific
, pthread_setspecific
);
310 void *__pthread_getspecific(pthread_key_t key
)
312 TEB
*teb
= NtCurrentTeb();
313 if (!teb
) return NULL
;
314 if (!teb
->pthread_data
) return NULL
;
315 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
317 strong_alias(__pthread_getspecific
, pthread_getspecific
);
320 /***** "EXCEPTION" FRAMES *****/
321 /* not implemented right now */
323 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
325 ((wine_cleanup
)buffer
)->routine
= routine
;
326 ((wine_cleanup
)buffer
)->arg
= arg
;
329 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
331 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
334 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
336 _pthread_cleanup_push(buffer
, routine
, arg
);
339 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
341 _pthread_cleanup_pop(buffer
, execute
);
345 /***** CONDITIONS *****/
346 /* not implemented right now */
348 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
350 P_OUTPUT("FIXME:pthread_cond_init\n");
354 int pthread_cond_destroy(pthread_cond_t
*cond
)
356 P_OUTPUT("FIXME:pthread_cond_destroy\n");
360 int pthread_cond_signal(pthread_cond_t
*cond
)
362 P_OUTPUT("FIXME:pthread_cond_signal\n");
366 int pthread_cond_broadcast(pthread_cond_t
*cond
)
368 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
372 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
374 P_OUTPUT("FIXME:pthread_cond_wait\n");
378 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
380 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
384 /**** CONDITION ATTRIBUTES *****/
385 /* not implemented right now */
387 int pthread_condattr_init(pthread_condattr_t
*attr
)
392 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
399 pthread_t
pthread_self(void)
401 return (pthread_t
)GetCurrentThreadId();
404 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
406 return (DWORD
)thread1
== (DWORD
)thread2
;
409 void pthread_exit(void *retval
)
411 /* FIXME: pthread cleanup */
412 ExitThread((DWORD
)retval
);
415 int pthread_setcanceltype(int type
, int *oldtype
)
417 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
421 /***** ANTI-OVERRIDES *****/
422 /* pthreads tries to override these, point them back to libc */
425 jump_alias(LIBC_SIGACTION
, sigaction
);
427 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
429 return LIBC_SIGACTION(signum
, act
, oldact
);
433 #endif /* __GLIBC__ */