blob: fde0a62b8bd5cd2ee2b4bdd437272567c50f924e [file] [log] [blame]
Daniel Veillardb8478642001-10-12 17:29:10 +00001/**
Daniel Veillarddee23482008-04-11 12:58:43 +00002 * threads.c: set of generic threading related routines
Daniel Veillardb8478642001-10-12 17:29:10 +00003 *
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));
Daniel Veillard2cba4152008-08-27 11:45:41 +000076extern int pthread_cond_destroy ()
77 __attribute((weak));
78extern int pthread_cond_wait ()
79 __attribute((weak));
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000080extern int pthread_equal ()
81 __attribute((weak));
82extern pthread_t pthread_self ()
83 __attribute((weak));
84extern int pthread_key_create ()
85 __attribute((weak));
86extern int pthread_cond_signal ()
87 __attribute((weak));
88#endif
89#endif /* linux */
90#endif /* __GNUC__ */
91#endif /* HAVE_PTHREAD_H */
92
Daniel Veillardb8478642001-10-12 17:29:10 +000093/*
94 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
95 * to avoid some crazyness since xmlMalloc/xmlFree may actually
96 * be hosted on allocated blocks needing them for the allocation ...
97 */
98
99/*
100 * xmlMutex are a simple mutual exception locks
101 */
102struct _xmlMutex {
103#ifdef HAVE_PTHREAD_H
104 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000105#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000106 HANDLE mutex;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000107#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000108 sem_id sem;
109 thread_id tid;
Daniel Veillardb8478642001-10-12 17:29:10 +0000110#else
111 int empty;
112#endif
113};
114
115/*
116 * xmlRMutex are reentrant mutual exception locks
117 */
118struct _xmlRMutex {
119#ifdef HAVE_PTHREAD_H
120 pthread_mutex_t lock;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000121 unsigned int held;
122 unsigned int waiters;
123 pthread_t tid;
124 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000125#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000126 CRITICAL_SECTION cs;
127 unsigned int count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000128#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000129 xmlMutexPtr lock;
130 thread_id tid;
131 int32 count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000132#else
133 int empty;
134#endif
135};
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000136
Daniel Veillardb8478642001-10-12 17:29:10 +0000137/*
138 * This module still has some internal static data.
139 * - xmlLibraryLock a global lock
140 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +0000141 */
Daniel Veillard6f350292001-10-14 09:56:15 +0000142
Daniel Veillardb8478642001-10-12 17:29:10 +0000143#ifdef HAVE_PTHREAD_H
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000144static pthread_key_t globalkey;
145static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +0000146static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000147static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000148#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000149#if defined(HAVE_COMPILER_TLS)
150static __declspec(thread) xmlGlobalState tlstate;
151static __declspec(thread) int tlstate_inited = 0;
152#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000153static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000154#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000155static DWORD mainthread;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000156static struct {
Daniel Veillard36616dd2005-02-25 07:31:49 +0000157 DWORD done;
158 DWORD control;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000159} run_once = { 0, 0};
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000160static volatile LPCRITICAL_SECTION global_init_lock = NULL;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000161
Daniel Veillard82cb3192003-10-29 13:39:15 +0000162/* endif HAVE_WIN32_THREADS */
163#elif defined HAVE_BEOS_THREADS
164int32 globalkey = 0;
165thread_id mainthread = 0;
166int32 run_once_init = 0;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000167static int32 global_init_lock = -1;
168static vint32 global_init_count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000169#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000170
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000171static xmlRMutexPtr xmlLibraryLock = NULL;
172
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000173#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000174static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000175#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000176
177/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000178 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000179 *
180 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
181 * synchronizing access to data.
182 *
183 * Returns a new simple mutex pointer or NULL in case of error
184 */
185xmlMutexPtr
186xmlNewMutex(void)
187{
188 xmlMutexPtr tok;
189
190 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
191 return (NULL);
192#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000193 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000194 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000195#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000196 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000197#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000198 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
199 free(tok);
200 return NULL;
201 }
202 tok->tid = -1;
Daniel Veillardb8478642001-10-12 17:29:10 +0000203#endif
204 return (tok);
205}
206
207/**
208 * xmlFreeMutex:
209 * @tok: the simple mutex
210 *
211 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
212 * struct.
213 */
214void
215xmlFreeMutex(xmlMutexPtr tok)
216{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000217 if (tok == NULL)
218 return;
Daniel Veillarddf101d82003-07-08 14:03:36 +0000219
Daniel Veillardb8478642001-10-12 17:29:10 +0000220#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000221 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000222 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000223#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000224 CloseHandle(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000225#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000226 delete_sem(tok->sem);
Daniel Veillardb8478642001-10-12 17:29:10 +0000227#endif
228 free(tok);
229}
230
231/**
232 * xmlMutexLock:
233 * @tok: the simple mutex
234 *
235 * xmlMutexLock() is used to lock a libxml2 token.
236 */
237void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000238xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000239{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000240 if (tok == NULL)
241 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000242#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000243 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000244 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000245#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000246 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000247#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000248 if (acquire_sem(tok->sem) != B_NO_ERROR) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000249#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000250 xmlGenericError(xmlGenericErrorContext,
251 "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
252 exit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000253#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000254 }
255 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000256#endif
257
258}
259
260/**
261 * xmlMutexUnlock:
262 * @tok: the simple mutex
263 *
264 * xmlMutexUnlock() is used to unlock a libxml2 token.
265 */
266void
Daniel Veillard5805be22003-08-28 08:03:23 +0000267xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000268{
Daniel Veillard5805be22003-08-28 08:03:23 +0000269 if (tok == NULL)
270 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000271#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000272 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000273 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000274#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000275 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000276#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000277 if (tok->tid == find_thread(NULL)) {
278 tok->tid = -1;
279 release_sem(tok->sem);
280 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000281#endif
282}
283
284/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000285 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000286 *
287 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
288 * synchronizing access to data. token_r is a re-entrant lock and thus useful
289 * for synchronizing access to data structures that may be manipulated in a
290 * recursive fashion.
291 *
292 * Returns the new reentrant mutex pointer or NULL in case of error
293 */
294xmlRMutexPtr
295xmlNewRMutex(void)
296{
297 xmlRMutexPtr tok;
298
299 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
300 return (NULL);
301#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000302 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000303 pthread_mutex_init(&tok->lock, NULL);
304 tok->held = 0;
305 tok->waiters = 0;
306 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000307 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000308#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000309 InitializeCriticalSection(&tok->cs);
310 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000311#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000312 if ((tok->lock = xmlNewMutex()) == NULL) {
313 free(tok);
314 return NULL;
315 }
316 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000317#endif
318 return (tok);
319}
320
321/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000322 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000323 * @tok: the reentrant mutex
324 *
325 * xmlRFreeMutex() is used to reclaim resources associated with a
326 * reentrant mutex.
327 */
328void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000329xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000330{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000331 if (tok == NULL)
332 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000333#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000334 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000335 pthread_mutex_destroy(&tok->lock);
336 pthread_cond_destroy(&tok->cv);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000337 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000338#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000339 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000340#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000341 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000342#endif
343 free(tok);
344}
345
346/**
347 * xmlRMutexLock:
348 * @tok: the reentrant mutex
349 *
350 * xmlRMutexLock() is used to lock a libxml2 token_r.
351 */
352void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000353xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000354{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000355 if (tok == NULL)
356 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000357#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000358 if (libxml_is_threaded == 0)
359 return;
360
Daniel Veillardb8478642001-10-12 17:29:10 +0000361 pthread_mutex_lock(&tok->lock);
362 if (tok->held) {
363 if (pthread_equal(tok->tid, pthread_self())) {
364 tok->held++;
365 pthread_mutex_unlock(&tok->lock);
366 return;
367 } else {
368 tok->waiters++;
369 while (tok->held)
370 pthread_cond_wait(&tok->cv, &tok->lock);
371 tok->waiters--;
372 }
373 }
374 tok->tid = pthread_self();
375 tok->held = 1;
376 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000377#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000378 EnterCriticalSection(&tok->cs);
379 ++tok->count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000380#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000381 if (tok->lock->tid == find_thread(NULL)) {
382 tok->count++;
383 return;
384 } else {
385 xmlMutexLock(tok->lock);
386 tok->count = 1;
387 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000388#endif
389}
390
391/**
392 * xmlRMutexUnlock:
393 * @tok: the reentrant mutex
394 *
395 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
396 */
397void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000398xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000399{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000400 if (tok == NULL)
401 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000402#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000403 if (libxml_is_threaded == 0)
404 return;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000405
Daniel Veillardb8478642001-10-12 17:29:10 +0000406 pthread_mutex_lock(&tok->lock);
407 tok->held--;
408 if (tok->held == 0) {
409 if (tok->waiters)
410 pthread_cond_signal(&tok->cv);
411 tok->tid = 0;
412 }
413 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000414#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000415 if (!--tok->count)
416 LeaveCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000417#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000418 if (tok->lock->tid == find_thread(NULL)) {
419 tok->count--;
420 if (tok->count == 0) {
421 xmlMutexUnlock(tok->lock);
422 }
423 return;
424 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000425#endif
426}
427
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000428/**
429 * xmlGlobalInitMutexLock
430 *
431 * Makes sure that the global initialization mutex is initialized and
432 * locks it.
433 */
434void
435__xmlGlobalInitMutexLock(void)
436{
437 /* Make sure the global init lock is initialized and then lock it. */
438#ifdef HAVE_PTHREAD_H
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000439 /* The mutex is statically initialized, so we just lock it. */
440 pthread_mutex_lock(&global_init_lock);
441#elif defined HAVE_WIN32_THREADS
442 LPCRITICAL_SECTION cs;
443
444 /* Create a new critical section */
445 if (global_init_lock == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000446 cs = malloc(sizeof(CRITICAL_SECTION));
447 if (cs == NULL) {
448 xmlGenericError(xmlGenericErrorContext,
449 "xmlGlobalInitMutexLock: out of memory\n");
450 return;
451 }
452 InitializeCriticalSection(cs);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000453
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000454 /* Swap it into the global_init_lock */
Rob Richardse967f0b2007-06-08 19:36:04 +0000455#ifdef InterlockedCompareExchangePointer
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000456 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
457#else /* Use older void* version */
458 InterlockedCompareExchange((void **) &global_init_lock,
459 (void *) cs, NULL);
Rob Richardse967f0b2007-06-08 19:36:04 +0000460#endif /* InterlockedCompareExchangePointer */
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000461
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000462 /* If another thread successfully recorded its critical
463 * section in the global_init_lock then discard the one
464 * allocated by this thread. */
465 if (global_init_lock != cs) {
466 DeleteCriticalSection(cs);
467 free(cs);
468 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000469 }
470
471 /* Lock the chosen critical section */
472 EnterCriticalSection(global_init_lock);
473#elif defined HAVE_BEOS_THREADS
474 int32 sem;
475
476 /* Allocate a new semaphore */
477 sem = create_sem(1, "xmlGlobalinitMutex");
478
479 while (global_init_lock == -1) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000480 if (atomic_add(&global_init_count, 1) == 0) {
481 global_init_lock = sem;
482 } else {
483 snooze(1);
484 atomic_add(&global_init_count, -1);
485 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000486 }
487
488 /* If another thread successfully recorded its critical
489 * section in the global_init_lock then discard the one
490 * allocated by this thread. */
491 if (global_init_lock != sem)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000492 delete_sem(sem);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000493
494 /* Acquire the chosen semaphore */
495 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
496#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000497 xmlGenericError(xmlGenericErrorContext,
498 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
499 exit();
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000500#endif
501 }
502#endif
503}
504
505void
506__xmlGlobalInitMutexUnlock(void)
507{
508#ifdef HAVE_PTHREAD_H
509 pthread_mutex_unlock(&global_init_lock);
510#elif defined HAVE_WIN32_THREADS
Daniel Veillard14d465d2008-03-24 11:12:55 +0000511 if (global_init_lock != NULL) {
512 LeaveCriticalSection(global_init_lock);
513 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000514#elif defined HAVE_BEOS_THREADS
515 release_sem(global_init_lock);
516#endif
517}
518
Rob Richards91eb5602007-11-16 10:54:59 +0000519/**
520 * xmlGlobalInitMutexDestroy
521 *
522 * Makes sure that the global initialization mutex is destroyed before
523 * application termination.
524 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000525void
526__xmlGlobalInitMutexDestroy(void)
Rob Richards91eb5602007-11-16 10:54:59 +0000527{
528#if defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000529 if (global_init_lock != NULL) {
530 DeleteCriticalSection(global_init_lock);
531 free(global_init_lock);
532 global_init_lock = NULL;
Rob Richards91eb5602007-11-16 10:54:59 +0000533 }
534#endif
535}
536
Daniel Veillardb8478642001-10-12 17:29:10 +0000537/************************************************************************
538 * *
539 * Per thread global state handling *
540 * *
541 ************************************************************************/
542
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000543#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000544#ifdef xmlLastError
545#undef xmlLastError
546#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000547
Daniel Veillardb8478642001-10-12 17:29:10 +0000548/**
549 * xmlFreeGlobalState:
550 * @state: a thread global state
551 *
552 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
553 * global state. It is is used here to reclaim memory resources.
554 */
555static void
556xmlFreeGlobalState(void *state)
557{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000558 xmlGlobalState *gs = (xmlGlobalState *) state;
559
560 /* free any memory allocated in the thread's xmlLastError */
561 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000562 free(state);
563}
564
565/**
566 * xmlNewGlobalState:
567 *
568 * xmlNewGlobalState() allocates a global state. This structure is used to
569 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000570 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000571 *
572 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
573 */
574static xmlGlobalStatePtr
575xmlNewGlobalState(void)
576{
577 xmlGlobalState *gs;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000578
Daniel Veillardb8478642001-10-12 17:29:10 +0000579 gs = malloc(sizeof(xmlGlobalState));
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000580 if (gs == NULL) {
581 xmlGenericError(xmlGenericErrorContext,
582 "xmlGetGlobalState: out of memory\n");
583 return (NULL);
584 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000585
William M. Brack8b2c7f12002-11-22 05:07:29 +0000586 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000587 xmlInitializeGlobalState(gs);
588 return (gs);
589}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000590#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000591
592
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000593#ifdef HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000594#if !defined(HAVE_COMPILER_TLS)
595#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000596typedef struct _xmlGlobalStateCleanupHelperParams {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000597 HANDLE thread;
598 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000599} xmlGlobalStateCleanupHelperParams;
600
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000601static void XMLCDECL
602xmlGlobalStateCleanupHelper(void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000603{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000604 xmlGlobalStateCleanupHelperParams *params =
605 (xmlGlobalStateCleanupHelperParams *) p;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000606 WaitForSingleObject(params->thread, INFINITE);
607 CloseHandle(params->thread);
608 xmlFreeGlobalState(params->memory);
609 free(params);
610 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000611}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000612#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
613
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000614typedef struct _xmlGlobalStateCleanupHelperParams {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000615 void *memory;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000616 struct _xmlGlobalStateCleanupHelperParams *prev;
617 struct _xmlGlobalStateCleanupHelperParams *next;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000618} xmlGlobalStateCleanupHelperParams;
619
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000620static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000621static CRITICAL_SECTION cleanup_helpers_cs;
622
623#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
624#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000625#endif /* HAVE_WIN32_THREADS */
626
Daniel Veillard82cb3192003-10-29 13:39:15 +0000627#if defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000628
William M. Brackb1d53162003-11-18 06:54:40 +0000629/**
630 * xmlGlobalStateCleanup:
631 * @data: unused parameter
632 *
633 * Used for Beos only
634 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000635void
636xmlGlobalStateCleanup(void *data)
Daniel Veillard82cb3192003-10-29 13:39:15 +0000637{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000638 void *globalval = tls_get(globalkey);
639
640 if (globalval != NULL)
641 xmlFreeGlobalState(globalval);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000642}
643#endif
644
Daniel Veillard01c13b52002-12-10 15:19:08 +0000645/**
646 * xmlGetGlobalState:
647 *
648 * xmlGetGlobalState() is called to retrieve the global state for a thread.
649 *
650 * Returns the thread global state or NULL in case of error
651 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000652xmlGlobalStatePtr
653xmlGetGlobalState(void)
654{
655#ifdef HAVE_PTHREAD_H
656 xmlGlobalState *globalval;
657
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000658 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000659 return (NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000660
Daniel Veillarde28313b2001-12-06 14:08:31 +0000661 pthread_once(&once_control, xmlOnceInit);
662
Daniel Veillardb8478642001-10-12 17:29:10 +0000663 if ((globalval = (xmlGlobalState *)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000664 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000665 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000666 if (tsd == NULL)
667 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000668
669 pthread_setspecific(globalkey, tsd);
670 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000671 }
672 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000673#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000674#if defined(HAVE_COMPILER_TLS)
675 if (!tlstate_inited) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000676 tlstate_inited = 1;
677 xmlInitializeGlobalState(&tlstate);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000678 }
679 return &tlstate;
680#else /* HAVE_COMPILER_TLS */
681 xmlGlobalState *globalval;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000682 xmlGlobalStateCleanupHelperParams *p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000683
Daniel Veillard62121e22005-02-24 15:38:52 +0000684 xmlOnceInit();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000685#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000686 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000687#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000688 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
689 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000690#endif
691 if (globalval == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000692 xmlGlobalState *tsd = xmlNewGlobalState();
693
694 if (tsd == NULL)
695 return(NULL);
696 p = (xmlGlobalStateCleanupHelperParams *)
697 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
698 if (p == NULL) {
699 xmlGenericError(xmlGenericErrorContext,
700 "xmlGetGlobalState: out of memory\n");
Daniel Veillardbf2ebff2009-01-18 14:57:04 +0000701 xmlFreeGlobalState(tsd);
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000702 return(NULL);
703 }
704 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000705#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000706 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
707 GetCurrentProcess(), &p->thread, 0, TRUE,
708 DUPLICATE_SAME_ACCESS);
709 TlsSetValue(globalkey, tsd);
710 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000711#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000712 EnterCriticalSection(&cleanup_helpers_cs);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000713 if (cleanup_helpers_head != NULL) {
714 cleanup_helpers_head->prev = p;
715 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000716 p->next = cleanup_helpers_head;
717 p->prev = NULL;
718 cleanup_helpers_head = p;
719 TlsSetValue(globalkey, p);
720 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000721#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000722
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000723 return (tsd);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000724 }
725 return (globalval);
726#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000727#elif defined HAVE_BEOS_THREADS
728 xmlGlobalState *globalval;
729
730 xmlOnceInit();
731
Daniel Veillard14d465d2008-03-24 11:12:55 +0000732 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000733 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000734 if (tsd == NULL)
735 return (NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000736
737 tls_set(globalkey, tsd);
738 on_exit_thread(xmlGlobalStateCleanup, NULL);
739 return (tsd);
740 }
741 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000742#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000743 return (NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000744#endif
745}
746
Daniel Veillardb8478642001-10-12 17:29:10 +0000747/************************************************************************
748 * *
749 * Library wide thread interfaces *
750 * *
751 ************************************************************************/
752
753/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000754 * xmlGetThreadId:
755 *
756 * xmlGetThreadId() find the current thread ID number
757 *
758 * Returns the current thread ID number
759 */
760int
761xmlGetThreadId(void)
762{
763#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000764 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000765 return (0);
766 return ((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000767#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000768 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000769#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000770 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000771#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000772 return ((int) 0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000773#endif
774}
775
776/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000777 * xmlIsMainThread:
778 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000779 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000780 *
781 * Returns 1 if the current thread is the main thread, 0 otherwise
782 */
783int
784xmlIsMainThread(void)
785{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000786#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000787 if (libxml_is_threaded == -1)
788 xmlInitThreads();
789 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000790 return (1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000791 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000792#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000793 xmlOnceInit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000794#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000795 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000796#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000797
Daniel Veillard6f350292001-10-14 09:56:15 +0000798#ifdef DEBUG_THREADS
799 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
800#endif
801#ifdef HAVE_PTHREAD_H
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000802 return (mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000803#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000804 return (mainthread == GetCurrentThreadId());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000805#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000806 return (mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000807#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000808 return (1);
Daniel Veillard6f350292001-10-14 09:56:15 +0000809#endif
810}
811
812/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000813 * xmlLockLibrary:
814 *
815 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
816 * library.
817 */
818void
819xmlLockLibrary(void)
820{
Daniel Veillard6f350292001-10-14 09:56:15 +0000821#ifdef DEBUG_THREADS
822 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
823#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000824 xmlRMutexLock(xmlLibraryLock);
825}
826
827/**
828 * xmlUnlockLibrary:
829 *
830 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
831 * library.
832 */
833void
834xmlUnlockLibrary(void)
835{
Daniel Veillard6f350292001-10-14 09:56:15 +0000836#ifdef DEBUG_THREADS
837 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
838#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000839 xmlRMutexUnlock(xmlLibraryLock);
840}
841
842/**
843 * xmlInitThreads:
844 *
845 * xmlInitThreads() is used to to initialize all the thread related
846 * data of the libxml2 library.
847 */
848void
849xmlInitThreads(void)
850{
Daniel Veillard6f350292001-10-14 09:56:15 +0000851#ifdef DEBUG_THREADS
852 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
853#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000854#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000855 InitializeCriticalSection(&cleanup_helpers_cs);
856#endif
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000857#ifdef HAVE_PTHREAD_H
858 if (libxml_is_threaded == -1) {
859 if ((pthread_once != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000860 (pthread_getspecific != NULL) &&
861 (pthread_setspecific != NULL) &&
862 (pthread_key_create != NULL) &&
863 (pthread_mutex_init != NULL) &&
864 (pthread_mutex_destroy != NULL) &&
865 (pthread_mutex_lock != NULL) &&
866 (pthread_mutex_unlock != NULL) &&
867 (pthread_cond_init != NULL) &&
Daniel Veillard2cba4152008-08-27 11:45:41 +0000868 (pthread_cond_destroy != NULL) &&
869 (pthread_cond_wait != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000870 (pthread_equal != NULL) &&
871 (pthread_self != NULL) &&
872 (pthread_cond_signal != NULL)) {
873 libxml_is_threaded = 1;
874
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000875/* fprintf(stderr, "Running multithreaded\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000876 } else {
877
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000878/* fprintf(stderr, "Running without multithread\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000879 libxml_is_threaded = 0;
880 }
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000881 }
882#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000883}
884
885/**
886 * xmlCleanupThreads:
887 *
888 * xmlCleanupThreads() is used to to cleanup all the thread related
889 * data of the libxml2 library once processing has ended.
890 */
891void
892xmlCleanupThreads(void)
893{
Daniel Veillard6f350292001-10-14 09:56:15 +0000894#ifdef DEBUG_THREADS
895 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
896#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000897#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000898 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000899 xmlGlobalStateCleanupHelperParams *p;
900
901 EnterCriticalSection(&cleanup_helpers_cs);
902 p = cleanup_helpers_head;
903 while (p != NULL) {
904 xmlGlobalStateCleanupHelperParams *temp = p;
905
906 p = p->next;
907 xmlFreeGlobalState(temp->memory);
908 free(temp);
909 }
910 cleanup_helpers_head = 0;
911 LeaveCriticalSection(&cleanup_helpers_cs);
912 TlsFree(globalkey);
913 globalkey = TLS_OUT_OF_INDEXES;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000914 }
915 DeleteCriticalSection(&cleanup_helpers_cs);
916#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000917}
Daniel Veillard6f350292001-10-14 09:56:15 +0000918
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000919#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000920
Daniel Veillarde28313b2001-12-06 14:08:31 +0000921/**
922 * xmlOnceInit
923 *
924 * xmlOnceInit() is used to initialize the value of mainthread for use
925 * in other routines. This function should only be called using
926 * pthread_once() in association with the once_control variable to ensure
927 * that the function is only called once. See man pthread_once for more
928 * details.
929 */
930static void
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000931xmlOnceInit(void)
932{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000933#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000934 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000935 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000936#endif
937
938#if defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000939 if (!run_once.done) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000940 if (InterlockedIncrement(&run_once.control) == 1) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000941#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000942 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000943#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000944 mainthread = GetCurrentThreadId();
945 run_once.done = 1;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000946 } else {
Daniel Veillard62121e22005-02-24 15:38:52 +0000947 /* Another thread is working; give up our slice and
948 * wait until they're done. */
949 while (!run_once.done)
950 Sleep(0);
951 }
952 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000953#endif
Daniel Veillard82cb3192003-10-29 13:39:15 +0000954
955#ifdef HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000956 if (atomic_add(&run_once_init, 1) == 0) {
957 globalkey = tls_allocate();
958 tls_set(globalkey, NULL);
959 mainthread = find_thread(NULL);
960 } else
961 atomic_add(&run_once_init, -1);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000962#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000963}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000964#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000965
966/**
William M. Brack7a821652003-08-15 07:27:40 +0000967 * DllMain:
968 * @hinstDLL: handle to DLL instance
969 * @fdwReason: Reason code for entry
970 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000971 *
972 * Entry point for Windows library. It is being used to free thread-specific
973 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000974 *
975 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000976 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000977#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
978#if defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000979BOOL XMLCALL
980xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000981#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000982BOOL WINAPI
983DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000984#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000985{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000986 switch (fdwReason) {
987 case DLL_THREAD_DETACH:
988 if (globalkey != TLS_OUT_OF_INDEXES) {
989 xmlGlobalState *globalval = NULL;
990 xmlGlobalStateCleanupHelperParams *p =
991 (xmlGlobalStateCleanupHelperParams *)
992 TlsGetValue(globalkey);
993 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
994 if (globalval) {
995 xmlFreeGlobalState(globalval);
996 TlsSetValue(globalkey, NULL);
997 }
998 if (p) {
999 EnterCriticalSection(&cleanup_helpers_cs);
1000 if (p == cleanup_helpers_head)
1001 cleanup_helpers_head = p->next;
1002 else
1003 p->prev->next = p->next;
1004 if (p->next != NULL)
1005 p->next->prev = p->prev;
1006 LeaveCriticalSection(&cleanup_helpers_cs);
1007 free(p);
1008 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001009 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001010 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001011 }
1012 return TRUE;
1013}
1014#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001015#define bottom_threads
1016#include "elfgcchack.h"