blob: c6464495bbdf1052960b8ff325746ac763c565d5 [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{
Daniel Veillarddf101d82003-07-08 14:03:36 +0000137 if (tok == NULL) return;
138
Daniel Veillardb8478642001-10-12 17:29:10 +0000139#ifdef HAVE_PTHREAD_H
140 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000141#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000142 CloseHandle(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000143#endif
144 free(tok);
145}
146
147/**
148 * xmlMutexLock:
149 * @tok: the simple mutex
150 *
151 * xmlMutexLock() is used to lock a libxml2 token.
152 */
153void
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000154xmlMutexLock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000155{
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000156 if (tok == NULL)
157 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000158#ifdef HAVE_PTHREAD_H
159 pthread_mutex_lock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000160#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000161 WaitForSingleObject(tok->mutex, INFINITE);
Daniel Veillardb8478642001-10-12 17:29:10 +0000162#endif
163
164}
165
166/**
167 * xmlMutexUnlock:
168 * @tok: the simple mutex
169 *
170 * xmlMutexUnlock() is used to unlock a libxml2 token.
171 */
172void
Daniel Veillard5805be22003-08-28 08:03:23 +0000173xmlMutexUnlock(xmlMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000174{
Daniel Veillard5805be22003-08-28 08:03:23 +0000175 if (tok == NULL)
176 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000177#ifdef HAVE_PTHREAD_H
178 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000179#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000180 ReleaseMutex(tok->mutex);
Daniel Veillardb8478642001-10-12 17:29:10 +0000181#endif
182}
183
184/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000185 * xmlNewRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000186 *
187 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
188 * synchronizing access to data. token_r is a re-entrant lock and thus useful
189 * for synchronizing access to data structures that may be manipulated in a
190 * recursive fashion.
191 *
192 * Returns the new reentrant mutex pointer or NULL in case of error
193 */
194xmlRMutexPtr
195xmlNewRMutex(void)
196{
197 xmlRMutexPtr tok;
198
199 if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
200 return (NULL);
201#ifdef HAVE_PTHREAD_H
202 pthread_mutex_init(&tok->lock, NULL);
203 tok->held = 0;
204 tok->waiters = 0;
William M. Brack59002e72003-07-04 17:01:59 +0000205 pthread_cond_init(&tok->cv, NULL);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000206#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000207 InitializeCriticalSection(&tok->cs);
208 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000209#endif
210 return (tok);
211}
212
213/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000214 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000215 * @tok: the reentrant mutex
216 *
217 * xmlRFreeMutex() is used to reclaim resources associated with a
218 * reentrant mutex.
219 */
220void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000221xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000222{
223#ifdef HAVE_PTHREAD_H
224 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000225#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000226 DeleteCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000227#endif
228 free(tok);
229}
230
231/**
232 * xmlRMutexLock:
233 * @tok: the reentrant mutex
234 *
235 * xmlRMutexLock() is used to lock a libxml2 token_r.
236 */
237void
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000238xmlRMutexLock(xmlRMutexPtr tok)
Daniel Veillardb8478642001-10-12 17:29:10 +0000239{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000240 if (tok == NULL)
241 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000242#ifdef HAVE_PTHREAD_H
243 pthread_mutex_lock(&tok->lock);
244 if (tok->held) {
245 if (pthread_equal(tok->tid, pthread_self())) {
246 tok->held++;
247 pthread_mutex_unlock(&tok->lock);
248 return;
249 } else {
250 tok->waiters++;
251 while (tok->held)
252 pthread_cond_wait(&tok->cv, &tok->lock);
253 tok->waiters--;
254 }
255 }
256 tok->tid = pthread_self();
257 tok->held = 1;
258 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000259#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000260 EnterCriticalSection(&tok->cs);
261 ++tok->count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000262#endif
263}
264
265/**
266 * xmlRMutexUnlock:
267 * @tok: the reentrant mutex
268 *
269 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
270 */
271void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000272xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000273{
Daniel Veillard5f1e1f82003-09-11 23:35:09 +0000274 if (tok == NULL)
275 return;
Daniel Veillardb8478642001-10-12 17:29:10 +0000276#ifdef HAVE_PTHREAD_H
277 pthread_mutex_lock(&tok->lock);
278 tok->held--;
279 if (tok->held == 0) {
280 if (tok->waiters)
281 pthread_cond_signal(&tok->cv);
282 tok->tid = 0;
283 }
284 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000285#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000286 if (!--tok->count)
287 LeaveCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000288#endif
289}
290
291/************************************************************************
292 * *
293 * Per thread global state handling *
294 * *
295 ************************************************************************/
296
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000297#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardb8478642001-10-12 17:29:10 +0000298/**
299 * xmlFreeGlobalState:
300 * @state: a thread global state
301 *
302 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
303 * global state. It is is used here to reclaim memory resources.
304 */
305static void
306xmlFreeGlobalState(void *state)
307{
308 free(state);
309}
310
311/**
312 * xmlNewGlobalState:
313 *
314 * xmlNewGlobalState() allocates a global state. This structure is used to
315 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000316 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000317 *
318 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
319 */
320static xmlGlobalStatePtr
321xmlNewGlobalState(void)
322{
323 xmlGlobalState *gs;
324
325 gs = malloc(sizeof(xmlGlobalState));
326 if (gs == NULL)
327 return(NULL);
328
William M. Brack8b2c7f12002-11-22 05:07:29 +0000329 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000330 xmlInitializeGlobalState(gs);
331 return (gs);
332}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000333#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000334
335
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000336#ifdef HAVE_WIN32_THREADS
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000337#if !defined(HAVE_COMPILER_TLS) && defined(LIBXML_STATIC)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000338typedef struct _xmlGlobalStateCleanupHelperParams
339{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000340 HANDLE thread;
341 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000342} xmlGlobalStateCleanupHelperParams;
343
Daniel Veillard01c13b52002-12-10 15:19:08 +0000344static void xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000345{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000346 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
347 WaitForSingleObject(params->thread, INFINITE);
348 CloseHandle(params->thread);
349 xmlFreeGlobalState(params->memory);
350 free(params);
351 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000352}
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000353#endif /* HAVE_COMPILER_TLS && LIBXML_STATIC */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000354#endif /* HAVE_WIN32_THREADS */
355
Daniel Veillard01c13b52002-12-10 15:19:08 +0000356/**
357 * xmlGetGlobalState:
358 *
359 * xmlGetGlobalState() is called to retrieve the global state for a thread.
360 *
361 * Returns the thread global state or NULL in case of error
362 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000363xmlGlobalStatePtr
364xmlGetGlobalState(void)
365{
366#ifdef HAVE_PTHREAD_H
367 xmlGlobalState *globalval;
368
Daniel Veillarde28313b2001-12-06 14:08:31 +0000369 pthread_once(&once_control, xmlOnceInit);
370
Daniel Veillardb8478642001-10-12 17:29:10 +0000371 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000372 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000373 xmlGlobalState *tsd = xmlNewGlobalState();
374
375 pthread_setspecific(globalkey, tsd);
376 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000377 }
378 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000379#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000380#if defined(HAVE_COMPILER_TLS)
381 if (!tlstate_inited) {
382 tlstate_inited = 1;
383 xmlInitializeGlobalState(&tlstate);
384 }
385 return &tlstate;
386#else /* HAVE_COMPILER_TLS */
387 xmlGlobalState *globalval;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000388
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000389 if (run_once_init) {
390 run_once_init = 0;
391 xmlOnceInit();
392 }
393 if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
394 xmlGlobalState *tsd = xmlNewGlobalState();
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000395#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000396 xmlGlobalStateCleanupHelperParams *p =
397 (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
398 p->memory = tsd;
399 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
400 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000401#endif
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000402 TlsSetValue(globalkey, tsd);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000403#if defined(LIBXML_STATIC)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000404 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000405#endif
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000406
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000407 return (tsd);
408 }
409 return (globalval);
410#endif /* HAVE_COMPILER_TLS */
Daniel Veillard6f350292001-10-14 09:56:15 +0000411#else
412 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000413#endif
414}
415
Daniel Veillardb8478642001-10-12 17:29:10 +0000416/************************************************************************
417 * *
418 * Library wide thread interfaces *
419 * *
420 ************************************************************************/
421
422/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000423 * xmlGetThreadId:
424 *
425 * xmlGetThreadId() find the current thread ID number
426 *
427 * Returns the current thread ID number
428 */
429int
430xmlGetThreadId(void)
431{
432#ifdef HAVE_PTHREAD_H
433 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000434#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000435 return GetCurrentThreadId();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000436#else
437 return((int) 0);
438#endif
439}
440
441/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000442 * xmlIsMainThread:
443 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000444 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000445 *
446 * Returns 1 if the current thread is the main thread, 0 otherwise
447 */
448int
449xmlIsMainThread(void)
450{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000451#ifdef HAVE_PTHREAD_H
452 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000453#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000454 if (run_once_init) {
455 run_once_init = 0;
456 xmlOnceInit ();
457 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000458#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000459
460#ifdef DEBUG_THREADS
461 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
462#endif
463#ifdef HAVE_PTHREAD_H
464 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000465#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000466 return(mainthread == GetCurrentThreadId ());
Daniel Veillard6f350292001-10-14 09:56:15 +0000467#else
468 return(1);
469#endif
470}
471
472/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000473 * xmlLockLibrary:
474 *
475 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
476 * library.
477 */
478void
479xmlLockLibrary(void)
480{
Daniel Veillard6f350292001-10-14 09:56:15 +0000481#ifdef DEBUG_THREADS
482 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
483#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000484 xmlRMutexLock(xmlLibraryLock);
485}
486
487/**
488 * xmlUnlockLibrary:
489 *
490 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
491 * library.
492 */
493void
494xmlUnlockLibrary(void)
495{
Daniel Veillard6f350292001-10-14 09:56:15 +0000496#ifdef DEBUG_THREADS
497 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
498#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000499 xmlRMutexUnlock(xmlLibraryLock);
500}
501
502/**
503 * xmlInitThreads:
504 *
505 * xmlInitThreads() is used to to initialize all the thread related
506 * data of the libxml2 library.
507 */
508void
509xmlInitThreads(void)
510{
Daniel Veillard6f350292001-10-14 09:56:15 +0000511#ifdef DEBUG_THREADS
512 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
513#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000514}
515
516/**
517 * xmlCleanupThreads:
518 *
519 * xmlCleanupThreads() is used to to cleanup all the thread related
520 * data of the libxml2 library once processing has ended.
521 */
522void
523xmlCleanupThreads(void)
524{
Daniel Veillard6f350292001-10-14 09:56:15 +0000525#ifdef DEBUG_THREADS
526 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
527#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000528}
Daniel Veillard6f350292001-10-14 09:56:15 +0000529
Daniel Veillarde28313b2001-12-06 14:08:31 +0000530/**
531 * xmlOnceInit
532 *
533 * xmlOnceInit() is used to initialize the value of mainthread for use
534 * in other routines. This function should only be called using
535 * pthread_once() in association with the once_control variable to ensure
536 * that the function is only called once. See man pthread_once for more
537 * details.
538 */
539static void
540xmlOnceInit(void) {
541#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000542 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000543 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000544#endif
545
546#if defined(HAVE_WIN32_THREADS)
547#if !defined(HAVE_COMPILER_TLS)
548 globalkey = TlsAlloc();
549#endif
550 mainthread = GetCurrentThreadId();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000551#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000552}
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000553
554/**
William M. Brack7a821652003-08-15 07:27:40 +0000555 * DllMain:
556 * @hinstDLL: handle to DLL instance
557 * @fdwReason: Reason code for entry
558 * @lpvReserved: generic pointer (depends upon reason code)
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000559 *
560 * Entry point for Windows library. It is being used to free thread-specific
561 * storage.
William M. Brack7a821652003-08-15 07:27:40 +0000562 *
563 * Returns TRUE always
Igor Zlatkovicd58a42d2003-05-17 10:55:15 +0000564 */
565#if defined(HAVE_WIN32_THREADS) && !defined(LIBXML_STATIC)
566BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
567{
568 switch(fdwReason) {
569 case DLL_THREAD_DETACH:
570 if (globalkey != TLS_OUT_OF_INDEXES) {
571 xmlGlobalState *globalval = (xmlGlobalState *)TlsGetValue(globalkey);
572 if (globalval) {
573 xmlFreeGlobalState(globalval);
574 TlsSetValue(globalkey, NULL);
575 }
576 }
577 break;
578 case DLL_PROCESS_DETACH:
579 if (globalkey != TLS_OUT_OF_INDEXES) {
580 TlsFree(globalkey);
581 globalkey = TLS_OUT_OF_INDEXES;
582 }
583 break;
584 }
585 return TRUE;
586}
587#endif
588