blob: 23cb200ac5742d2859c8731d8d8635587bc5c748 [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");
701 return(NULL);
702 }
703 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000704#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000705 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
706 GetCurrentProcess(), &p->thread, 0, TRUE,
707 DUPLICATE_SAME_ACCESS);
708 TlsSetValue(globalkey, tsd);
709 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000710#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000711 EnterCriticalSection(&cleanup_helpers_cs);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000712 if (cleanup_helpers_head != NULL) {
713 cleanup_helpers_head->prev = p;
714 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000715 p->next = cleanup_helpers_head;
716 p->prev = NULL;
717 cleanup_helpers_head = p;
718 TlsSetValue(globalkey, p);
719 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000720#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000721
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000722 return (tsd);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000723 }
724 return (globalval);
725#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000726#elif defined HAVE_BEOS_THREADS
727 xmlGlobalState *globalval;
728
729 xmlOnceInit();
730
Daniel Veillard14d465d2008-03-24 11:12:55 +0000731 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000732 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000733 if (tsd == NULL)
734 return (NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000735
736 tls_set(globalkey, tsd);
737 on_exit_thread(xmlGlobalStateCleanup, NULL);
738 return (tsd);
739 }
740 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000741#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000742 return (NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000743#endif
744}
745
Daniel Veillardb8478642001-10-12 17:29:10 +0000746/************************************************************************
747 * *
748 * Library wide thread interfaces *
749 * *
750 ************************************************************************/
751
752/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000753 * xmlGetThreadId:
754 *
755 * xmlGetThreadId() find the current thread ID number
756 *
757 * Returns the current thread ID number
758 */
759int
760xmlGetThreadId(void)
761{
762#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000763 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000764 return (0);
765 return ((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000766#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000767 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000768#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000769 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000770#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000771 return ((int) 0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000772#endif
773}
774
775/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000776 * xmlIsMainThread:
777 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000778 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000779 *
780 * Returns 1 if the current thread is the main thread, 0 otherwise
781 */
782int
783xmlIsMainThread(void)
784{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000785#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000786 if (libxml_is_threaded == -1)
787 xmlInitThreads();
788 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000789 return (1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000790 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000791#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000792 xmlOnceInit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000793#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000794 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000795#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000796
Daniel Veillard6f350292001-10-14 09:56:15 +0000797#ifdef DEBUG_THREADS
798 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
799#endif
800#ifdef HAVE_PTHREAD_H
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000801 return (mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000802#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000803 return (mainthread == GetCurrentThreadId());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000804#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000805 return (mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000806#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000807 return (1);
Daniel Veillard6f350292001-10-14 09:56:15 +0000808#endif
809}
810
811/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000812 * xmlLockLibrary:
813 *
814 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
815 * library.
816 */
817void
818xmlLockLibrary(void)
819{
Daniel Veillard6f350292001-10-14 09:56:15 +0000820#ifdef DEBUG_THREADS
821 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
822#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000823 xmlRMutexLock(xmlLibraryLock);
824}
825
826/**
827 * xmlUnlockLibrary:
828 *
829 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
830 * library.
831 */
832void
833xmlUnlockLibrary(void)
834{
Daniel Veillard6f350292001-10-14 09:56:15 +0000835#ifdef DEBUG_THREADS
836 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
837#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000838 xmlRMutexUnlock(xmlLibraryLock);
839}
840
841/**
842 * xmlInitThreads:
843 *
844 * xmlInitThreads() is used to to initialize all the thread related
845 * data of the libxml2 library.
846 */
847void
848xmlInitThreads(void)
849{
Daniel Veillard6f350292001-10-14 09:56:15 +0000850#ifdef DEBUG_THREADS
851 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
852#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000853#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000854 InitializeCriticalSection(&cleanup_helpers_cs);
855#endif
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000856#ifdef HAVE_PTHREAD_H
857 if (libxml_is_threaded == -1) {
858 if ((pthread_once != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000859 (pthread_getspecific != NULL) &&
860 (pthread_setspecific != NULL) &&
861 (pthread_key_create != NULL) &&
862 (pthread_mutex_init != NULL) &&
863 (pthread_mutex_destroy != NULL) &&
864 (pthread_mutex_lock != NULL) &&
865 (pthread_mutex_unlock != NULL) &&
866 (pthread_cond_init != NULL) &&
Daniel Veillard2cba4152008-08-27 11:45:41 +0000867 (pthread_cond_destroy != NULL) &&
868 (pthread_cond_wait != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000869 (pthread_equal != NULL) &&
870 (pthread_self != NULL) &&
871 (pthread_cond_signal != NULL)) {
872 libxml_is_threaded = 1;
873
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000874/* fprintf(stderr, "Running multithreaded\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000875 } else {
876
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000877/* fprintf(stderr, "Running without multithread\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000878 libxml_is_threaded = 0;
879 }
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000880 }
881#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000882}
883
884/**
885 * xmlCleanupThreads:
886 *
887 * xmlCleanupThreads() is used to to cleanup all the thread related
888 * data of the libxml2 library once processing has ended.
889 */
890void
891xmlCleanupThreads(void)
892{
Daniel Veillard6f350292001-10-14 09:56:15 +0000893#ifdef DEBUG_THREADS
894 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
895#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000896#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000897 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000898 xmlGlobalStateCleanupHelperParams *p;
899
900 EnterCriticalSection(&cleanup_helpers_cs);
901 p = cleanup_helpers_head;
902 while (p != NULL) {
903 xmlGlobalStateCleanupHelperParams *temp = p;
904
905 p = p->next;
906 xmlFreeGlobalState(temp->memory);
907 free(temp);
908 }
909 cleanup_helpers_head = 0;
910 LeaveCriticalSection(&cleanup_helpers_cs);
911 TlsFree(globalkey);
912 globalkey = TLS_OUT_OF_INDEXES;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000913 }
914 DeleteCriticalSection(&cleanup_helpers_cs);
915#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000916}
Daniel Veillard6f350292001-10-14 09:56:15 +0000917
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000918#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000919
Daniel Veillarde28313b2001-12-06 14:08:31 +0000920/**
921 * xmlOnceInit
922 *
923 * xmlOnceInit() is used to initialize the value of mainthread for use
924 * in other routines. This function should only be called using
925 * pthread_once() in association with the once_control variable to ensure
926 * that the function is only called once. See man pthread_once for more
927 * details.
928 */
929static void
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000930xmlOnceInit(void)
931{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000932#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000933 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000934 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000935#endif
936
937#if defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000938 if (!run_once.done) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000939 if (InterlockedIncrement(&run_once.control) == 1) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000940#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000941 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000942#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000943 mainthread = GetCurrentThreadId();
944 run_once.done = 1;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000945 } else {
Daniel Veillard62121e22005-02-24 15:38:52 +0000946 /* Another thread is working; give up our slice and
947 * wait until they're done. */
948 while (!run_once.done)
949 Sleep(0);
950 }
951 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000952#endif
Daniel Veillard82cb3192003-10-29 13:39:15 +0000953
954#ifdef HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000955 if (atomic_add(&run_once_init, 1) == 0) {
956 globalkey = tls_allocate();
957 tls_set(globalkey, NULL);
958 mainthread = find_thread(NULL);
959 } else
960 atomic_add(&run_once_init, -1);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000961#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000962}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000963#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000964
965/**
William M. Brack7a821652003-08-15 07:27:40 +0000966 * DllMain:
967 * @hinstDLL: handle to DLL instance
968 * @fdwReason: Reason code for entry
969 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000970 *
971 * Entry point for Windows library. It is being used to free thread-specific
972 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000973 *
974 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000975 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000976#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
977#if defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000978BOOL XMLCALL
979xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000980#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000981BOOL WINAPI
982DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000983#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000984{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000985 switch (fdwReason) {
986 case DLL_THREAD_DETACH:
987 if (globalkey != TLS_OUT_OF_INDEXES) {
988 xmlGlobalState *globalval = NULL;
989 xmlGlobalStateCleanupHelperParams *p =
990 (xmlGlobalStateCleanupHelperParams *)
991 TlsGetValue(globalkey);
992 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
993 if (globalval) {
994 xmlFreeGlobalState(globalval);
995 TlsSetValue(globalkey, NULL);
996 }
997 if (p) {
998 EnterCriticalSection(&cleanup_helpers_cs);
999 if (p == cleanup_helpers_head)
1000 cleanup_helpers_head = p->next;
1001 else
1002 p->prev->next = p->next;
1003 if (p->next != NULL)
1004 p->next->prev = p->prev;
1005 LeaveCriticalSection(&cleanup_helpers_cs);
1006 free(p);
1007 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001008 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001009 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001010 }
1011 return TRUE;
1012}
1013#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001014#define bottom_threads
1015#include "elfgcchack.h"