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__)
29 #ifdef NEED_UNDERSCORE_PREFIX
35 /* adapt as necessary (a construct like this is used in glibc sources) */
36 #define strong_alias(orig, alias) \
37 asm(".globl " PREFIX #alias "\n\t.set " PREFIX #alias "," PREFIX #orig)
39 /* NOTE: This is a truly extremely incredibly ugly hack!
40 * But it does seem to work... */
42 /* assume that pthread_mutex_t has room for at least one pointer,
43 * and hope that the users of pthread_mutex_t considers it opaque
44 * (never checks what's in it) */
46 CRITICAL_SECTION
*critsect
;
49 typedef struct _wine_cleanup
{
50 void (*routine
)(void *);
54 typedef const void *key_data
;
57 #define MAX_KEYS 16 /* libc6 doesn't use that many, but... */
59 static CRITICAL_SECTION init_sect
= CRITICAL_SECTION_INIT
;
61 #define P_OUTPUT(stuff) write(2,stuff,strlen(stuff))
63 void __pthread_initialize(void)
67 int __pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
69 static pthread_once_t the_once
= PTHREAD_ONCE_INIT
;
71 EnterCriticalSection(&init_sect
);
72 if (!memcmp(once_control
,&the_once
,sizeof(the_once
))) {
74 (*(int *)once_control
)++;
76 LeaveCriticalSection(&init_sect
);
79 strong_alias(__pthread_once
, pthread_once
);
81 int __pthread_mutex_init(pthread_mutex_t
*mutex
,
82 const pthread_mutexattr_t
*mutexattr
)
84 /* glibc has a tendency to initialize mutexes very often, even
85 in situations where they are not really used later on.
87 As for us, initializing a mutex is very expensive, we postpone
88 the real initialization until the time the mutex is first used. */
90 ((wine_mutex
)mutex
)->critsect
= NULL
;
93 strong_alias(__pthread_mutex_init
, pthread_mutex_init
);
95 static void mutex_real_init( pthread_mutex_t
*mutex
)
97 EnterCriticalSection(&init_sect
);
99 if (!((wine_mutex
)mutex
)->critsect
) {
100 ((wine_mutex
)mutex
)->critsect
= HeapAlloc(SystemHeap
, 0, sizeof(CRITICAL_SECTION
));
101 InitializeCriticalSection(((wine_mutex
)mutex
)->critsect
);
104 LeaveCriticalSection(&init_sect
);
107 int __pthread_mutex_lock(pthread_mutex_t
*mutex
)
109 if (!SystemHeap
) return 0;
110 if (!((wine_mutex
)mutex
)->critsect
)
111 mutex_real_init( mutex
);
113 EnterCriticalSection(((wine_mutex
)mutex
)->critsect
);
116 strong_alias(__pthread_mutex_lock
, pthread_mutex_lock
);
118 int __pthread_mutex_trylock(pthread_mutex_t
*mutex
)
120 if (!SystemHeap
) return 0;
121 if (!((wine_mutex
)mutex
)->critsect
)
122 mutex_real_init( mutex
);
124 if (!TryEnterCriticalSection(((wine_mutex
)mutex
)->critsect
)) {
130 strong_alias(__pthread_mutex_trylock
, pthread_mutex_trylock
);
132 int __pthread_mutex_unlock(pthread_mutex_t
*mutex
)
134 if (!((wine_mutex
)mutex
)->critsect
) return 0;
135 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
138 strong_alias(__pthread_mutex_unlock
, pthread_mutex_unlock
);
140 int __pthread_mutex_destroy(pthread_mutex_t
*mutex
)
142 if (!((wine_mutex
)mutex
)->critsect
) return 0;
143 if (((wine_mutex
)mutex
)->critsect
->RecursionCount
) {
144 #if 0 /* there seems to be a bug in libc6 that makes this a bad idea */
147 while (((wine_mutex
)mutex
)->critsect
->RecursionCount
)
148 LeaveCriticalSection(((wine_mutex
)mutex
)->critsect
);
151 DeleteCriticalSection(((wine_mutex
)mutex
)->critsect
);
152 HeapFree(SystemHeap
, 0, ((wine_mutex
)mutex
)->critsect
);
155 strong_alias(__pthread_mutex_destroy
, pthread_mutex_destroy
);
157 int __pthread_atfork(void (*prepare
)(void),
158 void (*parent
)(void),
161 /* are we going to fork? nah */
164 strong_alias(__pthread_atfork
, pthread_atfork
);
166 int __pthread_mutexattr_init(pthread_mutexattr_t
*attr
)
170 strong_alias(__pthread_mutexattr_init
, pthread_mutexattr_init
);
172 int __pthread_mutexattr_destroy(pthread_mutexattr_t
*attr
)
176 strong_alias(__pthread_mutexattr_destroy
, pthread_mutexattr_destroy
);
178 int __pthread_mutexattr_setkind_np(pthread_mutexattr_t
*attr
, int kind
)
182 strong_alias(__pthread_mutexattr_setkind_np
, pthread_mutexattr_setkind_np
);
184 int __pthread_mutexattr_getkind_np(pthread_mutexattr_t
*attr
, int *kind
)
186 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
189 strong_alias(__pthread_mutexattr_getkind_np
, pthread_mutexattr_getkind_np
);
191 int __pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int kind
)
195 strong_alias(__pthread_mutexattr_settype
, pthread_mutexattr_settype
);
197 int __pthread_mutexattr_gettype(pthread_mutexattr_t
*attr
, int *kind
)
199 *kind
= PTHREAD_MUTEX_RECURSIVE_NP
;
202 strong_alias(__pthread_mutexattr_gettype
, pthread_mutexattr_gettype
);
204 int __pthread_key_create(pthread_key_t
*key
, void (*destr_function
)(void *))
206 static pthread_key_t keycnt
= FIRST_KEY
;
207 EnterCriticalSection(&init_sect
);
209 LeaveCriticalSection(&init_sect
);
212 strong_alias(__pthread_key_create
, pthread_key_create
);
214 int __pthread_key_delete(pthread_key_t key
)
218 strong_alias(__pthread_key_delete
, pthread_key_delete
);
220 int __pthread_setspecific(pthread_key_t key
, const void *pointer
)
222 TEB
*teb
= NtCurrentTeb();
223 if (!teb
->pthread_data
) {
224 teb
->pthread_data
= calloc(MAX_KEYS
,sizeof(key_data
));
226 ((key_data
*)(teb
->pthread_data
))[key
] = pointer
;
229 strong_alias(__pthread_setspecific
, pthread_setspecific
);
231 void *__pthread_getspecific(pthread_key_t key
)
233 TEB
*teb
= NtCurrentTeb();
234 if (!teb
) return NULL
;
235 if (!teb
->pthread_data
) return NULL
;
236 return (void *)(((key_data
*)(teb
->pthread_data
))[key
]);
238 strong_alias(__pthread_getspecific
, pthread_getspecific
);
240 void _pthread_cleanup_push(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
242 ((wine_cleanup
)buffer
)->routine
= routine
;
243 ((wine_cleanup
)buffer
)->arg
= arg
;
246 void _pthread_cleanup_pop(struct _pthread_cleanup_buffer
*buffer
, int execute
)
248 if (execute
) (*(((wine_cleanup
)buffer
)->routine
))(((wine_cleanup
)buffer
)->arg
);
251 void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*buffer
, void (*routine
)(void *), void *arg
)
253 _pthread_cleanup_push(buffer
, routine
, arg
);
256 void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*buffer
, int execute
)
258 _pthread_cleanup_pop(buffer
, execute
);
261 pthread_t
pthread_self(void)
263 return (pthread_t
)GetCurrentThreadId();
266 int pthread_equal(pthread_t thread1
, pthread_t thread2
)
268 return (DWORD
)thread1
== (DWORD
)thread2
;
271 int pthread_setcanceltype(int type
, int *oldtype
)
273 if (oldtype
) *oldtype
= PTHREAD_CANCEL_ASYNCHRONOUS
;
277 #endif /* __GLIBC__ */