#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <libxml/threads.h>
#include <libxml/parser.h>
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#ifdef LIBXML_RELAXNG_ENABLED
#include <libxml/relaxng.h>
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/xmlschemastypes.h>
#endif
#if defined(SOLARIS)
#include <note.h>
#endif
#include "private/cata.h"
#include "private/dict.h"
#include "private/enc.h"
#include "private/error.h"
#include "private/globals.h"
#include "private/io.h"
#include "private/memory.h"
#include "private/threads.h"
#include "private/xpath.h"
static xmlRMutex xmlLibraryLock;
void
xmlInitMutex(xmlMutex *mutex)
{
#ifdef HAVE_POSIX_THREADS
pthread_mutex_init(&mutex->lock, NULL);
#elif defined HAVE_WIN32_THREADS
InitializeCriticalSection(&mutex->cs);
#else
(void) mutex;
#endif
}
xmlMutex *
xmlNewMutex(void)
{
xmlMutexPtr tok;
tok = malloc(sizeof(xmlMutex));
if (tok == NULL)
return (NULL);
xmlInitMutex(tok);
return (tok);
}
void
xmlCleanupMutex(xmlMutex *mutex)
{
#ifdef HAVE_POSIX_THREADS
pthread_mutex_destroy(&mutex->lock);
#elif defined HAVE_WIN32_THREADS
DeleteCriticalSection(&mutex->cs);
#else
(void) mutex;
#endif
}
void
xmlFreeMutex(xmlMutex *tok)
{
if (tok == NULL)
return;
xmlCleanupMutex(tok);
free(tok);
}
void
xmlMutexLock(xmlMutex *tok)
{
if (tok == NULL)
return;
#ifdef HAVE_POSIX_THREADS
pthread_mutex_lock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
EnterCriticalSection(&tok->cs);
#endif
}
void
xmlMutexUnlock(xmlMutex *tok)
{
if (tok == NULL)
return;
#ifdef HAVE_POSIX_THREADS
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
LeaveCriticalSection(&tok->cs);
#endif
}
void
xmlInitRMutex(xmlRMutex *tok) {
(void) tok;
#ifdef HAVE_POSIX_THREADS
pthread_mutex_init(&tok->lock, NULL);
tok->held = 0;
tok->waiters = 0;
pthread_cond_init(&tok->cv, NULL);
#elif defined HAVE_WIN32_THREADS
InitializeCriticalSection(&tok->cs);
#endif
}
xmlRMutex *
xmlNewRMutex(void)
{
xmlRMutexPtr tok;
tok = malloc(sizeof(xmlRMutex));
if (tok == NULL)
return (NULL);
xmlInitRMutex(tok);
return (tok);
}
void
xmlCleanupRMutex(xmlRMutex *tok) {
(void) tok;
#ifdef HAVE_POSIX_THREADS
pthread_mutex_destroy(&tok->lock);
pthread_cond_destroy(&tok->cv);
#elif defined HAVE_WIN32_THREADS
DeleteCriticalSection(&tok->cs);
#endif
}
void
xmlFreeRMutex(xmlRMutex *tok)
{
if (tok == NULL)
return;
xmlCleanupRMutex(tok);
free(tok);
}
void
xmlRMutexLock(xmlRMutex *tok)
{
if (tok == NULL)
return;
#ifdef HAVE_POSIX_THREADS
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);
#endif
}
void
xmlRMutexUnlock(xmlRMutex *tok ATTRIBUTE_UNUSED)
{
if (tok == NULL)
return;
#ifdef HAVE_POSIX_THREADS
pthread_mutex_lock(&tok->lock);
tok->held--;
if (tok->held == 0) {
if (tok->waiters)
pthread_cond_signal(&tok->cv);
memset(&tok->tid, 0, sizeof(tok->tid));
}
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
LeaveCriticalSection(&tok->cs);
#endif
}
void
xmlLockLibrary(void)
{
xmlRMutexLock(&xmlLibraryLock);
}
void
xmlUnlockLibrary(void)
{
xmlRMutexUnlock(&xmlLibraryLock);
}
void
xmlInitThreads(void)
{
xmlInitParser();
}
void
xmlCleanupThreads(void)
{
}
static void
xmlInitThreadsInternal(void) {
xmlInitRMutex(&xmlLibraryLock);
}
static void
xmlCleanupThreadsInternal(void) {
xmlCleanupRMutex(&xmlLibraryLock);
}
static int xmlParserInitialized = 0;
#ifdef HAVE_POSIX_THREADS
static pthread_once_t onceControl = PTHREAD_ONCE_INIT;
#elif defined HAVE_WIN32_THREADS
static INIT_ONCE onceControl = INIT_ONCE_STATIC_INIT;
#else
static int onceControl = 0;
#endif
static void
xmlInitParserInternal(void) {
xmlInitRandom();
xmlInitMemoryInternal();
xmlInitThreadsInternal();
xmlInitGlobalsInternal();
xmlInitDictInternal();
xmlInitEncodingInternal();
#if defined(LIBXML_XPATH_ENABLED)
xmlInitXPathInternal();
#endif
xmlInitIOCallbacks();
#ifdef LIBXML_CATALOG_ENABLED
xmlInitCatalogInternal();
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
xmlInitSchemasTypesInternal();
#endif
#ifdef LIBXML_RELAXNG_ENABLED
xmlInitRelaxNGInternal();
#endif
xmlParserInitialized = 1;
}
#if defined(HAVE_WIN32_THREADS)
static BOOL WINAPI
xmlInitParserWinWrapper(INIT_ONCE *initOnce ATTRIBUTE_UNUSED,
void *parameter ATTRIBUTE_UNUSED,
void **context ATTRIBUTE_UNUSED) {
xmlInitParserInternal();
return(TRUE);
}
#endif
void
xmlInitParser(void) {
#ifdef HAVE_POSIX_THREADS
pthread_once(&onceControl, xmlInitParserInternal);
#elif defined(HAVE_WIN32_THREADS)
InitOnceExecuteOnce(&onceControl, xmlInitParserWinWrapper, NULL, NULL);
#else
if (onceControl == 0) {
xmlInitParserInternal();
onceControl = 1;
}
#endif
}
void
xmlCleanupParser(void) {
if (!xmlParserInitialized)
return;
xmlCleanupCharEncodingHandlers();
#ifdef LIBXML_CATALOG_ENABLED
xmlCatalogCleanup();
xmlCleanupCatalogInternal();
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
xmlSchemaCleanupTypes();
#endif
#ifdef LIBXML_RELAXNG_ENABLED
xmlRelaxNGCleanupTypes();
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
xmlCleanupSchemasTypesInternal();
#endif
#ifdef LIBXML_RELAXNG_ENABLED
xmlCleanupRelaxNGInternal();
#endif
xmlCleanupDictInternal();
xmlCleanupRandom();
xmlCleanupGlobalsInternal();
xmlCleanupThreadsInternal();
xmlCleanupMemoryInternal();
xmlParserInitialized = 0;
#ifdef HAVE_POSIX_THREADS
{
pthread_once_t tmp = PTHREAD_ONCE_INIT;
memcpy(&onceControl, &tmp, sizeof(tmp));
}
#elif defined(HAVE_WIN32_THREADS)
{
INIT_ONCE tmp = INIT_ONCE_STATIC_INIT;
memcpy(&onceControl, &tmp, sizeof(tmp));
}
#else
onceControl = 0;
#endif
}
#if defined(HAVE_FUNC_ATTRIBUTE_DESTRUCTOR) && \
!defined(LIBXML_THREAD_ALLOC_ENABLED) && \
!defined(LIBXML_STATIC) && \
!defined(_WIN32)
static void
ATTRIBUTE_DESTRUCTOR
xmlDestructor(void) {
if (xmlFree == free)
xmlCleanupParser();
}
#endif