mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-08-01 10:06:59 +03:00
improvements to the Windows-side of thread handling
This commit is contained in:
61
threads.c
61
threads.c
@ -27,9 +27,10 @@
|
||||
#ifdef HAVE_PTHREAD_H
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_WIN32_THREADS
|
||||
#include <windows.h>
|
||||
#ifndef _MSC_VER
|
||||
#ifndef HAVE_COMPILER_TLS
|
||||
#include <process.h>
|
||||
#endif
|
||||
#endif
|
||||
@ -87,15 +88,16 @@ static pthread_key_t globalkey;
|
||||
static pthread_t mainthread;
|
||||
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
#if defined(HAVE_COMPILER_TLS)
|
||||
static __declspec(thread) xmlGlobalState tlstate;
|
||||
static __declspec(thread) int tlstate_inited = 0;
|
||||
#else
|
||||
#else /* HAVE_COMPILER_TLS */
|
||||
static DWORD globalkey;
|
||||
#endif /* _MSC_VER */
|
||||
#endif /* HAVE_COMPILER_TLS */
|
||||
static DWORD mainthread;
|
||||
static int run_once_init = 1;
|
||||
#endif /* HAVE_WIN32_THREADS */
|
||||
|
||||
static xmlRMutexPtr xmlLibraryLock = NULL;
|
||||
static void xmlOnceInit(void);
|
||||
|
||||
@ -270,7 +272,8 @@ xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
|
||||
}
|
||||
pthread_mutex_unlock(&tok->lock);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
if (!--tok->count) LeaveCriticalSection (&tok->cs);
|
||||
if (!--tok->count)
|
||||
LeaveCriticalSection(&tok->cs);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -281,7 +284,6 @@ xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
|
||||
************************************************************************/
|
||||
|
||||
#ifdef LIBXML_THREAD_ENABLED
|
||||
#ifndef _MSC_VER
|
||||
/**
|
||||
* xmlFreeGlobalState:
|
||||
* @state: a thread global state
|
||||
@ -317,7 +319,6 @@ xmlNewGlobalState(void)
|
||||
xmlInitializeGlobalState(gs);
|
||||
return (gs);
|
||||
}
|
||||
#endif /* _MSC_VER */
|
||||
#endif /* LIBXML_THREAD_ENABLED */
|
||||
|
||||
|
||||
@ -330,14 +331,14 @@ xmlNewGlobalState(void)
|
||||
*/
|
||||
|
||||
#ifdef HAVE_WIN32_THREADS
|
||||
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
|
||||
#if !defined(HAVE_COMPILER_TLS)
|
||||
typedef struct _xmlGlobalStateCleanupHelperParams
|
||||
{
|
||||
HANDLE thread;
|
||||
void *memory;
|
||||
} xmlGlobalStateCleanupHelperParams;
|
||||
|
||||
void __cdecl xmlGlobalStateCleanupHelper (void *p)
|
||||
void xmlGlobalStateCleanupHelper (void *p)
|
||||
{
|
||||
xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
|
||||
WaitForSingleObject(params->thread, INFINITE);
|
||||
@ -346,7 +347,7 @@ void __cdecl xmlGlobalStateCleanupHelper (void *p)
|
||||
free(params);
|
||||
_endthread();
|
||||
}
|
||||
#endif /* _MSC_VER */
|
||||
#endif /* HAVE_COMPILER_TLS */
|
||||
#endif /* HAVE_WIN32_THREADS */
|
||||
|
||||
xmlGlobalStatePtr
|
||||
@ -366,34 +367,33 @@ xmlGetGlobalState(void)
|
||||
}
|
||||
return (globalval);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
if (!tlstate_inited)
|
||||
{
|
||||
#if defined(HAVE_COMPILER_TLS)
|
||||
if (!tlstate_inited) {
|
||||
tlstate_inited = 1;
|
||||
xmlInitializeGlobalState(&tlstate);
|
||||
}
|
||||
|
||||
return &tlstate;
|
||||
#else /* !_MSC_VER */
|
||||
#else /* HAVE_COMPILER_TLS */
|
||||
xmlGlobalState *globalval;
|
||||
|
||||
if (run_once_init) { run_once_init = 0; xmlOnceInit (); }
|
||||
|
||||
if ((globalval = (xmlGlobalState *) TlsGetValue (globalkey)) == NULL)
|
||||
{
|
||||
if (run_once_init) {
|
||||
run_once_init = 0;
|
||||
xmlOnceInit();
|
||||
}
|
||||
if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
|
||||
xmlGlobalState *tsd = xmlNewGlobalState();
|
||||
xmlGlobalStateCleanupHelperParams *p = (xmlGlobalStateCleanupHelperParams *) malloc (sizeof (xmlGlobalStateCleanupHelperParams));
|
||||
|
||||
xmlGlobalStateCleanupHelperParams *p =
|
||||
(xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
|
||||
p->memory = tsd;
|
||||
DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), GetCurrentProcess (), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
||||
|
||||
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
||||
GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
||||
TlsSetValue(globalkey, tsd);
|
||||
_beginthread(xmlGlobalStateCleanupHelper, 0, p);
|
||||
|
||||
return (tsd);
|
||||
}
|
||||
return (globalval);
|
||||
#endif /* _MSC_VER */
|
||||
#endif /* HAVE_COMPILER_TLS */
|
||||
#else
|
||||
return(NULL);
|
||||
#endif
|
||||
@ -437,7 +437,10 @@ xmlIsMainThread(void)
|
||||
#ifdef HAVE_PTHREAD_H
|
||||
pthread_once(&once_control, xmlOnceInit);
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
if (run_once_init) { run_once_init = 0; xmlOnceInit (); }
|
||||
if (run_once_init) {
|
||||
run_once_init = 0;
|
||||
xmlOnceInit ();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_THREADS
|
||||
@ -524,10 +527,12 @@ xmlOnceInit(void) {
|
||||
#ifdef HAVE_PTHREAD_H
|
||||
(void) pthread_key_create(&globalkey, xmlFreeGlobalState);
|
||||
mainthread = pthread_self();
|
||||
#elif defined HAVE_WIN32_THREADS
|
||||
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_WIN32_THREADS)
|
||||
#if !defined(HAVE_COMPILER_TLS)
|
||||
globalkey = TlsAlloc();
|
||||
#endif /* _MSC_VER */
|
||||
#endif
|
||||
mainthread = GetCurrentThreadId();
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user