blob: 7124bca35dc5bb22e22e933546743de727ebf0a1 [file] [log] [blame]
Daniel Veillardb8478642001-10-12 17:29:10 +00001/**
2 * threads.c: set of generic threading related routines
3 *
4 * See Copyright for the status of this software.
5 *
6 * Gary Pennington <Gary.Pennington@uk.sun.com>
7 * daniel@veillard.com
8 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Daniel Veillardb8478642001-10-12 17:29:10 +000011#include "libxml.h"
12
13#include <string.h>
14
15#include <libxml/threads.h>
16#include <libxml/globals.h>
17
18#ifdef HAVE_SYS_TYPES_H
19#include <sys/types.h>
20#endif
21#ifdef HAVE_UNISTD_H
22#include <unistd.h>
23#endif
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_PTHREAD_H
28#include <pthread.h>
29#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000030
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000031#ifdef HAVE_WIN32_THREADS
32#include <windows.h>
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000033#ifndef HAVE_COMPILER_TLS
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000034#include <process.h>
35#endif
36#endif
Daniel Veillardb8478642001-10-12 17:29:10 +000037
Daniel Veillard82cb3192003-10-29 13:39:15 +000038#ifdef HAVE_BEOS_THREADS
39#include <OS.h>
40#include <TLS.h>
41#endif
42
Daniel Veillardb8478642001-10-12 17:29:10 +000043#if defined(SOLARIS)
44#include <note.h>
45#endif
46
Daniel Veillard6f350292001-10-14 09:56:15 +000047/* #define DEBUG_THREADS */
Daniel Veillardb8478642001-10-12 17:29:10 +000048
Daniel Veillarddbfe05a2005-05-04 09:18:00 +000049#ifdef HAVE_PTHREAD_H
50
51static int libxml_is_threaded = -1;
52#ifdef __GNUC__
53#ifdef linux
54#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
55extern int pthread_once (pthread_once_t *__once_control,
56 void (*__init_routine) (void))
57 __attribute((weak));
58extern void *pthread_getspecific (pthread_key_t __key)
59 __attribute((weak));
60extern int pthread_setspecific (pthread_key_t __key,
61 __const void *__pointer)
62 __attribute((weak));
63extern int pthread_key_create (pthread_key_t *__key,
64 void (*__destr_function) (void *))
65 __attribute((weak));
66extern int pthread_mutex_init ()
67 __attribute((weak));
68extern int pthread_mutex_destroy ()
69 __attribute((weak));
70extern int pthread_mutex_lock ()
71 __attribute((weak));
72extern int pthread_mutex_unlock ()
73 __attribute((weak));
74extern int pthread_cond_init ()
75 __attribute((weak));
76extern int pthread_equal ()
77 __attribute((weak));
78extern pthread_t pthread_self ()
79 __attribute((weak));
80extern int pthread_key_create ()
81 __attribute((weak));
82extern int pthread_cond_signal ()
83 __attribute((weak));
84#endif
85#endif /* linux */
86#endif /* __GNUC__ */
87#endif /* HAVE_PTHREAD_H */
88
Daniel Veillardb8478642001-10-12 17:29:10 +000089/*
90 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
91 * to avoid some crazyness since xmlMalloc/xmlFree may actually
92 * be hosted on allocated blocks needing them for the allocation ...
93 */
94
95/*
96 * xmlMutex are a simple mutual exception locks
97 */
98struct _xmlMutex {
99#ifdef HAVE_PTHREAD_H
100 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000101#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000102 HANDLE mutex;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000103#elif defined HAVE_BEOS_THREADS
104 sem_id sem;
Daniel Veillard254b1262003-11-01 17:04:58 +0000105 thread_id tid;
Daniel Veillardb8478642001-10-12 17:29:10 +0000106#else
107 int empty;
108#endif
109};
110
111/*
112 * xmlRMutex are reentrant mutual exception locks
113 */
114struct _xmlRMutex {
115#ifdef HAVE_PTHREAD_H
116 pthread_mutex_t lock;
117 unsigned int held;
118 unsigned int waiters;
119 pthread_t tid;
120 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000121#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000122 CRITICAL_SECTION cs;
123 unsigned int count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000124#elif defined HAVE_BEOS_THREADS
125 xmlMutexPtr lock;
126 thread_id tid;
127 int32 count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000128#else
129 int empty;
130#endif
131};
132/*
133 * This module still has some internal static data.
134 * - xmlLibraryLock a global lock
135 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +0000136 */
Daniel Veillard6f350292001-10-14 09:56:15 +0000137
Daniel Veillardb8478642001-10-12 17:29:10 +0000138#ifdef HAVE_PTHREAD_H
Daniel Veillardb8478642001-10-12 17:29:10 +0000139static pthread_key_t globalkey;
Daniel Veillard6f350292001-10-14 09:56:15 +0000140static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +0000141static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000142#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000143#if defined(HAVE_COMPILER_TLS)
144static __declspec(thread) xmlGlobalState tlstate;
145static __declspec(thread) int tlstate_inited = 0;
146#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000147static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000148#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000149static DWORD mainthread;
Daniel Veillard62121e22005-02-24 15:38:52 +0000150static struct
151{
Daniel Veillard36616dd2005-02-25 07:31:49 +0000152 DWORD done;
153 DWORD control;
Daniel Veillard62121e22005-02-24 15:38:52 +0000154} run_once = { 0, 0 };
Daniel Veillard82cb3192003-10-29 13:39:15 +0000155/* endif HAVE_WIN32_THREADS */
156#elif defined HAVE_BEOS_THREADS
157int32 globalkey = 0;
158thread_id mainthread = 0;
159int32 run_once_init = 0;
160#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000161
Daniel Veillardb8478642001-10-12 17:29:10 +0000162static xmlRMutexPtr xmlLibraryLock = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000163#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000164static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000165#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000166
167/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000168 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000169 *
170 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
171 * synchronizing access to data.
172 *
173 * Returns a new simple mutex pointer or NULL in case of error
174 */
175xmlMutexPtr
176xmlNewMutex(void)
177{
178 xmlMutexPtr tok;
179
180 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
181 return (NULL);
182#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000183 if (libxml_is_threaded != 0)
184 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000185#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000186 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000187#elif defined HAVE_BEOS_THREADS
188 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
189 free(tok);
190 return NULL;
191 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000192 tok->tid = -1;
Daniel Veillardb8478642001-10-12 17:29:10 +0000193#endif
194 return (tok);
195}
196
197/**
198 * xmlFreeMutex:
199 * @tok: the simple mutex
200 *
201 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
202 * struct.
203 */
204void
205xmlFreeMutex(xmlMutexPtr tok)
206{
Daniel Veillarddf101d82003-07-08 14:03:36 +0000207 if (tok == NULL) return;
208
Daniel Veillardb8478642001-10-12 17:29:10 +0000209#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000210 if (libxml_is_threaded != 0)
211 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000212#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000213 CloseHandle(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000214#elif defined HAVE_BEOS_THREADS
215 delete_sem(tok->sem);
Daniel Veillardb8478642001-10-12 17:29:10 +0000216#endif
217 free(tok);
218}
219
220/**
221 * xmlMutexLock:
222 * @tok: the simple mutex
223 *
224 * xmlMutexLock() is used to lock a libxml2 token.
225 */
226void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000227xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000228{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000229 if (tok == NULL)
230 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000231#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000232 if (libxml_is_threaded != 0)
233 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000234#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000235 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000236#elif defined HAVE_BEOS_THREADS
237 if (acquire_sem(tok->sem) != B_NO_ERROR) {
238#ifdef DEBUG_THREADS
239 xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
240 exit();
241#endif
242 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000243 tok->tid = find_thread(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000244#endif
245
246}
247
248/**
249 * xmlMutexUnlock:
250 * @tok: the simple mutex
251 *
252 * xmlMutexUnlock() is used to unlock a libxml2 token.
253 */
254void
Daniel Veillard5805be22003-08-28 08:03:23 +0000255xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000256{
Daniel Veillard5805be22003-08-28 08:03:23 +0000257 if (tok == NULL)
258 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000259#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000260 if (libxml_is_threaded != 0)
261 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000262#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000263 ReleaseMutex(tok->mutex);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000264#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000265 if (tok->tid == find_thread(NULL)) {
266 tok->tid = -1;
267 release_sem(tok->sem);
268 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000269#endif
270}
271
272/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000273 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000274 *
275 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
276 * synchronizing access to data. token_r is a re-entrant lock and thus useful
277 * for synchronizing access to data structures that may be manipulated in a
278 * recursive fashion.
279 *
280 * Returns the new reentrant mutex pointer or NULL in case of error
281 */
282xmlRMutexPtr
283xmlNewRMutex(void)
284{
285 xmlRMutexPtr tok;
286
287 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
288 return (NULL);
289#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000290 if (libxml_is_threaded != 0) {
291 pthread_mutex_init(&tok->lock, NULL);
292 tok->held = 0;
293 tok->waiters = 0;
294 pthread_cond_init(&tok->cv, NULL);
295 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000296#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000297 InitializeCriticalSection(&tok->cs);
298 tok->count = 0;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000299#elif defined HAVE_BEOS_THREADS
300 if ((tok->lock = xmlNewMutex()) == NULL) {
301 free(tok);
302 return NULL;
303 }
304 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000305#endif
306 return (tok);
307}
308
309/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000310 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000311 * @tok: the reentrant mutex
312 *
313 * xmlRFreeMutex() is used to reclaim resources associated with a
314 * reentrant mutex.
315 */
316void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000317xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000318{
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000319 if (tok == NULL)
320 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000321#ifdef HAVE_PTHREAD_H
Daniel Veillarda8b54132006-06-29 11:50:18 +0000322 if (libxml_is_threaded != 0) {
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000323 pthread_mutex_destroy(&tok->lock);
Daniel Veillarda8b54132006-06-29 11:50:18 +0000324 pthread_cond_destroy(&tok->cv);
325 }
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000326#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000327 DeleteCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000328#elif defined HAVE_BEOS_THREADS
329 xmlFreeMutex(tok->lock);
Daniel Veillardb8478642001-10-12 17:29:10 +0000330#endif
331 free(tok);
332}
333
334/**
335 * xmlRMutexLock:
336 * @tok: the reentrant mutex
337 *
338 * xmlRMutexLock() is used to lock a libxml2 token_r.
339 */
340void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000341xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000342{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000343 if (tok == NULL)
344 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000345#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000346 if (libxml_is_threaded == 0)
347 return;
348
Daniel Veillardb8478642001-10-12 17:29:10 +0000349 pthread_mutex_lock(&tok->lock);
350 if (tok->held) {
351 if (pthread_equal(tok->tid, pthread_self())) {
352 tok->held++;
353 pthread_mutex_unlock(&tok->lock);
354 return;
355 } else {
356 tok->waiters++;
357 while (tok->held)
358 pthread_cond_wait(&tok->cv, &tok->lock);
359 tok->waiters--;
360 }
361 }
362 tok->tid = pthread_self();
363 tok->held = 1;
364 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000365#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000366 EnterCriticalSection(&tok->cs);
367 ++tok->count;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000368#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000369 if (tok->lock->tid == find_thread(NULL)) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000370 tok->count++;
371 return;
372 } else {
373 xmlMutexLock(tok->lock);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000374 tok->count = 1;
375 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000376#endif
377}
378
379/**
380 * xmlRMutexUnlock:
381 * @tok: the reentrant mutex
382 *
383 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
384 */
385void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000386xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000387{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000388 if (tok == NULL)
389 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000390#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000391 if (libxml_is_threaded == 0)
392 return;
393
Daniel Veillardb8478642001-10-12 17:29:10 +0000394 pthread_mutex_lock(&tok->lock);
395 tok->held--;
396 if (tok->held == 0) {
397 if (tok->waiters)
398 pthread_cond_signal(&tok->cv);
399 tok->tid = 0;
400 }
401 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000402#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000403 if (!--tok->count)
404 LeaveCriticalSection(&tok->cs);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000405#elif defined HAVE_BEOS_THREADS
Daniel Veillard254b1262003-11-01 17:04:58 +0000406 if (tok->lock->tid == find_thread(NULL)) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000407 tok->count--;
408 if (tok->count == 0) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000409 xmlMutexUnlock(tok->lock);
410 }
411 return;
412 }
Daniel Veillardb8478642001-10-12 17:29:10 +0000413#endif
414}
415
416/************************************************************************
417 * *
418 * Per thread global state handling *
419 * *
420 ************************************************************************/
421
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000422#ifdef LIBXML_THREAD_ENABLED
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000423#ifdef xmlLastError
424#undef xmlLastError
425#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000426/**
427 * xmlFreeGlobalState:
428 * @state: a thread global state
429 *
430 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
431 * global state. It is is used here to reclaim memory resources.
432 */
433static void
434xmlFreeGlobalState(void *state)
435{
Daniel Veillard01c3bd52004-10-22 13:16:10 +0000436 xmlGlobalState *gs = (xmlGlobalState *) state;
437
438 /* free any memory allocated in the thread's xmlLastError */
439 xmlResetError(&(gs->xmlLastError));
Daniel Veillardb8478642001-10-12 17:29:10 +0000440 free(state);
441}
442
443/**
444 * xmlNewGlobalState:
445 *
446 * xmlNewGlobalState() allocates a global state. This structure is used to
447 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000448 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000449 *
450 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
451 */
452static xmlGlobalStatePtr
453xmlNewGlobalState(void)
454{
455 xmlGlobalState *gs;
456
457 gs = malloc(sizeof(xmlGlobalState));
458 if (gs == NULL)
459 return(NULL);
460
William M. Brack8b2c7f12002-11-22 05:07:29 +0000461 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000462 xmlInitializeGlobalState(gs);
463 return (gs);
464}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000465#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000466
467
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000468#ifdef HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000469#if !defined(HAVE_COMPILER_TLS)
470#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000471typedef struct _xmlGlobalStateCleanupHelperParams
472{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000473 HANDLE thread;
474 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000475} xmlGlobalStateCleanupHelperParams;
476
Daniel Veillardffa3c742005-07-21 13:24:09 +0000477static void XMLCDECL xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000478{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000479 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
480 WaitForSingleObject(params->thread, INFINITE);
481 CloseHandle(params->thread);
482 xmlFreeGlobalState(params->memory);
483 free(params);
484 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000485}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000486#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
487
488typedef struct _xmlGlobalStateCleanupHelperParams
489{
490 void *memory;
491 struct _xmlGlobalStateCleanupHelperParams * prev;
492 struct _xmlGlobalStateCleanupHelperParams * next;
493} xmlGlobalStateCleanupHelperParams;
494
495static xmlGlobalStateCleanupHelperParams * cleanup_helpers_head = NULL;
496static CRITICAL_SECTION cleanup_helpers_cs;
497
498#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
499#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000500#endif /* HAVE_WIN32_THREADS */
501
Daniel Veillard82cb3192003-10-29 13:39:15 +0000502#if defined HAVE_BEOS_THREADS
William M. Brackb1d53162003-11-18 06:54:40 +0000503/**
504 * xmlGlobalStateCleanup:
505 * @data: unused parameter
506 *
507 * Used for Beos only
508 */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000509void xmlGlobalStateCleanup(void *data)
510{
511 void *globalval = tls_get(globalkey);
512 if (globalval != NULL)
513 xmlFreeGlobalState(globalval);
514}
515#endif
516
Daniel Veillard01c13b52002-12-10 15:19:08 +0000517/**
518 * xmlGetGlobalState:
519 *
520 * xmlGetGlobalState() is called to retrieve the global state for a thread.
521 *
522 * Returns the thread global state or NULL in case of error
523 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000524xmlGlobalStatePtr
525xmlGetGlobalState(void)
526{
527#ifdef HAVE_PTHREAD_H
528 xmlGlobalState *globalval;
529
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000530 if (libxml_is_threaded == 0)
531 return(NULL);
532
Daniel Veillarde28313b2001-12-06 14:08:31 +0000533 pthread_once(&once_control, xmlOnceInit);
534
Daniel Veillardb8478642001-10-12 17:29:10 +0000535 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000536 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000537 xmlGlobalState *tsd = xmlNewGlobalState();
538
539 pthread_setspecific(globalkey, tsd);
540 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000541 }
542 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000543#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000544#if defined(HAVE_COMPILER_TLS)
545 if (!tlstate_inited) {
546 tlstate_inited = 1;
547 xmlInitializeGlobalState(&tlstate);
548 }
549 return &tlstate;
550#else /* HAVE_COMPILER_TLS */
551 xmlGlobalState *globalval;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000552 xmlGlobalStateCleanupHelperParams * p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000553
Daniel Veillard62121e22005-02-24 15:38:52 +0000554 xmlOnceInit();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000555#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
556 globalval = (xmlGlobalState *)TlsGetValue(globalkey);
557#else
558 p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
559 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
560#endif
561 if (globalval == NULL) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000562 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000563 p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000564 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000565#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000566 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
567 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
568 TlsSetValue(globalkey, tsd);
569 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000570#else
571 EnterCriticalSection(&cleanup_helpers_cs);
572 if (cleanup_helpers_head != NULL) {
573 cleanup_helpers_head->prev = p;
574 }
575 p->next = cleanup_helpers_head;
576 p->prev = NULL;
577 cleanup_helpers_head = p;
578 TlsSetValue(globalkey, p);
579 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000580#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000581
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000582 return (tsd);
583 }
584 return (globalval);
585#endif /* HAVE_COMPILER_TLS */
Daniel Veillard82cb3192003-10-29 13:39:15 +0000586#elif defined HAVE_BEOS_THREADS
587 xmlGlobalState *globalval;
588
589 xmlOnceInit();
590
591 if ((globalval = (xmlGlobalState *)
592 tls_get(globalkey)) == NULL) {
593 xmlGlobalState *tsd = xmlNewGlobalState();
594
595 tls_set(globalkey, tsd);
596 on_exit_thread(xmlGlobalStateCleanup, NULL);
597 return (tsd);
598 }
599 return (globalval);
Daniel Veillard6f350292001-10-14 09:56:15 +0000600#else
601 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000602#endif
603}
604
Daniel Veillardb8478642001-10-12 17:29:10 +0000605/************************************************************************
606 * *
607 * Library wide thread interfaces *
608 * *
609 ************************************************************************/
610
611/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000612 * xmlGetThreadId:
613 *
614 * xmlGetThreadId() find the current thread ID number
615 *
616 * Returns the current thread ID number
617 */
618int
619xmlGetThreadId(void)
620{
621#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000622 if (libxml_is_threaded == 0)
623 return(0);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000624 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000625#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000626 return GetCurrentThreadId();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000627#elif defined HAVE_BEOS_THREADS
628 return find_thread(NULL);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000629#else
630 return((int) 0);
631#endif
632}
633
634/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000635 * xmlIsMainThread:
636 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000637 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000638 *
639 * Returns 1 if the current thread is the main thread, 0 otherwise
640 */
641int
642xmlIsMainThread(void)
643{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000644#ifdef HAVE_PTHREAD_H
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000645 if (libxml_is_threaded == -1)
646 xmlInitThreads();
647 if (libxml_is_threaded == 0)
648 return(1);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000649 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000650#elif defined HAVE_WIN32_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000651 xmlOnceInit ();
Daniel Veillard82cb3192003-10-29 13:39:15 +0000652#elif defined HAVE_BEOS_THREADS
Daniel Veillard62121e22005-02-24 15:38:52 +0000653 xmlOnceInit();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000654#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000655
656#ifdef DEBUG_THREADS
657 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
658#endif
659#ifdef HAVE_PTHREAD_H
660 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000661#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000662 return(mainthread == GetCurrentThreadId ());
Daniel Veillard82cb3192003-10-29 13:39:15 +0000663#elif defined HAVE_BEOS_THREADS
664 return(mainthread == find_thread(NULL));
Daniel Veillard6f350292001-10-14 09:56:15 +0000665#else
666 return(1);
667#endif
668}
669
670/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000671 * xmlLockLibrary:
672 *
673 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
674 * library.
675 */
676void
677xmlLockLibrary(void)
678{
Daniel Veillard6f350292001-10-14 09:56:15 +0000679#ifdef DEBUG_THREADS
680 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
681#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000682 xmlRMutexLock(xmlLibraryLock);
683}
684
685/**
686 * xmlUnlockLibrary:
687 *
688 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
689 * library.
690 */
691void
692xmlUnlockLibrary(void)
693{
Daniel Veillard6f350292001-10-14 09:56:15 +0000694#ifdef DEBUG_THREADS
695 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
696#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000697 xmlRMutexUnlock(xmlLibraryLock);
698}
699
700/**
701 * xmlInitThreads:
702 *
703 * xmlInitThreads() is used to to initialize all the thread related
704 * data of the libxml2 library.
705 */
706void
707xmlInitThreads(void)
708{
Daniel Veillard6f350292001-10-14 09:56:15 +0000709#ifdef DEBUG_THREADS
710 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
711#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000712#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000713 InitializeCriticalSection(&cleanup_helpers_cs);
714#endif
Daniel Veillarddbfe05a2005-05-04 09:18:00 +0000715#ifdef HAVE_PTHREAD_H
716 if (libxml_is_threaded == -1) {
717 if ((pthread_once != NULL) &&
718 (pthread_getspecific != NULL) &&
719 (pthread_setspecific != NULL) &&
720 (pthread_key_create != NULL) &&
721 (pthread_mutex_init != NULL) &&
722 (pthread_mutex_destroy != NULL) &&
723 (pthread_mutex_lock != NULL) &&
724 (pthread_mutex_unlock != NULL) &&
725 (pthread_cond_init != NULL) &&
726 (pthread_equal != NULL) &&
727 (pthread_self != NULL) &&
728 (pthread_key_create != NULL) &&
729 (pthread_cond_signal != NULL)) {
730 libxml_is_threaded = 1;
731/* fprintf(stderr, "Running multithreaded\n"); */
732 } else {
733/* fprintf(stderr, "Running without multithread\n"); */
734 libxml_is_threaded = 0;
735 }
736 }
737#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000738}
739
740/**
741 * xmlCleanupThreads:
742 *
743 * xmlCleanupThreads() is used to to cleanup all the thread related
744 * data of the libxml2 library once processing has ended.
745 */
746void
747xmlCleanupThreads(void)
748{
Daniel Veillard6f350292001-10-14 09:56:15 +0000749#ifdef DEBUG_THREADS
750 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
751#endif
Daniel Veillardc790bf42003-10-11 10:50:10 +0000752#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000753 if (globalkey != TLS_OUT_OF_INDEXES) {
754 xmlGlobalStateCleanupHelperParams * p;
755 EnterCriticalSection(&cleanup_helpers_cs);
756 p = cleanup_helpers_head;
757 while (p != NULL) {
758 xmlGlobalStateCleanupHelperParams * temp = p;
759 p = p->next;
760 xmlFreeGlobalState(temp->memory);
761 free(temp);
762 }
763 cleanup_helpers_head = 0;
764 LeaveCriticalSection(&cleanup_helpers_cs);
765 TlsFree(globalkey);
766 globalkey = TLS_OUT_OF_INDEXES;
767 }
768 DeleteCriticalSection(&cleanup_helpers_cs);
769#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000770}
Daniel Veillard6f350292001-10-14 09:56:15 +0000771
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000772#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000773/**
774 * xmlOnceInit
775 *
776 * xmlOnceInit() is used to initialize the value of mainthread for use
777 * in other routines. This function should only be called using
778 * pthread_once() in association with the once_control variable to ensure
779 * that the function is only called once. See man pthread_once for more
780 * details.
781 */
782static void
783xmlOnceInit(void) {
784#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000785 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000786 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000787#endif
788
789#if defined(HAVE_WIN32_THREADS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000790 if (!run_once.done) {
Daniel Veillard36616dd2005-02-25 07:31:49 +0000791 if (InterlockedIncrement(&run_once.control) == 1)
Daniel Veillard62121e22005-02-24 15:38:52 +0000792 {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000793#if !defined(HAVE_COMPILER_TLS)
Daniel Veillard62121e22005-02-24 15:38:52 +0000794 globalkey = TlsAlloc();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000795#endif
Daniel Veillard62121e22005-02-24 15:38:52 +0000796 mainthread = GetCurrentThreadId();
797 run_once.done = 1;
798 }
799 else {
800 /* Another thread is working; give up our slice and
801 * wait until they're done. */
802 while (!run_once.done)
803 Sleep(0);
804 }
805 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000806#endif
Daniel Veillard82cb3192003-10-29 13:39:15 +0000807
808#ifdef HAVE_BEOS_THREADS
809 if (atomic_add(&run_once_init, 1) == 0) {
810 globalkey = tls_allocate();
811 tls_set(globalkey, NULL);
812 mainthread = find_thread(NULL);
813 } else
814 atomic_add(&run_once_init, -1);
815#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000816}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000817#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000818
819/**
William M. Brack7a821652003-08-15 07:27:40 +0000820 * DllMain:
821 * @hinstDLL: handle to DLL instance
822 * @fdwReason: Reason code for entry
823 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000824 *
825 * Entry point for Windows library. It is being used to free thread-specific
826 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000827 *
828 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000829 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000830#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
831#if defined(LIBXML_STATIC_FOR_DLL)
832BOOL WINAPI xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
833#else
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000834BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000835#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000836{
837 switch(fdwReason) {
838 case DLL_THREAD_DETACH:
839 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000840 xmlGlobalState *globalval = NULL;
841 xmlGlobalStateCleanupHelperParams * p =
842 (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
843 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
844 if (globalval) {
845 xmlFreeGlobalState(globalval);
846 TlsSetValue(globalkey,NULL);
847 }
848 if (p)
849 {
850 EnterCriticalSection(&cleanup_helpers_cs);
851 if (p == cleanup_helpers_head)
852 cleanup_helpers_head = p->next;
853 else
854 p->prev->next = p->next;
855 if (p->next != NULL)
856 p->next->prev = p->prev;
857 LeaveCriticalSection(&cleanup_helpers_cs);
858 free(p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000859 }
860 }
861 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000862 }
863 return TRUE;
864}
865#endif
Daniel Veillard5d4644e2005-04-01 13:11:58 +0000866#define bottom_threads
867#include "elfgcchack.h"