#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <libxml/threads.h>
#include <libxml/globals.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HAVE_WIN32_THREADS
#include <windows.h>
#ifndef HAVE_COMPILER_TLS
#include <process.h>
#endif
#endif
#if defined(SOLARIS)
#include <note.h>
#endif
struct _xmlMutex {
#ifdef HAVE_PTHREAD_H
pthread_mutex_t lock;
#elif defined HAVE_WIN32_THREADS
HANDLE mutex;
#else
int empty;
#endif
};
struct _xmlRMutex {
#ifdef HAVE_PTHREAD_H
pthread_mutex_t lock;
unsigned int held;
unsigned int waiters;
pthread_t tid;
pthread_cond_t cv;
#elif defined HAVE_WIN32_THREADS
CRITICAL_SECTION cs;
unsigned int count;
#else
int empty;
#endif
};
#ifdef HAVE_PTHREAD_H
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(HAVE_COMPILER_TLS)
static __declspec(thread) xmlGlobalState tlstate;
static __declspec(thread) int tlstate_inited = 0;
#else
static DWORD globalkey;
#endif
static DWORD mainthread;
static int run_once_init = 1;
#endif
static xmlRMutexPtr xmlLibraryLock = NULL;
static void xmlOnceInit(void);
xmlMutexPtr
xmlNewMutex(void)
{
xmlMutexPtr tok;
if ((tok = malloc(sizeof(xmlMutex))) == NULL)
return (NULL);
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&tok->lock, NULL);
#elif defined HAVE_WIN32_THREADS
tok->mutex = CreateMutex(NULL, FALSE, NULL);
#endif
return (tok);
}
void
xmlFreeMutex(xmlMutexPtr tok)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&tok->lock);
#elif defined HAVE_WIN32_THREADS
CloseHandle(tok->mutex);
#endif
free(tok);
}
void
xmlMutexLock(xmlMutexPtr tok ATTRIBUTE_UNUSED)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
WaitForSingleObject(tok->mutex, INFINITE);
#endif
}
void
xmlMutexUnlock(xmlMutexPtr tok ATTRIBUTE_UNUSED)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
ReleaseMutex(tok->mutex);
#endif
}
xmlRMutexPtr
xmlNewRMutex(void)
{
xmlRMutexPtr tok;
if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
return (NULL);
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&tok->lock, NULL);
tok->held = 0;
tok->waiters = 0;
#elif defined HAVE_WIN32_THREADS
InitializeCriticalSection(&tok->cs);
tok->count = 0;
#endif
return (tok);
}
void
xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&tok->lock);
#elif defined HAVE_WIN32_THREADS
DeleteCriticalSection(&tok->cs);
#endif
free(tok);
}
void
xmlRMutexLock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&tok->lock);
if (tok->held) {
if (pthread_equal(tok->tid, pthread_self())) {
tok->held++;
pthread_mutex_unlock(&tok->lock);
return;
} else {
tok->waiters++;
while (tok->held)
pthread_cond_wait(&tok->cv, &tok->lock);
tok->waiters--;
}
}
tok->tid = pthread_self();
tok->held = 1;
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
EnterCriticalSection(&tok->cs);
++tok->count;
#endif
}
void
xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&tok->lock);
tok->held--;
if (tok->held == 0) {
if (tok->waiters)
pthread_cond_signal(&tok->cv);
tok->tid = 0;
}
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
if (!--tok->count)
LeaveCriticalSection(&tok->cs);
#endif
}
#ifdef LIBXML_THREAD_ENABLED
static void
xmlFreeGlobalState(void *state)
{
free(state);
}
static xmlGlobalStatePtr
xmlNewGlobalState(void)
{
xmlGlobalState *gs;
gs = malloc(sizeof(xmlGlobalState));
if (gs == NULL)
return(NULL);
memset(gs, 0, sizeof(xmlGlobalState));
xmlInitializeGlobalState(gs);
return (gs);
}
#endif
#ifdef HAVE_WIN32_THREADS
#if !defined(HAVE_COMPILER_TLS)
typedef struct _xmlGlobalStateCleanupHelperParams
{
HANDLE thread;
void *memory;
} xmlGlobalStateCleanupHelperParams;
static void xmlGlobalStateCleanupHelper (void *p)
{
xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p;
WaitForSingleObject(params->thread, INFINITE);
CloseHandle(params->thread);
xmlFreeGlobalState(params->memory);
free(params);
_endthread();
}
#endif
#endif
xmlGlobalStatePtr
xmlGetGlobalState(void)
{
#ifdef HAVE_PTHREAD_H
xmlGlobalState *globalval;
pthread_once(&once_control, xmlOnceInit);
if ((globalval = (xmlGlobalState *)
pthread_getspecific(globalkey)) == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState();
pthread_setspecific(globalkey, tsd);
return (tsd);
}
return (globalval);
#elif defined HAVE_WIN32_THREADS
#if defined(HAVE_COMPILER_TLS)
if (!tlstate_inited) {
tlstate_inited = 1;
xmlInitializeGlobalState(&tlstate);
}
return &tlstate;
#else
xmlGlobalState *globalval;
if (run_once_init) {
run_once_init = 0;
xmlOnceInit();
}
if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
xmlGlobalState *tsd = xmlNewGlobalState();
xmlGlobalStateCleanupHelperParams *p =
(xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
p->memory = tsd;
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
TlsSetValue(globalkey, tsd);
_beginthread(xmlGlobalStateCleanupHelper, 0, p);
return (tsd);
}
return (globalval);
#endif
#else
return(NULL);
#endif
}
int
xmlGetThreadId(void)
{
#ifdef HAVE_PTHREAD_H
return((int) pthread_self());
#elif defined HAVE_WIN32_THREADS
return GetCurrentThreadId();
#else
return((int) 0);
#endif
}
int
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 ();
}
#endif
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
#endif
#ifdef HAVE_PTHREAD_H
return(mainthread == pthread_self());
#elif defined HAVE_WIN32_THREADS
return(mainthread == GetCurrentThreadId ());
#else
return(1);
#endif
}
void
xmlLockLibrary(void)
{
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
#endif
xmlRMutexLock(xmlLibraryLock);
}
void
xmlUnlockLibrary(void)
{
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
#endif
xmlRMutexUnlock(xmlLibraryLock);
}
void
xmlInitThreads(void)
{
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n");
#endif
}
void
xmlCleanupThreads(void)
{
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
#endif
}
static void
xmlOnceInit(void) {
#ifdef HAVE_PTHREAD_H
(void) pthread_key_create(&globalkey, xmlFreeGlobalState);
mainthread = pthread_self();
#endif
#if defined(HAVE_WIN32_THREADS)
#if !defined(HAVE_COMPILER_TLS)
globalkey = TlsAlloc();
#endif
mainthread = GetCurrentThreadId();
#endif
}