Commit d58a42de authored by Igor Zlatkovic's avatar Igor Zlatkovic

applied patch from Stephane for threads

parent 5f5b7bb7
...@@ -92,7 +92,7 @@ static pthread_once_t once_control = PTHREAD_ONCE_INIT; ...@@ -92,7 +92,7 @@ static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static __declspec(thread) xmlGlobalState tlstate; static __declspec(thread) xmlGlobalState tlstate;
static __declspec(thread) int tlstate_inited = 0; static __declspec(thread) int tlstate_inited = 0;
#else /* HAVE_COMPILER_TLS */ #else /* HAVE_COMPILER_TLS */
static DWORD globalkey; static DWORD globalkey = TLS_OUT_OF_INDEXES;
#endif /* HAVE_COMPILER_TLS */ #endif /* HAVE_COMPILER_TLS */
static DWORD mainthread; static DWORD mainthread;
static int run_once_init = 1; static int run_once_init = 1;
...@@ -323,7 +323,7 @@ xmlNewGlobalState(void) ...@@ -323,7 +323,7 @@ xmlNewGlobalState(void)
#ifdef HAVE_WIN32_THREADS #ifdef HAVE_WIN32_THREADS
#if !defined(HAVE_COMPILER_TLS) #if !defined(HAVE_COMPILER_TLS) && defined(LIBXML_STATIC)
typedef struct _xmlGlobalStateCleanupHelperParams typedef struct _xmlGlobalStateCleanupHelperParams
{ {
HANDLE thread; HANDLE thread;
...@@ -339,7 +339,7 @@ static void xmlGlobalStateCleanupHelper (void *p) ...@@ -339,7 +339,7 @@ static void xmlGlobalStateCleanupHelper (void *p)
free(params); free(params);
_endthread(); _endthread();
} }
#endif /* HAVE_COMPILER_TLS */ #endif /* HAVE_COMPILER_TLS && LIBXML_STATIC */
#endif /* HAVE_WIN32_THREADS */ #endif /* HAVE_WIN32_THREADS */
/** /**
...@@ -381,13 +381,17 @@ xmlGetGlobalState(void) ...@@ -381,13 +381,17 @@ xmlGetGlobalState(void)
} }
if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) { if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState(); xmlGlobalState *tsd = xmlNewGlobalState();
#if defined(LIBXML_STATIC)
xmlGlobalStateCleanupHelperParams *p = xmlGlobalStateCleanupHelperParams *p =
(xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams)); (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
p->memory = tsd; p->memory = tsd;
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS); GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
#endif
TlsSetValue(globalkey, tsd); TlsSetValue(globalkey, tsd);
#if defined(LIBXML_STATIC)
_beginthread(xmlGlobalStateCleanupHelper, 0, p); _beginthread(xmlGlobalStateCleanupHelper, 0, p);
#endif
return (tsd); return (tsd);
} }
...@@ -535,3 +539,34 @@ xmlOnceInit(void) { ...@@ -535,3 +539,34 @@ xmlOnceInit(void) {
mainthread = GetCurrentThreadId(); mainthread = GetCurrentThreadId();
#endif #endif
} }
/**
* DllMain
*
* Entry point for Windows library. It is being used to free thread-specific
* storage.
*/
#if defined(HAVE_WIN32_THREADS) && !defined(LIBXML_STATIC)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch(fdwReason) {
case DLL_THREAD_DETACH:
if (globalkey != TLS_OUT_OF_INDEXES) {
xmlGlobalState *globalval = (xmlGlobalState *)TlsGetValue(globalkey);
if (globalval) {
xmlFreeGlobalState(globalval);
TlsSetValue(globalkey, NULL);
}
}
break;
case DLL_PROCESS_DETACH:
if (globalkey != TLS_OUT_OF_INDEXES) {
TlsFree(globalkey);
globalkey = TLS_OUT_OF_INDEXES;
}
break;
}
return TRUE;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment