blob: 777339cc065504f71f3ed14c1e512b52a3c9b19d [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 Veillarde28313b2001-12-06 14:08:31 +0000102static void xmlOnceInit(void);
Daniel Veillardb8478642001-10-12 17:29:10 +0000103
104/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000105 * xmlNewMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000106 *
107 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
108 * synchronizing access to data.
109 *
110 * Returns a new simple mutex pointer or NULL in case of error
111 */
112xmlMutexPtr
113xmlNewMutex(void)
114{
115 xmlMutexPtr tok;
116
117 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
118 return (NULL);
119#ifdef HAVE_PTHREAD_H
120 pthread_mutex_init(&tok->lock, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000121#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000122 tok->mutex = CreateMutex(NULL, FALSE, NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000123#endif
124 return (tok);
125}
126
127/**
128 * xmlFreeMutex:
129 * @tok: the simple mutex
130 *
131 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
132 * struct.
133 */
134void
135xmlFreeMutex(xmlMutexPtr tok)
136{
137#ifdef HAVE_PTHREAD_H
138 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000139#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000140 CloseHandle(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000141#endif
142 free(tok);
143}
144
145/**
146 * xmlMutexLock:
147 * @tok: the simple mutex
148 *
149 * xmlMutexLock() is used to lock a libxml2 token.
150 */
151void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000152xmlMutexLock(xmlMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000153{
154#ifdef HAVE_PTHREAD_H
155 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000156#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000157 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillardb8478642001-10-12 17:29:10 +0000158#endif
159
160}
161
162/**
163 * xmlMutexUnlock:
164 * @tok: the simple mutex
165 *
166 * xmlMutexUnlock() is used to unlock a libxml2 token.
167 */
168void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000169xmlMutexUnlock(xmlMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000170{
171#ifdef HAVE_PTHREAD_H
172 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000173#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000174 ReleaseMutex(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000175#endif
176}
177
178/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000179 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000180 *
181 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
182 * synchronizing access to data. token_r is a re-entrant lock and thus useful
183 * for synchronizing access to data structures that may be manipulated in a
184 * recursive fashion.
185 *
186 * Returns the new reentrant mutex pointer or NULL in case of error
187 */
188xmlRMutexPtr
189xmlNewRMutex(void)
190{
191 xmlRMutexPtr tok;
192
193 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
194 return (NULL);
195#ifdef HAVE_PTHREAD_H
196 pthread_mutex_init(&tok->lock, NULL);
197 tok->held = 0;
198 tok->waiters = 0;
William M. Brack59002e72003-07-04 17:01:59 +0000199 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000200#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000201 InitializeCriticalSection(&tok->cs);
202 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000203#endif
204 return (tok);
205}
206
207/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000208 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000209 * @tok: the reentrant mutex
210 *
211 * xmlRFreeMutex() is used to reclaim resources associated with a
212 * reentrant mutex.
213 */
214void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000215xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000216{
217#ifdef HAVE_PTHREAD_H
218 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000219#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000220 DeleteCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000221#endif
222 free(tok);
223}
224
225/**
226 * xmlRMutexLock:
227 * @tok: the reentrant mutex
228 *
229 * xmlRMutexLock() is used to lock a libxml2 token_r.
230 */
231void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000232xmlRMutexLock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000233{
234#ifdef HAVE_PTHREAD_H
235 pthread_mutex_lock(&tok->lock);
236 if (tok->held) {
237 if (pthread_equal(tok->tid, pthread_self())) {
238 tok->held++;
239 pthread_mutex_unlock(&tok->lock);
240 return;
241 } else {
242 tok->waiters++;
243 while (tok->held)
244 pthread_cond_wait(&tok->cv, &tok->lock);
245 tok->waiters--;
246 }
247 }
248 tok->tid = pthread_self();
249 tok->held = 1;
250 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000251#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000252 EnterCriticalSection(&tok->cs);
253 ++tok->count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000254#endif
255}
256
257/**
258 * xmlRMutexUnlock:
259 * @tok: the reentrant mutex
260 *
261 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
262 */
263void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000264xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000265{
266#ifdef HAVE_PTHREAD_H
267 pthread_mutex_lock(&tok->lock);
268 tok->held--;
269 if (tok->held == 0) {
270 if (tok->waiters)
271 pthread_cond_signal(&tok->cv);
272 tok->tid = 0;
273 }
274 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000275#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000276 if (!--tok->count)
277 LeaveCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000278#endif
279}
280
281/************************************************************************
282 * *
283 * Per thread global state handling *
284 * *
285 ************************************************************************/
286
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000287#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardb8478642001-10-12 17:29:10 +0000288/**
289 * xmlFreeGlobalState:
290 * @state: a thread global state
291 *
292 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
293 * global state. It is is used here to reclaim memory resources.
294 */
295static void
296xmlFreeGlobalState(void *state)
297{
298 free(state);
299}
300
301/**
302 * xmlNewGlobalState:
303 *
304 * xmlNewGlobalState() allocates a global state. This structure is used to
305 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000306 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000307 *
308 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
309 */
310static xmlGlobalStatePtr
311xmlNewGlobalState(void)
312{
313 xmlGlobalState *gs;
314
315 gs = malloc(sizeof(xmlGlobalState));
316 if (gs == NULL)
317 return(NULL);
318
William M. Brack8b2c7f12002-11-22 05:07:29 +0000319 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000320 xmlInitializeGlobalState(gs);
321 return (gs);
322}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000323#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000324
325
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000326#ifdef HAVE_WIN32_THREADS
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000327#if !defined(HAVE_COMPILER_TLS) && defined(LIBXML_STATIC)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000328typedef struct _xmlGlobalStateCleanupHelperParams
329{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000330 HANDLE thread;
331 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000332} xmlGlobalStateCleanupHelperParams;
333
Daniel Veillard01c13b52002-12-10 15:19:08 +0000334static void xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000335{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000336 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
337 WaitForSingleObject(params->thread, INFINITE);
338 CloseHandle(params->thread);
339 xmlFreeGlobalState(params->memory);
340 free(params);
341 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000342}
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000343#endif /* HAVE_COMPILER_TLS && LIBXML_STATIC */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000344#endif /* HAVE_WIN32_THREADS */
345
Daniel Veillard01c13b52002-12-10 15:19:08 +0000346/**
347 * xmlGetGlobalState:
348 *
349 * xmlGetGlobalState() is called to retrieve the global state for a thread.
350 *
351 * Returns the thread global state or NULL in case of error
352 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000353xmlGlobalStatePtr
354xmlGetGlobalState(void)
355{
356#ifdef HAVE_PTHREAD_H
357 xmlGlobalState *globalval;
358
Daniel Veillarde28313b2001-12-06 14:08:31 +0000359 pthread_once(&once_control, xmlOnceInit);
360
Daniel Veillardb8478642001-10-12 17:29:10 +0000361 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000362 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000363 xmlGlobalState *tsd = xmlNewGlobalState();
364
365 pthread_setspecific(globalkey, tsd);
366 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000367 }
368 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000369#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000370#if defined(HAVE_COMPILER_TLS)
371 if (!tlstate_inited) {
372 tlstate_inited = 1;
373 xmlInitializeGlobalState(&tlstate);
374 }
375 return &tlstate;
376#else /* HAVE_COMPILER_TLS */
377 xmlGlobalState *globalval;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000378
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000379 if (run_once_init) {
380 run_once_init = 0;
381 xmlOnceInit();
382 }
383 if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
384 xmlGlobalState *tsd = xmlNewGlobalState();
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000385#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000386 xmlGlobalStateCleanupHelperParams *p =
387 (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
388 p->memory = tsd;
389 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
390 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000391#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000392 TlsSetValue(globalkey, tsd);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000393#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000394 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000395#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000396
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000397 return (tsd);
398 }
399 return (globalval);
400#endif /* HAVE_COMPILER_TLS */
Daniel Veillard6f350292001-10-14 09:56:15 +0000401#else
402 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000403#endif
404}
405
Daniel Veillardb8478642001-10-12 17:29:10 +0000406/************************************************************************
407 * *
408 * Library wide thread interfaces *
409 * *
410 ************************************************************************/
411
412/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000413 * xmlGetThreadId:
414 *
415 * xmlGetThreadId() find the current thread ID number
416 *
417 * Returns the current thread ID number
418 */
419int
420xmlGetThreadId(void)
421{
422#ifdef HAVE_PTHREAD_H
423 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000424#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000425 return GetCurrentThreadId();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000426#else
427 return((int) 0);
428#endif
429}
430
431/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000432 * xmlIsMainThread:
433 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000434 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000435 *
436 * Returns 1 if the current thread is the main thread, 0 otherwise
437 */
438int
439xmlIsMainThread(void)
440{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000441#ifdef HAVE_PTHREAD_H
442 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000443#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000444 if (run_once_init) {
445 run_once_init = 0;
446 xmlOnceInit ();
447 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000448#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000449
450#ifdef DEBUG_THREADS
451 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
452#endif
453#ifdef HAVE_PTHREAD_H
454 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000455#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000456 return(mainthread == GetCurrentThreadId ());
Daniel Veillard6f350292001-10-14 09:56:15 +0000457#else
458 return(1);
459#endif
460}
461
462/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000463 * xmlLockLibrary:
464 *
465 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
466 * library.
467 */
468void
469xmlLockLibrary(void)
470{
Daniel Veillard6f350292001-10-14 09:56:15 +0000471#ifdef DEBUG_THREADS
472 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
473#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000474 xmlRMutexLock(xmlLibraryLock);
475}
476
477/**
478 * xmlUnlockLibrary:
479 *
480 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
481 * library.
482 */
483void
484xmlUnlockLibrary(void)
485{
Daniel Veillard6f350292001-10-14 09:56:15 +0000486#ifdef DEBUG_THREADS
487 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
488#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000489 xmlRMutexUnlock(xmlLibraryLock);
490}
491
492/**
493 * xmlInitThreads:
494 *
495 * xmlInitThreads() is used to to initialize all the thread related
496 * data of the libxml2 library.
497 */
498void
499xmlInitThreads(void)
500{
Daniel Veillard6f350292001-10-14 09:56:15 +0000501#ifdef DEBUG_THREADS
502 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
503#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000504}
505
506/**
507 * xmlCleanupThreads:
508 *
509 * xmlCleanupThreads() is used to to cleanup all the thread related
510 * data of the libxml2 library once processing has ended.
511 */
512void
513xmlCleanupThreads(void)
514{
Daniel Veillard6f350292001-10-14 09:56:15 +0000515#ifdef DEBUG_THREADS
516 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
517#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000518}
Daniel Veillard6f350292001-10-14 09:56:15 +0000519
Daniel Veillarde28313b2001-12-06 14:08:31 +0000520/**
521 * xmlOnceInit
522 *
523 * xmlOnceInit() is used to initialize the value of mainthread for use
524 * in other routines. This function should only be called using
525 * pthread_once() in association with the once_control variable to ensure
526 * that the function is only called once. See man pthread_once for more
527 * details.
528 */
529static void
530xmlOnceInit(void) {
531#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000532 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000533 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000534#endif
535
536#if defined(HAVE_WIN32_THREADS)
537#if !defined(HAVE_COMPILER_TLS)
538 globalkey = TlsAlloc();
539#endif
540 mainthread = GetCurrentThreadId();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000541#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000542}
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000543
544/**
545 * DllMain
546 *
547 * Entry point for Windows library. It is being used to free thread-specific
548 * storage.
549 */
550#if defined(HAVE_WIN32_THREADS) && !defined(LIBXML_STATIC)
551BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
552{
553 switch(fdwReason) {
554 case DLL_THREAD_DETACH:
555 if (globalkey != TLS_OUT_OF_INDEXES) {
556 xmlGlobalState *globalval = (xmlGlobalState *)TlsGetValue(globalkey);
557 if (globalval) {
558 xmlFreeGlobalState(globalval);
559 TlsSetValue(globalkey, NULL);
560 }
561 }
562 break;
563 case DLL_PROCESS_DETACH:
564 if (globalkey != TLS_OUT_OF_INDEXES) {
565 TlsFree(globalkey);
566 globalkey = TLS_OUT_OF_INDEXES;
567 }
568 break;
569 }
570 return TRUE;
571}
572#endif
573