blob: 63d50b0ee12861a8894e22d6e4ec1cda6653202d [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
38#if defined(SOLARIS)
39#include <note.h>
40#endif
41
Daniel Veillard6f350292001-10-14 09:56:15 +000042/* #define DEBUG_THREADS */
Daniel Veillardb8478642001-10-12 17:29:10 +000043
44/*
45 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
46 * to avoid some crazyness since xmlMalloc/xmlFree may actually
47 * be hosted on allocated blocks needing them for the allocation ...
48 */
49
50/*
51 * xmlMutex are a simple mutual exception locks
52 */
53struct _xmlMutex {
54#ifdef HAVE_PTHREAD_H
55 pthread_mutex_t lock;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000056#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000057 HANDLE mutex;
Daniel Veillardb8478642001-10-12 17:29:10 +000058#else
59 int empty;
60#endif
61};
62
63/*
64 * xmlRMutex are reentrant mutual exception locks
65 */
66struct _xmlRMutex {
67#ifdef HAVE_PTHREAD_H
68 pthread_mutex_t lock;
69 unsigned int held;
70 unsigned int waiters;
71 pthread_t tid;
72 pthread_cond_t cv;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000073#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000074 CRITICAL_SECTION cs;
75 unsigned int count;
Daniel Veillardb8478642001-10-12 17:29:10 +000076#else
77 int empty;
78#endif
79};
80/*
81 * This module still has some internal static data.
82 * - xmlLibraryLock a global lock
83 * - globalkey used for per-thread data
Daniel Veillardb8478642001-10-12 17:29:10 +000084 */
Daniel Veillard6f350292001-10-14 09:56:15 +000085
Daniel Veillardb8478642001-10-12 17:29:10 +000086#ifdef HAVE_PTHREAD_H
Daniel Veillardb8478642001-10-12 17:29:10 +000087static pthread_key_t globalkey;
Daniel Veillard6f350292001-10-14 09:56:15 +000088static pthread_t mainthread;
Daniel Veillarde28313b2001-12-06 14:08:31 +000089static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000090#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000091#if defined(HAVE_COMPILER_TLS)
92static __declspec(thread) xmlGlobalState tlstate;
93static __declspec(thread) int tlstate_inited = 0;
94#else /* HAVE_COMPILER_TLS */
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +000095static DWORD globalkey = TLS_OUT_OF_INDEXES;
Igor Zlatkovicf2160a02002-10-31 15:58:42 +000096#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000097static DWORD mainthread;
98static int run_once_init = 1;
99#endif /* HAVE_WIN32_THREADS */
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000100
Daniel Veillardb8478642001-10-12 17:29:10 +0000101static xmlRMutexPtr xmlLibraryLock = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000102#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000103static void xmlOnceInit(void);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000104#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000105
106/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000107 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000108 *
109 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
110 * synchronizing access to data.
111 *
112 * Returns a new simple mutex pointer or NULL in case of error
113 */
114xmlMutexPtr
115xmlNewMutex(void)
116{
117 xmlMutexPtr tok;
118
119 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
120 return (NULL);
121#ifdef HAVE_PTHREAD_H
122 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000123#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000124 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000125#endif
126 return (tok);
127}
128
129/**
130 * xmlFreeMutex:
131 * @tok: the simple mutex
132 *
133 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
134 * struct.
135 */
136void
137xmlFreeMutex(xmlMutexPtr tok)
138{
Daniel Veillarddf101d82003-07-08 14:03:36 +0000139 if (tok == NULL) return;
140
Daniel Veillardb8478642001-10-12 17:29:10 +0000141#ifdef HAVE_PTHREAD_H
142 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000143#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000144 CloseHandle(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000145#endif
146 free(tok);
147}
148
149/**
150 * xmlMutexLock:
151 * @tok: the simple mutex
152 *
153 * xmlMutexLock() is used to lock a libxml2 token.
154 */
155void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000156xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000157{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000158 if (tok == NULL)
159 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000160#ifdef HAVE_PTHREAD_H
161 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000162#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000163 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillardb8478642001-10-12 17:29:10 +0000164#endif
165
166}
167
168/**
169 * xmlMutexUnlock:
170 * @tok: the simple mutex
171 *
172 * xmlMutexUnlock() is used to unlock a libxml2 token.
173 */
174void
Daniel Veillard5805be22003-08-28 08:03:23 +0000175xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000176{
Daniel Veillard5805be22003-08-28 08:03:23 +0000177 if (tok == NULL)
178 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000179#ifdef HAVE_PTHREAD_H
180 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000181#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000182 ReleaseMutex(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000183#endif
184}
185
186/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000187 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000188 *
189 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
190 * synchronizing access to data. token_r is a re-entrant lock and thus useful
191 * for synchronizing access to data structures that may be manipulated in a
192 * recursive fashion.
193 *
194 * Returns the new reentrant mutex pointer or NULL in case of error
195 */
196xmlRMutexPtr
197xmlNewRMutex(void)
198{
199 xmlRMutexPtr tok;
200
201 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
202 return (NULL);
203#ifdef HAVE_PTHREAD_H
204 pthread_mutex_init(&tok->lock, NULL);
205 tok->held = 0;
206 tok->waiters = 0;
William M. Brack59002e72003-07-04 17:01:59 +0000207 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000208#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000209 InitializeCriticalSection(&tok->cs);
210 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000211#endif
212 return (tok);
213}
214
215/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000216 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000217 * @tok: the reentrant mutex
218 *
219 * xmlRFreeMutex() is used to reclaim resources associated with a
220 * reentrant mutex.
221 */
222void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000223xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000224{
225#ifdef HAVE_PTHREAD_H
226 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000227#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000228 DeleteCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000229#endif
230 free(tok);
231}
232
233/**
234 * xmlRMutexLock:
235 * @tok: the reentrant mutex
236 *
237 * xmlRMutexLock() is used to lock a libxml2 token_r.
238 */
239void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000240xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000241{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000242 if (tok == NULL)
243 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000244#ifdef HAVE_PTHREAD_H
245 pthread_mutex_lock(&tok->lock);
246 if (tok->held) {
247 if (pthread_equal(tok->tid, pthread_self())) {
248 tok->held++;
249 pthread_mutex_unlock(&tok->lock);
250 return;
251 } else {
252 tok->waiters++;
253 while (tok->held)
254 pthread_cond_wait(&tok->cv, &tok->lock);
255 tok->waiters--;
256 }
257 }
258 tok->tid = pthread_self();
259 tok->held = 1;
260 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000261#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000262 EnterCriticalSection(&tok->cs);
263 ++tok->count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000264#endif
265}
266
267/**
268 * xmlRMutexUnlock:
269 * @tok: the reentrant mutex
270 *
271 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
272 */
273void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000274xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000275{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000276 if (tok == NULL)
277 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000278#ifdef HAVE_PTHREAD_H
279 pthread_mutex_lock(&tok->lock);
280 tok->held--;
281 if (tok->held == 0) {
282 if (tok->waiters)
283 pthread_cond_signal(&tok->cv);
284 tok->tid = 0;
285 }
286 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000287#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000288 if (!--tok->count)
289 LeaveCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000290#endif
291}
292
293/************************************************************************
294 * *
295 * Per thread global state handling *
296 * *
297 ************************************************************************/
298
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000299#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardb8478642001-10-12 17:29:10 +0000300/**
301 * xmlFreeGlobalState:
302 * @state: a thread global state
303 *
304 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
305 * global state. It is is used here to reclaim memory resources.
306 */
307static void
308xmlFreeGlobalState(void *state)
309{
310 free(state);
311}
312
313/**
314 * xmlNewGlobalState:
315 *
316 * xmlNewGlobalState() allocates a global state. This structure is used to
317 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000318 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000319 *
320 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
321 */
322static xmlGlobalStatePtr
323xmlNewGlobalState(void)
324{
325 xmlGlobalState *gs;
326
327 gs = malloc(sizeof(xmlGlobalState));
328 if (gs == NULL)
329 return(NULL);
330
William M. Brack8b2c7f12002-11-22 05:07:29 +0000331 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000332 xmlInitializeGlobalState(gs);
333 return (gs);
334}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000335#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000336
337
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000338#ifdef HAVE_WIN32_THREADS
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000339#if !defined(HAVE_COMPILER_TLS)
340#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000341typedef struct _xmlGlobalStateCleanupHelperParams
342{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000343 HANDLE thread;
344 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000345} xmlGlobalStateCleanupHelperParams;
346
Daniel Veillard01c13b52002-12-10 15:19:08 +0000347static void xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000348{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000349 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
350 WaitForSingleObject(params->thread, INFINITE);
351 CloseHandle(params->thread);
352 xmlFreeGlobalState(params->memory);
353 free(params);
354 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000355}
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000356#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
357
358typedef struct _xmlGlobalStateCleanupHelperParams
359{
360 void *memory;
361 struct _xmlGlobalStateCleanupHelperParams * prev;
362 struct _xmlGlobalStateCleanupHelperParams * next;
363} xmlGlobalStateCleanupHelperParams;
364
365static xmlGlobalStateCleanupHelperParams * cleanup_helpers_head = NULL;
366static CRITICAL_SECTION cleanup_helpers_cs;
367
368#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
369#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000370#endif /* HAVE_WIN32_THREADS */
371
Daniel Veillard01c13b52002-12-10 15:19:08 +0000372/**
373 * xmlGetGlobalState:
374 *
375 * xmlGetGlobalState() is called to retrieve the global state for a thread.
376 *
377 * Returns the thread global state or NULL in case of error
378 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000379xmlGlobalStatePtr
380xmlGetGlobalState(void)
381{
382#ifdef HAVE_PTHREAD_H
383 xmlGlobalState *globalval;
384
Daniel Veillarde28313b2001-12-06 14:08:31 +0000385 pthread_once(&once_control, xmlOnceInit);
386
Daniel Veillardb8478642001-10-12 17:29:10 +0000387 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000388 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000389 xmlGlobalState *tsd = xmlNewGlobalState();
390
391 pthread_setspecific(globalkey, tsd);
392 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000393 }
394 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000395#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000396#if defined(HAVE_COMPILER_TLS)
397 if (!tlstate_inited) {
398 tlstate_inited = 1;
399 xmlInitializeGlobalState(&tlstate);
400 }
401 return &tlstate;
402#else /* HAVE_COMPILER_TLS */
403 xmlGlobalState *globalval;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000404 xmlGlobalStateCleanupHelperParams * p;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000405
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000406 if (run_once_init) {
407 run_once_init = 0;
408 xmlOnceInit();
409 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000410#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
411 globalval = (xmlGlobalState *)TlsGetValue(globalkey);
412#else
413 p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
414 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
415#endif
416 if (globalval == NULL) {
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000417 xmlGlobalState *tsd = xmlNewGlobalState();
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000418 p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000419 p->memory = tsd;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000420#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000421 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
422 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
423 TlsSetValue(globalkey, tsd);
424 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000425#else
426 EnterCriticalSection(&cleanup_helpers_cs);
427 if (cleanup_helpers_head != NULL) {
428 cleanup_helpers_head->prev = p;
429 }
430 p->next = cleanup_helpers_head;
431 p->prev = NULL;
432 cleanup_helpers_head = p;
433 TlsSetValue(globalkey, p);
434 LeaveCriticalSection(&cleanup_helpers_cs);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000435#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000436
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000437 return (tsd);
438 }
439 return (globalval);
440#endif /* HAVE_COMPILER_TLS */
Daniel Veillard6f350292001-10-14 09:56:15 +0000441#else
442 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000443#endif
444}
445
Daniel Veillardb8478642001-10-12 17:29:10 +0000446/************************************************************************
447 * *
448 * Library wide thread interfaces *
449 * *
450 ************************************************************************/
451
452/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000453 * xmlGetThreadId:
454 *
455 * xmlGetThreadId() find the current thread ID number
456 *
457 * Returns the current thread ID number
458 */
459int
460xmlGetThreadId(void)
461{
462#ifdef HAVE_PTHREAD_H
463 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000464#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000465 return GetCurrentThreadId();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000466#else
467 return((int) 0);
468#endif
469}
470
471/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000472 * xmlIsMainThread:
473 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000474 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000475 *
476 * Returns 1 if the current thread is the main thread, 0 otherwise
477 */
478int
479xmlIsMainThread(void)
480{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000481#ifdef HAVE_PTHREAD_H
482 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000483#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000484 if (run_once_init) {
485 run_once_init = 0;
486 xmlOnceInit ();
487 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000488#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000489
490#ifdef DEBUG_THREADS
491 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
492#endif
493#ifdef HAVE_PTHREAD_H
494 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000495#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000496 return(mainthread == GetCurrentThreadId ());
Daniel Veillard6f350292001-10-14 09:56:15 +0000497#else
498 return(1);
499#endif
500}
501
502/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000503 * xmlLockLibrary:
504 *
505 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
506 * library.
507 */
508void
509xmlLockLibrary(void)
510{
Daniel Veillard6f350292001-10-14 09:56:15 +0000511#ifdef DEBUG_THREADS
512 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
513#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000514 xmlRMutexLock(xmlLibraryLock);
515}
516
517/**
518 * xmlUnlockLibrary:
519 *
520 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
521 * library.
522 */
523void
524xmlUnlockLibrary(void)
525{
Daniel Veillard6f350292001-10-14 09:56:15 +0000526#ifdef DEBUG_THREADS
527 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
528#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000529 xmlRMutexUnlock(xmlLibraryLock);
530}
531
532/**
533 * xmlInitThreads:
534 *
535 * xmlInitThreads() is used to to initialize all the thread related
536 * data of the libxml2 library.
537 */
538void
539xmlInitThreads(void)
540{
Daniel Veillard6f350292001-10-14 09:56:15 +0000541#ifdef DEBUG_THREADS
542 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
543#endif
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000544#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS)
545 InitializeCriticalSection(&cleanup_helpers_cs);
546#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000547}
548
549/**
550 * xmlCleanupThreads:
551 *
552 * xmlCleanupThreads() is used to to cleanup all the thread related
553 * data of the libxml2 library once processing has ended.
554 */
555void
556xmlCleanupThreads(void)
557{
Daniel Veillard6f350292001-10-14 09:56:15 +0000558#ifdef DEBUG_THREADS
559 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
560#endif
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000561#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS)
562 if (globalkey != TLS_OUT_OF_INDEXES) {
563 xmlGlobalStateCleanupHelperParams * p;
564 EnterCriticalSection(&cleanup_helpers_cs);
565 p = cleanup_helpers_head;
566 while (p != NULL) {
567 xmlGlobalStateCleanupHelperParams * temp = p;
568 p = p->next;
569 xmlFreeGlobalState(temp->memory);
570 free(temp);
571 }
572 cleanup_helpers_head = 0;
573 LeaveCriticalSection(&cleanup_helpers_cs);
574 TlsFree(globalkey);
575 globalkey = TLS_OUT_OF_INDEXES;
576 }
577 DeleteCriticalSection(&cleanup_helpers_cs);
578#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000579}
Daniel Veillard6f350292001-10-14 09:56:15 +0000580
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000581#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000582/**
583 * xmlOnceInit
584 *
585 * xmlOnceInit() is used to initialize the value of mainthread for use
586 * in other routines. This function should only be called using
587 * pthread_once() in association with the once_control variable to ensure
588 * that the function is only called once. See man pthread_once for more
589 * details.
590 */
591static void
592xmlOnceInit(void) {
593#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000594 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000595 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000596#endif
597
598#if defined(HAVE_WIN32_THREADS)
599#if !defined(HAVE_COMPILER_TLS)
600 globalkey = TlsAlloc();
601#endif
602 mainthread = GetCurrentThreadId();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000603#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000604}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000605#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000606
607/**
William M. Brack7a821652003-08-15 07:27:40 +0000608 * DllMain:
609 * @hinstDLL: handle to DLL instance
610 * @fdwReason: Reason code for entry
611 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000612 *
613 * Entry point for Windows library. It is being used to free thread-specific
614 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000615 *
616 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000617 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000618#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
619#if defined(LIBXML_STATIC_FOR_DLL)
620BOOL WINAPI xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
621#else
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000622BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000623#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000624{
625 switch(fdwReason) {
626 case DLL_THREAD_DETACH:
627 if (globalkey != TLS_OUT_OF_INDEXES) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000628 xmlGlobalState *globalval = NULL;
629 xmlGlobalStateCleanupHelperParams * p =
630 (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey);
631 globalval = (xmlGlobalState *)(p ? p->memory : NULL);
632 if (globalval) {
633 xmlFreeGlobalState(globalval);
634 TlsSetValue(globalkey,NULL);
635 }
636 if (p)
637 {
638 EnterCriticalSection(&cleanup_helpers_cs);
639 if (p == cleanup_helpers_head)
640 cleanup_helpers_head = p->next;
641 else
642 p->prev->next = p->next;
643 if (p->next != NULL)
644 p->next->prev = p->prev;
645 LeaveCriticalSection(&cleanup_helpers_cs);
646 free(p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000647 }
648 }
649 break;
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000650 }
651 return TRUE;
652}
653#endif