blob: b51caa21f3d9c0db6ca53b84752706e7afde5033 [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
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000339#if !defined(HAVE_COMPILER_TLS) && defined(LIBXML_STATIC)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000340typedef struct _xmlGlobalStateCleanupHelperParams
341{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000342 HANDLE thread;
343 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000344} xmlGlobalStateCleanupHelperParams;
345
Daniel Veillard01c13b52002-12-10 15:19:08 +0000346static void xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000347{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000348 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
349 WaitForSingleObject(params->thread, INFINITE);
350 CloseHandle(params->thread);
351 xmlFreeGlobalState(params->memory);
352 free(params);
353 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000354}
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000355#endif /* HAVE_COMPILER_TLS && LIBXML_STATIC */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000356#endif /* HAVE_WIN32_THREADS */
357
Daniel Veillard01c13b52002-12-10 15:19:08 +0000358/**
359 * xmlGetGlobalState:
360 *
361 * xmlGetGlobalState() is called to retrieve the global state for a thread.
362 *
363 * Returns the thread global state or NULL in case of error
364 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000365xmlGlobalStatePtr
366xmlGetGlobalState(void)
367{
368#ifdef HAVE_PTHREAD_H
369 xmlGlobalState *globalval;
370
Daniel Veillarde28313b2001-12-06 14:08:31 +0000371 pthread_once(&once_control, xmlOnceInit);
372
Daniel Veillardb8478642001-10-12 17:29:10 +0000373 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000374 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000375 xmlGlobalState *tsd = xmlNewGlobalState();
376
377 pthread_setspecific(globalkey, tsd);
378 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000379 }
380 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000381#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000382#if defined(HAVE_COMPILER_TLS)
383 if (!tlstate_inited) {
384 tlstate_inited = 1;
385 xmlInitializeGlobalState(&tlstate);
386 }
387 return &tlstate;
388#else /* HAVE_COMPILER_TLS */
389 xmlGlobalState *globalval;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000390
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000391 if (run_once_init) {
392 run_once_init = 0;
393 xmlOnceInit();
394 }
395 if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
396 xmlGlobalState *tsd = xmlNewGlobalState();
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000397#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000398 xmlGlobalStateCleanupHelperParams *p =
399 (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
400 p->memory = tsd;
401 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
402 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000403#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000404 TlsSetValue(globalkey, tsd);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000405#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000406 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000407#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000408
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000409 return (tsd);
410 }
411 return (globalval);
412#endif /* HAVE_COMPILER_TLS */
Daniel Veillard6f350292001-10-14 09:56:15 +0000413#else
414 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000415#endif
416}
417
Daniel Veillardb8478642001-10-12 17:29:10 +0000418/************************************************************************
419 * *
420 * Library wide thread interfaces *
421 * *
422 ************************************************************************/
423
424/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000425 * xmlGetThreadId:
426 *
427 * xmlGetThreadId() find the current thread ID number
428 *
429 * Returns the current thread ID number
430 */
431int
432xmlGetThreadId(void)
433{
434#ifdef HAVE_PTHREAD_H
435 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000436#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000437 return GetCurrentThreadId();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000438#else
439 return((int) 0);
440#endif
441}
442
443/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000444 * xmlIsMainThread:
445 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000446 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000447 *
448 * Returns 1 if the current thread is the main thread, 0 otherwise
449 */
450int
451xmlIsMainThread(void)
452{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000453#ifdef HAVE_PTHREAD_H
454 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000455#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000456 if (run_once_init) {
457 run_once_init = 0;
458 xmlOnceInit ();
459 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000460#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000461
462#ifdef DEBUG_THREADS
463 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
464#endif
465#ifdef HAVE_PTHREAD_H
466 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000467#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000468 return(mainthread == GetCurrentThreadId ());
Daniel Veillard6f350292001-10-14 09:56:15 +0000469#else
470 return(1);
471#endif
472}
473
474/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000475 * xmlLockLibrary:
476 *
477 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
478 * library.
479 */
480void
481xmlLockLibrary(void)
482{
Daniel Veillard6f350292001-10-14 09:56:15 +0000483#ifdef DEBUG_THREADS
484 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
485#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000486 xmlRMutexLock(xmlLibraryLock);
487}
488
489/**
490 * xmlUnlockLibrary:
491 *
492 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
493 * library.
494 */
495void
496xmlUnlockLibrary(void)
497{
Daniel Veillard6f350292001-10-14 09:56:15 +0000498#ifdef DEBUG_THREADS
499 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
500#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000501 xmlRMutexUnlock(xmlLibraryLock);
502}
503
504/**
505 * xmlInitThreads:
506 *
507 * xmlInitThreads() is used to to initialize all the thread related
508 * data of the libxml2 library.
509 */
510void
511xmlInitThreads(void)
512{
Daniel Veillard6f350292001-10-14 09:56:15 +0000513#ifdef DEBUG_THREADS
514 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
515#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000516}
517
518/**
519 * xmlCleanupThreads:
520 *
521 * xmlCleanupThreads() is used to to cleanup all the thread related
522 * data of the libxml2 library once processing has ended.
523 */
524void
525xmlCleanupThreads(void)
526{
Daniel Veillard6f350292001-10-14 09:56:15 +0000527#ifdef DEBUG_THREADS
528 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
529#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000530}
Daniel Veillard6f350292001-10-14 09:56:15 +0000531
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000532#ifdef LIBXML_THREAD_ENABLED
Daniel Veillarde28313b2001-12-06 14:08:31 +0000533/**
534 * xmlOnceInit
535 *
536 * xmlOnceInit() is used to initialize the value of mainthread for use
537 * in other routines. This function should only be called using
538 * pthread_once() in association with the once_control variable to ensure
539 * that the function is only called once. See man pthread_once for more
540 * details.
541 */
542static void
543xmlOnceInit(void) {
544#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000545 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000546 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000547#endif
548
549#if defined(HAVE_WIN32_THREADS)
550#if !defined(HAVE_COMPILER_TLS)
551 globalkey = TlsAlloc();
552#endif
553 mainthread = GetCurrentThreadId();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000554#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000555}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000556#endif
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000557
558/**
William M. Brack7a821652003-08-15 07:27:40 +0000559 * DllMain:
560 * @hinstDLL: handle to DLL instance
561 * @fdwReason: Reason code for entry
562 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000563 *
564 * Entry point for Windows library. It is being used to free thread-specific
565 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000566 *
567 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000568 */
569#if defined(HAVE_WIN32_THREADS) && !defined(LIBXML_STATIC)
570BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
571{
572 switch(fdwReason) {
573 case DLL_THREAD_DETACH:
574 if (globalkey != TLS_OUT_OF_INDEXES) {
575 xmlGlobalState *globalval = (xmlGlobalState *)TlsGetValue(globalkey);
576 if (globalval) {
577 xmlFreeGlobalState(globalval);
578 TlsSetValue(globalkey, NULL);
579 }
580 }
581 break;
582 case DLL_PROCESS_DETACH:
583 if (globalkey != TLS_OUT_OF_INDEXES) {
584 TlsFree(globalkey);
585 globalkey = TLS_OUT_OF_INDEXES;
586 }
587 break;
588 }
589 return TRUE;
590}
591#endif
592