blob: 8c572634e93a18f0c2ae5e3ccab4511e25281ca3 [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;
Michael Heimpoldfff8a6b2014-12-22 11:12:12 +080050#if defined(__GNUC__) && defined(__GLIBC__)
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000051#ifdef linux
52#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
Nick Wellnhofer1f09aea2017-06-17 15:05:34 +020053#pragma weak pthread_once
54#pragma weak pthread_getspecific
55#pragma weak pthread_setspecific
56#pragma weak pthread_key_create
57#pragma weak pthread_key_delete
58#pragma weak pthread_mutex_init
59#pragma weak pthread_mutex_destroy
60#pragma weak pthread_mutex_lock
61#pragma weak pthread_mutex_unlock
62#pragma weak pthread_cond_init
63#pragma weak pthread_cond_destroy
64#pragma weak pthread_cond_wait
65#pragma weak pthread_equal
66#pragma weak pthread_self
67#pragma weak pthread_key_create
68#pragma weak pthread_key_delete
69#pragma weak pthread_cond_signal
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000070#endif
71#endif /* linux */
Michael Heimpoldfff8a6b2014-12-22 11:12:12 +080072#endif /* defined(__GNUC__) && defined(__GLIBC__) */
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000073#endif /* HAVE_PTHREAD_H */
74
Daniel Veillardb8478642001-10-12 17:29:10 +000075/*
76 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
77 * to avoid some crazyness since xmlMalloc/xmlFree may actually
78 * be hosted on allocated blocks needing them for the allocation ...
79 */
80
81/*
82 * xmlMutex are a simple mutual exception locks
83 */
84struct _xmlMutex {
85#ifdef HAVE_PTHREAD_H
86 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000087#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000088 HANDLE mutex;
Daniel Veillard82cb3192003-10-29 13:39:15 +000089#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +000090 sem_id sem;
91 thread_id tid;
Daniel Veillardb8478642001-10-12 17:29:10 +000092#else
93 int empty;
94#endif
95};
96
97/*
98 * xmlRMutex are reentrant mutual exception locks
99 */
100struct _xmlRMutex {
101#ifdef HAVE_PTHREAD_H
102 pthread_mutex_t lock;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000103 unsigned int held;
104 unsigned int waiters;
105 pthread_t tid;
106 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000107#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000108 CRITICAL_SECTION cs;
109 unsigned int count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000110#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000111 xmlMutexPtr lock;
112 thread_id tid;
113 int32 count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000114#else
115 int empty;
116#endif
117};
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000118
Daniel Veillardb8478642001-10-12 17:29:10 +0000119/*
120 * This module still has some internal static data.
121 * - xmlLibraryLock a global lock
122 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +0000123 */
Daniel Veillard6f350292001-10-14 09:56:15 +0000124
Daniel Veillardb8478642001-10-12 17:29:10 +0000125#ifdef HAVE_PTHREAD_H
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000126static pthread_key_t globalkey;
127static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +0000128static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Friedrich Haubensak3f6cfbd2012-09-12 17:34:53 +0200129static pthread_once_t once_control_init = PTHREAD_ONCE_INIT;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000130static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000131#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000132#if defined(HAVE_COMPILER_TLS)
133static __declspec(thread) xmlGlobalState tlstate;
134static __declspec(thread) int tlstate_inited = 0;
135#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000136static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000137#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000138static DWORD mainthread;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000139static struct {
Daniel Veillard36616dd2005-02-25 07:31:49 +0000140 DWORD done;
141 DWORD control;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000142} run_once = { 0, 0};
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000143static volatile LPCRITICAL_SECTION global_init_lock = NULL;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000144
Daniel Veillard82cb3192003-10-29 13:39:15 +0000145/* endif HAVE_WIN32_THREADS */
146#elif defined HAVE_BEOS_THREADS
147int32 globalkey = 0;
148thread_id mainthread = 0;
149int32 run_once_init = 0;
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000150static int32 global_init_lock = -1;
151static vint32 global_init_count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000152#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000153
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000154static xmlRMutexPtr xmlLibraryLock = NULL;
155
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000156#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000157static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000158#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000159
160/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000161 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000162 *
163 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
164 * synchronizing access to data.
165 *
166 * Returns a new simple mutex pointer or NULL in case of error
167 */
168xmlMutexPtr
169xmlNewMutex(void)
170{
171 xmlMutexPtr tok;
172
173 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
174 return (NULL);
175#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000176 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000177 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000178#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000179 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000180#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000181 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
182 free(tok);
183 return NULL;
184 }
185 tok->tid = -1;
Daniel Veillardb8478642001-10-12 17:29:10 +0000186#endif
187 return (tok);
188}
189
190/**
191 * xmlFreeMutex:
192 * @tok: the simple mutex
193 *
194 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
195 * struct.
196 */
197void
198xmlFreeMutex(xmlMutexPtr tok)
199{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000200 if (tok == NULL)
201 return;
Daniel Veillarddf101d82003-07-08 14:03:36 +0000202
Daniel Veillardb8478642001-10-12 17:29:10 +0000203#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000204 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000205 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000206#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000207 CloseHandle(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000208#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000209 delete_sem(tok->sem);
Daniel Veillardb8478642001-10-12 17:29:10 +0000210#endif
211 free(tok);
212}
213
214/**
215 * xmlMutexLock:
216 * @tok: the simple mutex
217 *
218 * xmlMutexLock() is used to lock a libxml2 token.
219 */
220void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000221xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000222{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000223 if (tok == NULL)
224 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000225#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000226 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000227 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000228#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000229 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000230#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000231 if (acquire_sem(tok->sem) != B_NO_ERROR) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000232#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000233 xmlGenericError(xmlGenericErrorContext,
234 "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
Daniel Veillard82cb3192003-10-29 13:39:15 +0000235#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000236 }
237 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000238#endif
239
240}
241
242/**
243 * xmlMutexUnlock:
244 * @tok: the simple mutex
245 *
246 * xmlMutexUnlock() is used to unlock a libxml2 token.
247 */
248void
Daniel Veillard5805be22003-08-28 08:03:23 +0000249xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000250{
Daniel Veillard5805be22003-08-28 08:03:23 +0000251 if (tok == NULL)
252 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000253#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000254 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000255 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000256#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000257 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000258#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000259 if (tok->tid == find_thread(NULL)) {
260 tok->tid = -1;
261 release_sem(tok->sem);
262 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000263#endif
264}
265
266/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000267 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000268 *
269 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
270 * synchronizing access to data. token_r is a re-entrant lock and thus useful
271 * for synchronizing access to data structures that may be manipulated in a
272 * recursive fashion.
273 *
274 * Returns the new reentrant mutex pointer or NULL in case of error
275 */
276xmlRMutexPtr
277xmlNewRMutex(void)
278{
279 xmlRMutexPtr tok;
280
281 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
282 return (NULL);
283#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000284 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000285 pthread_mutex_init(&tok->lock, NULL);
286 tok->held = 0;
287 tok->waiters = 0;
288 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000289 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000290#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000291 InitializeCriticalSection(&tok->cs);
292 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000293#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000294 if ((tok->lock = xmlNewMutex()) == NULL) {
295 free(tok);
296 return NULL;
297 }
298 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000299#endif
300 return (tok);
301}
302
303/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000304 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000305 * @tok: the reentrant mutex
306 *
307 * xmlRFreeMutex() is used to reclaim resources associated with a
308 * reentrant mutex.
309 */
310void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000311xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000312{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000313 if (tok == NULL)
314 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000315#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000316 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000317 pthread_mutex_destroy(&tok->lock);
318 pthread_cond_destroy(&tok->cv);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000319 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000320#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000321 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000322#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000323 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000324#endif
325 free(tok);
326}
327
328/**
329 * xmlRMutexLock:
330 * @tok: the reentrant mutex
331 *
332 * xmlRMutexLock() is used to lock a libxml2 token_r.
333 */
334void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000335xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000336{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000337 if (tok == NULL)
338 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000339#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000340 if (libxml_is_threaded == 0)
341 return;
342
Daniel Veillardb8478642001-10-12 17:29:10 +0000343 pthread_mutex_lock(&tok->lock);
344 if (tok->held) {
345 if (pthread_equal(tok->tid, pthread_self())) {
346 tok->held++;
347 pthread_mutex_unlock(&tok->lock);
348 return;
349 } else {
350 tok->waiters++;
351 while (tok->held)
352 pthread_cond_wait(&tok->cv, &tok->lock);
353 tok->waiters--;
354 }
355 }
356 tok->tid = pthread_self();
357 tok->held = 1;
358 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000359#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000360 EnterCriticalSection(&tok->cs);
Daniel Veillard8854e462014-10-13 15:03:58 +0800361 tok->count++;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000362#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000363 if (tok->lock->tid == find_thread(NULL)) {
364 tok->count++;
365 return;
366 } else {
367 xmlMutexLock(tok->lock);
368 tok->count = 1;
369 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000370#endif
371}
372
373/**
374 * xmlRMutexUnlock:
375 * @tok: the reentrant mutex
376 *
377 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
378 */
379void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000380xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000381{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000382 if (tok == NULL)
383 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000384#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000385 if (libxml_is_threaded == 0)
386 return;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000387
Daniel Veillardb8478642001-10-12 17:29:10 +0000388 pthread_mutex_lock(&tok->lock);
389 tok->held--;
390 if (tok->held == 0) {
391 if (tok->waiters)
392 pthread_cond_signal(&tok->cv);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200393 memset(&tok->tid, 0, sizeof(tok->tid));
Daniel Veillardb8478642001-10-12 17:29:10 +0000394 }
395 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000396#elif defined HAVE_WIN32_THREADS
Daniel Veillard8854e462014-10-13 15:03:58 +0800397 if (tok->count > 0) {
Daniel Veillard8854e462014-10-13 15:03:58 +0800398 tok->count--;
Steve Nairn620a7062015-03-03 19:40:06 +0800399 LeaveCriticalSection(&tok->cs);
Daniel Veillard8854e462014-10-13 15:03:58 +0800400 }
Daniel Veillard82cb3192003-10-29 13:39:15 +0000401#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000402 if (tok->lock->tid == find_thread(NULL)) {
403 tok->count--;
404 if (tok->count == 0) {
405 xmlMutexUnlock(tok->lock);
406 }
407 return;
408 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000409#endif
410}
411
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000412/**
413 * xmlGlobalInitMutexLock
414 *
415 * Makes sure that the global initialization mutex is initialized and
416 * locks it.
417 */
418void
419__xmlGlobalInitMutexLock(void)
420{
421 /* Make sure the global init lock is initialized and then lock it. */
422#ifdef HAVE_PTHREAD_H
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000423 /* The mutex is statically initialized, so we just lock it. */
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800424 if (pthread_mutex_lock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200425 pthread_mutex_lock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000426#elif defined HAVE_WIN32_THREADS
427 LPCRITICAL_SECTION cs;
428
429 /* Create a new critical section */
430 if (global_init_lock == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000431 cs = malloc(sizeof(CRITICAL_SECTION));
432 if (cs == NULL) {
433 xmlGenericError(xmlGenericErrorContext,
434 "xmlGlobalInitMutexLock: out of memory\n");
435 return;
436 }
437 InitializeCriticalSection(cs);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000438
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000439 /* Swap it into the global_init_lock */
Rob Richardse967f0b2007-06-08 19:36:04 +0000440#ifdef InterlockedCompareExchangePointer
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000441 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
442#else /* Use older void* version */
443 InterlockedCompareExchange((void **) &global_init_lock,
444 (void *) cs, NULL);
Rob Richardse967f0b2007-06-08 19:36:04 +0000445#endif /* InterlockedCompareExchangePointer */
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000446
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000447 /* If another thread successfully recorded its critical
448 * section in the global_init_lock then discard the one
449 * allocated by this thread. */
450 if (global_init_lock != cs) {
451 DeleteCriticalSection(cs);
452 free(cs);
453 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000454 }
455
456 /* Lock the chosen critical section */
457 EnterCriticalSection(global_init_lock);
458#elif defined HAVE_BEOS_THREADS
459 int32 sem;
460
461 /* Allocate a new semaphore */
462 sem = create_sem(1, "xmlGlobalinitMutex");
463
464 while (global_init_lock == -1) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000465 if (atomic_add(&global_init_count, 1) == 0) {
466 global_init_lock = sem;
467 } else {
468 snooze(1);
469 atomic_add(&global_init_count, -1);
470 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000471 }
472
473 /* If another thread successfully recorded its critical
474 * section in the global_init_lock then discard the one
475 * allocated by this thread. */
476 if (global_init_lock != sem)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000477 delete_sem(sem);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000478
479 /* Acquire the chosen semaphore */
480 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
481#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000482 xmlGenericError(xmlGenericErrorContext,
483 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000484#endif
485 }
486#endif
487}
488
489void
490__xmlGlobalInitMutexUnlock(void)
491{
492#ifdef HAVE_PTHREAD_H
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800493 if (pthread_mutex_unlock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200494 pthread_mutex_unlock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000495#elif defined HAVE_WIN32_THREADS
Daniel Veillard14d465d2008-03-24 11:12:55 +0000496 if (global_init_lock != NULL) {
497 LeaveCriticalSection(global_init_lock);
498 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000499#elif defined HAVE_BEOS_THREADS
500 release_sem(global_init_lock);
501#endif
502}
503
Rob Richards91eb5602007-11-16 10:54:59 +0000504/**
505 * xmlGlobalInitMutexDestroy
506 *
507 * Makes sure that the global initialization mutex is destroyed before
508 * application termination.
509 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000510void
511__xmlGlobalInitMutexDestroy(void)
Rob Richards91eb5602007-11-16 10:54:59 +0000512{
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200513#ifdef HAVE_PTHREAD_H
514#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000515 if (global_init_lock != NULL) {
516 DeleteCriticalSection(global_init_lock);
517 free(global_init_lock);
518 global_init_lock = NULL;
Rob Richards91eb5602007-11-16 10:54:59 +0000519 }
520#endif
521}
522
Daniel Veillardb8478642001-10-12 17:29:10 +0000523/************************************************************************
524 * *
525 * Per thread global state handling *
526 * *
527 ************************************************************************/
528
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000529#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000530#ifdef xmlLastError
531#undef xmlLastError
532#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000533
Daniel Veillardb8478642001-10-12 17:29:10 +0000534/**
535 * xmlFreeGlobalState:
536 * @state: a thread global state
537 *
538 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
539 * global state. It is is used here to reclaim memory resources.
540 */
541static void
542xmlFreeGlobalState(void *state)
543{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000544 xmlGlobalState *gs = (xmlGlobalState *) state;
545
546 /* free any memory allocated in the thread's xmlLastError */
547 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000548 free(state);
549}
550
551/**
552 * xmlNewGlobalState:
553 *
554 * xmlNewGlobalState() allocates a global state. This structure is used to
555 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000556 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000557 *
558 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
559 */
560static xmlGlobalStatePtr
561xmlNewGlobalState(void)
562{
563 xmlGlobalState *gs;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000564
Daniel Veillardb8478642001-10-12 17:29:10 +0000565 gs = malloc(sizeof(xmlGlobalState));
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000566 if (gs == NULL) {
567 xmlGenericError(xmlGenericErrorContext,
568 "xmlGetGlobalState: out of memory\n");
569 return (NULL);
570 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000571
William M. Brack8b2c7f12002-11-22 05:07:29 +0000572 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000573 xmlInitializeGlobalState(gs);
574 return (gs);
575}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000576#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000577
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200578#ifdef HAVE_PTHREAD_H
Eric Zurcher243b0342009-10-01 00:13:07 +0200579#elif defined HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000580#if !defined(HAVE_COMPILER_TLS)
581#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000582typedef struct _xmlGlobalStateCleanupHelperParams {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000583 HANDLE thread;
584 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000585} xmlGlobalStateCleanupHelperParams;
586
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000587static void XMLCDECL
588xmlGlobalStateCleanupHelper(void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000589{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000590 xmlGlobalStateCleanupHelperParams *params =
591 (xmlGlobalStateCleanupHelperParams *) p;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000592 WaitForSingleObject(params->thread, INFINITE);
593 CloseHandle(params->thread);
594 xmlFreeGlobalState(params->memory);
595 free(params);
596 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000597}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000598#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
599
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000600typedef struct _xmlGlobalStateCleanupHelperParams {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000601 void *memory;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000602 struct _xmlGlobalStateCleanupHelperParams *prev;
603 struct _xmlGlobalStateCleanupHelperParams *next;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000604} xmlGlobalStateCleanupHelperParams;
605
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000606static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000607static CRITICAL_SECTION cleanup_helpers_cs;
608
609#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
610#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000611#endif /* HAVE_WIN32_THREADS */
612
Daniel Veillard82cb3192003-10-29 13:39:15 +0000613#if defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000614
William M. Brackb1d53162003-11-18 06:54:40 +0000615/**
616 * xmlGlobalStateCleanup:
617 * @data: unused parameter
618 *
619 * Used for Beos only
620 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000621void
622xmlGlobalStateCleanup(void *data)
Daniel Veillard82cb3192003-10-29 13:39:15 +0000623{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000624 void *globalval = tls_get(globalkey);
625
626 if (globalval != NULL)
627 xmlFreeGlobalState(globalval);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000628}
629#endif
630
Daniel Veillard01c13b52002-12-10 15:19:08 +0000631/**
632 * xmlGetGlobalState:
633 *
634 * xmlGetGlobalState() is called to retrieve the global state for a thread.
635 *
636 * Returns the thread global state or NULL in case of error
637 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000638xmlGlobalStatePtr
639xmlGetGlobalState(void)
640{
641#ifdef HAVE_PTHREAD_H
642 xmlGlobalState *globalval;
643
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000644 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000645 return (NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000646
Daniel Veillarde28313b2001-12-06 14:08:31 +0000647 pthread_once(&once_control, xmlOnceInit);
648
Daniel Veillardb8478642001-10-12 17:29:10 +0000649 if ((globalval = (xmlGlobalState *)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000650 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000651 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000652 if (tsd == NULL)
653 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000654
655 pthread_setspecific(globalkey, tsd);
656 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000657 }
658 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000659#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000660#if defined(HAVE_COMPILER_TLS)
661 if (!tlstate_inited) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000662 tlstate_inited = 1;
663 xmlInitializeGlobalState(&tlstate);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000664 }
665 return &tlstate;
666#else /* HAVE_COMPILER_TLS */
667 xmlGlobalState *globalval;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000668 xmlGlobalStateCleanupHelperParams *p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000669
Daniel Veillard62121e22005-02-24 15:38:52 +0000670 xmlOnceInit();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000671#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000672 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000673#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000674 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
675 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000676#endif
677 if (globalval == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000678 xmlGlobalState *tsd = xmlNewGlobalState();
679
680 if (tsd == NULL)
681 return(NULL);
682 p = (xmlGlobalStateCleanupHelperParams *)
683 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
684 if (p == NULL) {
685 xmlGenericError(xmlGenericErrorContext,
686 "xmlGetGlobalState: out of memory\n");
Daniel Veillardbf2ebff2009-01-18 14:57:04 +0000687 xmlFreeGlobalState(tsd);
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000688 return(NULL);
689 }
690 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000691#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000692 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
693 GetCurrentProcess(), &p->thread, 0, TRUE,
694 DUPLICATE_SAME_ACCESS);
695 TlsSetValue(globalkey, tsd);
696 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000697#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000698 EnterCriticalSection(&cleanup_helpers_cs);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000699 if (cleanup_helpers_head != NULL) {
700 cleanup_helpers_head->prev = p;
701 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000702 p->next = cleanup_helpers_head;
703 p->prev = NULL;
704 cleanup_helpers_head = p;
705 TlsSetValue(globalkey, p);
706 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000707#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000708
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000709 return (tsd);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000710 }
711 return (globalval);
712#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000713#elif defined HAVE_BEOS_THREADS
714 xmlGlobalState *globalval;
715
716 xmlOnceInit();
717
Daniel Veillard14d465d2008-03-24 11:12:55 +0000718 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000719 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000720 if (tsd == NULL)
721 return (NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000722
723 tls_set(globalkey, tsd);
724 on_exit_thread(xmlGlobalStateCleanup, NULL);
725 return (tsd);
726 }
727 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000728#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000729 return (NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000730#endif
731}
732
Daniel Veillardb8478642001-10-12 17:29:10 +0000733/************************************************************************
734 * *
735 * Library wide thread interfaces *
736 * *
737 ************************************************************************/
738
739/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000740 * xmlGetThreadId:
741 *
742 * xmlGetThreadId() find the current thread ID number
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200743 * Note that this is likely to be broken on some platforms using pthreads
744 * as the specification doesn't mandate pthread_t to be an integer type
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000745 *
746 * Returns the current thread ID number
747 */
748int
749xmlGetThreadId(void)
750{
751#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200752 pthread_t id;
753 int ret;
754
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000755 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000756 return (0);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200757 id = pthread_self();
758 /* horrible but preserves compat, see warning above */
759 memcpy(&ret, &id, sizeof(ret));
760 return (ret);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000761#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000762 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000763#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000764 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000765#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000766 return ((int) 0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000767#endif
768}
769
770/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000771 * xmlIsMainThread:
772 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000773 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000774 *
775 * Returns 1 if the current thread is the main thread, 0 otherwise
776 */
777int
778xmlIsMainThread(void)
779{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000780#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000781 if (libxml_is_threaded == -1)
782 xmlInitThreads();
783 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000784 return (1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000785 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000786#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000787 xmlOnceInit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000788#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000789 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000790#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000791
Daniel Veillard6f350292001-10-14 09:56:15 +0000792#ifdef DEBUG_THREADS
793 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
794#endif
795#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200796 return (pthread_equal(mainthread,pthread_self()));
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000797#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000798 return (mainthread == GetCurrentThreadId());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000799#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000800 return (mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000801#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000802 return (1);
Daniel Veillard6f350292001-10-14 09:56:15 +0000803#endif
804}
805
806/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000807 * xmlLockLibrary:
808 *
809 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
810 * library.
811 */
812void
813xmlLockLibrary(void)
814{
Daniel Veillard6f350292001-10-14 09:56:15 +0000815#ifdef DEBUG_THREADS
816 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
817#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000818 xmlRMutexLock(xmlLibraryLock);
819}
820
821/**
822 * xmlUnlockLibrary:
823 *
824 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
825 * library.
826 */
827void
828xmlUnlockLibrary(void)
829{
Daniel Veillard6f350292001-10-14 09:56:15 +0000830#ifdef DEBUG_THREADS
831 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
832#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000833 xmlRMutexUnlock(xmlLibraryLock);
834}
835
836/**
837 * xmlInitThreads:
838 *
839 * xmlInitThreads() is used to to initialize all the thread related
840 * data of the libxml2 library.
841 */
842void
843xmlInitThreads(void)
844{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000845#ifdef HAVE_PTHREAD_H
846 if (libxml_is_threaded == -1) {
847 if ((pthread_once != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000848 (pthread_getspecific != NULL) &&
849 (pthread_setspecific != NULL) &&
850 (pthread_key_create != NULL) &&
Daniel Veillardd4a3f242009-01-18 15:41:30 +0000851 (pthread_key_delete != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000852 (pthread_mutex_init != NULL) &&
853 (pthread_mutex_destroy != NULL) &&
854 (pthread_mutex_lock != NULL) &&
855 (pthread_mutex_unlock != NULL) &&
856 (pthread_cond_init != NULL) &&
Daniel Veillard2cba4152008-08-27 11:45:41 +0000857 (pthread_cond_destroy != NULL) &&
858 (pthread_cond_wait != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000859 (pthread_equal != NULL) &&
860 (pthread_self != NULL) &&
861 (pthread_cond_signal != NULL)) {
862 libxml_is_threaded = 1;
863
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000864/* fprintf(stderr, "Running multithreaded\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000865 } else {
866
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000867/* fprintf(stderr, "Running without multithread\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000868 libxml_is_threaded = 0;
869 }
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000870 }
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200871#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
872 InitializeCriticalSection(&cleanup_helpers_cs);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000873#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000874}
875
876/**
877 * xmlCleanupThreads:
878 *
879 * xmlCleanupThreads() is used to to cleanup all the thread related
880 * data of the libxml2 library once processing has ended.
Daniel Veillard01101202009-02-21 09:22:04 +0000881 *
882 * WARNING: if your application is multithreaded or has plugin support
883 * calling this may crash the application if another thread or
884 * a plugin is still using libxml2. It's sometimes very hard to
885 * guess if libxml2 is in use in the application, some libraries
886 * or plugins may use it without notice. In case of doubt abstain
887 * from calling this function or do it just before calling exit()
888 * to avoid leak reports from valgrind !
Daniel Veillardb8478642001-10-12 17:29:10 +0000889 */
890void
891xmlCleanupThreads(void)
892{
Daniel Veillard6f350292001-10-14 09:56:15 +0000893#ifdef DEBUG_THREADS
894 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
895#endif
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200896#ifdef HAVE_PTHREAD_H
897 if ((libxml_is_threaded) && (pthread_key_delete != NULL))
898 pthread_key_delete(globalkey);
Friedrich Haubensak3f6cfbd2012-09-12 17:34:53 +0200899 once_control = once_control_init;
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200900#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000901 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000902 xmlGlobalStateCleanupHelperParams *p;
903
904 EnterCriticalSection(&cleanup_helpers_cs);
905 p = cleanup_helpers_head;
906 while (p != NULL) {
907 xmlGlobalStateCleanupHelperParams *temp = p;
908
909 p = p->next;
910 xmlFreeGlobalState(temp->memory);
911 free(temp);
912 }
913 cleanup_helpers_head = 0;
914 LeaveCriticalSection(&cleanup_helpers_cs);
915 TlsFree(globalkey);
916 globalkey = TLS_OUT_OF_INDEXES;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000917 }
918 DeleteCriticalSection(&cleanup_helpers_cs);
919#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000920}
Daniel Veillard6f350292001-10-14 09:56:15 +0000921
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000922#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000923
Daniel Veillarde28313b2001-12-06 14:08:31 +0000924/**
925 * xmlOnceInit
926 *
927 * xmlOnceInit() is used to initialize the value of mainthread for use
928 * in other routines. This function should only be called using
929 * pthread_once() in association with the once_control variable to ensure
930 * that the function is only called once. See man pthread_once for more
931 * details.
932 */
933static void
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000934xmlOnceInit(void)
935{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000936#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000937 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000938 mainthread = pthread_self();
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800939 __xmlInitializeDict();
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200940#elif defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000941 if (!run_once.done) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000942 if (InterlockedIncrement(&run_once.control) == 1) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000943#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000944 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000945#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000946 mainthread = GetCurrentThreadId();
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800947 __xmlInitializeDict();
Daniel Veillard62121e22005-02-24 15:38:52 +0000948 run_once.done = 1;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000949 } else {
Daniel Veillard62121e22005-02-24 15:38:52 +0000950 /* Another thread is working; give up our slice and
951 * wait until they're done. */
952 while (!run_once.done)
953 Sleep(0);
954 }
955 }
Eric Zurcher243b0342009-10-01 00:13:07 +0200956#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000957 if (atomic_add(&run_once_init, 1) == 0) {
958 globalkey = tls_allocate();
959 tls_set(globalkey, NULL);
960 mainthread = find_thread(NULL);
Daniel Veillard5fe9e9e2013-04-05 23:10:41 +0800961 __xmlInitializeDict();
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000962 } else
963 atomic_add(&run_once_init, -1);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000964#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000965}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000966#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000967
968/**
William M. Brack7a821652003-08-15 07:27:40 +0000969 * DllMain:
970 * @hinstDLL: handle to DLL instance
971 * @fdwReason: Reason code for entry
972 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000973 *
974 * Entry point for Windows library. It is being used to free thread-specific
975 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000976 *
977 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000978 */
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200979#ifdef HAVE_PTHREAD_H
980#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000981#if defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000982BOOL XMLCALL
983xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000984#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000985BOOL WINAPI
986DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000987#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000988{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000989 switch (fdwReason) {
990 case DLL_THREAD_DETACH:
991 if (globalkey != TLS_OUT_OF_INDEXES) {
992 xmlGlobalState *globalval = NULL;
993 xmlGlobalStateCleanupHelperParams *p =
994 (xmlGlobalStateCleanupHelperParams *)
995 TlsGetValue(globalkey);
996 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
997 if (globalval) {
998 xmlFreeGlobalState(globalval);
999 TlsSetValue(globalkey, NULL);
1000 }
1001 if (p) {
1002 EnterCriticalSection(&cleanup_helpers_cs);
1003 if (p == cleanup_helpers_head)
1004 cleanup_helpers_head = p->next;
1005 else
1006 p->prev->next = p->next;
1007 if (p->next != NULL)
1008 p->next->prev = p->prev;
1009 LeaveCriticalSection(&cleanup_helpers_cs);
1010 free(p);
1011 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001012 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001013 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001014 }
1015 return TRUE;
1016}
1017#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001018#define bottom_threads
1019#include "elfgcchack.h"