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
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 if (pthread_mutex_lock != NULL)
443 pthread_mutex_lock(&global_init_lock);
444 #elif defined HAVE_WIN32_THREADS
445 LPCRITICAL_SECTION cs;
447 /* Create a new critical section */
448 if (global_init_lock == NULL) {
449 cs = malloc(sizeof(CRITICAL_SECTION));
451 xmlGenericError(xmlGenericErrorContext,
452 "xmlGlobalInitMutexLock: out of memory\n");
455 InitializeCriticalSection(cs);
457 /* Swap it into the global_init_lock */
458 #ifdef InterlockedCompareExchangePointer
459 InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
460 #else /* Use older void* version */
461 InterlockedCompareExchange((void **) &global_init_lock,
463 #endif /* InterlockedCompareExchangePointer */
465 /* If another thread successfully recorded its critical
466 * section in the global_init_lock then discard the one
467 * allocated by this thread. */
468 if (global_init_lock != cs) {
469 DeleteCriticalSection(cs);
474 /* Lock the chosen critical section */
475 EnterCriticalSection(global_init_lock);
476 #elif defined HAVE_BEOS_THREADS
479 /* Allocate a new semaphore */
480 sem = create_sem(1, "xmlGlobalinitMutex");
482 while (global_init_lock == -1) {
483 if (atomic_add(&global_init_count, 1) == 0) {
484 global_init_lock = sem;
487 atomic_add(&global_init_count, -1);
491 /* If another thread successfully recorded its critical
492 * section in the global_init_lock then discard the one
493 * allocated by this thread. */
494 if (global_init_lock != sem)
497 /* Acquire the chosen semaphore */
498 if (acquire_sem(global_init_lock) != B_NO_ERROR) {
500 xmlGenericError(xmlGenericErrorContext,
501 "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
508 __xmlGlobalInitMutexUnlock(void)
510 #ifdef HAVE_PTHREAD_H
511 if (pthread_mutex_unlock != NULL)
512 pthread_mutex_unlock(&global_init_lock);
513 #elif defined HAVE_WIN32_THREADS
514 if (global_init_lock != NULL) {
515 LeaveCriticalSection(global_init_lock);
517 #elif defined HAVE_BEOS_THREADS
518 release_sem(global_init_lock);
523 * xmlGlobalInitMutexDestroy
525 * Makes sure that the global initialization mutex is destroyed before
526 * application termination.
529 __xmlGlobalInitMutexDestroy(void)
531 #ifdef HAVE_PTHREAD_H
532 #elif defined HAVE_WIN32_THREADS
533 if (global_init_lock != NULL) {
534 DeleteCriticalSection(global_init_lock);
535 free(global_init_lock);
536 global_init_lock = NULL;
541 /************************************************************************
543 * Per thread global state handling *
545 ************************************************************************/
547 #ifdef LIBXML_THREAD_ENABLED
553 * xmlFreeGlobalState:
554 * @state: a thread global state
556 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
557 * global state. It is is used here to reclaim memory resources.
560 xmlFreeGlobalState(void *state)
562 xmlGlobalState *gs = (xmlGlobalState *) state;
564 /* free any memory allocated in the thread's xmlLastError */
565 xmlResetError(&(gs->xmlLastError));
572 * xmlNewGlobalState() allocates a global state. This structure is used to
573 * hold all data for use by a thread when supporting backwards compatibility
574 * of libxml2 to pre-thread-safe behaviour.
576 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
578 static xmlGlobalStatePtr
579 xmlNewGlobalState(void)
583 gs = malloc(sizeof(xmlGlobalState));
585 xmlGenericError(xmlGenericErrorContext,
586 "xmlGetGlobalState: out of memory\n");
590 memset(gs, 0, sizeof(xmlGlobalState));
591 xmlInitializeGlobalState(gs);
594 #endif /* LIBXML_THREAD_ENABLED */
596 #ifdef HAVE_PTHREAD_H
597 #elif defined HAVE_WIN32_THREADS
598 #if !defined(HAVE_COMPILER_TLS)
599 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
600 typedef struct _xmlGlobalStateCleanupHelperParams {
603 } xmlGlobalStateCleanupHelperParams;
606 xmlGlobalStateCleanupHelper(void *p)
608 xmlGlobalStateCleanupHelperParams *params =
609 (xmlGlobalStateCleanupHelperParams *) p;
610 WaitForSingleObject(params->thread, INFINITE);
611 CloseHandle(params->thread);
612 xmlFreeGlobalState(params->memory);
616 #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
618 typedef struct _xmlGlobalStateCleanupHelperParams {
620 struct _xmlGlobalStateCleanupHelperParams *prev;
621 struct _xmlGlobalStateCleanupHelperParams *next;
622 } xmlGlobalStateCleanupHelperParams;
624 static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
625 static CRITICAL_SECTION cleanup_helpers_cs;
627 #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
628 #endif /* HAVE_COMPILER_TLS */
629 #endif /* HAVE_WIN32_THREADS */
631 #if defined HAVE_BEOS_THREADS
634 * xmlGlobalStateCleanup:
635 * @data: unused parameter
640 xmlGlobalStateCleanup(void *data)
642 void *globalval = tls_get(globalkey);
644 if (globalval != NULL)
645 xmlFreeGlobalState(globalval);
652 * xmlGetGlobalState() is called to retrieve the global state for a thread.
654 * Returns the thread global state or NULL in case of error
657 xmlGetGlobalState(void)
659 #ifdef HAVE_PTHREAD_H
660 xmlGlobalState *globalval;
662 if (libxml_is_threaded == 0)
665 pthread_once(&once_control, xmlOnceInit);
667 if ((globalval = (xmlGlobalState *)
668 pthread_getspecific(globalkey)) == NULL) {
669 xmlGlobalState *tsd = xmlNewGlobalState();
673 pthread_setspecific(globalkey, tsd);
677 #elif defined HAVE_WIN32_THREADS
678 #if defined(HAVE_COMPILER_TLS)
679 if (!tlstate_inited) {
681 xmlInitializeGlobalState(&tlstate);
684 #else /* HAVE_COMPILER_TLS */
685 xmlGlobalState *globalval;
686 xmlGlobalStateCleanupHelperParams *p;
689 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
690 globalval = (xmlGlobalState *) TlsGetValue(globalkey);
692 p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
693 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
695 if (globalval == NULL) {
696 xmlGlobalState *tsd = xmlNewGlobalState();
700 p = (xmlGlobalStateCleanupHelperParams *)
701 malloc(sizeof(xmlGlobalStateCleanupHelperParams));
703 xmlGenericError(xmlGenericErrorContext,
704 "xmlGetGlobalState: out of memory\n");
705 xmlFreeGlobalState(tsd);
709 #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
710 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
711 GetCurrentProcess(), &p->thread, 0, TRUE,
712 DUPLICATE_SAME_ACCESS);
713 TlsSetValue(globalkey, tsd);
714 _beginthread(xmlGlobalStateCleanupHelper, 0, p);
716 EnterCriticalSection(&cleanup_helpers_cs);
717 if (cleanup_helpers_head != NULL) {
718 cleanup_helpers_head->prev = p;
720 p->next = cleanup_helpers_head;
722 cleanup_helpers_head = p;
723 TlsSetValue(globalkey, p);
724 LeaveCriticalSection(&cleanup_helpers_cs);
730 #endif /* HAVE_COMPILER_TLS */
731 #elif defined HAVE_BEOS_THREADS
732 xmlGlobalState *globalval;
736 if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
737 xmlGlobalState *tsd = xmlNewGlobalState();
741 tls_set(globalkey, tsd);
742 on_exit_thread(xmlGlobalStateCleanup, NULL);
751 /************************************************************************
753 * Library wide thread interfaces *
755 ************************************************************************/
760 * xmlGetThreadId() find the current thread ID number
761 * Note that this is likely to be broken on some platforms using pthreads
762 * as the specification doesn't mandate pthread_t to be an integer type
764 * Returns the current thread ID number
769 #ifdef HAVE_PTHREAD_H
773 if (libxml_is_threaded == 0)
776 /* horrible but preserves compat, see warning above */
777 memcpy(&ret, &id, sizeof(ret));
779 #elif defined HAVE_WIN32_THREADS
780 return GetCurrentThreadId();
781 #elif defined HAVE_BEOS_THREADS
782 return find_thread(NULL);
791 * xmlIsMainThread() check whether the current thread is the main thread.
793 * Returns 1 if the current thread is the main thread, 0 otherwise
796 xmlIsMainThread(void)
798 #ifdef HAVE_PTHREAD_H
799 if (libxml_is_threaded == -1)
801 if (libxml_is_threaded == 0)
803 pthread_once(&once_control, xmlOnceInit);
804 #elif defined HAVE_WIN32_THREADS
806 #elif defined HAVE_BEOS_THREADS
811 xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
813 #ifdef HAVE_PTHREAD_H
814 return (pthread_equal(mainthread,pthread_self()));
815 #elif defined HAVE_WIN32_THREADS
816 return (mainthread == GetCurrentThreadId());
817 #elif defined HAVE_BEOS_THREADS
818 return (mainthread == find_thread(NULL));
827 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
834 xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
836 xmlRMutexLock(xmlLibraryLock);
842 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
846 xmlUnlockLibrary(void)
849 xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
851 xmlRMutexUnlock(xmlLibraryLock);
857 * xmlInitThreads() is used to to initialize all the thread related
858 * data of the libxml2 library.
863 #ifdef HAVE_PTHREAD_H
864 if (libxml_is_threaded == -1) {
865 if ((pthread_once != NULL) &&
866 (pthread_getspecific != NULL) &&
867 (pthread_setspecific != NULL) &&
868 (pthread_key_create != NULL) &&
869 (pthread_key_delete != NULL) &&
870 (pthread_mutex_init != NULL) &&
871 (pthread_mutex_destroy != NULL) &&
872 (pthread_mutex_lock != NULL) &&
873 (pthread_mutex_unlock != NULL) &&
874 (pthread_cond_init != NULL) &&
875 (pthread_cond_destroy != NULL) &&
876 (pthread_cond_wait != NULL) &&
877 (pthread_equal != NULL) &&
878 (pthread_self != NULL) &&
879 (pthread_cond_signal != NULL)) {
880 libxml_is_threaded = 1;
882 /* fprintf(stderr, "Running multithreaded\n"); */
885 /* fprintf(stderr, "Running without multithread\n"); */
886 libxml_is_threaded = 0;
889 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
890 InitializeCriticalSection(&cleanup_helpers_cs);
897 * xmlCleanupThreads() is used to to cleanup all the thread related
898 * data of the libxml2 library once processing has ended.
900 * WARNING: if your application is multithreaded or has plugin support
901 * calling this may crash the application if another thread or
902 * a plugin is still using libxml2. It's sometimes very hard to
903 * guess if libxml2 is in use in the application, some libraries
904 * or plugins may use it without notice. In case of doubt abstain
905 * from calling this function or do it just before calling exit()
906 * to avoid leak reports from valgrind !
909 xmlCleanupThreads(void)
912 xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
914 #ifdef HAVE_PTHREAD_H
915 if ((libxml_is_threaded) && (pthread_key_delete != NULL))
916 pthread_key_delete(globalkey);
917 once_control = once_control_init;
918 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
919 if (globalkey != TLS_OUT_OF_INDEXES) {
920 xmlGlobalStateCleanupHelperParams *p;
922 EnterCriticalSection(&cleanup_helpers_cs);
923 p = cleanup_helpers_head;
925 xmlGlobalStateCleanupHelperParams *temp = p;
928 xmlFreeGlobalState(temp->memory);
931 cleanup_helpers_head = 0;
932 LeaveCriticalSection(&cleanup_helpers_cs);
934 globalkey = TLS_OUT_OF_INDEXES;
936 DeleteCriticalSection(&cleanup_helpers_cs);
940 #ifdef LIBXML_THREAD_ENABLED
945 * xmlOnceInit() is used to initialize the value of mainthread for use
946 * in other routines. This function should only be called using
947 * pthread_once() in association with the once_control variable to ensure
948 * that the function is only called once. See man pthread_once for more
954 #ifdef HAVE_PTHREAD_H
955 (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
956 mainthread = pthread_self();
957 __xmlInitializeDict();
958 #elif defined(HAVE_WIN32_THREADS)
959 if (!run_once.done) {
960 if (InterlockedIncrement(&run_once.control) == 1) {
961 #if !defined(HAVE_COMPILER_TLS)
962 globalkey = TlsAlloc();
964 mainthread = GetCurrentThreadId();
965 __xmlInitializeDict();
968 /* Another thread is working; give up our slice and
969 * wait until they're done. */
970 while (!run_once.done)
974 #elif defined HAVE_BEOS_THREADS
975 if (atomic_add(&run_once_init, 1) == 0) {
976 globalkey = tls_allocate();
977 tls_set(globalkey, NULL);
978 mainthread = find_thread(NULL);
979 __xmlInitializeDict();
981 atomic_add(&run_once_init, -1);
988 * @hinstDLL: handle to DLL instance
989 * @fdwReason: Reason code for entry
990 * @lpvReserved: generic pointer (depends upon reason code)
992 * Entry point for Windows library. It is being used to free thread-specific
995 * Returns TRUE always
997 #ifdef HAVE_PTHREAD_H
998 #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
999 #if defined(LIBXML_STATIC_FOR_DLL)
1001 xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1004 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1007 switch (fdwReason) {
1008 case DLL_THREAD_DETACH:
1009 if (globalkey != TLS_OUT_OF_INDEXES) {
1010 xmlGlobalState *globalval = NULL;
1011 xmlGlobalStateCleanupHelperParams *p =
1012 (xmlGlobalStateCleanupHelperParams *)
1013 TlsGetValue(globalkey);
1014 globalval = (xmlGlobalState *) (p ? p->memory : NULL);
1016 xmlFreeGlobalState(globalval);
1017 TlsSetValue(globalkey, NULL);
1020 EnterCriticalSection(&cleanup_helpers_cs);
1021 if (p == cleanup_helpers_head)
1022 cleanup_helpers_head = p->next;
1024 p->prev->next = p->next;
1025 if (p->next != NULL)
1026 p->next->prev = p->prev;
1027 LeaveCriticalSection(&cleanup_helpers_cs);
1036 #define bottom_threads
1037 #include "elfgcchack.h"