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\t.set " PSTR(alias) "," PSTR(orig))
43 /* strong_alias does not work on external symbols (.o format limitation?),
44 * so for those, we need to use the pogo stick */
46 #define jump_alias(orig, alias) \
47 asm(".globl " PSTR(alias) "\n\t" PSTR(alias) ":\n\tjmp " PSTR(orig))
50 /* get necessary libc symbols */
51 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1) && defined(HAVE___LIBC_FORK)
52 #define LIBC_FORK __libc_fork
53 #define PTHREAD_FORK __fork
56 #define LIBC_FORK __fork
57 #define PTHREAD_FORK fork
59 extern pid_t
LIBC_FORK(void);
61 #define LIBC_SIGACTION __sigaction
63 /* NOTE: This is a truly extremely incredibly ugly hack!
64 * But it does seem to work... */
66 /* assume that pthread_mutex_t has room for at least one pointer,
67 * and hope that the users of pthread_mutex_t considers it opaque
68 * (never checks what's in it) */
70 CRITICAL_SECTION
*critsect
;
73 typedef struct _wine_cleanup
{
74 void (*routine
)(void *);
78 typedef const void *key_data
;
81 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
83 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
85 void __pthread_initialize(void)
89 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
91 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
92 LONG once_now
= *(LONG
*)&the_once
;
94 if (InterlockedCompareExchange((PVOID
*)once_control
, (PVOID
)(once_now
+1), (PVOID
)once_now
) == (PVOID
)once_now
)
98 strong_alias(__pthread_once
, pthread_once
);
100 void __pthread_kill_other_threads_np(void)
102 /* FIXME: this is supposed to be preparation for exec() */
103 if (SystemHeap
) P_OUTPUT("fixme:pthread_kill_other_threads_np\n");
105 strong_alias(__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
);
109 #define MAX_ATFORK 8 /* libc doesn't need that many anyway */
111 static CRITICAL_SECTION atfork_section
= CRITICAL_SECTION_INIT
;
112 typedef void (*atfork_handler
)();
113 static atfork_handler atfork_prepare
[MAX_ATFORK
];
114 static atfork_handler atfork_parent
[MAX_ATFORK
];
115 static atfork_handler atfork_child
[MAX_ATFORK
];
116 static int atfork_count
;
118 int __pthread_atfork(void (*prepare
)(void),
119 void (*parent
)(void),
122 EnterCriticalSection( &atfork_section
);
123 assert( atfork_count
< MAX_ATFORK
);
124 atfork_prepare
[atfork_count
] = prepare
;
125 atfork_parent
[atfork_count
] = parent
;
126 atfork_child
[atfork_count
] = child
;
128 LeaveCriticalSection( &atfork_section
);
131 strong_alias(__pthread_atfork
, pthread_atfork
);
133 pid_t
PTHREAD_FORK(void)
138 EnterCriticalSection( &atfork_section
);
139 /* prepare handlers are called in reverse insertion order */
140 for (i
= atfork_count
- 1; i
>= 0; i
--) atfork_prepare
[i
]();
141 if (!(pid
= LIBC_FORK()))
143 InitializeCriticalSection( &atfork_section
);
144 for (i
= 0; i
< atfork_count
; i
++) atfork_child
[i
]();
148 for (i
= 0; i
< atfork_count
; i
++) atfork_parent
[i
]();
149 LeaveCriticalSection( &atfork_section
);
154 strong_alias(PTHREAD_FORK
, fork
);
157 /***** MUTEXES *****/
159 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
160 const pthread_mutexattr_t
*mutexattr
)
162 /* glibc has a tendency to initialize mutexes very often, even
163 in situations where they are not really used later on.
165 As for us, initializing a mutex is very expensive, we postpone
166 the real initialization until the time the mutex is first used. */
168 ((wine_mutex
)mutex
)->critsect
= NULL
;
171 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
173 static void mutex_real_init( pthread_mutex_t
*mutex
)
175 CRITICAL_SECTION
*critsect
= HeapAlloc(SystemHeap
, 0, sizeof(CRITICAL_SECTION
));
176 InitializeCriticalSection(critsect
);
178 if (InterlockedCompareExchange((PVOID
*)&(((wine_mutex
)mutex
)->critsect
),critsect
,NULL
) != NULL
) {
179 /* too late, some other thread already did it */
180 DeleteCriticalSection(critsect
);
181 HeapFree(SystemHeap
, 0, critsect
);
185 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
187 if (!SystemHeap
) return 0;
188 if (!((wine_mutex
)mutex
)->critsect
)
189 mutex_real_init( mutex
);
191 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
194 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
196 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
198 if (!SystemHeap
) return 0;
199 if (!((wine_mutex
)mutex
)->critsect
)
200 mutex_real_init( mutex
);
202 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
208 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
210 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
212 if (!((wine_mutex
)mutex
)->critsect
) return 0;
213 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
216 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
218 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
220 if (!((wine_mutex
)mutex
)->critsect
) return 0;
221 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
222 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
225 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
226 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
229 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
230 HeapFree(SystemHeap
, 0, ((wine_mutex
)mutex
)->critsect
);
233 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
236 /***** MUTEX ATTRIBUTES *****/
237 /* just dummies, since critical sections are always recursive */
239 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
243 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
245 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
249 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
251 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
255 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
257 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
259 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
262 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
264 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
268 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
270 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
272 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
275 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
278 /***** THREAD-SPECIFIC VARIABLES (KEYS) *****/
280 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
282 static LONG keycnt
= FIRST_KEY
;
283 *key
= InterlockedExchangeAdd(&keycnt
, 1);
286 strong_alias(__pthread_key_create
, pthread_key_create
);
288 int __pthread_key_delete(pthread_key_t key
)
292 strong_alias(__pthread_key_delete
, pthread_key_delete
);
294 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
296 TEB
*teb
= NtCurrentTeb();
297 if (!teb
->pthread_data
) {
298 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
300 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
303 strong_alias(__pthread_setspecific
, pthread_setspecific
);
305 void *__pthread_getspecific(pthread_key_t key
)
307 TEB
*teb
= NtCurrentTeb();
308 if (!teb
) return NULL
;
309 if (!teb
->pthread_data
) return NULL
;
310 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
312 strong_alias(__pthread_getspecific
, pthread_getspecific
);
315 /***** "EXCEPTION" FRAMES *****/
316 /* not implemented right now */
318 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
320 ((wine_cleanup
)buffer
)->routine
= routine
;
321 ((wine_cleanup
)buffer
)->arg
= arg
;
324 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
326 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
329 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
331 _pthread_cleanup_push(buffer
, routine
, arg
);
334 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
336 _pthread_cleanup_pop(buffer
, execute
);
340 /***** CONDITIONS *****/
341 /* not implemented right now */
343 int pthread_cond_init(pthread_cond_t
*cond
, const pthread_condattr_t
*cond_attr
)
345 P_OUTPUT("FIXME:pthread_cond_init\n");
349 int pthread_cond_destroy(pthread_cond_t
*cond
)
351 P_OUTPUT("FIXME:pthread_cond_destroy\n");
355 int pthread_cond_signal(pthread_cond_t
*cond
)
357 P_OUTPUT("FIXME:pthread_cond_signal\n");
361 int pthread_cond_broadcast(pthread_cond_t
*cond
)
363 P_OUTPUT("FIXME:pthread_cond_broadcast\n");
367 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
369 P_OUTPUT("FIXME:pthread_cond_wait\n");
373 int pthread_cond_timedwait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
, const struct timespec
*abstime
)
375 P_OUTPUT("FIXME:pthread_cond_timedwait\n");
379 /**** CONDITION ATTRIBUTES *****/
380 /* not implemented right now */
382 int pthread_condattr_init(pthread_condattr_t
*attr
)
387 int pthread_condattr_destroy(pthread_condattr_t
*attr
)
394 pthread_t
pthread_self(void)
396 return (pthread_t
)GetCurrentThreadId();
399 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
401 return (DWORD
)thread1
== (DWORD
)thread2
;
404 void pthread_exit(void *retval
)
406 /* FIXME: pthread cleanup */
407 ExitThread((DWORD
)retval
);
410 int pthread_setcanceltype(int type
, int *oldtype
)
412 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
416 /***** ANTI-OVERRIDES *****/
417 /* pthreads tries to override these, point them back to libc */
420 jump_alias(LIBC_SIGACTION
, sigaction
);
422 int sigaction(int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
424 return LIBC_SIGACTION(signum
, act
, oldact
);
428 #endif /* __GLIBC__ */