blob: 87d0d365095c24ad52b2b78ac2b8dd315aabf5b9 [file] [log] [blame]
Daniel Veillardb8478642001-10-12 17:29:10 +00001/**
2 * threads.c: set of generic threading related routines
3 *
4 * See Copyright for the status of this software.
5 *
6 * Gary Pennington <Gary.Pennington@uk.sun.com>
7 * daniel@veillard.com
8 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Daniel Veillardb8478642001-10-12 17:29:10 +000011#include "libxml.h"
12
13#include <string.h>
14
15#include <libxml/threads.h>
16#include <libxml/globals.h>
17
18#ifdef HAVE_SYS_TYPES_H
19#include <sys/types.h>
20#endif
21#ifdef HAVE_UNISTD_H
22#include <unistd.h>
23#endif
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_PTHREAD_H
28#include <pthread.h>
29#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000030
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000031#ifdef HAVE_WIN32_THREADS
32#include <windows.h>
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000033#ifndef HAVE_COMPILER_TLS
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000034#include <process.h>
35#endif
36#endif
Daniel Veillardb8478642001-10-12 17:29:10 +000037
Daniel Veillard82cb3192003-10-29 13:39:15 +000038#ifdef HAVE_BEOS_THREADS
39#include <OS.h>
40#include <TLS.h>
41#endif
42
Daniel Veillardb8478642001-10-12 17:29:10 +000043#if defined(SOLARIS)
44#include <note.h>
45#endif
46
Daniel Veillard6f350292001-10-14 09:56:15 +000047/* #define DEBUG_THREADS */
Daniel Veillardb8478642001-10-12 17:29:10 +000048
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000049#ifdef HAVE_PTHREAD_H
50
51static int libxml_is_threaded = -1;
52#ifdef __GNUC__
53#ifdef linux
54#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
55extern int pthread_once (pthread_once_t *__once_control,
56 void (*__init_routine) (void))
57 __attribute((weak));
58extern void *pthread_getspecific (pthread_key_t __key)
59 __attribute((weak));
60extern int pthread_setspecific (pthread_key_t __key,
61 __const void *__pointer)
62 __attribute((weak));
63extern int pthread_key_create (pthread_key_t *__key,
64 void (*__destr_function) (void *))
65 __attribute((weak));
66extern int pthread_mutex_init ()
67 __attribute((weak));
68extern int pthread_mutex_destroy ()
69 __attribute((weak));
70extern int pthread_mutex_lock ()
71 __attribute((weak));
72extern int pthread_mutex_unlock ()
73 __attribute((weak));
74extern int pthread_cond_init ()
75 __attribute((weak));
76extern int pthread_equal ()
77 __attribute((weak));
78extern pthread_t pthread_self ()
79 __attribute((weak));
80extern int pthread_key_create ()
81 __attribute((weak));
82extern int pthread_cond_signal ()
83 __attribute((weak));
84#endif
85#endif /* linux */
86#endif /* __GNUC__ */
87#endif /* HAVE_PTHREAD_H */
88
Daniel Veillardb8478642001-10-12 17:29:10 +000089/*
90 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
91 * to avoid some crazyness since xmlMalloc/xmlFree may actually
92 * be hosted on allocated blocks needing them for the allocation ...
93 */
94
95/*
96 * xmlMutex are a simple mutual exception locks
97 */
98struct _xmlMutex {
99#ifdef HAVE_PTHREAD_H
100 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000101#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000102 HANDLE mutex;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000103#elif defined HAVE_BEOS_THREADS
104 sem_id sem;
Daniel Veillard254b1262003-11-01 17:04:58 +0000105 thread_id tid;
Daniel Veillardb8478642001-10-12 17:29:10 +0000106#else
107 int empty;
108#endif
109};
110
111/*
112 * xmlRMutex are reentrant mutual exception locks
113 */
114struct _xmlRMutex {
115#ifdef HAVE_PTHREAD_H
116 pthread_mutex_t lock;
117 unsigned int held;
118 unsigned int waiters;
119 pthread_t tid;
120 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000121#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000122 CRITICAL_SECTION cs;
123 unsigned int count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000124#elif defined HAVE_BEOS_THREADS
125 xmlMutexPtr lock;
126 thread_id tid;
127 int32 count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000128#else
129 int empty;
130#endif
131};
132/*
133 * This module still has some internal static data.
134 * - xmlLibraryLock a global lock
135 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +0000136 */
Daniel Veillard6f350292001-10-14 09:56:15 +0000137
Daniel Veillardb8478642001-10-12 17:29:10 +0000138#ifdef HAVE_PTHREAD_H
Daniel Veillardb8478642001-10-12 17:29:10 +0000139static pthread_key_t globalkey;
Daniel Veillard6f350292001-10-14 09:56:15 +0000140static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +0000141static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000142static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000143#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000144#if defined(HAVE_COMPILER_TLS)
145static __declspec(thread) xmlGlobalState tlstate;
146static __declspec(thread) int tlstate_inited = 0;
147#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000148static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000149#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000150static DWORD mainthread;
Daniel Veillard62121e22005-02-24 15:38:52 +0000151static struct
152{
Daniel Veillard36616dd2005-02-25 07:31:49 +0000153 DWORD done;
154 DWORD control;
Daniel Veillard62121e22005-02-24 15:38:52 +0000155} run_once = { 0, 0 };
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000156static volatile LPCRITICAL_SECTION global_init_lock = NULL;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000157/* endif HAVE_WIN32_THREADS */
158#elif defined HAVE_BEOS_THREADS
159int32 globalkey = 0;
160thread_id mainthread = 0;
161int32 run_once_init = 0;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000162static int32 global_init_lock = -1;
163static vint32 global_init_count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000164#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000165
Daniel Veillardb8478642001-10-12 17:29:10 +0000166static xmlRMutexPtr xmlLibraryLock = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000167#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000168static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000169#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000170
171/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000172 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000173 *
174 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
175 * synchronizing access to data.
176 *
177 * Returns a new simple mutex pointer or NULL in case of error
178 */
179xmlMutexPtr
180xmlNewMutex(void)
181{
182 xmlMutexPtr tok;
183
184 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
185 return (NULL);
186#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000187 if (libxml_is_threaded != 0)
188 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000189#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000190 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000191#elif defined HAVE_BEOS_THREADS
192 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
193 free(tok);
194 return NULL;
195 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000196 tok->tid = -1;
Daniel Veillardb8478642001-10-12 17:29:10 +0000197#endif
198 return (tok);
199}
200
201/**
202 * xmlFreeMutex:
203 * @tok: the simple mutex
204 *
205 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
206 * struct.
207 */
208void
209xmlFreeMutex(xmlMutexPtr tok)
210{
Daniel Veillarddf101d82003-07-08 14:03:36 +0000211 if (tok == NULL) return;
212
Daniel Veillardb8478642001-10-12 17:29:10 +0000213#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000214 if (libxml_is_threaded != 0)
215 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000216#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000217 CloseHandle(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000218#elif defined HAVE_BEOS_THREADS
219 delete_sem(tok->sem);
Daniel Veillardb8478642001-10-12 17:29:10 +0000220#endif
221 free(tok);
222}
223
224/**
225 * xmlMutexLock:
226 * @tok: the simple mutex
227 *
228 * xmlMutexLock() is used to lock a libxml2 token.
229 */
230void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000231xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000232{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000233 if (tok == NULL)
234 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000235#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000236 if (libxml_is_threaded != 0)
237 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000238#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000239 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000240#elif defined HAVE_BEOS_THREADS
241 if (acquire_sem(tok->sem) != B_NO_ERROR) {
242#ifdef DEBUG_THREADS
243 xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
244 exit();
245#endif
246 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000247 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000248#endif
249
250}
251
252/**
253 * xmlMutexUnlock:
254 * @tok: the simple mutex
255 *
256 * xmlMutexUnlock() is used to unlock a libxml2 token.
257 */
258void
Daniel Veillard5805be22003-08-28 08:03:23 +0000259xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000260{
Daniel Veillard5805be22003-08-28 08:03:23 +0000261 if (tok == NULL)
262 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000263#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000264 if (libxml_is_threaded != 0)
265 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000266#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000267 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000268#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000269 if (tok->tid == find_thread(NULL)) {
270 tok->tid = -1;
271 release_sem(tok->sem);
272 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000273#endif
274}
275
276/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000277 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000278 *
279 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
280 * synchronizing access to data. token_r is a re-entrant lock and thus useful
281 * for synchronizing access to data structures that may be manipulated in a
282 * recursive fashion.
283 *
284 * Returns the new reentrant mutex pointer or NULL in case of error
285 */
286xmlRMutexPtr
287xmlNewRMutex(void)
288{
289 xmlRMutexPtr tok;
290
291 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
292 return (NULL);
293#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000294 if (libxml_is_threaded != 0) {
295 pthread_mutex_init(&tok->lock, NULL);
296 tok->held = 0;
297 tok->waiters = 0;
298 pthread_cond_init(&tok->cv, NULL);
299 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000300#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000301 InitializeCriticalSection(&tok->cs);
302 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000303#elif defined HAVE_BEOS_THREADS
304 if ((tok->lock = xmlNewMutex()) == NULL) {
305 free(tok);
306 return NULL;
307 }
308 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000309#endif
310 return (tok);
311}
312
313/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000314 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000315 * @tok: the reentrant mutex
316 *
317 * xmlRFreeMutex() is used to reclaim resources associated with a
318 * reentrant mutex.
319 */
320void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000321xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000322{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000323 if (tok == NULL)
324 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000325#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000326 if (libxml_is_threaded != 0) {
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000327 pthread_mutex_destroy(&tok->lock);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000328 pthread_cond_destroy(&tok->cv);
329 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000330#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000331 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000332#elif defined HAVE_BEOS_THREADS
333 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000334#endif
335 free(tok);
336}
337
338/**
339 * xmlRMutexLock:
340 * @tok: the reentrant mutex
341 *
342 * xmlRMutexLock() is used to lock a libxml2 token_r.
343 */
344void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000345xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000346{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000347 if (tok == NULL)
348 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000349#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000350 if (libxml_is_threaded == 0)
351 return;
352
Daniel Veillardb8478642001-10-12 17:29:10 +0000353 pthread_mutex_lock(&tok->lock);
354 if (tok->held) {
355 if (pthread_equal(tok->tid, pthread_self())) {
356 tok->held++;
357 pthread_mutex_unlock(&tok->lock);
358 return;
359 } else {
360 tok->waiters++;
361 while (tok->held)
362 pthread_cond_wait(&tok->cv, &tok->lock);
363 tok->waiters--;
364 }
365 }
366 tok->tid = pthread_self();
367 tok->held = 1;
368 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000369#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000370 EnterCriticalSection(&tok->cs);
371 ++tok->count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000372#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000373 if (tok->lock->tid == find_thread(NULL)) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000374 tok->count++;
375 return;
376 } else {
377 xmlMutexLock(tok->lock);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000378 tok->count = 1;
379 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000380#endif
381}
382
383/**
384 * xmlRMutexUnlock:
385 * @tok: the reentrant mutex
386 *
387 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
388 */
389void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000390xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000391{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000392 if (tok == NULL)
393 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000394#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000395 if (libxml_is_threaded == 0)
396 return;
397
Daniel Veillardb8478642001-10-12 17:29:10 +0000398 pthread_mutex_lock(&tok->lock);
399 tok->held--;
400 if (tok->held == 0) {
401 if (tok->waiters)
402 pthread_cond_signal(&tok->cv);
403 tok->tid = 0;
404 }
405 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000406#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000407 if (!--tok->count)
408 LeaveCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000409#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000410 if (tok->lock->tid == find_thread(NULL)) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000411 tok->count--;
412 if (tok->count == 0) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000413 xmlMutexUnlock(tok->lock);
414 }
415 return;
416 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000417#endif
418}
419
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000420/**
421 * xmlGlobalInitMutexLock
422 *
423 * Makes sure that the global initialization mutex is initialized and
424 * locks it.
425 */
426void
427__xmlGlobalInitMutexLock(void)
428{
429 /* Make sure the global init lock is initialized and then lock it. */
430#ifdef HAVE_PTHREAD_H
431 int err;
432
433 /* The mutex is statically initialized, so we just lock it. */
434 pthread_mutex_lock(&global_init_lock);
435#elif defined HAVE_WIN32_THREADS
436 LPCRITICAL_SECTION cs;
437
438 /* Create a new critical section */
439 if (global_init_lock == NULL) {
440 cs = malloc(sizeof(CRITICAL_SECTION));
441 InitializeCriticalSection(cs);
442
443 /* Swap it into the global_init_lock */
444 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
445
446 /* If another thread successfully recorded its critical
447 * section in the global_init_lock then discard the one
448 * allocated by this thread. */
449 if (global_init_lock != cs) {
450 free(cs);
451 }
452 }
453
454 /* Lock the chosen critical section */
455 EnterCriticalSection(global_init_lock);
456#elif defined HAVE_BEOS_THREADS
457 int32 sem;
458
459 /* Allocate a new semaphore */
460 sem = create_sem(1, "xmlGlobalinitMutex");
461
462 while (global_init_lock == -1) {
463 if (atomic_add(&global_init_count, 1) == 0) {
464 global_init_lock = sem;
465 } else {
466 snooze(1);
467 atomic_add(&global_init_count, -1);
468 }
469 }
470
471 /* If another thread successfully recorded its critical
472 * section in the global_init_lock then discard the one
473 * allocated by this thread. */
474 if (global_init_lock != sem)
475 delete_sem(sem);
476
477 /* Acquire the chosen semaphore */
478 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
479#ifdef DEBUG_THREADS
480 xmlGenericError(xmlGenericErrorContext, "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
481 exit();
482#endif
483 }
484#endif
485}
486
487void
488__xmlGlobalInitMutexUnlock(void)
489{
490#ifdef HAVE_PTHREAD_H
491 pthread_mutex_unlock(&global_init_lock);
492#elif defined HAVE_WIN32_THREADS
493 LeaveCriticalSection(global_init_lock);
494#elif defined HAVE_BEOS_THREADS
495 release_sem(global_init_lock);
496#endif
497}
498
Daniel Veillardb8478642001-10-12 17:29:10 +0000499/************************************************************************
500 * *
501 * Per thread global state handling *
502 * *
503 ************************************************************************/
504
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000505#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000506#ifdef xmlLastError
507#undef xmlLastError
508#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000509/**
510 * xmlFreeGlobalState:
511 * @state: a thread global state
512 *
513 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
514 * global state. It is is used here to reclaim memory resources.
515 */
516static void
517xmlFreeGlobalState(void *state)
518{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000519 xmlGlobalState *gs = (xmlGlobalState *) state;
520
521 /* free any memory allocated in the thread's xmlLastError */
522 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000523 free(state);
524}
525
526/**
527 * xmlNewGlobalState:
528 *
529 * xmlNewGlobalState() allocates a global state. This structure is used to
530 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000531 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000532 *
533 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
534 */
535static xmlGlobalStatePtr
536xmlNewGlobalState(void)
537{
538 xmlGlobalState *gs;
539
540 gs = malloc(sizeof(xmlGlobalState));
541 if (gs == NULL)
542 return(NULL);
543
William M. Brack8b2c7f12002-11-22 05:07:29 +0000544 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000545 xmlInitializeGlobalState(gs);
546 return (gs);
547}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000548#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000549
550
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000551#ifdef HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000552#if !defined(HAVE_COMPILER_TLS)
553#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000554typedef struct _xmlGlobalStateCleanupHelperParams
555{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000556 HANDLE thread;
557 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000558} xmlGlobalStateCleanupHelperParams;
559
Daniel Veillardffa3c742005-07-21 13:24:09 +0000560static void XMLCDECL xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000561{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000562 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
563 WaitForSingleObject(params->thread, INFINITE);
564 CloseHandle(params->thread);
565 xmlFreeGlobalState(params->memory);
566 free(params);
567 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000568}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000569#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
570
571typedef struct _xmlGlobalStateCleanupHelperParams
572{
573 void *memory;
574 struct _xmlGlobalStateCleanupHelperParams * prev;
575 struct _xmlGlobalStateCleanupHelperParams * next;
576} xmlGlobalStateCleanupHelperParams;
577
578static xmlGlobalStateCleanupHelperParams * cleanup_helpers_head = NULL;
579static CRITICAL_SECTION cleanup_helpers_cs;
580
581#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
582#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000583#endif /* HAVE_WIN32_THREADS */
584
Daniel Veillard82cb3192003-10-29 13:39:15 +0000585#if defined HAVE_BEOS_THREADS
William M. Brackb1d53162003-11-18 06:54:40 +0000586/**
587 * xmlGlobalStateCleanup:
588 * @data: unused parameter
589 *
590 * Used for Beos only
591 */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000592void xmlGlobalStateCleanup(void *data)
593{
594 void *globalval = tls_get(globalkey);
595 if (globalval != NULL)
596 xmlFreeGlobalState(globalval);
597}
598#endif
599
Daniel Veillard01c13b52002-12-10 15:19:08 +0000600/**
601 * xmlGetGlobalState:
602 *
603 * xmlGetGlobalState() is called to retrieve the global state for a thread.
604 *
605 * Returns the thread global state or NULL in case of error
606 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000607xmlGlobalStatePtr
608xmlGetGlobalState(void)
609{
610#ifdef HAVE_PTHREAD_H
611 xmlGlobalState *globalval;
612
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000613 if (libxml_is_threaded == 0)
614 return(NULL);
615
Daniel Veillarde28313b2001-12-06 14:08:31 +0000616 pthread_once(&once_control, xmlOnceInit);
617
Daniel Veillardb8478642001-10-12 17:29:10 +0000618 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000619 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000620 xmlGlobalState *tsd = xmlNewGlobalState();
621
622 pthread_setspecific(globalkey, tsd);
623 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000624 }
625 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000626#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000627#if defined(HAVE_COMPILER_TLS)
628 if (!tlstate_inited) {
629 tlstate_inited = 1;
630 xmlInitializeGlobalState(&tlstate);
631 }
632 return &tlstate;
633#else /* HAVE_COMPILER_TLS */
634 xmlGlobalState *globalval;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000635 xmlGlobalStateCleanupHelperParams * p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000636
Daniel Veillard62121e22005-02-24 15:38:52 +0000637 xmlOnceInit();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000638#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
639 globalval = (xmlGlobalState *)TlsGetValue(globalkey);
640#else
641 p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
642 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
643#endif
644 if (globalval == NULL) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000645 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000646 p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000647 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000648#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000649 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
650 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
651 TlsSetValue(globalkey, tsd);
652 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000653#else
654 EnterCriticalSection(&cleanup_helpers_cs);
655 if (cleanup_helpers_head != NULL) {
656 cleanup_helpers_head->prev = p;
657 }
658 p->next = cleanup_helpers_head;
659 p->prev = NULL;
660 cleanup_helpers_head = p;
661 TlsSetValue(globalkey, p);
662 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000663#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000664
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000665 return (tsd);
666 }
667 return (globalval);
668#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000669#elif defined HAVE_BEOS_THREADS
670 xmlGlobalState *globalval;
671
672 xmlOnceInit();
673
674 if ((globalval = (xmlGlobalState *)
675 tls_get(globalkey)) == NULL) {
676 xmlGlobalState *tsd = xmlNewGlobalState();
677
678 tls_set(globalkey, tsd);
679 on_exit_thread(xmlGlobalStateCleanup, NULL);
680 return (tsd);
681 }
682 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000683#else
684 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000685#endif
686}
687
Daniel Veillardb8478642001-10-12 17:29:10 +0000688/************************************************************************
689 * *
690 * Library wide thread interfaces *
691 * *
692 ************************************************************************/
693
694/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000695 * xmlGetThreadId:
696 *
697 * xmlGetThreadId() find the current thread ID number
698 *
699 * Returns the current thread ID number
700 */
701int
702xmlGetThreadId(void)
703{
704#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000705 if (libxml_is_threaded == 0)
706 return(0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000707 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000708#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000709 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000710#elif defined HAVE_BEOS_THREADS
711 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000712#else
713 return((int) 0);
714#endif
715}
716
717/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000718 * xmlIsMainThread:
719 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000720 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000721 *
722 * Returns 1 if the current thread is the main thread, 0 otherwise
723 */
724int
725xmlIsMainThread(void)
726{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000727#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000728 if (libxml_is_threaded == -1)
729 xmlInitThreads();
730 if (libxml_is_threaded == 0)
731 return(1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000732 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000733#elif defined HAVE_WIN32_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000734 xmlOnceInit ();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000735#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000736 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000737#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000738
739#ifdef DEBUG_THREADS
740 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
741#endif
742#ifdef HAVE_PTHREAD_H
743 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000744#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000745 return(mainthread == GetCurrentThreadId ());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000746#elif defined HAVE_BEOS_THREADS
747 return(mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000748#else
749 return(1);
750#endif
751}
752
753/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000754 * xmlLockLibrary:
755 *
756 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
757 * library.
758 */
759void
760xmlLockLibrary(void)
761{
Daniel Veillard6f350292001-10-14 09:56:15 +0000762#ifdef DEBUG_THREADS
763 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
764#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000765 xmlRMutexLock(xmlLibraryLock);
766}
767
768/**
769 * xmlUnlockLibrary:
770 *
771 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
772 * library.
773 */
774void
775xmlUnlockLibrary(void)
776{
Daniel Veillard6f350292001-10-14 09:56:15 +0000777#ifdef DEBUG_THREADS
778 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
779#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000780 xmlRMutexUnlock(xmlLibraryLock);
781}
782
783/**
784 * xmlInitThreads:
785 *
786 * xmlInitThreads() is used to to initialize all the thread related
787 * data of the libxml2 library.
788 */
789void
790xmlInitThreads(void)
791{
Daniel Veillard6f350292001-10-14 09:56:15 +0000792#ifdef DEBUG_THREADS
793 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
794#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000795#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000796 InitializeCriticalSection(&cleanup_helpers_cs);
797#endif
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000798#ifdef HAVE_PTHREAD_H
799 if (libxml_is_threaded == -1) {
800 if ((pthread_once != NULL) &&
801 (pthread_getspecific != NULL) &&
802 (pthread_setspecific != NULL) &&
803 (pthread_key_create != NULL) &&
804 (pthread_mutex_init != NULL) &&
805 (pthread_mutex_destroy != NULL) &&
806 (pthread_mutex_lock != NULL) &&
807 (pthread_mutex_unlock != NULL) &&
808 (pthread_cond_init != NULL) &&
809 (pthread_equal != NULL) &&
810 (pthread_self != NULL) &&
811 (pthread_key_create != NULL) &&
812 (pthread_cond_signal != NULL)) {
813 libxml_is_threaded = 1;
814/* fprintf(stderr, "Running multithreaded\n"); */
815 } else {
816/* fprintf(stderr, "Running without multithread\n"); */
817 libxml_is_threaded = 0;
818 }
819 }
820#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000821}
822
823/**
824 * xmlCleanupThreads:
825 *
826 * xmlCleanupThreads() is used to to cleanup all the thread related
827 * data of the libxml2 library once processing has ended.
828 */
829void
830xmlCleanupThreads(void)
831{
Daniel Veillard6f350292001-10-14 09:56:15 +0000832#ifdef DEBUG_THREADS
833 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
834#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000835#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000836 if (globalkey != TLS_OUT_OF_INDEXES) {
837 xmlGlobalStateCleanupHelperParams * p;
838 EnterCriticalSection(&cleanup_helpers_cs);
839 p = cleanup_helpers_head;
840 while (p != NULL) {
841 xmlGlobalStateCleanupHelperParams * temp = p;
842 p = p->next;
843 xmlFreeGlobalState(temp->memory);
844 free(temp);
845 }
846 cleanup_helpers_head = 0;
847 LeaveCriticalSection(&cleanup_helpers_cs);
848 TlsFree(globalkey);
849 globalkey = TLS_OUT_OF_INDEXES;
850 }
851 DeleteCriticalSection(&cleanup_helpers_cs);
852#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000853}
Daniel Veillard6f350292001-10-14 09:56:15 +0000854
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000855#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000856/**
857 * xmlOnceInit
858 *
859 * xmlOnceInit() is used to initialize the value of mainthread for use
860 * in other routines. This function should only be called using
861 * pthread_once() in association with the once_control variable to ensure
862 * that the function is only called once. See man pthread_once for more
863 * details.
864 */
865static void
866xmlOnceInit(void) {
867#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000868 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000869 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000870#endif
871
872#if defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000873 if (!run_once.done) {
Daniel Veillard36616dd2005-02-25 07:31:49 +0000874 if (InterlockedIncrement(&run_once.control) == 1)
Daniel Veillard62121e22005-02-24 15:38:52 +0000875 {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000876#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000877 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000878#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000879 mainthread = GetCurrentThreadId();
880 run_once.done = 1;
881 }
882 else {
883 /* Another thread is working; give up our slice and
884 * wait until they're done. */
885 while (!run_once.done)
886 Sleep(0);
887 }
888 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000889#endif
Daniel Veillard82cb3192003-10-29 13:39:15 +0000890
891#ifdef HAVE_BEOS_THREADS
892 if (atomic_add(&run_once_init, 1) == 0) {
893 globalkey = tls_allocate();
894 tls_set(globalkey, NULL);
895 mainthread = find_thread(NULL);
896 } else
897 atomic_add(&run_once_init, -1);
898#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000899}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000900#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000901
902/**
William M. Brack7a821652003-08-15 07:27:40 +0000903 * DllMain:
904 * @hinstDLL: handle to DLL instance
905 * @fdwReason: Reason code for entry
906 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000907 *
908 * Entry point for Windows library. It is being used to free thread-specific
909 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000910 *
911 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000912 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000913#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
914#if defined(LIBXML_STATIC_FOR_DLL)
915BOOL WINAPI xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
916#else
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000917BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000918#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000919{
920 switch(fdwReason) {
921 case DLL_THREAD_DETACH:
922 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000923 xmlGlobalState *globalval = NULL;
924 xmlGlobalStateCleanupHelperParams * p =
925 (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
926 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
927 if (globalval) {
928 xmlFreeGlobalState(globalval);
929 TlsSetValue(globalkey,NULL);
930 }
931 if (p)
932 {
933 EnterCriticalSection(&cleanup_helpers_cs);
934 if (p == cleanup_helpers_head)
935 cleanup_helpers_head = p->next;
936 else
937 p->prev->next = p->next;
938 if (p->next != NULL)
939 p->next->prev = p->prev;
940 LeaveCriticalSection(&cleanup_helpers_cs);
941 free(p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000942 }
943 }
944 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000945 }
946 return TRUE;
947}
948#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +0000949#define bottom_threads
950#include "elfgcchack.h"