blob: f2f2703c4f681eaa81737c5855f9308db4172911 [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>
Eric Zurcher243b0342009-10-01 00:13:07 +020029#elif defined HAVE_WIN32_THREADS
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000030#include <windows.h>
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000031#ifndef HAVE_COMPILER_TLS
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000032#include <process.h>
33#endif
34#endif
Daniel Veillardb8478642001-10-12 17:29:10 +000035
Daniel Veillard82cb3192003-10-29 13:39:15 +000036#ifdef HAVE_BEOS_THREADS
37#include <OS.h>
38#include <TLS.h>
39#endif
40
Daniel Veillardb8478642001-10-12 17:29:10 +000041#if defined(SOLARIS)
42#include <note.h>
43#endif
44
Daniel Veillard6f350292001-10-14 09:56:15 +000045/* #define DEBUG_THREADS */
Daniel Veillardb8478642001-10-12 17:29:10 +000046
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000047#ifdef HAVE_PTHREAD_H
48
49static int libxml_is_threaded = -1;
50#ifdef __GNUC__
51#ifdef linux
52#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
53extern int pthread_once (pthread_once_t *__once_control,
54 void (*__init_routine) (void))
55 __attribute((weak));
56extern void *pthread_getspecific (pthread_key_t __key)
57 __attribute((weak));
58extern int pthread_setspecific (pthread_key_t __key,
59 __const void *__pointer)
60 __attribute((weak));
61extern int pthread_key_create (pthread_key_t *__key,
62 void (*__destr_function) (void *))
63 __attribute((weak));
Daniel Veillardd4a3f242009-01-18 15:41:30 +000064extern int pthread_key_delete (pthread_key_t __key)
65 __attribute((weak));
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000066extern 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));
Daniel Veillardd4a3f242009-01-18 15:41:30 +000086extern int pthread_key_delete ()
87 __attribute((weak));
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000088extern int pthread_cond_signal ()
89 __attribute((weak));
90#endif
91#endif /* linux */
92#endif /* __GNUC__ */
93#endif /* HAVE_PTHREAD_H */
94
Daniel Veillardb8478642001-10-12 17:29:10 +000095/*
96 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
97 * to avoid some crazyness since xmlMalloc/xmlFree may actually
98 * be hosted on allocated blocks needing them for the allocation ...
99 */
100
101/*
102 * xmlMutex are a simple mutual exception locks
103 */
104struct _xmlMutex {
105#ifdef HAVE_PTHREAD_H
106 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000107#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000108 HANDLE mutex;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000109#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000110 sem_id sem;
111 thread_id tid;
Daniel Veillardb8478642001-10-12 17:29:10 +0000112#else
113 int empty;
114#endif
115};
116
117/*
118 * xmlRMutex are reentrant mutual exception locks
119 */
120struct _xmlRMutex {
121#ifdef HAVE_PTHREAD_H
122 pthread_mutex_t lock;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000123 unsigned int held;
124 unsigned int waiters;
125 pthread_t tid;
126 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000127#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000128 CRITICAL_SECTION cs;
129 unsigned int count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000130#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000131 xmlMutexPtr lock;
132 thread_id tid;
133 int32 count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000134#else
135 int empty;
136#endif
137};
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000138
Daniel Veillardb8478642001-10-12 17:29:10 +0000139/*
140 * This module still has some internal static data.
141 * - xmlLibraryLock a global lock
142 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +0000143 */
Daniel Veillard6f350292001-10-14 09:56:15 +0000144
Daniel Veillardb8478642001-10-12 17:29:10 +0000145#ifdef HAVE_PTHREAD_H
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000146static pthread_key_t globalkey;
147static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +0000148static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Friedrich Haubensak3f6cfbd2012-09-12 17:34:53 +0200149static pthread_once_t once_control_init = PTHREAD_ONCE_INIT;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000150static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000151#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000152#if defined(HAVE_COMPILER_TLS)
153static __declspec(thread) xmlGlobalState tlstate;
154static __declspec(thread) int tlstate_inited = 0;
155#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000156static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000157#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000158static DWORD mainthread;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000159static struct {
Daniel Veillard36616dd2005-02-25 07:31:49 +0000160 DWORD done;
161 DWORD control;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000162} run_once = { 0, 0};
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000163static volatile LPCRITICAL_SECTION global_init_lock = NULL;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000164
Daniel Veillard82cb3192003-10-29 13:39:15 +0000165/* endif HAVE_WIN32_THREADS */
166#elif defined HAVE_BEOS_THREADS
167int32 globalkey = 0;
168thread_id mainthread = 0;
169int32 run_once_init = 0;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000170static int32 global_init_lock = -1;
171static vint32 global_init_count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000172#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000173
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000174static xmlRMutexPtr xmlLibraryLock = NULL;
175
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000176#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000177static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000178#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000179
180/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000181 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000182 *
183 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
184 * synchronizing access to data.
185 *
186 * Returns a new simple mutex pointer or NULL in case of error
187 */
188xmlMutexPtr
189xmlNewMutex(void)
190{
191 xmlMutexPtr tok;
192
193 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
194 return (NULL);
195#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000196 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000197 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000198#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000199 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000200#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000201 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
202 free(tok);
203 return NULL;
204 }
205 tok->tid = -1;
Daniel Veillardb8478642001-10-12 17:29:10 +0000206#endif
207 return (tok);
208}
209
210/**
211 * xmlFreeMutex:
212 * @tok: the simple mutex
213 *
214 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
215 * struct.
216 */
217void
218xmlFreeMutex(xmlMutexPtr tok)
219{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000220 if (tok == NULL)
221 return;
Daniel Veillarddf101d82003-07-08 14:03:36 +0000222
Daniel Veillardb8478642001-10-12 17:29:10 +0000223#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000224 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000225 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000226#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000227 CloseHandle(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000228#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000229 delete_sem(tok->sem);
Daniel Veillardb8478642001-10-12 17:29:10 +0000230#endif
231 free(tok);
232}
233
234/**
235 * xmlMutexLock:
236 * @tok: the simple mutex
237 *
238 * xmlMutexLock() is used to lock a libxml2 token.
239 */
240void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000241xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000242{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000243 if (tok == NULL)
244 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000245#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000246 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000247 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000248#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000249 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000250#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000251 if (acquire_sem(tok->sem) != B_NO_ERROR) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000252#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000253 xmlGenericError(xmlGenericErrorContext,
254 "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
Daniel Veillard82cb3192003-10-29 13:39:15 +0000255#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000256 }
257 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000258#endif
259
260}
261
262/**
263 * xmlMutexUnlock:
264 * @tok: the simple mutex
265 *
266 * xmlMutexUnlock() is used to unlock a libxml2 token.
267 */
268void
Daniel Veillard5805be22003-08-28 08:03:23 +0000269xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000270{
Daniel Veillard5805be22003-08-28 08:03:23 +0000271 if (tok == NULL)
272 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000273#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000274 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000275 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000276#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000277 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000278#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000279 if (tok->tid == find_thread(NULL)) {
280 tok->tid = -1;
281 release_sem(tok->sem);
282 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000283#endif
284}
285
286/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000287 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000288 *
289 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
290 * synchronizing access to data. token_r is a re-entrant lock and thus useful
291 * for synchronizing access to data structures that may be manipulated in a
292 * recursive fashion.
293 *
294 * Returns the new reentrant mutex pointer or NULL in case of error
295 */
296xmlRMutexPtr
297xmlNewRMutex(void)
298{
299 xmlRMutexPtr tok;
300
301 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
302 return (NULL);
303#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000304 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000305 pthread_mutex_init(&tok->lock, NULL);
306 tok->held = 0;
307 tok->waiters = 0;
308 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000309 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000310#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000311 InitializeCriticalSection(&tok->cs);
312 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000313#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000314 if ((tok->lock = xmlNewMutex()) == NULL) {
315 free(tok);
316 return NULL;
317 }
318 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000319#endif
320 return (tok);
321}
322
323/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000324 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000325 * @tok: the reentrant mutex
326 *
327 * xmlRFreeMutex() is used to reclaim resources associated with a
328 * reentrant mutex.
329 */
330void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000331xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000332{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000333 if (tok == NULL)
334 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000335#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000336 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000337 pthread_mutex_destroy(&tok->lock);
338 pthread_cond_destroy(&tok->cv);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000339 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000340#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000341 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000342#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000343 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000344#endif
345 free(tok);
346}
347
348/**
349 * xmlRMutexLock:
350 * @tok: the reentrant mutex
351 *
352 * xmlRMutexLock() is used to lock a libxml2 token_r.
353 */
354void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000355xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000356{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000357 if (tok == NULL)
358 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000359#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000360 if (libxml_is_threaded == 0)
361 return;
362
Daniel Veillardb8478642001-10-12 17:29:10 +0000363 pthread_mutex_lock(&tok->lock);
364 if (tok->held) {
365 if (pthread_equal(tok->tid, pthread_self())) {
366 tok->held++;
367 pthread_mutex_unlock(&tok->lock);
368 return;
369 } else {
370 tok->waiters++;
371 while (tok->held)
372 pthread_cond_wait(&tok->cv, &tok->lock);
373 tok->waiters--;
374 }
375 }
376 tok->tid = pthread_self();
377 tok->held = 1;
378 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000379#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000380 EnterCriticalSection(&tok->cs);
381 ++tok->count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000382#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000383 if (tok->lock->tid == find_thread(NULL)) {
384 tok->count++;
385 return;
386 } else {
387 xmlMutexLock(tok->lock);
388 tok->count = 1;
389 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000390#endif
391}
392
393/**
394 * xmlRMutexUnlock:
395 * @tok: the reentrant mutex
396 *
397 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
398 */
399void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000400xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000401{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000402 if (tok == NULL)
403 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000404#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000405 if (libxml_is_threaded == 0)
406 return;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000407
Daniel Veillardb8478642001-10-12 17:29:10 +0000408 pthread_mutex_lock(&tok->lock);
409 tok->held--;
410 if (tok->held == 0) {
411 if (tok->waiters)
412 pthread_cond_signal(&tok->cv);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200413 memset(&tok->tid, 0, sizeof(tok->tid));
Daniel Veillardb8478642001-10-12 17:29:10 +0000414 }
415 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000416#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000417 if (!--tok->count)
418 LeaveCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000419#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000420 if (tok->lock->tid == find_thread(NULL)) {
421 tok->count--;
422 if (tok->count == 0) {
423 xmlMutexUnlock(tok->lock);
424 }
425 return;
426 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000427#endif
428}
429
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000430/**
431 * xmlGlobalInitMutexLock
432 *
433 * Makes sure that the global initialization mutex is initialized and
434 * locks it.
435 */
436void
437__xmlGlobalInitMutexLock(void)
438{
439 /* Make sure the global init lock is initialized and then lock it. */
440#ifdef HAVE_PTHREAD_H
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000441 /* The mutex is statically initialized, so we just lock it. */
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800442 if (pthread_mutex_lock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200443 pthread_mutex_lock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000444#elif defined HAVE_WIN32_THREADS
445 LPCRITICAL_SECTION cs;
446
447 /* Create a new critical section */
448 if (global_init_lock == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000449 cs = malloc(sizeof(CRITICAL_SECTION));
450 if (cs == NULL) {
451 xmlGenericError(xmlGenericErrorContext,
452 "xmlGlobalInitMutexLock: out of memory\n");
453 return;
454 }
455 InitializeCriticalSection(cs);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000456
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000457 /* Swap it into the global_init_lock */
Rob Richardse967f0b2007-06-08 19:36:04 +0000458#ifdef InterlockedCompareExchangePointer
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000459 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
460#else /* Use older void* version */
461 InterlockedCompareExchange((void **) &global_init_lock,
462 (void *) cs, NULL);
Rob Richardse967f0b2007-06-08 19:36:04 +0000463#endif /* InterlockedCompareExchangePointer */
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000464
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000465 /* If another thread successfully recorded its critical
466 * section in the global_init_lock then discard the one
467 * allocated by this thread. */
468 if (global_init_lock != cs) {
469 DeleteCriticalSection(cs);
470 free(cs);
471 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000472 }
473
474 /* Lock the chosen critical section */
475 EnterCriticalSection(global_init_lock);
476#elif defined HAVE_BEOS_THREADS
477 int32 sem;
478
479 /* Allocate a new semaphore */
480 sem = create_sem(1, "xmlGlobalinitMutex");
481
482 while (global_init_lock == -1) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000483 if (atomic_add(&global_init_count, 1) == 0) {
484 global_init_lock = sem;
485 } else {
486 snooze(1);
487 atomic_add(&global_init_count, -1);
488 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000489 }
490
491 /* If another thread successfully recorded its critical
492 * section in the global_init_lock then discard the one
493 * allocated by this thread. */
494 if (global_init_lock != sem)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000495 delete_sem(sem);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000496
497 /* Acquire the chosen semaphore */
498 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
499#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000500 xmlGenericError(xmlGenericErrorContext,
501 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000502#endif
503 }
504#endif
505}
506
507void
508__xmlGlobalInitMutexUnlock(void)
509{
510#ifdef HAVE_PTHREAD_H
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800511 if (pthread_mutex_unlock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200512 pthread_mutex_unlock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000513#elif defined HAVE_WIN32_THREADS
Daniel Veillard14d465d2008-03-24 11:12:55 +0000514 if (global_init_lock != NULL) {
515 LeaveCriticalSection(global_init_lock);
516 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000517#elif defined HAVE_BEOS_THREADS
518 release_sem(global_init_lock);
519#endif
520}
521
Rob Richards91eb5602007-11-16 10:54:59 +0000522/**
523 * xmlGlobalInitMutexDestroy
524 *
525 * Makes sure that the global initialization mutex is destroyed before
526 * application termination.
527 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000528void
529__xmlGlobalInitMutexDestroy(void)
Rob Richards91eb5602007-11-16 10:54:59 +0000530{
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200531#ifdef HAVE_PTHREAD_H
532#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000533 if (global_init_lock != NULL) {
534 DeleteCriticalSection(global_init_lock);
535 free(global_init_lock);
536 global_init_lock = NULL;
Rob Richards91eb5602007-11-16 10:54:59 +0000537 }
538#endif
539}
540
Daniel Veillardb8478642001-10-12 17:29:10 +0000541/************************************************************************
542 * *
543 * Per thread global state handling *
544 * *
545 ************************************************************************/
546
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000547#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000548#ifdef xmlLastError
549#undef xmlLastError
550#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000551
Daniel Veillardb8478642001-10-12 17:29:10 +0000552/**
553 * xmlFreeGlobalState:
554 * @state: a thread global state
555 *
556 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
557 * global state. It is is used here to reclaim memory resources.
558 */
559static void
560xmlFreeGlobalState(void *state)
561{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000562 xmlGlobalState *gs = (xmlGlobalState *) state;
563
564 /* free any memory allocated in the thread's xmlLastError */
565 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000566 free(state);
567}
568
569/**
570 * xmlNewGlobalState:
571 *
572 * xmlNewGlobalState() allocates a global state. This structure is used to
573 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000574 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000575 *
576 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
577 */
578static xmlGlobalStatePtr
579xmlNewGlobalState(void)
580{
581 xmlGlobalState *gs;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000582
Daniel Veillardb8478642001-10-12 17:29:10 +0000583 gs = malloc(sizeof(xmlGlobalState));
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000584 if (gs == NULL) {
585 xmlGenericError(xmlGenericErrorContext,
586 "xmlGetGlobalState: out of memory\n");
587 return (NULL);
588 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000589
William M. Brack8b2c7f12002-11-22 05:07:29 +0000590 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000591 xmlInitializeGlobalState(gs);
592 return (gs);
593}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000594#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000595
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200596#ifdef HAVE_PTHREAD_H
Eric Zurcher243b0342009-10-01 00:13:07 +0200597#elif defined HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000598#if !defined(HAVE_COMPILER_TLS)
599#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000600typedef struct _xmlGlobalStateCleanupHelperParams {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000601 HANDLE thread;
602 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000603} xmlGlobalStateCleanupHelperParams;
604
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000605static void XMLCDECL
606xmlGlobalStateCleanupHelper(void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000607{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000608 xmlGlobalStateCleanupHelperParams *params =
609 (xmlGlobalStateCleanupHelperParams *) p;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000610 WaitForSingleObject(params->thread, INFINITE);
611 CloseHandle(params->thread);
612 xmlFreeGlobalState(params->memory);
613 free(params);
614 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000615}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000616#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
617
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000618typedef struct _xmlGlobalStateCleanupHelperParams {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000619 void *memory;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000620 struct _xmlGlobalStateCleanupHelperParams *prev;
621 struct _xmlGlobalStateCleanupHelperParams *next;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000622} xmlGlobalStateCleanupHelperParams;
623
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000624static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000625static CRITICAL_SECTION cleanup_helpers_cs;
626
627#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
628#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000629#endif /* HAVE_WIN32_THREADS */
630
Daniel Veillard82cb3192003-10-29 13:39:15 +0000631#if defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000632
William M. Brackb1d53162003-11-18 06:54:40 +0000633/**
634 * xmlGlobalStateCleanup:
635 * @data: unused parameter
636 *
637 * Used for Beos only
638 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000639void
640xmlGlobalStateCleanup(void *data)
Daniel Veillard82cb3192003-10-29 13:39:15 +0000641{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000642 void *globalval = tls_get(globalkey);
643
644 if (globalval != NULL)
645 xmlFreeGlobalState(globalval);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000646}
647#endif
648
Daniel Veillard01c13b52002-12-10 15:19:08 +0000649/**
650 * xmlGetGlobalState:
651 *
652 * xmlGetGlobalState() is called to retrieve the global state for a thread.
653 *
654 * Returns the thread global state or NULL in case of error
655 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000656xmlGlobalStatePtr
657xmlGetGlobalState(void)
658{
659#ifdef HAVE_PTHREAD_H
660 xmlGlobalState *globalval;
661
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000662 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000663 return (NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000664
Daniel Veillarde28313b2001-12-06 14:08:31 +0000665 pthread_once(&once_control, xmlOnceInit);
666
Daniel Veillardb8478642001-10-12 17:29:10 +0000667 if ((globalval = (xmlGlobalState *)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000668 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000669 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000670 if (tsd == NULL)
671 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000672
673 pthread_setspecific(globalkey, tsd);
674 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000675 }
676 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000677#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000678#if defined(HAVE_COMPILER_TLS)
679 if (!tlstate_inited) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000680 tlstate_inited = 1;
681 xmlInitializeGlobalState(&tlstate);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000682 }
683 return &tlstate;
684#else /* HAVE_COMPILER_TLS */
685 xmlGlobalState *globalval;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000686 xmlGlobalStateCleanupHelperParams *p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000687
Daniel Veillard62121e22005-02-24 15:38:52 +0000688 xmlOnceInit();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000689#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000690 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000691#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000692 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
693 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000694#endif
695 if (globalval == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000696 xmlGlobalState *tsd = xmlNewGlobalState();
697
698 if (tsd == NULL)
699 return(NULL);
700 p = (xmlGlobalStateCleanupHelperParams *)
701 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
702 if (p == NULL) {
703 xmlGenericError(xmlGenericErrorContext,
704 "xmlGetGlobalState: out of memory\n");
Daniel Veillardbf2ebff2009-01-18 14:57:04 +0000705 xmlFreeGlobalState(tsd);
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000706 return(NULL);
707 }
708 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000709#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000710 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
711 GetCurrentProcess(), &p->thread, 0, TRUE,
712 DUPLICATE_SAME_ACCESS);
713 TlsSetValue(globalkey, tsd);
714 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000715#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000716 EnterCriticalSection(&cleanup_helpers_cs);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000717 if (cleanup_helpers_head != NULL) {
718 cleanup_helpers_head->prev = p;
719 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000720 p->next = cleanup_helpers_head;
721 p->prev = NULL;
722 cleanup_helpers_head = p;
723 TlsSetValue(globalkey, p);
724 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000725#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000726
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000727 return (tsd);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000728 }
729 return (globalval);
730#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000731#elif defined HAVE_BEOS_THREADS
732 xmlGlobalState *globalval;
733
734 xmlOnceInit();
735
Daniel Veillard14d465d2008-03-24 11:12:55 +0000736 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000737 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000738 if (tsd == NULL)
739 return (NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000740
741 tls_set(globalkey, tsd);
742 on_exit_thread(xmlGlobalStateCleanup, NULL);
743 return (tsd);
744 }
745 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000746#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000747 return (NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000748#endif
749}
750
Daniel Veillardb8478642001-10-12 17:29:10 +0000751/************************************************************************
752 * *
753 * Library wide thread interfaces *
754 * *
755 ************************************************************************/
756
757/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000758 * xmlGetThreadId:
759 *
760 * xmlGetThreadId() find the current thread ID number
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200761 * Note that this is likely to be broken on some platforms using pthreads
762 * as the specification doesn't mandate pthread_t to be an integer type
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000763 *
764 * Returns the current thread ID number
765 */
766int
767xmlGetThreadId(void)
768{
769#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200770 pthread_t id;
771 int ret;
772
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000773 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000774 return (0);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200775 id = pthread_self();
776 /* horrible but preserves compat, see warning above */
777 memcpy(&ret, &id, sizeof(ret));
778 return (ret);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000779#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000780 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000781#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000782 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000783#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000784 return ((int) 0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000785#endif
786}
787
788/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000789 * xmlIsMainThread:
790 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000791 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000792 *
793 * Returns 1 if the current thread is the main thread, 0 otherwise
794 */
795int
796xmlIsMainThread(void)
797{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000798#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000799 if (libxml_is_threaded == -1)
800 xmlInitThreads();
801 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000802 return (1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000803 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000804#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000805 xmlOnceInit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000806#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000807 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000808#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000809
Daniel Veillard6f350292001-10-14 09:56:15 +0000810#ifdef DEBUG_THREADS
811 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
812#endif
813#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200814 return (pthread_equal(mainthread,pthread_self()));
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000815#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000816 return (mainthread == GetCurrentThreadId());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000817#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000818 return (mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000819#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000820 return (1);
Daniel Veillard6f350292001-10-14 09:56:15 +0000821#endif
822}
823
824/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000825 * xmlLockLibrary:
826 *
827 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
828 * library.
829 */
830void
831xmlLockLibrary(void)
832{
Daniel Veillard6f350292001-10-14 09:56:15 +0000833#ifdef DEBUG_THREADS
834 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
835#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000836 xmlRMutexLock(xmlLibraryLock);
837}
838
839/**
840 * xmlUnlockLibrary:
841 *
842 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
843 * library.
844 */
845void
846xmlUnlockLibrary(void)
847{
Daniel Veillard6f350292001-10-14 09:56:15 +0000848#ifdef DEBUG_THREADS
849 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
850#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000851 xmlRMutexUnlock(xmlLibraryLock);
852}
853
854/**
855 * xmlInitThreads:
856 *
857 * xmlInitThreads() is used to to initialize all the thread related
858 * data of the libxml2 library.
859 */
860void
861xmlInitThreads(void)
862{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000863#ifdef HAVE_PTHREAD_H
864 if (libxml_is_threaded == -1) {
865 if ((pthread_once != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000866 (pthread_getspecific != NULL) &&
867 (pthread_setspecific != NULL) &&
868 (pthread_key_create != NULL) &&
Daniel Veillardd4a3f242009-01-18 15:41:30 +0000869 (pthread_key_delete != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000870 (pthread_mutex_init != NULL) &&
871 (pthread_mutex_destroy != NULL) &&
872 (pthread_mutex_lock != NULL) &&
873 (pthread_mutex_unlock != NULL) &&
874 (pthread_cond_init != NULL) &&
Daniel Veillard2cba4152008-08-27 11:45:41 +0000875 (pthread_cond_destroy != NULL) &&
876 (pthread_cond_wait != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000877 (pthread_equal != NULL) &&
878 (pthread_self != NULL) &&
879 (pthread_cond_signal != NULL)) {
880 libxml_is_threaded = 1;
881
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000882/* fprintf(stderr, "Running multithreaded\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000883 } else {
884
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000885/* fprintf(stderr, "Running without multithread\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000886 libxml_is_threaded = 0;
887 }
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000888 }
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200889#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
890 InitializeCriticalSection(&cleanup_helpers_cs);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000891#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000892}
893
894/**
895 * xmlCleanupThreads:
896 *
897 * xmlCleanupThreads() is used to to cleanup all the thread related
898 * data of the libxml2 library once processing has ended.
Daniel Veillard01101202009-02-21 09:22:04 +0000899 *
900 * WARNING: if your application is multithreaded or has plugin support
901 * calling this may crash the application if another thread or
902 * a plugin is still using libxml2. It's sometimes very hard to
903 * guess if libxml2 is in use in the application, some libraries
904 * or plugins may use it without notice. In case of doubt abstain
905 * from calling this function or do it just before calling exit()
906 * to avoid leak reports from valgrind !
Daniel Veillardb8478642001-10-12 17:29:10 +0000907 */
908void
909xmlCleanupThreads(void)
910{
Daniel Veillard6f350292001-10-14 09:56:15 +0000911#ifdef DEBUG_THREADS
912 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
913#endif
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200914#ifdef HAVE_PTHREAD_H
915 if ((libxml_is_threaded) && (pthread_key_delete != NULL))
916 pthread_key_delete(globalkey);
Friedrich Haubensak3f6cfbd2012-09-12 17:34:53 +0200917 once_control = once_control_init;
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200918#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000919 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000920 xmlGlobalStateCleanupHelperParams *p;
921
922 EnterCriticalSection(&cleanup_helpers_cs);
923 p = cleanup_helpers_head;
924 while (p != NULL) {
925 xmlGlobalStateCleanupHelperParams *temp = p;
926
927 p = p->next;
928 xmlFreeGlobalState(temp->memory);
929 free(temp);
930 }
931 cleanup_helpers_head = 0;
932 LeaveCriticalSection(&cleanup_helpers_cs);
933 TlsFree(globalkey);
934 globalkey = TLS_OUT_OF_INDEXES;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000935 }
936 DeleteCriticalSection(&cleanup_helpers_cs);
937#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000938}
Daniel Veillard6f350292001-10-14 09:56:15 +0000939
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000940#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000941
Daniel Veillarde28313b2001-12-06 14:08:31 +0000942/**
943 * xmlOnceInit
944 *
945 * xmlOnceInit() is used to initialize the value of mainthread for use
946 * in other routines. This function should only be called using
947 * pthread_once() in association with the once_control variable to ensure
948 * that the function is only called once. See man pthread_once for more
949 * details.
950 */
951static void
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000952xmlOnceInit(void)
953{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000954#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000955 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000956 mainthread = pthread_self();
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800957 __xmlInitializeDict();
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200958#elif defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000959 if (!run_once.done) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000960 if (InterlockedIncrement(&run_once.control) == 1) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000961#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000962 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000963#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000964 mainthread = GetCurrentThreadId();
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800965 __xmlInitializeDict();
Daniel Veillard62121e22005-02-24 15:38:52 +0000966 run_once.done = 1;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000967 } else {
Daniel Veillard62121e22005-02-24 15:38:52 +0000968 /* Another thread is working; give up our slice and
969 * wait until they're done. */
970 while (!run_once.done)
971 Sleep(0);
972 }
973 }
Eric Zurcher243b0342009-10-01 00:13:07 +0200974#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000975 if (atomic_add(&run_once_init, 1) == 0) {
976 globalkey = tls_allocate();
977 tls_set(globalkey, NULL);
978 mainthread = find_thread(NULL);
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800979 __xmlInitializeDict();
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000980 } else
981 atomic_add(&run_once_init, -1);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000982#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000983}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000984#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000985
986/**
William M. Brack7a821652003-08-15 07:27:40 +0000987 * DllMain:
988 * @hinstDLL: handle to DLL instance
989 * @fdwReason: Reason code for entry
990 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000991 *
992 * Entry point for Windows library. It is being used to free thread-specific
993 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000994 *
995 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000996 */
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200997#ifdef HAVE_PTHREAD_H
998#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000999#if defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001000BOOL XMLCALL
1001xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001002#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001003BOOL WINAPI
1004DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001005#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001006{
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001007 switch (fdwReason) {
1008 case DLL_THREAD_DETACH:
1009 if (globalkey != TLS_OUT_OF_INDEXES) {
1010 xmlGlobalState *globalval = NULL;
1011 xmlGlobalStateCleanupHelperParams *p =
1012 (xmlGlobalStateCleanupHelperParams *)
1013 TlsGetValue(globalkey);
1014 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
1015 if (globalval) {
1016 xmlFreeGlobalState(globalval);
1017 TlsSetValue(globalkey, NULL);
1018 }
1019 if (p) {
1020 EnterCriticalSection(&cleanup_helpers_cs);
1021 if (p == cleanup_helpers_head)
1022 cleanup_helpers_head = p->next;
1023 else
1024 p->prev->next = p->next;
1025 if (p->next != NULL)
1026 p->next->prev = p->prev;
1027 LeaveCriticalSection(&cleanup_helpers_cs);
1028 free(p);
1029 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001030 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001031 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001032 }
1033 return TRUE;
1034}
1035#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001036#define bottom_threads
1037#include "elfgcchack.h"