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_once_t once_control_init = PTHREAD_ONCE_INIT;
150 static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
151 #elif defined HAVE_WIN32_THREADS
152 #if defined(HAVE_COMPILER_TLS)
153 static __declspec(thread) xmlGlobalState tlstate;
154 static __declspec(thread) int tlstate_inited = 0;
155 #else /* HAVE_COMPILER_TLS */
156 static DWORD globalkey = TLS_OUT_OF_INDEXES;
157 #endif /* HAVE_COMPILER_TLS */
158 static DWORD mainthread;
162 } run_once = { 0, 0};
163 static volatile LPCRITICAL_SECTION global_init_lock = NULL;
165 /* endif HAVE_WIN32_THREADS */
166 #elif defined HAVE_BEOS_THREADS
168 thread_id mainthread = 0;
169 int32 run_once_init = 0;
170 static int32 global_init_lock = -1;
171 static vint32 global_init_count = 0;
174 static xmlRMutexPtr xmlLibraryLock = NULL;
176 #ifdef LIBXML_THREAD_ENABLED
177 static void xmlOnceInit(void);
183 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
184 * synchronizing access to data.
186 * Returns a new simple mutex pointer or NULL in case of error
193 if ((tok = malloc(sizeof(xmlMutex))) == NULL)
195 #ifdef HAVE_PTHREAD_H
196 if (libxml_is_threaded != 0)
197 pthread_mutex_init(&tok->lock, NULL);
198 #elif defined HAVE_WIN32_THREADS
199 tok->mutex = CreateMutex(NULL, FALSE, NULL);
200 #elif defined HAVE_BEOS_THREADS
201 if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
212 * @tok: the simple mutex
214 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
218 xmlFreeMutex(xmlMutexPtr tok)
223 #ifdef HAVE_PTHREAD_H
224 if (libxml_is_threaded != 0)
225 pthread_mutex_destroy(&tok->lock);
226 #elif defined HAVE_WIN32_THREADS
227 CloseHandle(tok->mutex);
228 #elif defined HAVE_BEOS_THREADS
229 delete_sem(tok->sem);
236 * @tok: the simple mutex
238 * xmlMutexLock() is used to lock a libxml2 token.
241 xmlMutexLock(xmlMutexPtr tok)
245 #ifdef HAVE_PTHREAD_H
246 if (libxml_is_threaded != 0)
247 pthread_mutex_lock(&tok->lock);
248 #elif defined HAVE_WIN32_THREADS
249 WaitForSingleObject(tok->mutex, INFINITE);
250 #elif defined HAVE_BEOS_THREADS
251 if (acquire_sem(tok->sem) != B_NO_ERROR) {
253 xmlGenericError(xmlGenericErrorContext,
254 "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
417 if (tok->count > 0) {
418 LeaveCriticalSection(&tok->cs);
421 #elif defined HAVE_BEOS_THREADS
422 if (tok->lock->tid == find_thread(NULL)) {
424 if (tok->count == 0) {
425 xmlMutexUnlock(tok->lock);
433 * xmlGlobalInitMutexLock
435 * Makes sure that the global initialization mutex is initialized and
439 __xmlGlobalInitMutexLock(void)
441 /* Make sure the global init lock is initialized and then lock it. */
442 #ifdef HAVE_PTHREAD_H
443 /* The mutex is statically initialized, so we just lock it. */
444 if (pthread_mutex_lock != NULL)
445 pthread_mutex_lock(&global_init_lock);
446 #elif defined HAVE_WIN32_THREADS
447 LPCRITICAL_SECTION cs;
449 /* Create a new critical section */
450 if (global_init_lock == NULL) {
451 cs = malloc(sizeof(CRITICAL_SECTION));
453 xmlGenericError(xmlGenericErrorContext,
454 "xmlGlobalInitMutexLock: out of memory\n");
457 InitializeCriticalSection(cs);
459 /* Swap it into the global_init_lock */
460 #ifdef InterlockedCompareExchangePointer
461 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
462 #else /* Use older void* version */
463 InterlockedCompareExchange((void **) &global_init_lock,
465 #endif /* InterlockedCompareExchangePointer */
467 /* If another thread successfully recorded its critical
468 * section in the global_init_lock then discard the one
469 * allocated by this thread. */
470 if (global_init_lock != cs) {
471 DeleteCriticalSection(cs);
476 /* Lock the chosen critical section */
477 EnterCriticalSection(global_init_lock);
478 #elif defined HAVE_BEOS_THREADS
481 /* Allocate a new semaphore */
482 sem = create_sem(1, "xmlGlobalinitMutex");
484 while (global_init_lock == -1) {
485 if (atomic_add(&global_init_count, 1) == 0) {
486 global_init_lock = sem;
489 atomic_add(&global_init_count, -1);
493 /* If another thread successfully recorded its critical
494 * section in the global_init_lock then discard the one
495 * allocated by this thread. */
496 if (global_init_lock != sem)
499 /* Acquire the chosen semaphore */
500 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
502 xmlGenericError(xmlGenericErrorContext,
503 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
510 __xmlGlobalInitMutexUnlock(void)
512 #ifdef HAVE_PTHREAD_H
513 if (pthread_mutex_unlock != NULL)
514 pthread_mutex_unlock(&global_init_lock);
515 #elif defined HAVE_WIN32_THREADS
516 if (global_init_lock != NULL) {
517 LeaveCriticalSection(global_init_lock);
519 #elif defined HAVE_BEOS_THREADS
520 release_sem(global_init_lock);
525 * xmlGlobalInitMutexDestroy
527 * Makes sure that the global initialization mutex is destroyed before
528 * application termination.
531 __xmlGlobalInitMutexDestroy(void)
533 #ifdef HAVE_PTHREAD_H
534 #elif defined HAVE_WIN32_THREADS
535 if (global_init_lock != NULL) {
536 DeleteCriticalSection(global_init_lock);
537 free(global_init_lock);
538 global_init_lock = NULL;
543 /************************************************************************
545 * Per thread global state handling *
547 ************************************************************************/
549 #ifdef LIBXML_THREAD_ENABLED
555 * xmlFreeGlobalState:
556 * @state: a thread global state
558 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
559 * global state. It is is used here to reclaim memory resources.
562 xmlFreeGlobalState(void *state)
564 xmlGlobalState *gs = (xmlGlobalState *) state;
566 /* free any memory allocated in the thread's xmlLastError */
567 xmlResetError(&(gs->xmlLastError));
574 * xmlNewGlobalState() allocates a global state. This structure is used to
575 * hold all data for use by a thread when supporting backwards compatibility
576 * of libxml2 to pre-thread-safe behaviour.
578 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
580 static xmlGlobalStatePtr
581 xmlNewGlobalState(void)
585 gs = malloc(sizeof(xmlGlobalState));
587 xmlGenericError(xmlGenericErrorContext,
588 "xmlGetGlobalState: out of memory\n");
592 memset(gs, 0, sizeof(xmlGlobalState));
593 xmlInitializeGlobalState(gs);
596 #endif /* LIBXML_THREAD_ENABLED */
598 #ifdef HAVE_PTHREAD_H
599 #elif defined HAVE_WIN32_THREADS
600 #if !defined(HAVE_COMPILER_TLS)
601 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
602 typedef struct _xmlGlobalStateCleanupHelperParams {
605 } xmlGlobalStateCleanupHelperParams;
608 xmlGlobalStateCleanupHelper(void *p)
610 xmlGlobalStateCleanupHelperParams *params =
611 (xmlGlobalStateCleanupHelperParams *) p;
612 WaitForSingleObject(params->thread, INFINITE);
613 CloseHandle(params->thread);
614 xmlFreeGlobalState(params->memory);
618 #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
620 typedef struct _xmlGlobalStateCleanupHelperParams {
622 struct _xmlGlobalStateCleanupHelperParams *prev;
623 struct _xmlGlobalStateCleanupHelperParams *next;
624 } xmlGlobalStateCleanupHelperParams;
626 static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
627 static CRITICAL_SECTION cleanup_helpers_cs;
629 #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
630 #endif /* HAVE_COMPILER_TLS */
631 #endif /* HAVE_WIN32_THREADS */
633 #if defined HAVE_BEOS_THREADS
636 * xmlGlobalStateCleanup:
637 * @data: unused parameter
642 xmlGlobalStateCleanup(void *data)
644 void *globalval = tls_get(globalkey);
646 if (globalval != NULL)
647 xmlFreeGlobalState(globalval);
654 * xmlGetGlobalState() is called to retrieve the global state for a thread.
656 * Returns the thread global state or NULL in case of error
659 xmlGetGlobalState(void)
661 #ifdef HAVE_PTHREAD_H
662 xmlGlobalState *globalval;
664 if (libxml_is_threaded == 0)
667 pthread_once(&once_control, xmlOnceInit);
669 if ((globalval = (xmlGlobalState *)
670 pthread_getspecific(globalkey)) == NULL) {
671 xmlGlobalState *tsd = xmlNewGlobalState();
675 pthread_setspecific(globalkey, tsd);
679 #elif defined HAVE_WIN32_THREADS
680 #if defined(HAVE_COMPILER_TLS)
681 if (!tlstate_inited) {
683 xmlInitializeGlobalState(&tlstate);
686 #else /* HAVE_COMPILER_TLS */
687 xmlGlobalState *globalval;
688 xmlGlobalStateCleanupHelperParams *p;
691 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
692 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
694 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
695 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
697 if (globalval == NULL) {
698 xmlGlobalState *tsd = xmlNewGlobalState();
702 p = (xmlGlobalStateCleanupHelperParams *)
703 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
705 xmlGenericError(xmlGenericErrorContext,
706 "xmlGetGlobalState: out of memory\n");
707 xmlFreeGlobalState(tsd);
711 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
712 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
713 GetCurrentProcess(), &p->thread, 0, TRUE,
714 DUPLICATE_SAME_ACCESS);
715 TlsSetValue(globalkey, tsd);
716 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
718 EnterCriticalSection(&cleanup_helpers_cs);
719 if (cleanup_helpers_head != NULL) {
720 cleanup_helpers_head->prev = p;
722 p->next = cleanup_helpers_head;
724 cleanup_helpers_head = p;
725 TlsSetValue(globalkey, p);
726 LeaveCriticalSection(&cleanup_helpers_cs);
732 #endif /* HAVE_COMPILER_TLS */
733 #elif defined HAVE_BEOS_THREADS
734 xmlGlobalState *globalval;
738 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
739 xmlGlobalState *tsd = xmlNewGlobalState();
743 tls_set(globalkey, tsd);
744 on_exit_thread(xmlGlobalStateCleanup, NULL);
753 /************************************************************************
755 * Library wide thread interfaces *
757 ************************************************************************/
762 * xmlGetThreadId() find the current thread ID number
763 * Note that this is likely to be broken on some platforms using pthreads
764 * as the specification doesn't mandate pthread_t to be an integer type
766 * Returns the current thread ID number
771 #ifdef HAVE_PTHREAD_H
775 if (libxml_is_threaded == 0)
778 /* horrible but preserves compat, see warning above */
779 memcpy(&ret, &id, sizeof(ret));
781 #elif defined HAVE_WIN32_THREADS
782 return GetCurrentThreadId();
783 #elif defined HAVE_BEOS_THREADS
784 return find_thread(NULL);
793 * xmlIsMainThread() check whether the current thread is the main thread.
795 * Returns 1 if the current thread is the main thread, 0 otherwise
798 xmlIsMainThread(void)
800 #ifdef HAVE_PTHREAD_H
801 if (libxml_is_threaded == -1)
803 if (libxml_is_threaded == 0)
805 pthread_once(&once_control, xmlOnceInit);
806 #elif defined HAVE_WIN32_THREADS
808 #elif defined HAVE_BEOS_THREADS
813 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
815 #ifdef HAVE_PTHREAD_H
816 return (pthread_equal(mainthread,pthread_self()));
817 #elif defined HAVE_WIN32_THREADS
818 return (mainthread == GetCurrentThreadId());
819 #elif defined HAVE_BEOS_THREADS
820 return (mainthread == find_thread(NULL));
829 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
836 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
838 xmlRMutexLock(xmlLibraryLock);
844 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
848 xmlUnlockLibrary(void)
851 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
853 xmlRMutexUnlock(xmlLibraryLock);
859 * xmlInitThreads() is used to to initialize all the thread related
860 * data of the libxml2 library.
865 #ifdef HAVE_PTHREAD_H
866 if (libxml_is_threaded == -1) {
867 if ((pthread_once != NULL) &&
868 (pthread_getspecific != NULL) &&
869 (pthread_setspecific != NULL) &&
870 (pthread_key_create != NULL) &&
871 (pthread_key_delete != NULL) &&
872 (pthread_mutex_init != NULL) &&
873 (pthread_mutex_destroy != NULL) &&
874 (pthread_mutex_lock != NULL) &&
875 (pthread_mutex_unlock != NULL) &&
876 (pthread_cond_init != NULL) &&
877 (pthread_cond_destroy != NULL) &&
878 (pthread_cond_wait != NULL) &&
879 (pthread_equal != NULL) &&
880 (pthread_self != NULL) &&
881 (pthread_cond_signal != NULL)) {
882 libxml_is_threaded = 1;
884 /* fprintf(stderr, "Running multithreaded\n"); */
887 /* fprintf(stderr, "Running without multithread\n"); */
888 libxml_is_threaded = 0;
891 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
892 InitializeCriticalSection(&cleanup_helpers_cs);
899 * xmlCleanupThreads() is used to to cleanup all the thread related
900 * data of the libxml2 library once processing has ended.
902 * WARNING: if your application is multithreaded or has plugin support
903 * calling this may crash the application if another thread or
904 * a plugin is still using libxml2. It's sometimes very hard to
905 * guess if libxml2 is in use in the application, some libraries
906 * or plugins may use it without notice. In case of doubt abstain
907 * from calling this function or do it just before calling exit()
908 * to avoid leak reports from valgrind !
911 xmlCleanupThreads(void)
914 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
916 #ifdef HAVE_PTHREAD_H
917 if ((libxml_is_threaded) && (pthread_key_delete != NULL))
918 pthread_key_delete(globalkey);
919 once_control = once_control_init;
920 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
921 if (globalkey != TLS_OUT_OF_INDEXES) {
922 xmlGlobalStateCleanupHelperParams *p;
924 EnterCriticalSection(&cleanup_helpers_cs);
925 p = cleanup_helpers_head;
927 xmlGlobalStateCleanupHelperParams *temp = p;
930 xmlFreeGlobalState(temp->memory);
933 cleanup_helpers_head = 0;
934 LeaveCriticalSection(&cleanup_helpers_cs);
936 globalkey = TLS_OUT_OF_INDEXES;
938 DeleteCriticalSection(&cleanup_helpers_cs);
942 #ifdef LIBXML_THREAD_ENABLED
947 * xmlOnceInit() is used to initialize the value of mainthread for use
948 * in other routines. This function should only be called using
949 * pthread_once() in association with the once_control variable to ensure
950 * that the function is only called once. See man pthread_once for more
956 #ifdef HAVE_PTHREAD_H
957 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
958 mainthread = pthread_self();
959 __xmlInitializeDict();
960 #elif defined(HAVE_WIN32_THREADS)
961 if (!run_once.done) {
962 if (InterlockedIncrement(&run_once.control) == 1) {
963 #if !defined(HAVE_COMPILER_TLS)
964 globalkey = TlsAlloc();
966 mainthread = GetCurrentThreadId();
967 __xmlInitializeDict();
970 /* Another thread is working; give up our slice and
971 * wait until they're done. */
972 while (!run_once.done)
976 #elif defined HAVE_BEOS_THREADS
977 if (atomic_add(&run_once_init, 1) == 0) {
978 globalkey = tls_allocate();
979 tls_set(globalkey, NULL);
980 mainthread = find_thread(NULL);
981 __xmlInitializeDict();
983 atomic_add(&run_once_init, -1);
990 * @hinstDLL: handle to DLL instance
991 * @fdwReason: Reason code for entry
992 * @lpvReserved: generic pointer (depends upon reason code)
994 * Entry point for Windows library. It is being used to free thread-specific
997 * Returns TRUE always
999 #ifdef HAVE_PTHREAD_H
1000 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
1001 #if defined(LIBXML_STATIC_FOR_DLL)
1003 xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1006 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1009 switch (fdwReason) {
1010 case DLL_THREAD_DETACH:
1011 if (globalkey != TLS_OUT_OF_INDEXES) {
1012 xmlGlobalState *globalval = NULL;
1013 xmlGlobalStateCleanupHelperParams *p =
1014 (xmlGlobalStateCleanupHelperParams *)
1015 TlsGetValue(globalkey);
1016 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
1018 xmlFreeGlobalState(globalval);
1019 TlsSetValue(globalkey, NULL);
1022 EnterCriticalSection(&cleanup_helpers_cs);
1023 if (p == cleanup_helpers_head)
1024 cleanup_helpers_head = p->next;
1026 p->prev->next = p->next;
1027 if (p->next != NULL)
1028 p->next->prev = p->prev;
1029 LeaveCriticalSection(&cleanup_helpers_cs);
1038 #define bottom_threads
1039 #include "elfgcchack.h"