2 * threads.c: set of generic threading related routines
4 * See Copyright for the status of this software.
6 * Gary Pennington <Gary.Pennington@uk.sun.com>
15 #include <libxml/threads.h>
16 #include <libxml/globals.h>
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
29 #elif defined HAVE_WIN32_THREADS
31 #ifndef HAVE_COMPILER_TLS
36 #ifdef HAVE_BEOS_THREADS
45 /* #define DEBUG_THREADS */
49 static int libxml_is_threaded
= -1;
52 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
53 extern int pthread_once (pthread_once_t
*__once_control
,
54 void (*__init_routine
) (void))
56 extern void *pthread_getspecific (pthread_key_t __key
)
58 extern int pthread_setspecific (pthread_key_t __key
,
59 __const
void *__pointer
)
61 extern int pthread_key_create (pthread_key_t
*__key
,
62 void (*__destr_function
) (void *))
64 extern int pthread_key_delete (pthread_key_t __key
)
66 extern int pthread_mutex_init ()
68 extern int pthread_mutex_destroy ()
70 extern int pthread_mutex_lock ()
72 extern int pthread_mutex_unlock ()
74 extern int pthread_cond_init ()
76 extern int pthread_cond_destroy ()
78 extern int pthread_cond_wait ()
80 extern int pthread_equal ()
82 extern pthread_t
pthread_self ()
84 extern int pthread_key_create ()
86 extern int pthread_key_delete ()
88 extern int pthread_cond_signal ()
93 #endif /* HAVE_PTHREAD_H */
96 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
97 * to avoid some crazyness since xmlMalloc/xmlFree may actually
98 * be hosted on allocated blocks needing them for the allocation ...
102 * xmlMutex are a simple mutual exception locks
105 #ifdef HAVE_PTHREAD_H
106 pthread_mutex_t lock
;
107 #elif defined HAVE_WIN32_THREADS
109 #elif defined HAVE_BEOS_THREADS
118 * xmlRMutex are reentrant mutual exception locks
121 #ifdef HAVE_PTHREAD_H
122 pthread_mutex_t lock
;
124 unsigned int waiters
;
127 #elif defined HAVE_WIN32_THREADS
130 #elif defined HAVE_BEOS_THREADS
140 * This module still has some internal static data.
141 * - xmlLibraryLock a global lock
142 * - globalkey used for per-thread data
145 #ifdef HAVE_PTHREAD_H
146 static pthread_key_t globalkey
;
147 static pthread_t mainthread
;
148 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
149 static pthread_mutex_t global_init_lock
= PTHREAD_MUTEX_INITIALIZER
;
150 #elif defined HAVE_WIN32_THREADS
151 #if defined(HAVE_COMPILER_TLS)
152 static __declspec(thread
) xmlGlobalState tlstate
;
153 static __declspec(thread
) int tlstate_inited
= 0;
154 #else /* HAVE_COMPILER_TLS */
155 static DWORD globalkey
= TLS_OUT_OF_INDEXES
;
156 #endif /* HAVE_COMPILER_TLS */
157 static DWORD mainthread
;
161 } run_once
= { 0, 0};
162 static volatile LPCRITICAL_SECTION global_init_lock
= NULL
;
164 /* endif HAVE_WIN32_THREADS */
165 #elif defined HAVE_BEOS_THREADS
167 thread_id mainthread
= 0;
168 int32 run_once_init
= 0;
169 static int32 global_init_lock
= -1;
170 static vint32 global_init_count
= 0;
173 static xmlRMutexPtr xmlLibraryLock
= NULL
;
175 #ifdef LIBXML_THREAD_ENABLED
176 static void xmlOnceInit(void);
182 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
183 * synchronizing access to data.
185 * Returns a new simple mutex pointer or NULL in case of error
192 if ((tok
= malloc(sizeof(xmlMutex
))) == NULL
)
194 #ifdef HAVE_PTHREAD_H
195 if (libxml_is_threaded
!= 0)
196 pthread_mutex_init(&tok
->lock
, NULL
);
197 #elif defined HAVE_WIN32_THREADS
198 tok
->mutex
= CreateMutex(NULL
, FALSE
, NULL
);
199 #elif defined HAVE_BEOS_THREADS
200 if ((tok
->sem
= create_sem(1, "xmlMutex")) < B_OK
) {
211 * @tok: the simple mutex
213 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
217 xmlFreeMutex(xmlMutexPtr tok
)
222 #ifdef HAVE_PTHREAD_H
223 if (libxml_is_threaded
!= 0)
224 pthread_mutex_destroy(&tok
->lock
);
225 #elif defined HAVE_WIN32_THREADS
226 CloseHandle(tok
->mutex
);
227 #elif defined HAVE_BEOS_THREADS
228 delete_sem(tok
->sem
);
235 * @tok: the simple mutex
237 * xmlMutexLock() is used to lock a libxml2 token.
240 xmlMutexLock(xmlMutexPtr tok
)
244 #ifdef HAVE_PTHREAD_H
245 if (libxml_is_threaded
!= 0)
246 pthread_mutex_lock(&tok
->lock
);
247 #elif defined HAVE_WIN32_THREADS
248 WaitForSingleObject(tok
->mutex
, INFINITE
);
249 #elif defined HAVE_BEOS_THREADS
250 if (acquire_sem(tok
->sem
) != B_NO_ERROR
) {
252 xmlGenericError(xmlGenericErrorContext
,
253 "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
257 tok
->tid
= find_thread(NULL
);
264 * @tok: the simple mutex
266 * xmlMutexUnlock() is used to unlock a libxml2 token.
269 xmlMutexUnlock(xmlMutexPtr tok
)
273 #ifdef HAVE_PTHREAD_H
274 if (libxml_is_threaded
!= 0)
275 pthread_mutex_unlock(&tok
->lock
);
276 #elif defined HAVE_WIN32_THREADS
277 ReleaseMutex(tok
->mutex
);
278 #elif defined HAVE_BEOS_THREADS
279 if (tok
->tid
== find_thread(NULL
)) {
281 release_sem(tok
->sem
);
289 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
290 * synchronizing access to data. token_r is a re-entrant lock and thus useful
291 * for synchronizing access to data structures that may be manipulated in a
294 * Returns the new reentrant mutex pointer or NULL in case of error
301 if ((tok
= malloc(sizeof(xmlRMutex
))) == NULL
)
303 #ifdef HAVE_PTHREAD_H
304 if (libxml_is_threaded
!= 0) {
305 pthread_mutex_init(&tok
->lock
, NULL
);
308 pthread_cond_init(&tok
->cv
, NULL
);
310 #elif defined HAVE_WIN32_THREADS
311 InitializeCriticalSection(&tok
->cs
);
313 #elif defined HAVE_BEOS_THREADS
314 if ((tok
->lock
= xmlNewMutex()) == NULL
) {
325 * @tok: the reentrant mutex
327 * xmlRFreeMutex() is used to reclaim resources associated with a
331 xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED
)
335 #ifdef HAVE_PTHREAD_H
336 if (libxml_is_threaded
!= 0) {
337 pthread_mutex_destroy(&tok
->lock
);
338 pthread_cond_destroy(&tok
->cv
);
340 #elif defined HAVE_WIN32_THREADS
341 DeleteCriticalSection(&tok
->cs
);
342 #elif defined HAVE_BEOS_THREADS
343 xmlFreeMutex(tok
->lock
);
350 * @tok: the reentrant mutex
352 * xmlRMutexLock() is used to lock a libxml2 token_r.
355 xmlRMutexLock(xmlRMutexPtr tok
)
359 #ifdef HAVE_PTHREAD_H
360 if (libxml_is_threaded
== 0)
363 pthread_mutex_lock(&tok
->lock
);
365 if (pthread_equal(tok
->tid
, pthread_self())) {
367 pthread_mutex_unlock(&tok
->lock
);
372 pthread_cond_wait(&tok
->cv
, &tok
->lock
);
376 tok
->tid
= pthread_self();
378 pthread_mutex_unlock(&tok
->lock
);
379 #elif defined HAVE_WIN32_THREADS
380 EnterCriticalSection(&tok
->cs
);
382 #elif defined HAVE_BEOS_THREADS
383 if (tok
->lock
->tid
== find_thread(NULL
)) {
387 xmlMutexLock(tok
->lock
);
395 * @tok: the reentrant mutex
397 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
400 xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED
)
404 #ifdef HAVE_PTHREAD_H
405 if (libxml_is_threaded
== 0)
408 pthread_mutex_lock(&tok
->lock
);
410 if (tok
->held
== 0) {
412 pthread_cond_signal(&tok
->cv
);
413 memset(&tok
->tid
, 0, sizeof(tok
->tid
));
415 pthread_mutex_unlock(&tok
->lock
);
416 #elif defined HAVE_WIN32_THREADS
418 LeaveCriticalSection(&tok
->cs
);
419 #elif defined HAVE_BEOS_THREADS
420 if (tok
->lock
->tid
== find_thread(NULL
)) {
422 if (tok
->count
== 0) {
423 xmlMutexUnlock(tok
->lock
);
431 * xmlGlobalInitMutexLock
433 * Makes sure that the global initialization mutex is initialized and
437 __xmlGlobalInitMutexLock(void)
439 /* Make sure the global init lock is initialized and then lock it. */
440 #ifdef HAVE_PTHREAD_H
441 /* The mutex is statically initialized, so we just lock it. */
442 pthread_mutex_lock(&global_init_lock
);
443 #elif defined HAVE_WIN32_THREADS
444 LPCRITICAL_SECTION cs
;
446 /* Create a new critical section */
447 if (global_init_lock
== NULL
) {
448 cs
= malloc(sizeof(CRITICAL_SECTION
));
450 xmlGenericError(xmlGenericErrorContext
,
451 "xmlGlobalInitMutexLock: out of memory\n");
454 InitializeCriticalSection(cs
);
456 /* Swap it into the global_init_lock */
457 #ifdef InterlockedCompareExchangePointer
458 InterlockedCompareExchangePointer(&global_init_lock
, cs
, NULL
);
459 #else /* Use older void* version */
460 InterlockedCompareExchange((void **) &global_init_lock
,
462 #endif /* InterlockedCompareExchangePointer */
464 /* If another thread successfully recorded its critical
465 * section in the global_init_lock then discard the one
466 * allocated by this thread. */
467 if (global_init_lock
!= cs
) {
468 DeleteCriticalSection(cs
);
473 /* Lock the chosen critical section */
474 EnterCriticalSection(global_init_lock
);
475 #elif defined HAVE_BEOS_THREADS
478 /* Allocate a new semaphore */
479 sem
= create_sem(1, "xmlGlobalinitMutex");
481 while (global_init_lock
== -1) {
482 if (atomic_add(&global_init_count
, 1) == 0) {
483 global_init_lock
= sem
;
486 atomic_add(&global_init_count
, -1);
490 /* If another thread successfully recorded its critical
491 * section in the global_init_lock then discard the one
492 * allocated by this thread. */
493 if (global_init_lock
!= sem
)
496 /* Acquire the chosen semaphore */
497 if (acquire_sem(global_init_lock
) != B_NO_ERROR
) {
499 xmlGenericError(xmlGenericErrorContext
,
500 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
508 __xmlGlobalInitMutexUnlock(void)
510 #ifdef HAVE_PTHREAD_H
511 pthread_mutex_unlock(&global_init_lock
);
512 #elif defined HAVE_WIN32_THREADS
513 if (global_init_lock
!= NULL
) {
514 LeaveCriticalSection(global_init_lock
);
516 #elif defined HAVE_BEOS_THREADS
517 release_sem(global_init_lock
);
522 * xmlGlobalInitMutexDestroy
524 * Makes sure that the global initialization mutex is destroyed before
525 * application termination.
528 __xmlGlobalInitMutexDestroy(void)
530 #ifdef HAVE_PTHREAD_H
531 #elif defined HAVE_WIN32_THREADS
532 if (global_init_lock
!= NULL
) {
533 DeleteCriticalSection(global_init_lock
);
534 free(global_init_lock
);
535 global_init_lock
= NULL
;
540 /************************************************************************
542 * Per thread global state handling *
544 ************************************************************************/
546 #ifdef LIBXML_THREAD_ENABLED
552 * xmlFreeGlobalState:
553 * @state: a thread global state
555 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
556 * global state. It is is used here to reclaim memory resources.
559 xmlFreeGlobalState(void *state
)
561 xmlGlobalState
*gs
= (xmlGlobalState
*) state
;
563 /* free any memory allocated in the thread's xmlLastError */
564 xmlResetError(&(gs
->xmlLastError
));
571 * xmlNewGlobalState() allocates a global state. This structure is used to
572 * hold all data for use by a thread when supporting backwards compatibility
573 * of libxml2 to pre-thread-safe behaviour.
575 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
577 static xmlGlobalStatePtr
578 xmlNewGlobalState(void)
582 gs
= malloc(sizeof(xmlGlobalState
));
584 xmlGenericError(xmlGenericErrorContext
,
585 "xmlGetGlobalState: out of memory\n");
589 memset(gs
, 0, sizeof(xmlGlobalState
));
590 xmlInitializeGlobalState(gs
);
593 #endif /* LIBXML_THREAD_ENABLED */
595 #ifdef HAVE_PTHREAD_H
596 #elif defined HAVE_WIN32_THREADS
597 #if !defined(HAVE_COMPILER_TLS)
598 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
599 typedef struct _xmlGlobalStateCleanupHelperParams
{
602 } xmlGlobalStateCleanupHelperParams
;
605 xmlGlobalStateCleanupHelper(void *p
)
607 xmlGlobalStateCleanupHelperParams
*params
=
608 (xmlGlobalStateCleanupHelperParams
*) p
;
609 WaitForSingleObject(params
->thread
, INFINITE
);
610 CloseHandle(params
->thread
);
611 xmlFreeGlobalState(params
->memory
);
615 #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
617 typedef struct _xmlGlobalStateCleanupHelperParams
{
619 struct _xmlGlobalStateCleanupHelperParams
*prev
;
620 struct _xmlGlobalStateCleanupHelperParams
*next
;
621 } xmlGlobalStateCleanupHelperParams
;
623 static xmlGlobalStateCleanupHelperParams
*cleanup_helpers_head
= NULL
;
624 static CRITICAL_SECTION cleanup_helpers_cs
;
626 #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
627 #endif /* HAVE_COMPILER_TLS */
628 #endif /* HAVE_WIN32_THREADS */
630 #if defined HAVE_BEOS_THREADS
633 * xmlGlobalStateCleanup:
634 * @data: unused parameter
639 xmlGlobalStateCleanup(void *data
)
641 void *globalval
= tls_get(globalkey
);
643 if (globalval
!= NULL
)
644 xmlFreeGlobalState(globalval
);
651 * xmlGetGlobalState() is called to retrieve the global state for a thread.
653 * Returns the thread global state or NULL in case of error
656 xmlGetGlobalState(void)
658 #ifdef HAVE_PTHREAD_H
659 xmlGlobalState
*globalval
;
661 if (libxml_is_threaded
== 0)
664 pthread_once(&once_control
, xmlOnceInit
);
666 if ((globalval
= (xmlGlobalState
*)
667 pthread_getspecific(globalkey
)) == NULL
) {
668 xmlGlobalState
*tsd
= xmlNewGlobalState();
672 pthread_setspecific(globalkey
, tsd
);
676 #elif defined HAVE_WIN32_THREADS
677 #if defined(HAVE_COMPILER_TLS)
678 if (!tlstate_inited
) {
680 xmlInitializeGlobalState(&tlstate
);
683 #else /* HAVE_COMPILER_TLS */
684 xmlGlobalState
*globalval
;
685 xmlGlobalStateCleanupHelperParams
*p
;
688 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
689 globalval
= (xmlGlobalState
*) TlsGetValue(globalkey
);
691 p
= (xmlGlobalStateCleanupHelperParams
*) TlsGetValue(globalkey
);
692 globalval
= (xmlGlobalState
*) (p
? p
->memory
: NULL
);
694 if (globalval
== NULL
) {
695 xmlGlobalState
*tsd
= xmlNewGlobalState();
699 p
= (xmlGlobalStateCleanupHelperParams
*)
700 malloc(sizeof(xmlGlobalStateCleanupHelperParams
));
702 xmlGenericError(xmlGenericErrorContext
,
703 "xmlGetGlobalState: out of memory\n");
704 xmlFreeGlobalState(tsd
);
708 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
709 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
710 GetCurrentProcess(), &p
->thread
, 0, TRUE
,
711 DUPLICATE_SAME_ACCESS
);
712 TlsSetValue(globalkey
, tsd
);
713 _beginthread(xmlGlobalStateCleanupHelper
, 0, p
);
715 EnterCriticalSection(&cleanup_helpers_cs
);
716 if (cleanup_helpers_head
!= NULL
) {
717 cleanup_helpers_head
->prev
= p
;
719 p
->next
= cleanup_helpers_head
;
721 cleanup_helpers_head
= p
;
722 TlsSetValue(globalkey
, p
);
723 LeaveCriticalSection(&cleanup_helpers_cs
);
729 #endif /* HAVE_COMPILER_TLS */
730 #elif defined HAVE_BEOS_THREADS
731 xmlGlobalState
*globalval
;
735 if ((globalval
= (xmlGlobalState
*) tls_get(globalkey
)) == NULL
) {
736 xmlGlobalState
*tsd
= xmlNewGlobalState();
740 tls_set(globalkey
, tsd
);
741 on_exit_thread(xmlGlobalStateCleanup
, NULL
);
750 /************************************************************************
752 * Library wide thread interfaces *
754 ************************************************************************/
759 * xmlGetThreadId() find the current thread ID number
760 * Note that this is likely to be broken on some platforms using pthreads
761 * as the specification doesn't mandate pthread_t to be an integer type
763 * Returns the current thread ID number
768 #ifdef HAVE_PTHREAD_H
772 if (libxml_is_threaded
== 0)
775 /* horrible but preserves compat, see warning above */
776 memcpy(&ret
, &id
, sizeof(ret
));
778 #elif defined HAVE_WIN32_THREADS
779 return GetCurrentThreadId();
780 #elif defined HAVE_BEOS_THREADS
781 return find_thread(NULL
);
790 * xmlIsMainThread() check whether the current thread is the main thread.
792 * Returns 1 if the current thread is the main thread, 0 otherwise
795 xmlIsMainThread(void)
797 #ifdef HAVE_PTHREAD_H
798 if (libxml_is_threaded
== -1)
800 if (libxml_is_threaded
== 0)
802 pthread_once(&once_control
, xmlOnceInit
);
803 #elif defined HAVE_WIN32_THREADS
805 #elif defined HAVE_BEOS_THREADS
810 xmlGenericError(xmlGenericErrorContext
, "xmlIsMainThread()\n");
812 #ifdef HAVE_PTHREAD_H
813 return (pthread_equal(mainthread
,pthread_self()));
814 #elif defined HAVE_WIN32_THREADS
815 return (mainthread
== GetCurrentThreadId());
816 #elif defined HAVE_BEOS_THREADS
817 return (mainthread
== find_thread(NULL
));
826 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
833 xmlGenericError(xmlGenericErrorContext
, "xmlLockLibrary()\n");
835 xmlRMutexLock(xmlLibraryLock
);
841 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
845 xmlUnlockLibrary(void)
848 xmlGenericError(xmlGenericErrorContext
, "xmlUnlockLibrary()\n");
850 xmlRMutexUnlock(xmlLibraryLock
);
856 * xmlInitThreads() is used to to initialize all the thread related
857 * data of the libxml2 library.
862 #ifdef HAVE_PTHREAD_H
863 if (libxml_is_threaded
== -1) {
864 if ((pthread_once
!= NULL
) &&
865 (pthread_getspecific
!= NULL
) &&
866 (pthread_setspecific
!= NULL
) &&
867 (pthread_key_create
!= NULL
) &&
868 (pthread_key_delete
!= NULL
) &&
869 (pthread_mutex_init
!= NULL
) &&
870 (pthread_mutex_destroy
!= NULL
) &&
871 (pthread_mutex_lock
!= NULL
) &&
872 (pthread_mutex_unlock
!= NULL
) &&
873 (pthread_cond_init
!= NULL
) &&
874 (pthread_cond_destroy
!= NULL
) &&
875 (pthread_cond_wait
!= NULL
) &&
876 (pthread_equal
!= NULL
) &&
877 (pthread_self
!= NULL
) &&
878 (pthread_cond_signal
!= NULL
)) {
879 libxml_is_threaded
= 1;
881 /* fprintf(stderr, "Running multithreaded\n"); */
884 /* fprintf(stderr, "Running without multithread\n"); */
885 libxml_is_threaded
= 0;
888 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
889 InitializeCriticalSection(&cleanup_helpers_cs
);
896 * xmlCleanupThreads() is used to to cleanup all the thread related
897 * data of the libxml2 library once processing has ended.
899 * WARNING: if your application is multithreaded or has plugin support
900 * calling this may crash the application if another thread or
901 * a plugin is still using libxml2. It's sometimes very hard to
902 * guess if libxml2 is in use in the application, some libraries
903 * or plugins may use it without notice. In case of doubt abstain
904 * from calling this function or do it just before calling exit()
905 * to avoid leak reports from valgrind !
908 xmlCleanupThreads(void)
911 xmlGenericError(xmlGenericErrorContext
, "xmlCleanupThreads()\n");
913 #ifdef HAVE_PTHREAD_H
914 if ((libxml_is_threaded
) && (pthread_key_delete
!= NULL
))
915 pthread_key_delete(globalkey
);
916 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
917 if (globalkey
!= TLS_OUT_OF_INDEXES
) {
918 xmlGlobalStateCleanupHelperParams
*p
;
920 EnterCriticalSection(&cleanup_helpers_cs
);
921 p
= cleanup_helpers_head
;
923 xmlGlobalStateCleanupHelperParams
*temp
= p
;
926 xmlFreeGlobalState(temp
->memory
);
929 cleanup_helpers_head
= 0;
930 LeaveCriticalSection(&cleanup_helpers_cs
);
932 globalkey
= TLS_OUT_OF_INDEXES
;
934 DeleteCriticalSection(&cleanup_helpers_cs
);
938 #ifdef LIBXML_THREAD_ENABLED
943 * xmlOnceInit() is used to initialize the value of mainthread for use
944 * in other routines. This function should only be called using
945 * pthread_once() in association with the once_control variable to ensure
946 * that the function is only called once. See man pthread_once for more
952 #ifdef HAVE_PTHREAD_H
953 (void) pthread_key_create(&globalkey
, xmlFreeGlobalState
);
954 mainthread
= pthread_self();
955 #elif defined(HAVE_WIN32_THREADS)
956 if (!run_once
.done
) {
957 if (InterlockedIncrement(&run_once
.control
) == 1) {
958 #if !defined(HAVE_COMPILER_TLS)
959 globalkey
= TlsAlloc();
961 mainthread
= GetCurrentThreadId();
964 /* Another thread is working; give up our slice and
965 * wait until they're done. */
966 while (!run_once
.done
)
970 #elif defined HAVE_BEOS_THREADS
971 if (atomic_add(&run_once_init
, 1) == 0) {
972 globalkey
= tls_allocate();
973 tls_set(globalkey
, NULL
);
974 mainthread
= find_thread(NULL
);
976 atomic_add(&run_once_init
, -1);
983 * @hinstDLL: handle to DLL instance
984 * @fdwReason: Reason code for entry
985 * @lpvReserved: generic pointer (depends upon reason code)
987 * Entry point for Windows library. It is being used to free thread-specific
990 * Returns TRUE always
992 #ifdef HAVE_PTHREAD_H
993 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
994 #if defined(LIBXML_STATIC_FOR_DLL)
996 xmlDllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
999 DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1002 switch (fdwReason
) {
1003 case DLL_THREAD_DETACH
:
1004 if (globalkey
!= TLS_OUT_OF_INDEXES
) {
1005 xmlGlobalState
*globalval
= NULL
;
1006 xmlGlobalStateCleanupHelperParams
*p
=
1007 (xmlGlobalStateCleanupHelperParams
*)
1008 TlsGetValue(globalkey
);
1009 globalval
= (xmlGlobalState
*) (p
? p
->memory
: NULL
);
1011 xmlFreeGlobalState(globalval
);
1012 TlsSetValue(globalkey
, NULL
);
1015 EnterCriticalSection(&cleanup_helpers_cs
);
1016 if (p
== cleanup_helpers_head
)
1017 cleanup_helpers_head
= p
->next
;
1019 p
->prev
->next
= p
->next
;
1020 if (p
->next
!= NULL
)
1021 p
->next
->prev
= p
->prev
;
1022 LeaveCriticalSection(&cleanup_helpers_cs
);
1031 #define bottom_threads
1032 #include "elfgcchack.h"