blob: 6aa5ecc307dc20fcb34da5d495a97ed7ba639c18 [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 */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +000095static DWORD globalkey;
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;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000199#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000200 InitializeCriticalSection(&tok->cs);
201 tok->count = 0;
Daniel Veillardb8478642001-10-12 17:29:10 +0000202#endif
203 return (tok);
204}
205
206/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000207 * xmlFreeRMutex:
Daniel Veillardb8478642001-10-12 17:29:10 +0000208 * @tok: the reentrant mutex
209 *
210 * xmlRFreeMutex() is used to reclaim resources associated with a
211 * reentrant mutex.
212 */
213void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000214xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000215{
216#ifdef HAVE_PTHREAD_H
217 pthread_mutex_destroy(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000218#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000219 DeleteCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000220#endif
221 free(tok);
222}
223
224/**
225 * xmlRMutexLock:
226 * @tok: the reentrant mutex
227 *
228 * xmlRMutexLock() is used to lock a libxml2 token_r.
229 */
230void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000231xmlRMutexLock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000232{
233#ifdef HAVE_PTHREAD_H
234 pthread_mutex_lock(&tok->lock);
235 if (tok->held) {
236 if (pthread_equal(tok->tid, pthread_self())) {
237 tok->held++;
238 pthread_mutex_unlock(&tok->lock);
239 return;
240 } else {
241 tok->waiters++;
242 while (tok->held)
243 pthread_cond_wait(&tok->cv, &tok->lock);
244 tok->waiters--;
245 }
246 }
247 tok->tid = pthread_self();
248 tok->held = 1;
249 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000250#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000251 EnterCriticalSection(&tok->cs);
252 ++tok->count;
Daniel Veillardb8478642001-10-12 17:29:10 +0000253#endif
254}
255
256/**
257 * xmlRMutexUnlock:
258 * @tok: the reentrant mutex
259 *
260 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
261 */
262void
Daniel Veillard0ba59232002-02-10 13:20:39 +0000263xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Daniel Veillardb8478642001-10-12 17:29:10 +0000264{
265#ifdef HAVE_PTHREAD_H
266 pthread_mutex_lock(&tok->lock);
267 tok->held--;
268 if (tok->held == 0) {
269 if (tok->waiters)
270 pthread_cond_signal(&tok->cv);
271 tok->tid = 0;
272 }
273 pthread_mutex_unlock(&tok->lock);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000274#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000275 if (!--tok->count)
276 LeaveCriticalSection(&tok->cs);
Daniel Veillardb8478642001-10-12 17:29:10 +0000277#endif
278}
279
280/************************************************************************
281 * *
282 * Per thread global state handling *
283 * *
284 ************************************************************************/
285
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000286#ifdef LIBXML_THREAD_ENABLED
Daniel Veillardb8478642001-10-12 17:29:10 +0000287/**
288 * xmlFreeGlobalState:
289 * @state: a thread global state
290 *
291 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
292 * global state. It is is used here to reclaim memory resources.
293 */
294static void
295xmlFreeGlobalState(void *state)
296{
297 free(state);
298}
299
300/**
301 * xmlNewGlobalState:
302 *
303 * xmlNewGlobalState() allocates a global state. This structure is used to
304 * hold all data for use by a thread when supporting backwards compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000305 * of libxml2 to pre-thread-safe behaviour.
Daniel Veillardb8478642001-10-12 17:29:10 +0000306 *
307 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
308 */
309static xmlGlobalStatePtr
310xmlNewGlobalState(void)
311{
312 xmlGlobalState *gs;
313
314 gs = malloc(sizeof(xmlGlobalState));
315 if (gs == NULL)
316 return(NULL);
317
William M. Brack8b2c7f12002-11-22 05:07:29 +0000318 memset(gs, 0, sizeof(xmlGlobalState));
Daniel Veillardb8478642001-10-12 17:29:10 +0000319 xmlInitializeGlobalState(gs);
320 return (gs);
321}
Daniel Veillard8bdb91d2001-10-31 17:52:43 +0000322#endif /* LIBXML_THREAD_ENABLED */
Daniel Veillardb8478642001-10-12 17:29:10 +0000323
324
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000325#ifdef HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000326#if !defined(HAVE_COMPILER_TLS)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000327typedef struct _xmlGlobalStateCleanupHelperParams
328{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000329 HANDLE thread;
330 void *memory;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000331} xmlGlobalStateCleanupHelperParams;
332
Daniel Veillard01c13b52002-12-10 15:19:08 +0000333static void xmlGlobalStateCleanupHelper (void *p)
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000334{
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000335 xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
336 WaitForSingleObject(params->thread, INFINITE);
337 CloseHandle(params->thread);
338 xmlFreeGlobalState(params->memory);
339 free(params);
340 _endthread();
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000341}
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000342#endif /* HAVE_COMPILER_TLS */
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000343#endif /* HAVE_WIN32_THREADS */
344
Daniel Veillard01c13b52002-12-10 15:19:08 +0000345/**
346 * xmlGetGlobalState:
347 *
348 * xmlGetGlobalState() is called to retrieve the global state for a thread.
349 *
350 * Returns the thread global state or NULL in case of error
351 */
Daniel Veillardb8478642001-10-12 17:29:10 +0000352xmlGlobalStatePtr
353xmlGetGlobalState(void)
354{
355#ifdef HAVE_PTHREAD_H
356 xmlGlobalState *globalval;
357
Daniel Veillarde28313b2001-12-06 14:08:31 +0000358 pthread_once(&once_control, xmlOnceInit);
359
Daniel Veillardb8478642001-10-12 17:29:10 +0000360 if ((globalval = (xmlGlobalState *)
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000361 pthread_getspecific(globalkey)) == NULL) {
Daniel Veillardb8478642001-10-12 17:29:10 +0000362 xmlGlobalState *tsd = xmlNewGlobalState();
363
364 pthread_setspecific(globalkey, tsd);
365 return (tsd);
Daniel Veillard6f350292001-10-14 09:56:15 +0000366 }
367 return (globalval);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000368#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000369#if defined(HAVE_COMPILER_TLS)
370 if (!tlstate_inited) {
371 tlstate_inited = 1;
372 xmlInitializeGlobalState(&tlstate);
373 }
374 return &tlstate;
375#else /* HAVE_COMPILER_TLS */
376 xmlGlobalState *globalval;
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000377
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000378 if (run_once_init) {
379 run_once_init = 0;
380 xmlOnceInit();
381 }
382 if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
383 xmlGlobalState *tsd = xmlNewGlobalState();
384 xmlGlobalStateCleanupHelperParams *p =
385 (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
386 p->memory = tsd;
387 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
388 GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
389 TlsSetValue(globalkey, tsd);
390 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000391
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000392 return (tsd);
393 }
394 return (globalval);
395#endif /* HAVE_COMPILER_TLS */
Daniel Veillard6f350292001-10-14 09:56:15 +0000396#else
397 return(NULL);
Daniel Veillardb8478642001-10-12 17:29:10 +0000398#endif
399}
400
Daniel Veillardb8478642001-10-12 17:29:10 +0000401/************************************************************************
402 * *
403 * Library wide thread interfaces *
404 * *
405 ************************************************************************/
406
407/**
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000408 * xmlGetThreadId:
409 *
410 * xmlGetThreadId() find the current thread ID number
411 *
412 * Returns the current thread ID number
413 */
414int
415xmlGetThreadId(void)
416{
417#ifdef HAVE_PTHREAD_H
418 return((int) pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000419#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000420 return GetCurrentThreadId();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000421#else
422 return((int) 0);
423#endif
424}
425
426/**
Daniel Veillard6f350292001-10-14 09:56:15 +0000427 * xmlIsMainThread:
428 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000429 * xmlIsMainThread() check whether the current thread is the main thread.
Daniel Veillard6f350292001-10-14 09:56:15 +0000430 *
431 * Returns 1 if the current thread is the main thread, 0 otherwise
432 */
433int
434xmlIsMainThread(void)
435{
Daniel Veillarde28313b2001-12-06 14:08:31 +0000436#ifdef HAVE_PTHREAD_H
437 pthread_once(&once_control, xmlOnceInit);
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000438#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000439 if (run_once_init) {
440 run_once_init = 0;
441 xmlOnceInit ();
442 }
Daniel Veillarde28313b2001-12-06 14:08:31 +0000443#endif
Daniel Veillard6f350292001-10-14 09:56:15 +0000444
445#ifdef DEBUG_THREADS
446 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
447#endif
448#ifdef HAVE_PTHREAD_H
449 return(mainthread == pthread_self());
Daniel Veillarddb0eb8d2002-01-13 13:35:00 +0000450#elif defined HAVE_WIN32_THREADS
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000451 return(mainthread == GetCurrentThreadId ());
Daniel Veillard6f350292001-10-14 09:56:15 +0000452#else
453 return(1);
454#endif
455}
456
457/**
Daniel Veillardb8478642001-10-12 17:29:10 +0000458 * xmlLockLibrary:
459 *
460 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
461 * library.
462 */
463void
464xmlLockLibrary(void)
465{
Daniel Veillard6f350292001-10-14 09:56:15 +0000466#ifdef DEBUG_THREADS
467 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
468#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000469 xmlRMutexLock(xmlLibraryLock);
470}
471
472/**
473 * xmlUnlockLibrary:
474 *
475 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
476 * library.
477 */
478void
479xmlUnlockLibrary(void)
480{
Daniel Veillard6f350292001-10-14 09:56:15 +0000481#ifdef DEBUG_THREADS
482 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
483#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000484 xmlRMutexUnlock(xmlLibraryLock);
485}
486
487/**
488 * xmlInitThreads:
489 *
490 * xmlInitThreads() is used to to initialize all the thread related
491 * data of the libxml2 library.
492 */
493void
494xmlInitThreads(void)
495{
Daniel Veillard6f350292001-10-14 09:56:15 +0000496#ifdef DEBUG_THREADS
497 xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
498#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000499}
500
501/**
502 * xmlCleanupThreads:
503 *
504 * xmlCleanupThreads() is used to to cleanup all the thread related
505 * data of the libxml2 library once processing has ended.
506 */
507void
508xmlCleanupThreads(void)
509{
Daniel Veillard6f350292001-10-14 09:56:15 +0000510#ifdef DEBUG_THREADS
511 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
512#endif
Daniel Veillarde28313b2001-12-06 14:08:31 +0000513}
Daniel Veillard6f350292001-10-14 09:56:15 +0000514
Daniel Veillarde28313b2001-12-06 14:08:31 +0000515/**
516 * xmlOnceInit
517 *
518 * xmlOnceInit() is used to initialize the value of mainthread for use
519 * in other routines. This function should only be called using
520 * pthread_once() in association with the once_control variable to ensure
521 * that the function is only called once. See man pthread_once for more
522 * details.
523 */
524static void
525xmlOnceInit(void) {
526#ifdef HAVE_PTHREAD_H
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000527 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Daniel Veillarde28313b2001-12-06 14:08:31 +0000528 mainthread = pthread_self();
Igor Zlatkovicf2160a02002-10-31 15:58:42 +0000529#endif
530
531#if defined(HAVE_WIN32_THREADS)
532#if !defined(HAVE_COMPILER_TLS)
533 globalkey = TlsAlloc();
534#endif
535 mainthread = GetCurrentThreadId();
Daniel Veillarde28313b2001-12-06 14:08:31 +0000536#endif
Daniel Veillardb8478642001-10-12 17:29:10 +0000537}