1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-26 00:37:43 +03:00

check some malloc returns with Ashwin patch, add error messages and

* threads.c: check some malloc returns with Ashwin patch, add
  error messages and reindent the module.
Daniel

svn path=/trunk/; revision=3709
This commit is contained in:
Daniel Veillard
2008-03-18 08:24:25 +00:00
parent 14dad705d5
commit ddbe38b4f1
2 changed files with 247 additions and 209 deletions

View File

@@ -1,3 +1,8 @@
Tue Mar 18 09:23:05 CET 2008 Daniel Veillard <daniel@veillard.com>
* threads.c: check some malloc returns with Ashwin patch, add
error messages and reindent the module.
Fri Mar 14 15:28:43 CET 2008 Daniel Veillard <daniel@veillard.com> Fri Mar 14 15:28:43 CET 2008 Daniel Veillard <daniel@veillard.com>
* xmlreader.c: patch from Ashwin removing duplicate tests * xmlreader.c: patch from Ashwin removing duplicate tests

443
threads.c
View File

@@ -101,8 +101,8 @@ struct _xmlMutex {
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
HANDLE mutex; HANDLE mutex;
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
sem_id sem; sem_id sem;
thread_id tid; thread_id tid;
#else #else
int empty; int empty;
#endif #endif
@@ -114,21 +114,22 @@ struct _xmlMutex {
struct _xmlRMutex { struct _xmlRMutex {
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
pthread_mutex_t lock; pthread_mutex_t lock;
unsigned int held; unsigned int held;
unsigned int waiters; unsigned int waiters;
pthread_t tid; pthread_t tid;
pthread_cond_t cv; pthread_cond_t cv;
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
unsigned int count; unsigned int count;
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
xmlMutexPtr lock; xmlMutexPtr lock;
thread_id tid; thread_id tid;
int32 count; int32 count;
#else #else
int empty; int empty;
#endif #endif
}; };
/* /*
* This module still has some internal static data. * This module still has some internal static data.
* - xmlLibraryLock a global lock * - xmlLibraryLock a global lock
@@ -136,8 +137,8 @@ struct _xmlRMutex {
*/ */
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
static pthread_key_t globalkey; 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;
static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
@@ -148,12 +149,12 @@ static __declspec(thread) int tlstate_inited = 0;
static DWORD globalkey = TLS_OUT_OF_INDEXES; static DWORD globalkey = TLS_OUT_OF_INDEXES;
#endif /* HAVE_COMPILER_TLS */ #endif /* HAVE_COMPILER_TLS */
static DWORD mainthread; static DWORD mainthread;
static struct static struct {
{
DWORD done; DWORD done;
DWORD control; DWORD control;
} run_once = { 0, 0 }; } run_once = { 0, 0};
static volatile LPCRITICAL_SECTION global_init_lock = NULL; static volatile LPCRITICAL_SECTION global_init_lock = NULL;
/* endif HAVE_WIN32_THREADS */ /* endif HAVE_WIN32_THREADS */
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
int32 globalkey = 0; int32 globalkey = 0;
@@ -163,7 +164,8 @@ static int32 global_init_lock = -1;
static vint32 global_init_count = 0; static vint32 global_init_count = 0;
#endif #endif
static xmlRMutexPtr xmlLibraryLock = NULL; static xmlRMutexPtr xmlLibraryLock = NULL;
#ifdef LIBXML_THREAD_ENABLED #ifdef LIBXML_THREAD_ENABLED
static void xmlOnceInit(void); static void xmlOnceInit(void);
#endif #endif
@@ -185,15 +187,15 @@ xmlNewMutex(void)
return (NULL); return (NULL);
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) if (libxml_is_threaded != 0)
pthread_mutex_init(&tok->lock, NULL); pthread_mutex_init(&tok->lock, NULL);
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
tok->mutex = CreateMutex(NULL, FALSE, NULL); tok->mutex = CreateMutex(NULL, FALSE, NULL);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) { if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
free(tok); free(tok);
return NULL; return NULL;
} }
tok->tid = -1; tok->tid = -1;
#endif #endif
return (tok); return (tok);
} }
@@ -208,15 +210,16 @@ xmlNewMutex(void)
void void
xmlFreeMutex(xmlMutexPtr tok) xmlFreeMutex(xmlMutexPtr tok)
{ {
if (tok == NULL) return; if (tok == NULL)
return;
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) if (libxml_is_threaded != 0)
pthread_mutex_destroy(&tok->lock); pthread_mutex_destroy(&tok->lock);
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
CloseHandle(tok->mutex); CloseHandle(tok->mutex);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
delete_sem(tok->sem); delete_sem(tok->sem);
#endif #endif
free(tok); free(tok);
} }
@@ -234,17 +237,18 @@ xmlMutexLock(xmlMutexPtr tok)
return; return;
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) if (libxml_is_threaded != 0)
pthread_mutex_lock(&tok->lock); pthread_mutex_lock(&tok->lock);
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
WaitForSingleObject(tok->mutex, INFINITE); WaitForSingleObject(tok->mutex, INFINITE);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if (acquire_sem(tok->sem) != B_NO_ERROR) { if (acquire_sem(tok->sem) != B_NO_ERROR) {
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n"); xmlGenericError(xmlGenericErrorContext,
exit(); "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
exit();
#endif #endif
} }
tok->tid = find_thread(NULL); tok->tid = find_thread(NULL);
#endif #endif
} }
@@ -262,14 +266,14 @@ xmlMutexUnlock(xmlMutexPtr tok)
return; return;
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) if (libxml_is_threaded != 0)
pthread_mutex_unlock(&tok->lock); pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
ReleaseMutex(tok->mutex); ReleaseMutex(tok->mutex);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if (tok->tid == find_thread(NULL)) { if (tok->tid == find_thread(NULL)) {
tok->tid = -1; tok->tid = -1;
release_sem(tok->sem); release_sem(tok->sem);
} }
#endif #endif
} }
@@ -292,20 +296,20 @@ xmlNewRMutex(void)
return (NULL); return (NULL);
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) { if (libxml_is_threaded != 0) {
pthread_mutex_init(&tok->lock, NULL); pthread_mutex_init(&tok->lock, NULL);
tok->held = 0; tok->held = 0;
tok->waiters = 0; tok->waiters = 0;
pthread_cond_init(&tok->cv, NULL); pthread_cond_init(&tok->cv, NULL);
} }
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
InitializeCriticalSection(&tok->cs); InitializeCriticalSection(&tok->cs);
tok->count = 0; tok->count = 0;
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if ((tok->lock = xmlNewMutex()) == NULL) { if ((tok->lock = xmlNewMutex()) == NULL) {
free(tok); free(tok);
return NULL; return NULL;
} }
tok->count = 0; tok->count = 0;
#endif #endif
return (tok); return (tok);
} }
@@ -324,13 +328,13 @@ xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
return; return;
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded != 0) { if (libxml_is_threaded != 0) {
pthread_mutex_destroy(&tok->lock); pthread_mutex_destroy(&tok->lock);
pthread_cond_destroy(&tok->cv); pthread_cond_destroy(&tok->cv);
} }
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
DeleteCriticalSection(&tok->cs); DeleteCriticalSection(&tok->cs);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
xmlFreeMutex(tok->lock); xmlFreeMutex(tok->lock);
#endif #endif
free(tok); free(tok);
} }
@@ -370,13 +374,13 @@ xmlRMutexLock(xmlRMutexPtr tok)
EnterCriticalSection(&tok->cs); EnterCriticalSection(&tok->cs);
++tok->count; ++tok->count;
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if (tok->lock->tid == find_thread(NULL)) { if (tok->lock->tid == find_thread(NULL)) {
tok->count++; tok->count++;
return; return;
} else { } else {
xmlMutexLock(tok->lock); xmlMutexLock(tok->lock);
tok->count = 1; tok->count = 1;
} }
#endif #endif
} }
@@ -405,15 +409,15 @@ 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) if (!--tok->count)
LeaveCriticalSection(&tok->cs); LeaveCriticalSection(&tok->cs);
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
if (tok->lock->tid == find_thread(NULL)) { if (tok->lock->tid == find_thread(NULL)) {
tok->count--; tok->count--;
if (tok->count == 0) { if (tok->count == 0) {
xmlMutexUnlock(tok->lock); xmlMutexUnlock(tok->lock);
} }
return; return;
} }
#endif #endif
} }
@@ -435,23 +439,29 @@ __xmlGlobalInitMutexLock(void)
/* Create a new critical section */ /* Create a new critical section */
if (global_init_lock == NULL) { if (global_init_lock == NULL) {
cs = malloc(sizeof(CRITICAL_SECTION)); cs = malloc(sizeof(CRITICAL_SECTION));
InitializeCriticalSection(cs); if (cs == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlGlobalInitMutexLock: out of memory\n");
return;
}
InitializeCriticalSection(cs);
/* Swap it into the global_init_lock */ /* Swap it into the global_init_lock */
#ifdef InterlockedCompareExchangePointer #ifdef InterlockedCompareExchangePointer
InterlockedCompareExchangePointer(&global_init_lock, cs, NULL); InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
#else /* Use older void* version */ #else /* Use older void* version */
InterlockedCompareExchange((void **)&global_init_lock, (void *)cs, NULL); InterlockedCompareExchange((void **) &global_init_lock,
(void *) cs, NULL);
#endif /* InterlockedCompareExchangePointer */ #endif /* InterlockedCompareExchangePointer */
/* If another thread successfully recorded its critical /* If another thread successfully recorded its critical
* section in the global_init_lock then discard the one * section in the global_init_lock then discard the one
* allocated by this thread. */ * allocated by this thread. */
if (global_init_lock != cs) { if (global_init_lock != cs) {
DeleteCriticalSection(cs); DeleteCriticalSection(cs);
free(cs); free(cs);
} }
} }
/* Lock the chosen critical section */ /* Lock the chosen critical section */
@@ -463,25 +473,26 @@ __xmlGlobalInitMutexLock(void)
sem = create_sem(1, "xmlGlobalinitMutex"); sem = create_sem(1, "xmlGlobalinitMutex");
while (global_init_lock == -1) { while (global_init_lock == -1) {
if (atomic_add(&global_init_count, 1) == 0) { if (atomic_add(&global_init_count, 1) == 0) {
global_init_lock = sem; global_init_lock = sem;
} else { } else {
snooze(1); snooze(1);
atomic_add(&global_init_count, -1); atomic_add(&global_init_count, -1);
} }
} }
/* If another thread successfully recorded its critical /* If another thread successfully recorded its critical
* section in the global_init_lock then discard the one * section in the global_init_lock then discard the one
* allocated by this thread. */ * allocated by this thread. */
if (global_init_lock != sem) if (global_init_lock != sem)
delete_sem(sem); delete_sem(sem);
/* Acquire the chosen semaphore */ /* Acquire the chosen semaphore */
if (acquire_sem(global_init_lock) != B_NO_ERROR) { if (acquire_sem(global_init_lock) != B_NO_ERROR) {
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n"); xmlGenericError(xmlGenericErrorContext,
exit(); "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
exit();
#endif #endif
} }
#endif #endif
@@ -505,14 +516,14 @@ __xmlGlobalInitMutexUnlock(void)
* Makes sure that the global initialization mutex is destroyed before * Makes sure that the global initialization mutex is destroyed before
* application termination. * application termination.
*/ */
void __xmlGlobalInitMutexDestroy(void) void
__xmlGlobalInitMutexDestroy(void)
{ {
#if defined HAVE_WIN32_THREADS #if defined HAVE_WIN32_THREADS
if (global_init_lock != NULL) if (global_init_lock != NULL) {
{ DeleteCriticalSection(global_init_lock);
DeleteCriticalSection(global_init_lock); free(global_init_lock);
free(global_init_lock); global_init_lock = NULL;
global_init_lock = NULL;
} }
#endif #endif
} }
@@ -527,6 +538,7 @@ void __xmlGlobalInitMutexDestroy(void)
#ifdef xmlLastError #ifdef xmlLastError
#undef xmlLastError #undef xmlLastError
#endif #endif
/** /**
* xmlFreeGlobalState: * xmlFreeGlobalState:
* @state: a thread global state * @state: a thread global state
@@ -559,8 +571,11 @@ xmlNewGlobalState(void)
xmlGlobalState *gs; xmlGlobalState *gs;
gs = malloc(sizeof(xmlGlobalState)); gs = malloc(sizeof(xmlGlobalState));
if (gs == NULL) if (gs == NULL) {
return(NULL); xmlGenericError(xmlGenericErrorContext,
"xmlGetGlobalState: out of memory\n");
return (NULL);
}
memset(gs, 0, sizeof(xmlGlobalState)); memset(gs, 0, sizeof(xmlGlobalState));
xmlInitializeGlobalState(gs); xmlInitializeGlobalState(gs);
@@ -572,15 +587,16 @@ xmlNewGlobalState(void)
#ifdef HAVE_WIN32_THREADS #ifdef HAVE_WIN32_THREADS
#if !defined(HAVE_COMPILER_TLS) #if !defined(HAVE_COMPILER_TLS)
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
typedef struct _xmlGlobalStateCleanupHelperParams typedef struct _xmlGlobalStateCleanupHelperParams {
{
HANDLE thread; HANDLE thread;
void *memory; void *memory;
} xmlGlobalStateCleanupHelperParams; } xmlGlobalStateCleanupHelperParams;
static void XMLCDECL xmlGlobalStateCleanupHelper (void *p) static void XMLCDECL
xmlGlobalStateCleanupHelper(void *p)
{ {
xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p; xmlGlobalStateCleanupHelperParams *params =
(xmlGlobalStateCleanupHelperParams *) p;
WaitForSingleObject(params->thread, INFINITE); WaitForSingleObject(params->thread, INFINITE);
CloseHandle(params->thread); CloseHandle(params->thread);
xmlFreeGlobalState(params->memory); xmlFreeGlobalState(params->memory);
@@ -589,14 +605,13 @@ static void XMLCDECL xmlGlobalStateCleanupHelper (void *p)
} }
#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */ #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
typedef struct _xmlGlobalStateCleanupHelperParams typedef struct _xmlGlobalStateCleanupHelperParams {
{
void *memory; void *memory;
struct _xmlGlobalStateCleanupHelperParams * prev; struct _xmlGlobalStateCleanupHelperParams *prev;
struct _xmlGlobalStateCleanupHelperParams * next; struct _xmlGlobalStateCleanupHelperParams *next;
} xmlGlobalStateCleanupHelperParams; } xmlGlobalStateCleanupHelperParams;
static xmlGlobalStateCleanupHelperParams * cleanup_helpers_head = NULL; static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
static CRITICAL_SECTION cleanup_helpers_cs; static CRITICAL_SECTION cleanup_helpers_cs;
#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */ #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
@@ -604,17 +619,20 @@ static CRITICAL_SECTION cleanup_helpers_cs;
#endif /* HAVE_WIN32_THREADS */ #endif /* HAVE_WIN32_THREADS */
#if defined HAVE_BEOS_THREADS #if defined HAVE_BEOS_THREADS
/** /**
* xmlGlobalStateCleanup: * xmlGlobalStateCleanup:
* @data: unused parameter * @data: unused parameter
* *
* Used for Beos only * Used for Beos only
*/ */
void xmlGlobalStateCleanup(void *data) void
xmlGlobalStateCleanup(void *data)
{ {
void *globalval = tls_get(globalkey); void *globalval = tls_get(globalkey);
if (globalval != NULL)
xmlFreeGlobalState(globalval); if (globalval != NULL)
xmlFreeGlobalState(globalval);
} }
#endif #endif
@@ -632,12 +650,12 @@ xmlGetGlobalState(void)
xmlGlobalState *globalval; xmlGlobalState *globalval;
if (libxml_is_threaded == 0) if (libxml_is_threaded == 0)
return(NULL); return (NULL);
pthread_once(&once_control, xmlOnceInit); pthread_once(&once_control, xmlOnceInit);
if ((globalval = (xmlGlobalState *) if ((globalval = (xmlGlobalState *)
pthread_getspecific(globalkey)) == NULL) { pthread_getspecific(globalkey)) == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState(); xmlGlobalState *tsd = xmlNewGlobalState();
pthread_setspecific(globalkey, tsd); pthread_setspecific(globalkey, tsd);
@@ -647,43 +665,53 @@ xmlGetGlobalState(void)
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
#if defined(HAVE_COMPILER_TLS) #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 /* HAVE_COMPILER_TLS */ #else /* HAVE_COMPILER_TLS */
xmlGlobalState *globalval; xmlGlobalState *globalval;
xmlGlobalStateCleanupHelperParams * p; xmlGlobalStateCleanupHelperParams *p;
xmlOnceInit(); xmlOnceInit();
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
globalval = (xmlGlobalState *)TlsGetValue(globalkey); globalval = (xmlGlobalState *) TlsGetValue(globalkey);
#else #else
p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
globalval = (xmlGlobalState *)(p ? p->memory : NULL); globalval = (xmlGlobalState *) (p ? p->memory : NULL);
#endif #endif
if (globalval == NULL) { if (globalval == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState(); xmlGlobalState *tsd = xmlNewGlobalState();
p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
p->memory = tsd; if (tsd == NULL)
return(NULL);
p = (xmlGlobalStateCleanupHelperParams *)
malloc(sizeof(xmlGlobalStateCleanupHelperParams));
if (p == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlGetGlobalState: out of memory\n");
return(NULL);
}
p->memory = tsd;
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS); GetCurrentProcess(), &p->thread, 0, TRUE,
TlsSetValue(globalkey, tsd); DUPLICATE_SAME_ACCESS);
_beginthread(xmlGlobalStateCleanupHelper, 0, p); TlsSetValue(globalkey, tsd);
_beginthread(xmlGlobalStateCleanupHelper, 0, p);
#else #else
EnterCriticalSection(&cleanup_helpers_cs); EnterCriticalSection(&cleanup_helpers_cs);
if (cleanup_helpers_head != NULL) { if (cleanup_helpers_head != NULL) {
cleanup_helpers_head->prev = p; cleanup_helpers_head->prev = p;
} }
p->next = cleanup_helpers_head; p->next = cleanup_helpers_head;
p->prev = NULL; p->prev = NULL;
cleanup_helpers_head = p; cleanup_helpers_head = p;
TlsSetValue(globalkey, p); TlsSetValue(globalkey, p);
LeaveCriticalSection(&cleanup_helpers_cs); LeaveCriticalSection(&cleanup_helpers_cs);
#endif #endif
return (tsd); return (tsd);
} }
return (globalval); return (globalval);
#endif /* HAVE_COMPILER_TLS */ #endif /* HAVE_COMPILER_TLS */
@@ -693,7 +721,7 @@ xmlGetGlobalState(void)
xmlOnceInit(); xmlOnceInit();
if ((globalval = (xmlGlobalState *) if ((globalval = (xmlGlobalState *)
tls_get(globalkey)) == NULL) { tls_get(globalkey)) == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState(); xmlGlobalState *tsd = xmlNewGlobalState();
tls_set(globalkey, tsd); tls_set(globalkey, tsd);
@@ -702,7 +730,7 @@ xmlGetGlobalState(void)
} }
return (globalval); return (globalval);
#else #else
return(NULL); return (NULL);
#endif #endif
} }
@@ -724,14 +752,14 @@ xmlGetThreadId(void)
{ {
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded == 0) if (libxml_is_threaded == 0)
return(0); return (0);
return((int) pthread_self()); return ((int) pthread_self());
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
return GetCurrentThreadId(); return GetCurrentThreadId();
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
return find_thread(NULL); return find_thread(NULL);
#else #else
return((int) 0); return ((int) 0);
#endif #endif
} }
@@ -749,10 +777,10 @@ xmlIsMainThread(void)
if (libxml_is_threaded == -1) if (libxml_is_threaded == -1)
xmlInitThreads(); xmlInitThreads();
if (libxml_is_threaded == 0) if (libxml_is_threaded == 0)
return(1); return (1);
pthread_once(&once_control, xmlOnceInit); pthread_once(&once_control, xmlOnceInit);
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
xmlOnceInit (); xmlOnceInit();
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
xmlOnceInit(); xmlOnceInit();
#endif #endif
@@ -761,13 +789,13 @@ xmlIsMainThread(void)
xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
#endif #endif
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
return(mainthread == pthread_self()); return (mainthread == pthread_self());
#elif defined HAVE_WIN32_THREADS #elif defined HAVE_WIN32_THREADS
return(mainthread == GetCurrentThreadId ()); return (mainthread == GetCurrentThreadId());
#elif defined HAVE_BEOS_THREADS #elif defined HAVE_BEOS_THREADS
return(mainthread == find_thread(NULL)); return (mainthread == find_thread(NULL));
#else #else
return(1); return (1);
#endif #endif
} }
@@ -819,24 +847,25 @@ xmlInitThreads(void)
#ifdef HAVE_PTHREAD_H #ifdef HAVE_PTHREAD_H
if (libxml_is_threaded == -1) { if (libxml_is_threaded == -1) {
if ((pthread_once != NULL) && if ((pthread_once != NULL) &&
(pthread_getspecific != NULL) && (pthread_getspecific != NULL) &&
(pthread_setspecific != NULL) && (pthread_setspecific != NULL) &&
(pthread_key_create != NULL) && (pthread_key_create != NULL) &&
(pthread_mutex_init != NULL) && (pthread_mutex_init != NULL) &&
(pthread_mutex_destroy != NULL) && (pthread_mutex_destroy != NULL) &&
(pthread_mutex_lock != NULL) && (pthread_mutex_lock != NULL) &&
(pthread_mutex_unlock != NULL) && (pthread_mutex_unlock != NULL) &&
(pthread_cond_init != NULL) && (pthread_cond_init != NULL) &&
(pthread_equal != NULL) && (pthread_equal != NULL) &&
(pthread_self != NULL) && (pthread_self != NULL) &&
(pthread_key_create != NULL) && (pthread_cond_signal != NULL)) {
(pthread_cond_signal != NULL)) { libxml_is_threaded = 1;
libxml_is_threaded = 1;
/* fprintf(stderr, "Running multithreaded\n"); */ /* fprintf(stderr, "Running multithreaded\n"); */
} else { } else {
/* fprintf(stderr, "Running without multithread\n"); */ /* fprintf(stderr, "Running without multithread\n"); */
libxml_is_threaded = 0; libxml_is_threaded = 0;
} }
} }
#endif #endif
} }
@@ -855,25 +884,28 @@ xmlCleanupThreads(void)
#endif #endif
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) #if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
if (globalkey != TLS_OUT_OF_INDEXES) { if (globalkey != TLS_OUT_OF_INDEXES) {
xmlGlobalStateCleanupHelperParams * p; xmlGlobalStateCleanupHelperParams *p;
EnterCriticalSection(&cleanup_helpers_cs);
p = cleanup_helpers_head; EnterCriticalSection(&cleanup_helpers_cs);
while (p != NULL) { p = cleanup_helpers_head;
xmlGlobalStateCleanupHelperParams * temp = p; while (p != NULL) {
p = p->next; xmlGlobalStateCleanupHelperParams *temp = p;
xmlFreeGlobalState(temp->memory);
free(temp); p = p->next;
} xmlFreeGlobalState(temp->memory);
cleanup_helpers_head = 0; free(temp);
LeaveCriticalSection(&cleanup_helpers_cs); }
TlsFree(globalkey); cleanup_helpers_head = 0;
globalkey = TLS_OUT_OF_INDEXES; LeaveCriticalSection(&cleanup_helpers_cs);
TlsFree(globalkey);
globalkey = TLS_OUT_OF_INDEXES;
} }
DeleteCriticalSection(&cleanup_helpers_cs); DeleteCriticalSection(&cleanup_helpers_cs);
#endif #endif
} }
#ifdef LIBXML_THREAD_ENABLED #ifdef LIBXML_THREAD_ENABLED
/** /**
* xmlOnceInit * xmlOnceInit
* *
@@ -884,7 +916,8 @@ xmlCleanupThreads(void)
* details. * details.
*/ */
static void static void
xmlOnceInit(void) { 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();
@@ -892,15 +925,13 @@ xmlOnceInit(void) {
#if defined(HAVE_WIN32_THREADS) #if defined(HAVE_WIN32_THREADS)
if (!run_once.done) { if (!run_once.done) {
if (InterlockedIncrement(&run_once.control) == 1) if (InterlockedIncrement(&run_once.control) == 1) {
{
#if !defined(HAVE_COMPILER_TLS) #if !defined(HAVE_COMPILER_TLS)
globalkey = TlsAlloc(); globalkey = TlsAlloc();
#endif #endif
mainthread = GetCurrentThreadId(); mainthread = GetCurrentThreadId();
run_once.done = 1; run_once.done = 1;
} } else {
else {
/* Another thread is working; give up our slice and /* Another thread is working; give up our slice and
* wait until they're done. */ * wait until they're done. */
while (!run_once.done) while (!run_once.done)
@@ -910,12 +941,12 @@ xmlOnceInit(void) {
#endif #endif
#ifdef HAVE_BEOS_THREADS #ifdef HAVE_BEOS_THREADS
if (atomic_add(&run_once_init, 1) == 0) { if (atomic_add(&run_once_init, 1) == 0) {
globalkey = tls_allocate(); globalkey = tls_allocate();
tls_set(globalkey, NULL); tls_set(globalkey, NULL);
mainthread = find_thread(NULL); mainthread = find_thread(NULL);
} else } else
atomic_add(&run_once_init, -1); atomic_add(&run_once_init, -1);
#endif #endif
} }
#endif #endif
@@ -933,36 +964,38 @@ xmlOnceInit(void) {
*/ */
#if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) #if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
#if defined(LIBXML_STATIC_FOR_DLL) #if defined(LIBXML_STATIC_FOR_DLL)
BOOL XMLCALL xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) BOOL XMLCALL
xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
#else #else
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
#endif #endif
{ {
switch(fdwReason) { switch (fdwReason) {
case DLL_THREAD_DETACH: case DLL_THREAD_DETACH:
if (globalkey != TLS_OUT_OF_INDEXES) { if (globalkey != TLS_OUT_OF_INDEXES) {
xmlGlobalState *globalval = NULL; xmlGlobalState *globalval = NULL;
xmlGlobalStateCleanupHelperParams * p = xmlGlobalStateCleanupHelperParams *p =
(xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); (xmlGlobalStateCleanupHelperParams *)
globalval = (xmlGlobalState *)(p ? p->memory : NULL); TlsGetValue(globalkey);
if (globalval) { globalval = (xmlGlobalState *) (p ? p->memory : NULL);
xmlFreeGlobalState(globalval); if (globalval) {
TlsSetValue(globalkey,NULL); xmlFreeGlobalState(globalval);
TlsSetValue(globalkey, NULL);
}
if (p) {
EnterCriticalSection(&cleanup_helpers_cs);
if (p == cleanup_helpers_head)
cleanup_helpers_head = p->next;
else
p->prev->next = p->next;
if (p->next != NULL)
p->next->prev = p->prev;
LeaveCriticalSection(&cleanup_helpers_cs);
free(p);
}
} }
if (p) break;
{
EnterCriticalSection(&cleanup_helpers_cs);
if (p == cleanup_helpers_head)
cleanup_helpers_head = p->next;
else
p->prev->next = p->next;
if (p->next != NULL)
p->next->prev = p->prev;
LeaveCriticalSection(&cleanup_helpers_cs);
free(p);
}
}
break;
} }
return TRUE; return TRUE;
} }