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