blob: 7e85a26fac0308a7adcfee11db117690eb4ea953 [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");
255 exit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000256#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000257 }
258 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000259#endif
260
261}
262
263/**
264 * xmlMutexUnlock:
265 * @tok: the simple mutex
266 *
267 * xmlMutexUnlock() is used to unlock a libxml2 token.
268 */
269void
Daniel Veillard5805be22003-08-28 08:03:23 +0000270xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000271{
Daniel Veillard5805be22003-08-28 08:03:23 +0000272 if (tok == NULL)
273 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000274#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000275 if (libxml_is_threaded != 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000276 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000277#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000278 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000279#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000280 if (tok->tid == find_thread(NULL)) {
281 tok->tid = -1;
282 release_sem(tok->sem);
283 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000284#endif
285}
286
287/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000288 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000289 *
290 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
291 * synchronizing access to data. token_r is a re-entrant lock and thus useful
292 * for synchronizing access to data structures that may be manipulated in a
293 * recursive fashion.
294 *
295 * Returns the new reentrant mutex pointer or NULL in case of error
296 */
297xmlRMutexPtr
298xmlNewRMutex(void)
299{
300 xmlRMutexPtr tok;
301
302 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
303 return (NULL);
304#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000305 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000306 pthread_mutex_init(&tok->lock, NULL);
307 tok->held = 0;
308 tok->waiters = 0;
309 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000310 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000311#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000312 InitializeCriticalSection(&tok->cs);
313 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000314#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000315 if ((tok->lock = xmlNewMutex()) == NULL) {
316 free(tok);
317 return NULL;
318 }
319 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000320#endif
321 return (tok);
322}
323
324/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000325 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000326 * @tok: the reentrant mutex
327 *
328 * xmlRFreeMutex() is used to reclaim resources associated with a
329 * reentrant mutex.
330 */
331void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000332xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000333{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000334 if (tok == NULL)
335 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000336#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000337 if (libxml_is_threaded != 0) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000338 pthread_mutex_destroy(&tok->lock);
339 pthread_cond_destroy(&tok->cv);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000340 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000341#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000342 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000343#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000344 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000345#endif
346 free(tok);
347}
348
349/**
350 * xmlRMutexLock:
351 * @tok: the reentrant mutex
352 *
353 * xmlRMutexLock() is used to lock a libxml2 token_r.
354 */
355void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000356xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000357{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000358 if (tok == NULL)
359 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000360#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000361 if (libxml_is_threaded == 0)
362 return;
363
Daniel Veillardb8478642001-10-12 17:29:10 +0000364 pthread_mutex_lock(&tok->lock);
365 if (tok->held) {
366 if (pthread_equal(tok->tid, pthread_self())) {
367 tok->held++;
368 pthread_mutex_unlock(&tok->lock);
369 return;
370 } else {
371 tok->waiters++;
372 while (tok->held)
373 pthread_cond_wait(&tok->cv, &tok->lock);
374 tok->waiters--;
375 }
376 }
377 tok->tid = pthread_self();
378 tok->held = 1;
379 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000380#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000381 EnterCriticalSection(&tok->cs);
382 ++tok->count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000383#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000384 if (tok->lock->tid == find_thread(NULL)) {
385 tok->count++;
386 return;
387 } else {
388 xmlMutexLock(tok->lock);
389 tok->count = 1;
390 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000391#endif
392}
393
394/**
395 * xmlRMutexUnlock:
396 * @tok: the reentrant mutex
397 *
398 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
399 */
400void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000401xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000402{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000403 if (tok == NULL)
404 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000405#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000406 if (libxml_is_threaded == 0)
407 return;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000408
Daniel Veillardb8478642001-10-12 17:29:10 +0000409 pthread_mutex_lock(&tok->lock);
410 tok->held--;
411 if (tok->held == 0) {
412 if (tok->waiters)
413 pthread_cond_signal(&tok->cv);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200414 memset(&tok->tid, 0, sizeof(tok->tid));
Daniel Veillardb8478642001-10-12 17:29:10 +0000415 }
416 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000417#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000418 if (!--tok->count)
419 LeaveCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000420#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000421 if (tok->lock->tid == find_thread(NULL)) {
422 tok->count--;
423 if (tok->count == 0) {
424 xmlMutexUnlock(tok->lock);
425 }
426 return;
427 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000428#endif
429}
430
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000431/**
432 * xmlGlobalInitMutexLock
433 *
434 * Makes sure that the global initialization mutex is initialized and
435 * locks it.
436 */
437void
438__xmlGlobalInitMutexLock(void)
439{
440 /* Make sure the global init lock is initialized and then lock it. */
441#ifdef HAVE_PTHREAD_H
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000442 /* The mutex is statically initialized, so we just lock it. */
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800443 if (pthread_mutex_lock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200444 pthread_mutex_lock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000445#elif defined HAVE_WIN32_THREADS
446 LPCRITICAL_SECTION cs;
447
448 /* Create a new critical section */
449 if (global_init_lock == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000450 cs = malloc(sizeof(CRITICAL_SECTION));
451 if (cs == NULL) {
452 xmlGenericError(xmlGenericErrorContext,
453 "xmlGlobalInitMutexLock: out of memory\n");
454 return;
455 }
456 InitializeCriticalSection(cs);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000457
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000458 /* Swap it into the global_init_lock */
Rob Richardse967f0b2007-06-08 19:36:04 +0000459#ifdef InterlockedCompareExchangePointer
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000460 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
461#else /* Use older void* version */
462 InterlockedCompareExchange((void **) &global_init_lock,
463 (void *) cs, NULL);
Rob Richardse967f0b2007-06-08 19:36:04 +0000464#endif /* InterlockedCompareExchangePointer */
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000465
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000466 /* If another thread successfully recorded its critical
467 * section in the global_init_lock then discard the one
468 * allocated by this thread. */
469 if (global_init_lock != cs) {
470 DeleteCriticalSection(cs);
471 free(cs);
472 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000473 }
474
475 /* Lock the chosen critical section */
476 EnterCriticalSection(global_init_lock);
477#elif defined HAVE_BEOS_THREADS
478 int32 sem;
479
480 /* Allocate a new semaphore */
481 sem = create_sem(1, "xmlGlobalinitMutex");
482
483 while (global_init_lock == -1) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000484 if (atomic_add(&global_init_count, 1) == 0) {
485 global_init_lock = sem;
486 } else {
487 snooze(1);
488 atomic_add(&global_init_count, -1);
489 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000490 }
491
492 /* If another thread successfully recorded its critical
493 * section in the global_init_lock then discard the one
494 * allocated by this thread. */
495 if (global_init_lock != sem)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000496 delete_sem(sem);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000497
498 /* Acquire the chosen semaphore */
499 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
500#ifdef DEBUG_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000501 xmlGenericError(xmlGenericErrorContext,
502 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
503 exit();
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000504#endif
505 }
506#endif
507}
508
509void
510__xmlGlobalInitMutexUnlock(void)
511{
512#ifdef HAVE_PTHREAD_H
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800513 if (pthread_mutex_unlock != NULL)
Mike Hommeye6f05092010-10-15 19:50:03 +0200514 pthread_mutex_unlock(&global_init_lock);
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000515#elif defined HAVE_WIN32_THREADS
Daniel Veillard14d465d2008-03-24 11:12:55 +0000516 if (global_init_lock != NULL) {
517 LeaveCriticalSection(global_init_lock);
518 }
Daniel Veillardfde5b0b2007-02-12 17:31:53 +0000519#elif defined HAVE_BEOS_THREADS
520 release_sem(global_init_lock);
521#endif
522}
523
Rob Richards91eb5602007-11-16 10:54:59 +0000524/**
525 * xmlGlobalInitMutexDestroy
526 *
527 * Makes sure that the global initialization mutex is destroyed before
528 * application termination.
529 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000530void
531__xmlGlobalInitMutexDestroy(void)
Rob Richards91eb5602007-11-16 10:54:59 +0000532{
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200533#ifdef HAVE_PTHREAD_H
534#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000535 if (global_init_lock != NULL) {
536 DeleteCriticalSection(global_init_lock);
537 free(global_init_lock);
538 global_init_lock = NULL;
Rob Richards91eb5602007-11-16 10:54:59 +0000539 }
540#endif
541}
542
Daniel Veillardb8478642001-10-12 17:29:10 +0000543/************************************************************************
544 * *
545 * Per thread global state handling *
546 * *
547 ************************************************************************/
548
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000549#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000550#ifdef xmlLastError
551#undef xmlLastError
552#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000553
Daniel Veillardb8478642001-10-12 17:29:10 +0000554/**
555 * xmlFreeGlobalState:
556 * @state: a thread global state
557 *
558 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
559 * global state. It is is used here to reclaim memory resources.
560 */
561static void
562xmlFreeGlobalState(void *state)
563{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000564 xmlGlobalState *gs = (xmlGlobalState *) state;
565
566 /* free any memory allocated in the thread's xmlLastError */
567 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000568 free(state);
569}
570
571/**
572 * xmlNewGlobalState:
573 *
574 * xmlNewGlobalState() allocates a global state. This structure is used to
575 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000576 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000577 *
578 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
579 */
580static xmlGlobalStatePtr
581xmlNewGlobalState(void)
582{
583 xmlGlobalState *gs;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000584
Daniel Veillardb8478642001-10-12 17:29:10 +0000585 gs = malloc(sizeof(xmlGlobalState));
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000586 if (gs == NULL) {
587 xmlGenericError(xmlGenericErrorContext,
588 "xmlGetGlobalState: out of memory\n");
589 return (NULL);
590 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000591
William M. Brack8b2c7f12002-11-22 05:07:29 +0000592 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000593 xmlInitializeGlobalState(gs);
594 return (gs);
595}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000596#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000597
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200598#ifdef HAVE_PTHREAD_H
Eric Zurcher243b0342009-10-01 00:13:07 +0200599#elif defined HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000600#if !defined(HAVE_COMPILER_TLS)
601#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000602typedef struct _xmlGlobalStateCleanupHelperParams {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000603 HANDLE thread;
604 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000605} xmlGlobalStateCleanupHelperParams;
606
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000607static void XMLCDECL
608xmlGlobalStateCleanupHelper(void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000609{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000610 xmlGlobalStateCleanupHelperParams *params =
611 (xmlGlobalStateCleanupHelperParams *) p;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000612 WaitForSingleObject(params->thread, INFINITE);
613 CloseHandle(params->thread);
614 xmlFreeGlobalState(params->memory);
615 free(params);
616 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000617}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000618#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
619
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000620typedef struct _xmlGlobalStateCleanupHelperParams {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000621 void *memory;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000622 struct _xmlGlobalStateCleanupHelperParams *prev;
623 struct _xmlGlobalStateCleanupHelperParams *next;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000624} xmlGlobalStateCleanupHelperParams;
625
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000626static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000627static CRITICAL_SECTION cleanup_helpers_cs;
628
629#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
630#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000631#endif /* HAVE_WIN32_THREADS */
632
Daniel Veillard82cb3192003-10-29 13:39:15 +0000633#if defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000634
William M. Brackb1d53162003-11-18 06:54:40 +0000635/**
636 * xmlGlobalStateCleanup:
637 * @data: unused parameter
638 *
639 * Used for Beos only
640 */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000641void
642xmlGlobalStateCleanup(void *data)
Daniel Veillard82cb3192003-10-29 13:39:15 +0000643{
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000644 void *globalval = tls_get(globalkey);
645
646 if (globalval != NULL)
647 xmlFreeGlobalState(globalval);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000648}
649#endif
650
Daniel Veillard01c13b52002-12-10 15:19:08 +0000651/**
652 * xmlGetGlobalState:
653 *
654 * xmlGetGlobalState() is called to retrieve the global state for a thread.
655 *
656 * Returns the thread global state or NULL in case of error
657 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000658xmlGlobalStatePtr
659xmlGetGlobalState(void)
660{
661#ifdef HAVE_PTHREAD_H
662 xmlGlobalState *globalval;
663
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000664 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000665 return (NULL);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000666
Daniel Veillarde28313b2001-12-06 14:08:31 +0000667 pthread_once(&once_control, xmlOnceInit);
668
Daniel Veillardb8478642001-10-12 17:29:10 +0000669 if ((globalval = (xmlGlobalState *)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000670 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000671 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000672 if (tsd == NULL)
673 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000674
675 pthread_setspecific(globalkey, tsd);
676 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000677 }
678 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000679#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000680#if defined(HAVE_COMPILER_TLS)
681 if (!tlstate_inited) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000682 tlstate_inited = 1;
683 xmlInitializeGlobalState(&tlstate);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000684 }
685 return &tlstate;
686#else /* HAVE_COMPILER_TLS */
687 xmlGlobalState *globalval;
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000688 xmlGlobalStateCleanupHelperParams *p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000689
Daniel Veillard62121e22005-02-24 15:38:52 +0000690 xmlOnceInit();
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 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000693#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000694 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
695 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000696#endif
697 if (globalval == NULL) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000698 xmlGlobalState *tsd = xmlNewGlobalState();
699
700 if (tsd == NULL)
701 return(NULL);
702 p = (xmlGlobalStateCleanupHelperParams *)
703 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
704 if (p == NULL) {
705 xmlGenericError(xmlGenericErrorContext,
706 "xmlGetGlobalState: out of memory\n");
Daniel Veillardbf2ebff2009-01-18 14:57:04 +0000707 xmlFreeGlobalState(tsd);
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000708 return(NULL);
709 }
710 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000711#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000712 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
713 GetCurrentProcess(), &p->thread, 0, TRUE,
714 DUPLICATE_SAME_ACCESS);
715 TlsSetValue(globalkey, tsd);
716 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000717#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000718 EnterCriticalSection(&cleanup_helpers_cs);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000719 if (cleanup_helpers_head != NULL) {
720 cleanup_helpers_head->prev = p;
721 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000722 p->next = cleanup_helpers_head;
723 p->prev = NULL;
724 cleanup_helpers_head = p;
725 TlsSetValue(globalkey, p);
726 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000727#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000728
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000729 return (tsd);
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000730 }
731 return (globalval);
732#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000733#elif defined HAVE_BEOS_THREADS
734 xmlGlobalState *globalval;
735
736 xmlOnceInit();
737
Daniel Veillard14d465d2008-03-24 11:12:55 +0000738 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000739 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillard14d465d2008-03-24 11:12:55 +0000740 if (tsd == NULL)
741 return (NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000742
743 tls_set(globalkey, tsd);
744 on_exit_thread(xmlGlobalStateCleanup, NULL);
745 return (tsd);
746 }
747 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000748#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000749 return (NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000750#endif
751}
752
Daniel Veillardb8478642001-10-12 17:29:10 +0000753/************************************************************************
754 * *
755 * Library wide thread interfaces *
756 * *
757 ************************************************************************/
758
759/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000760 * xmlGetThreadId:
761 *
762 * xmlGetThreadId() find the current thread ID number
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200763 * Note that this is likely to be broken on some platforms using pthreads
764 * as the specification doesn't mandate pthread_t to be an integer type
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000765 *
766 * Returns the current thread ID number
767 */
768int
769xmlGetThreadId(void)
770{
771#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200772 pthread_t id;
773 int ret;
774
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000775 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000776 return (0);
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200777 id = pthread_self();
778 /* horrible but preserves compat, see warning above */
779 memcpy(&ret, &id, sizeof(ret));
780 return (ret);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000781#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000782 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000783#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000784 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000785#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000786 return ((int) 0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000787#endif
788}
789
790/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000791 * xmlIsMainThread:
792 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000793 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000794 *
795 * Returns 1 if the current thread is the main thread, 0 otherwise
796 */
797int
798xmlIsMainThread(void)
799{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000800#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000801 if (libxml_is_threaded == -1)
802 xmlInitThreads();
803 if (libxml_is_threaded == 0)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000804 return (1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000805 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000806#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000807 xmlOnceInit();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000808#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000809 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000810#endif
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000811
Daniel Veillard6f350292001-10-14 09:56:15 +0000812#ifdef DEBUG_THREADS
813 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
814#endif
815#ifdef HAVE_PTHREAD_H
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200816 return (pthread_equal(mainthread,pthread_self()));
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000817#elif defined HAVE_WIN32_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000818 return (mainthread == GetCurrentThreadId());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000819#elif defined HAVE_BEOS_THREADS
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000820 return (mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000821#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000822 return (1);
Daniel Veillard6f350292001-10-14 09:56:15 +0000823#endif
824}
825
826/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000827 * xmlLockLibrary:
828 *
829 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
830 * library.
831 */
832void
833xmlLockLibrary(void)
834{
Daniel Veillard6f350292001-10-14 09:56:15 +0000835#ifdef DEBUG_THREADS
836 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
837#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000838 xmlRMutexLock(xmlLibraryLock);
839}
840
841/**
842 * xmlUnlockLibrary:
843 *
844 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
845 * library.
846 */
847void
848xmlUnlockLibrary(void)
849{
Daniel Veillard6f350292001-10-14 09:56:15 +0000850#ifdef DEBUG_THREADS
851 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
852#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000853 xmlRMutexUnlock(xmlLibraryLock);
854}
855
856/**
857 * xmlInitThreads:
858 *
859 * xmlInitThreads() is used to to initialize all the thread related
860 * data of the libxml2 library.
861 */
862void
863xmlInitThreads(void)
864{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000865#ifdef HAVE_PTHREAD_H
866 if (libxml_is_threaded == -1) {
867 if ((pthread_once != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000868 (pthread_getspecific != NULL) &&
869 (pthread_setspecific != NULL) &&
870 (pthread_key_create != NULL) &&
Daniel Veillardd4a3f242009-01-18 15:41:30 +0000871 (pthread_key_delete != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000872 (pthread_mutex_init != NULL) &&
873 (pthread_mutex_destroy != NULL) &&
874 (pthread_mutex_lock != NULL) &&
875 (pthread_mutex_unlock != NULL) &&
876 (pthread_cond_init != NULL) &&
Daniel Veillard2cba4152008-08-27 11:45:41 +0000877 (pthread_cond_destroy != NULL) &&
878 (pthread_cond_wait != NULL) &&
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000879 (pthread_equal != NULL) &&
880 (pthread_self != NULL) &&
881 (pthread_cond_signal != NULL)) {
882 libxml_is_threaded = 1;
883
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000884/* fprintf(stderr, "Running multithreaded\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000885 } else {
886
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000887/* fprintf(stderr, "Running without multithread\n"); */
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000888 libxml_is_threaded = 0;
889 }
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000890 }
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200891#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
892 InitializeCriticalSection(&cleanup_helpers_cs);
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000893#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000894}
895
896/**
897 * xmlCleanupThreads:
898 *
899 * xmlCleanupThreads() is used to to cleanup all the thread related
900 * data of the libxml2 library once processing has ended.
Daniel Veillard01101202009-02-21 09:22:04 +0000901 *
902 * WARNING: if your application is multithreaded or has plugin support
903 * calling this may crash the application if another thread or
904 * a plugin is still using libxml2. It's sometimes very hard to
905 * guess if libxml2 is in use in the application, some libraries
906 * or plugins may use it without notice. In case of doubt abstain
907 * from calling this function or do it just before calling exit()
908 * to avoid leak reports from valgrind !
Daniel Veillardb8478642001-10-12 17:29:10 +0000909 */
910void
911xmlCleanupThreads(void)
912{
Daniel Veillard6f350292001-10-14 09:56:15 +0000913#ifdef DEBUG_THREADS
914 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
915#endif
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200916#ifdef HAVE_PTHREAD_H
917 if ((libxml_is_threaded) && (pthread_key_delete != NULL))
918 pthread_key_delete(globalkey);
Friedrich Haubensak3f6cfbd2012-09-12 17:34:53 +0200919 once_control = once_control_init;
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200920#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000921 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000922 xmlGlobalStateCleanupHelperParams *p;
923
924 EnterCriticalSection(&cleanup_helpers_cs);
925 p = cleanup_helpers_head;
926 while (p != NULL) {
927 xmlGlobalStateCleanupHelperParams *temp = p;
928
929 p = p->next;
930 xmlFreeGlobalState(temp->memory);
931 free(temp);
932 }
933 cleanup_helpers_head = 0;
934 LeaveCriticalSection(&cleanup_helpers_cs);
935 TlsFree(globalkey);
936 globalkey = TLS_OUT_OF_INDEXES;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000937 }
938 DeleteCriticalSection(&cleanup_helpers_cs);
939#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000940}
Daniel Veillard6f350292001-10-14 09:56:15 +0000941
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000942#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000943
Daniel Veillarde28313b2001-12-06 14:08:31 +0000944/**
945 * xmlOnceInit
946 *
947 * xmlOnceInit() is used to initialize the value of mainthread for use
948 * in other routines. This function should only be called using
949 * pthread_once() in association with the once_control variable to ensure
950 * that the function is only called once. See man pthread_once for more
951 * details.
952 */
953static void
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000954xmlOnceInit(void)
955{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000956#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000957 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000958 mainthread = pthread_self();
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200959#elif defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000960 if (!run_once.done) {
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000961 if (InterlockedIncrement(&run_once.control) == 1) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000962#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000963 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000964#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000965 mainthread = GetCurrentThreadId();
966 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);
979 } else
980 atomic_add(&run_once_init, -1);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000981#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000982}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000983#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000984
985/**
William M. Brack7a821652003-08-15 07:27:40 +0000986 * DllMain:
987 * @hinstDLL: handle to DLL instance
988 * @fdwReason: Reason code for entry
989 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000990 *
991 * Entry point for Windows library. It is being used to free thread-specific
992 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000993 *
994 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000995 */
Daniel Veillardd87c5d12009-09-10 17:46:07 +0200996#ifdef HAVE_PTHREAD_H
997#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000998#if defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillardddbe38b2008-03-18 08:24:25 +0000999BOOL XMLCALL
1000xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001001#else
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001002BOOL WINAPI
1003DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001004#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001005{
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001006 switch (fdwReason) {
1007 case DLL_THREAD_DETACH:
1008 if (globalkey != TLS_OUT_OF_INDEXES) {
1009 xmlGlobalState *globalval = NULL;
1010 xmlGlobalStateCleanupHelperParams *p =
1011 (xmlGlobalStateCleanupHelperParams *)
1012 TlsGetValue(globalkey);
1013 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
1014 if (globalval) {
1015 xmlFreeGlobalState(globalval);
1016 TlsSetValue(globalkey, NULL);
1017 }
1018 if (p) {
1019 EnterCriticalSection(&cleanup_helpers_cs);
1020 if (p == cleanup_helpers_head)
1021 cleanup_helpers_head = p->next;
1022 else
1023 p->prev->next = p->next;
1024 if (p->next != NULL)
1025 p->next->prev = p->prev;
1026 LeaveCriticalSection(&cleanup_helpers_cs);
1027 free(p);
1028 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001029 }
Daniel Veillardddbe38b2008-03-18 08:24:25 +00001030 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +00001031 }
1032 return TRUE;
1033}
1034#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001035#define bottom_threads
1036#include "elfgcchack.h"