Blame gettext-tools/gnulib-lib/libxml/threads.c

Packit Bot 06c835
/**
Packit Bot 06c835
 * threads.c: set of generic threading related routines
Packit Bot 06c835
 *
Packit Bot 06c835
 * See Copyright for the status of this software.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Gary Pennington <Gary.Pennington@uk.sun.com>
Packit Bot 06c835
 * daniel@veillard.com
Packit Bot 06c835
 */
Packit Bot 06c835
Packit Bot 06c835
#define IN_LIBXML
Packit Bot 06c835
#include "libxml.h"
Packit Bot 06c835
Packit Bot 06c835
#include <string.h>
Packit Bot 06c835
Packit Bot 06c835
#include <libxml/threads.h>
Packit Bot 06c835
#include <libxml/globals.h>
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_SYS_TYPES_H
Packit Bot 06c835
#include <sys/types.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
#ifdef HAVE_UNISTD_H
Packit Bot 06c835
#include <unistd.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
#ifdef HAVE_STDLIB_H
Packit Bot 06c835
#include <stdlib.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
#include <pthread.h>
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
#include <windows.h>
Packit Bot 06c835
#ifndef HAVE_COMPILER_TLS
Packit Bot 06c835
#include <process.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_BEOS_THREADS
Packit Bot 06c835
#include <OS.h>
Packit Bot 06c835
#include <TLS.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
#if defined(SOLARIS)
Packit Bot 06c835
#include <note.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/* #define DEBUG_THREADS */
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
Packit Bot 06c835
static int libxml_is_threaded = -1;
Packit Bot 06c835
#if defined(__GNUC__) && defined(__GLIBC__)
Packit Bot 06c835
#ifdef linux
Packit Bot 06c835
#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)
Packit Bot 06c835
extern int pthread_once (pthread_once_t *__once_control,
Packit Bot 06c835
                         void (*__init_routine) (void))
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern void *pthread_getspecific (pthread_key_t __key)
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_setspecific (pthread_key_t __key,
Packit Bot 06c835
                                __const void *__pointer)
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_key_create (pthread_key_t *__key,
Packit Bot 06c835
                               void (*__destr_function) (void *))
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_key_delete (pthread_key_t __key)
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_mutex_init ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_mutex_destroy ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_mutex_lock ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_mutex_unlock ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_cond_init ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_cond_destroy ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_cond_wait ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_equal ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern pthread_t pthread_self ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_key_create ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_key_delete ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
extern int pthread_cond_signal ()
Packit Bot 06c835
	   __attribute((weak));
Packit Bot 06c835
#endif
Packit Bot 06c835
#endif /* linux */
Packit Bot 06c835
#endif /* defined(__GNUC__) && defined(__GLIBC__) */
Packit Bot 06c835
#endif /* HAVE_PTHREAD_H */
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
Packit Bot 06c835
 *       to avoid some crazyness since xmlMalloc/xmlFree may actually
Packit Bot 06c835
 *       be hosted on allocated blocks needing them for the allocation ...
Packit Bot 06c835
 */
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 * xmlMutex are a simple mutual exception locks
Packit Bot 06c835
 */
Packit Bot 06c835
struct _xmlMutex {
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    pthread_mutex_t lock;
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    HANDLE mutex;
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    sem_id sem;
Packit Bot 06c835
    thread_id tid;
Packit Bot 06c835
#else
Packit Bot 06c835
    int empty;
Packit Bot 06c835
#endif
Packit Bot 06c835
};
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 * xmlRMutex are reentrant mutual exception locks
Packit Bot 06c835
 */
Packit Bot 06c835
struct _xmlRMutex {
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    pthread_mutex_t lock;
Packit Bot 06c835
    unsigned int held;
Packit Bot 06c835
    unsigned int waiters;
Packit Bot 06c835
    pthread_t tid;
Packit Bot 06c835
    pthread_cond_t cv;
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    CRITICAL_SECTION cs;
Packit Bot 06c835
    unsigned int count;
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    xmlMutexPtr lock;
Packit Bot 06c835
    thread_id tid;
Packit Bot 06c835
    int32 count;
Packit Bot 06c835
#else
Packit Bot 06c835
    int empty;
Packit Bot 06c835
#endif
Packit Bot 06c835
};
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 * This module still has some internal static data.
Packit Bot 06c835
 *   - xmlLibraryLock a global lock
Packit Bot 06c835
 *   - globalkey used for per-thread data
Packit Bot 06c835
 */
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
static pthread_key_t globalkey;
Packit Bot 06c835
static pthread_t mainthread;
Packit Bot 06c835
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
Packit Bot 06c835
static pthread_once_t once_control_init = PTHREAD_ONCE_INIT;
Packit Bot 06c835
static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
#if defined(HAVE_COMPILER_TLS)
Packit Bot 06c835
static __declspec(thread) xmlGlobalState tlstate;
Packit Bot 06c835
static __declspec(thread) int tlstate_inited = 0;
Packit Bot 06c835
#else /* HAVE_COMPILER_TLS */
Packit Bot 06c835
static DWORD globalkey = TLS_OUT_OF_INDEXES;
Packit Bot 06c835
#endif /* HAVE_COMPILER_TLS */
Packit Bot 06c835
static DWORD mainthread;
Packit Bot 06c835
static struct {
Packit Bot 06c835
    DWORD done;
Packit Bot 06c835
    DWORD control;
Packit Bot 06c835
} run_once = { 0, 0};
Packit Bot 06c835
static volatile LPCRITICAL_SECTION global_init_lock = NULL;
Packit Bot 06c835
Packit Bot 06c835
/* endif HAVE_WIN32_THREADS */
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
int32 globalkey = 0;
Packit Bot 06c835
thread_id mainthread = 0;
Packit Bot 06c835
int32 run_once_init = 0;
Packit Bot 06c835
static int32 global_init_lock = -1;
Packit Bot 06c835
static vint32 global_init_count = 0;
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
static xmlRMutexPtr xmlLibraryLock = NULL;
Packit Bot 06c835
Packit Bot 06c835
#ifdef LIBXML_THREAD_ENABLED
Packit Bot 06c835
static void xmlOnceInit(void);
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlNewMutex:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlNewMutex() is used to allocate a libxml2 token struct for use in
Packit Bot 06c835
 * synchronizing access to data.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns a new simple mutex pointer or NULL in case of error
Packit Bot 06c835
 */
Packit Bot 06c835
xmlMutexPtr
Packit Bot 06c835
xmlNewMutex(void)
Packit Bot 06c835
{
Packit Bot 06c835
    xmlMutexPtr tok;
Packit Bot 06c835
Packit Bot 06c835
    if ((tok = malloc(sizeof(xmlMutex))) == NULL)
Packit Bot 06c835
        return (NULL);
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0)
Packit Bot 06c835
        pthread_mutex_init(&tok->lock, NULL);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    tok->mutex = CreateMutex(NULL, FALSE, NULL);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) {
Packit Bot 06c835
        free(tok);
Packit Bot 06c835
        return NULL;
Packit Bot 06c835
    }
Packit Bot 06c835
    tok->tid = -1;
Packit Bot 06c835
#endif
Packit Bot 06c835
    return (tok);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlFreeMutex:
Packit Bot 06c835
 * @tok:  the simple mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token
Packit Bot 06c835
 * struct.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlFreeMutex(xmlMutexPtr tok)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0)
Packit Bot 06c835
        pthread_mutex_destroy(&tok->lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    CloseHandle(tok->mutex);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    delete_sem(tok->sem);
Packit Bot 06c835
#endif
Packit Bot 06c835
    free(tok);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlMutexLock:
Packit Bot 06c835
 * @tok:  the simple mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlMutexLock() is used to lock a libxml2 token.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlMutexLock(xmlMutexPtr tok)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0)
Packit Bot 06c835
        pthread_mutex_lock(&tok->lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    WaitForSingleObject(tok->mutex, INFINITE);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if (acquire_sem(tok->sem) != B_NO_ERROR) {
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
        xmlGenericError(xmlGenericErrorContext,
Packit Bot 06c835
                        "xmlMutexLock():BeOS:Couldn't aquire semaphore\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
    }
Packit Bot 06c835
    tok->tid = find_thread(NULL);
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlMutexUnlock:
Packit Bot 06c835
 * @tok:  the simple mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlMutexUnlock() is used to unlock a libxml2 token.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlMutexUnlock(xmlMutexPtr tok)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0)
Packit Bot 06c835
        pthread_mutex_unlock(&tok->lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    ReleaseMutex(tok->mutex);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if (tok->tid == find_thread(NULL)) {
Packit Bot 06c835
        tok->tid = -1;
Packit Bot 06c835
        release_sem(tok->sem);
Packit Bot 06c835
    }
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlNewRMutex:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlRNewMutex() is used to allocate a reentrant mutex for use in
Packit Bot 06c835
 * synchronizing access to data. token_r is a re-entrant lock and thus useful
Packit Bot 06c835
 * for synchronizing access to data structures that may be manipulated in a
Packit Bot 06c835
 * recursive fashion.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns the new reentrant mutex pointer or NULL in case of error
Packit Bot 06c835
 */
Packit Bot 06c835
xmlRMutexPtr
Packit Bot 06c835
xmlNewRMutex(void)
Packit Bot 06c835
{
Packit Bot 06c835
    xmlRMutexPtr tok;
Packit Bot 06c835
Packit Bot 06c835
    if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
Packit Bot 06c835
        return (NULL);
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0) {
Packit Bot 06c835
        pthread_mutex_init(&tok->lock, NULL);
Packit Bot 06c835
        tok->held = 0;
Packit Bot 06c835
        tok->waiters = 0;
Packit Bot 06c835
        pthread_cond_init(&tok->cv, NULL);
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    InitializeCriticalSection(&tok->cs);
Packit Bot 06c835
    tok->count = 0;
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if ((tok->lock = xmlNewMutex()) == NULL) {
Packit Bot 06c835
        free(tok);
Packit Bot 06c835
        return NULL;
Packit Bot 06c835
    }
Packit Bot 06c835
    tok->count = 0;
Packit Bot 06c835
#endif
Packit Bot 06c835
    return (tok);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlFreeRMutex:
Packit Bot 06c835
 * @tok:  the reentrant mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlRFreeMutex() is used to reclaim resources associated with a
Packit Bot 06c835
 * reentrant mutex.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded != 0) {
Packit Bot 06c835
        pthread_mutex_destroy(&tok->lock);
Packit Bot 06c835
        pthread_cond_destroy(&tok->cv);
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    DeleteCriticalSection(&tok->cs);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    xmlFreeMutex(tok->lock);
Packit Bot 06c835
#endif
Packit Bot 06c835
    free(tok);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlRMutexLock:
Packit Bot 06c835
 * @tok:  the reentrant mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlRMutexLock() is used to lock a libxml2 token_r.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlRMutexLock(xmlRMutexPtr tok)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded == 0)
Packit Bot 06c835
        return;
Packit Bot 06c835
Packit Bot 06c835
    pthread_mutex_lock(&tok->lock);
Packit Bot 06c835
    if (tok->held) {
Packit Bot 06c835
        if (pthread_equal(tok->tid, pthread_self())) {
Packit Bot 06c835
            tok->held++;
Packit Bot 06c835
            pthread_mutex_unlock(&tok->lock);
Packit Bot 06c835
            return;
Packit Bot 06c835
        } else {
Packit Bot 06c835
            tok->waiters++;
Packit Bot 06c835
            while (tok->held)
Packit Bot 06c835
                pthread_cond_wait(&tok->cv, &tok->lock);
Packit Bot 06c835
            tok->waiters--;
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
    tok->tid = pthread_self();
Packit Bot 06c835
    tok->held = 1;
Packit Bot 06c835
    pthread_mutex_unlock(&tok->lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    EnterCriticalSection(&tok->cs);
Packit Bot 06c835
    tok->count++;
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if (tok->lock->tid == find_thread(NULL)) {
Packit Bot 06c835
        tok->count++;
Packit Bot 06c835
        return;
Packit Bot 06c835
    } else {
Packit Bot 06c835
        xmlMutexLock(tok->lock);
Packit Bot 06c835
        tok->count = 1;
Packit Bot 06c835
    }
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlRMutexUnlock:
Packit Bot 06c835
 * @tok:  the reentrant mutex
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlRMutexUnlock() is used to unlock a libxml2 token_r.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
Packit Bot 06c835
{
Packit Bot 06c835
    if (tok == NULL)
Packit Bot 06c835
        return;
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded == 0)
Packit Bot 06c835
        return;
Packit Bot 06c835
Packit Bot 06c835
    pthread_mutex_lock(&tok->lock);
Packit Bot 06c835
    tok->held--;
Packit Bot 06c835
    if (tok->held == 0) {
Packit Bot 06c835
        if (tok->waiters)
Packit Bot 06c835
            pthread_cond_signal(&tok->cv);
Packit Bot 06c835
        memset(&tok->tid, 0, sizeof(tok->tid));
Packit Bot 06c835
    }
Packit Bot 06c835
    pthread_mutex_unlock(&tok->lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    if (tok->count > 0) {
Packit Bot 06c835
	tok->count--;
Packit Bot 06c835
        LeaveCriticalSection(&tok->cs);
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if (tok->lock->tid == find_thread(NULL)) {
Packit Bot 06c835
        tok->count--;
Packit Bot 06c835
        if (tok->count == 0) {
Packit Bot 06c835
            xmlMutexUnlock(tok->lock);
Packit Bot 06c835
        }
Packit Bot 06c835
        return;
Packit Bot 06c835
    }
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlGlobalInitMutexLock
Packit Bot 06c835
 *
Packit Bot 06c835
 * Makes sure that the global initialization mutex is initialized and
Packit Bot 06c835
 * locks it.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
__xmlGlobalInitMutexLock(void)
Packit Bot 06c835
{
Packit Bot 06c835
    /* Make sure the global init lock is initialized and then lock it. */
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    /* The mutex is statically initialized, so we just lock it. */
Packit Bot 06c835
    if (pthread_mutex_lock != NULL)
Packit Bot 06c835
        pthread_mutex_lock(&global_init_lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    LPCRITICAL_SECTION cs;
Packit Bot 06c835
Packit Bot 06c835
    /* Create a new critical section */
Packit Bot 06c835
    if (global_init_lock == NULL) {
Packit Bot 06c835
        cs = malloc(sizeof(CRITICAL_SECTION));
Packit Bot 06c835
        if (cs == NULL) {
Packit Bot 06c835
            xmlGenericError(xmlGenericErrorContext,
Packit Bot 06c835
                            "xmlGlobalInitMutexLock: out of memory\n");
Packit Bot 06c835
            return;
Packit Bot 06c835
        }
Packit Bot 06c835
        InitializeCriticalSection(cs);
Packit Bot 06c835
Packit Bot 06c835
        /* Swap it into the global_init_lock */
Packit Bot 06c835
#ifdef InterlockedCompareExchangePointer
Packit Bot 06c835
        InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);
Packit Bot 06c835
#else /* Use older void* version */
Packit Bot 06c835
        InterlockedCompareExchange((void **) &global_init_lock,
Packit Bot 06c835
                                   (void *) cs, NULL);
Packit Bot 06c835
#endif /* InterlockedCompareExchangePointer */
Packit Bot 06c835
Packit Bot 06c835
        /* If another thread successfully recorded its critical
Packit Bot 06c835
         * section in the global_init_lock then discard the one
Packit Bot 06c835
         * allocated by this thread. */
Packit Bot 06c835
        if (global_init_lock != cs) {
Packit Bot 06c835
            DeleteCriticalSection(cs);
Packit Bot 06c835
            free(cs);
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
    /* Lock the chosen critical section */
Packit Bot 06c835
    EnterCriticalSection(global_init_lock);
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    int32 sem;
Packit Bot 06c835
Packit Bot 06c835
    /* Allocate a new semaphore */
Packit Bot 06c835
    sem = create_sem(1, "xmlGlobalinitMutex");
Packit Bot 06c835
Packit Bot 06c835
    while (global_init_lock == -1) {
Packit Bot 06c835
        if (atomic_add(&global_init_count, 1) == 0) {
Packit Bot 06c835
            global_init_lock = sem;
Packit Bot 06c835
        } else {
Packit Bot 06c835
            snooze(1);
Packit Bot 06c835
            atomic_add(&global_init_count, -1);
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
    /* If another thread successfully recorded its critical
Packit Bot 06c835
     * section in the global_init_lock then discard the one
Packit Bot 06c835
     * allocated by this thread. */
Packit Bot 06c835
    if (global_init_lock != sem)
Packit Bot 06c835
        delete_sem(sem);
Packit Bot 06c835
Packit Bot 06c835
    /* Acquire the chosen semaphore */
Packit Bot 06c835
    if (acquire_sem(global_init_lock) != B_NO_ERROR) {
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
        xmlGenericError(xmlGenericErrorContext,
Packit Bot 06c835
                        "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
    }
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
void
Packit Bot 06c835
__xmlGlobalInitMutexUnlock(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (pthread_mutex_unlock != NULL)
Packit Bot 06c835
        pthread_mutex_unlock(&global_init_lock);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    if (global_init_lock != NULL) {
Packit Bot 06c835
	LeaveCriticalSection(global_init_lock);
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    release_sem(global_init_lock);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlGlobalInitMutexDestroy
Packit Bot 06c835
 *
Packit Bot 06c835
 * Makes sure that the global initialization mutex is destroyed before
Packit Bot 06c835
 * application termination.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
__xmlGlobalInitMutexDestroy(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    if (global_init_lock != NULL) {
Packit Bot 06c835
        DeleteCriticalSection(global_init_lock);
Packit Bot 06c835
        free(global_init_lock);
Packit Bot 06c835
        global_init_lock = NULL;
Packit Bot 06c835
    }
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/************************************************************************
Packit Bot 06c835
 *									*
Packit Bot 06c835
 *			Per thread global state handling		*
Packit Bot 06c835
 *									*
Packit Bot 06c835
 ************************************************************************/
Packit Bot 06c835
Packit Bot 06c835
#ifdef LIBXML_THREAD_ENABLED
Packit Bot 06c835
#ifdef xmlLastError
Packit Bot 06c835
#undef xmlLastError
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlFreeGlobalState:
Packit Bot 06c835
 * @state:  a thread global state
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlFreeGlobalState() is called when a thread terminates with a non-NULL
Packit Bot 06c835
 * global state. It is is used here to reclaim memory resources.
Packit Bot 06c835
 */
Packit Bot 06c835
static void
Packit Bot 06c835
xmlFreeGlobalState(void *state)
Packit Bot 06c835
{
Packit Bot 06c835
    xmlGlobalState *gs = (xmlGlobalState *) state;
Packit Bot 06c835
Packit Bot 06c835
    /* free any memory allocated in the thread's xmlLastError */
Packit Bot 06c835
    xmlResetError(&(gs->xmlLastError));
Packit Bot 06c835
    free(state);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlNewGlobalState:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlNewGlobalState() allocates a global state. This structure is used to
Packit Bot 06c835
 * hold all data for use by a thread when supporting backwards compatibility
Packit Bot 06c835
 * of libxml2 to pre-thread-safe behaviour.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error
Packit Bot 06c835
 */
Packit Bot 06c835
static xmlGlobalStatePtr
Packit Bot 06c835
xmlNewGlobalState(void)
Packit Bot 06c835
{
Packit Bot 06c835
    xmlGlobalState *gs;
Packit Bot 06c835
Packit Bot 06c835
    gs = malloc(sizeof(xmlGlobalState));
Packit Bot 06c835
    if (gs == NULL) {
Packit Bot 06c835
	xmlGenericError(xmlGenericErrorContext,
Packit Bot 06c835
			"xmlGetGlobalState: out of memory\n");
Packit Bot 06c835
        return (NULL);
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
    memset(gs, 0, sizeof(xmlGlobalState));
Packit Bot 06c835
    xmlInitializeGlobalState(gs);
Packit Bot 06c835
    return (gs);
Packit Bot 06c835
}
Packit Bot 06c835
#endif /* LIBXML_THREAD_ENABLED */
Packit Bot 06c835
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
#if !defined(HAVE_COMPILER_TLS)
Packit Bot 06c835
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Packit Bot 06c835
typedef struct _xmlGlobalStateCleanupHelperParams {
Packit Bot 06c835
    HANDLE thread;
Packit Bot 06c835
    void *memory;
Packit Bot 06c835
} xmlGlobalStateCleanupHelperParams;
Packit Bot 06c835
Packit Bot 06c835
static void XMLCDECL
Packit Bot 06c835
xmlGlobalStateCleanupHelper(void *p)
Packit Bot 06c835
{
Packit Bot 06c835
    xmlGlobalStateCleanupHelperParams *params =
Packit Bot 06c835
        (xmlGlobalStateCleanupHelperParams *) p;
Packit Bot 06c835
    WaitForSingleObject(params->thread, INFINITE);
Packit Bot 06c835
    CloseHandle(params->thread);
Packit Bot 06c835
    xmlFreeGlobalState(params->memory);
Packit Bot 06c835
    free(params);
Packit Bot 06c835
    _endthread();
Packit Bot 06c835
}
Packit Bot 06c835
#else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */
Packit Bot 06c835
Packit Bot 06c835
typedef struct _xmlGlobalStateCleanupHelperParams {
Packit Bot 06c835
    void *memory;
Packit Bot 06c835
    struct _xmlGlobalStateCleanupHelperParams *prev;
Packit Bot 06c835
    struct _xmlGlobalStateCleanupHelperParams *next;
Packit Bot 06c835
} xmlGlobalStateCleanupHelperParams;
Packit Bot 06c835
Packit Bot 06c835
static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL;
Packit Bot 06c835
static CRITICAL_SECTION cleanup_helpers_cs;
Packit Bot 06c835
Packit Bot 06c835
#endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */
Packit Bot 06c835
#endif /* HAVE_COMPILER_TLS */
Packit Bot 06c835
#endif /* HAVE_WIN32_THREADS */
Packit Bot 06c835
Packit Bot 06c835
#if defined HAVE_BEOS_THREADS
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlGlobalStateCleanup:
Packit Bot 06c835
 * @data: unused parameter
Packit Bot 06c835
 *
Packit Bot 06c835
 * Used for Beos only
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlGlobalStateCleanup(void *data)
Packit Bot 06c835
{
Packit Bot 06c835
    void *globalval = tls_get(globalkey);
Packit Bot 06c835
Packit Bot 06c835
    if (globalval != NULL)
Packit Bot 06c835
        xmlFreeGlobalState(globalval);
Packit Bot 06c835
}
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlGetGlobalState:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlGetGlobalState() is called to retrieve the global state for a thread.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns the thread global state or NULL in case of error
Packit Bot 06c835
 */
Packit Bot 06c835
xmlGlobalStatePtr
Packit Bot 06c835
xmlGetGlobalState(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    xmlGlobalState *globalval;
Packit Bot 06c835
Packit Bot 06c835
    if (libxml_is_threaded == 0)
Packit Bot 06c835
        return (NULL);
Packit Bot 06c835
Packit Bot 06c835
    pthread_once(&once_control, xmlOnceInit);
Packit Bot 06c835
Packit Bot 06c835
    if ((globalval = (xmlGlobalState *)
Packit Bot 06c835
         pthread_getspecific(globalkey)) == NULL) {
Packit Bot 06c835
        xmlGlobalState *tsd = xmlNewGlobalState();
Packit Bot 06c835
	if (tsd == NULL)
Packit Bot 06c835
	    return(NULL);
Packit Bot 06c835
Packit Bot 06c835
        pthread_setspecific(globalkey, tsd);
Packit Bot 06c835
        return (tsd);
Packit Bot 06c835
    }
Packit Bot 06c835
    return (globalval);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
#if defined(HAVE_COMPILER_TLS)
Packit Bot 06c835
    if (!tlstate_inited) {
Packit Bot 06c835
        tlstate_inited = 1;
Packit Bot 06c835
        xmlInitializeGlobalState(&tlstate);
Packit Bot 06c835
    }
Packit Bot 06c835
    return &tlstate;
Packit Bot 06c835
#else /* HAVE_COMPILER_TLS */
Packit Bot 06c835
    xmlGlobalState *globalval;
Packit Bot 06c835
    xmlGlobalStateCleanupHelperParams *p;
Packit Bot 06c835
Packit Bot 06c835
    xmlOnceInit();
Packit Bot 06c835
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Packit Bot 06c835
    globalval = (xmlGlobalState *) TlsGetValue(globalkey);
Packit Bot 06c835
#else
Packit Bot 06c835
    p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey);
Packit Bot 06c835
    globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Packit Bot 06c835
#endif
Packit Bot 06c835
    if (globalval == NULL) {
Packit Bot 06c835
        xmlGlobalState *tsd = xmlNewGlobalState();
Packit Bot 06c835
Packit Bot 06c835
        if (tsd == NULL)
Packit Bot 06c835
	    return(NULL);
Packit Bot 06c835
        p = (xmlGlobalStateCleanupHelperParams *)
Packit Bot 06c835
            malloc(sizeof(xmlGlobalStateCleanupHelperParams));
Packit Bot 06c835
	if (p == NULL) {
Packit Bot 06c835
            xmlGenericError(xmlGenericErrorContext,
Packit Bot 06c835
                            "xmlGetGlobalState: out of memory\n");
Packit Bot 06c835
            xmlFreeGlobalState(tsd);
Packit Bot 06c835
	    return(NULL);
Packit Bot 06c835
	}
Packit Bot 06c835
        p->memory = tsd;
Packit Bot 06c835
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
Packit Bot 06c835
        DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
Packit Bot 06c835
                        GetCurrentProcess(), &p->thread, 0, TRUE,
Packit Bot 06c835
                        DUPLICATE_SAME_ACCESS);
Packit Bot 06c835
        TlsSetValue(globalkey, tsd);
Packit Bot 06c835
        _beginthread(xmlGlobalStateCleanupHelper, 0, p);
Packit Bot 06c835
#else
Packit Bot 06c835
        EnterCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
        if (cleanup_helpers_head != NULL) {
Packit Bot 06c835
            cleanup_helpers_head->prev = p;
Packit Bot 06c835
        }
Packit Bot 06c835
        p->next = cleanup_helpers_head;
Packit Bot 06c835
        p->prev = NULL;
Packit Bot 06c835
        cleanup_helpers_head = p;
Packit Bot 06c835
        TlsSetValue(globalkey, p);
Packit Bot 06c835
        LeaveCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
        return (tsd);
Packit Bot 06c835
    }
Packit Bot 06c835
    return (globalval);
Packit Bot 06c835
#endif /* HAVE_COMPILER_TLS */
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    xmlGlobalState *globalval;
Packit Bot 06c835
Packit Bot 06c835
    xmlOnceInit();
Packit Bot 06c835
Packit Bot 06c835
    if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
Packit Bot 06c835
        xmlGlobalState *tsd = xmlNewGlobalState();
Packit Bot 06c835
	if (tsd == NULL)
Packit Bot 06c835
	    return (NULL);
Packit Bot 06c835
Packit Bot 06c835
        tls_set(globalkey, tsd);
Packit Bot 06c835
        on_exit_thread(xmlGlobalStateCleanup, NULL);
Packit Bot 06c835
        return (tsd);
Packit Bot 06c835
    }
Packit Bot 06c835
    return (globalval);
Packit Bot 06c835
#else
Packit Bot 06c835
    return (NULL);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/************************************************************************
Packit Bot 06c835
 *									*
Packit Bot 06c835
 *			Library wide thread interfaces			*
Packit Bot 06c835
 *									*
Packit Bot 06c835
 ************************************************************************/
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlGetThreadId:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlGetThreadId() find the current thread ID number
Packit Bot 06c835
 * Note that this is likely to be broken on some platforms using pthreads
Packit Bot 06c835
 * as the specification doesn't mandate pthread_t to be an integer type
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns the current thread ID number
Packit Bot 06c835
 */
Packit Bot 06c835
int
Packit Bot 06c835
xmlGetThreadId(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    pthread_t id;
Packit Bot 06c835
    int ret;
Packit Bot 06c835
Packit Bot 06c835
    if (libxml_is_threaded == 0)
Packit Bot 06c835
        return (0);
Packit Bot 06c835
    id = pthread_self();
Packit Bot 06c835
    /* horrible but preserves compat, see warning above */
Packit Bot 06c835
    memcpy(&ret, &id, sizeof(ret));
Packit Bot 06c835
    return (ret);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    return GetCurrentThreadId();
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    return find_thread(NULL);
Packit Bot 06c835
#else
Packit Bot 06c835
    return ((int) 0);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlIsMainThread:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlIsMainThread() check whether the current thread is the main thread.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns 1 if the current thread is the main thread, 0 otherwise
Packit Bot 06c835
 */
Packit Bot 06c835
int
Packit Bot 06c835
xmlIsMainThread(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded == -1)
Packit Bot 06c835
        xmlInitThreads();
Packit Bot 06c835
    if (libxml_is_threaded == 0)
Packit Bot 06c835
        return (1);
Packit Bot 06c835
    pthread_once(&once_control, xmlOnceInit);
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    xmlOnceInit();
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    xmlOnceInit();
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
    xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    return (pthread_equal(mainthread,pthread_self()));
Packit Bot 06c835
#elif defined HAVE_WIN32_THREADS
Packit Bot 06c835
    return (mainthread == GetCurrentThreadId());
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    return (mainthread == find_thread(NULL));
Packit Bot 06c835
#else
Packit Bot 06c835
    return (1);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlLockLibrary:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
Packit Bot 06c835
 * library.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlLockLibrary(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
    xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
    xmlRMutexLock(xmlLibraryLock);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlUnlockLibrary:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2
Packit Bot 06c835
 * library.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlUnlockLibrary(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
    xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
    xmlRMutexUnlock(xmlLibraryLock);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlInitThreads:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlInitThreads() is used to to initialize all the thread related
Packit Bot 06c835
 * data of the libxml2 library.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlInitThreads(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if (libxml_is_threaded == -1) {
Packit Bot 06c835
        if ((pthread_once != NULL) &&
Packit Bot 06c835
            (pthread_getspecific != NULL) &&
Packit Bot 06c835
            (pthread_setspecific != NULL) &&
Packit Bot 06c835
            (pthread_key_create != NULL) &&
Packit Bot 06c835
            (pthread_key_delete != NULL) &&
Packit Bot 06c835
            (pthread_mutex_init != NULL) &&
Packit Bot 06c835
            (pthread_mutex_destroy != NULL) &&
Packit Bot 06c835
            (pthread_mutex_lock != NULL) &&
Packit Bot 06c835
            (pthread_mutex_unlock != NULL) &&
Packit Bot 06c835
            (pthread_cond_init != NULL) &&
Packit Bot 06c835
            (pthread_cond_destroy != NULL) &&
Packit Bot 06c835
            (pthread_cond_wait != NULL) &&
Packit Bot 06c835
            (pthread_equal != NULL) &&
Packit Bot 06c835
            (pthread_self != NULL) &&
Packit Bot 06c835
            (pthread_cond_signal != NULL)) {
Packit Bot 06c835
            libxml_is_threaded = 1;
Packit Bot 06c835
Packit Bot 06c835
/* fprintf(stderr, "Running multithreaded\n"); */
Packit Bot 06c835
        } else {
Packit Bot 06c835
Packit Bot 06c835
/* fprintf(stderr, "Running without multithread\n"); */
Packit Bot 06c835
            libxml_is_threaded = 0;
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Packit Bot 06c835
    InitializeCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlCleanupThreads:
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlCleanupThreads() is used to to cleanup all the thread related
Packit Bot 06c835
 * data of the libxml2 library once processing has ended.
Packit Bot 06c835
 *
Packit Bot 06c835
 * WARNING: if your application is multithreaded or has plugin support
Packit Bot 06c835
 *          calling this may crash the application if another thread or
Packit Bot 06c835
 *          a plugin is still using libxml2. It's sometimes very hard to
Packit Bot 06c835
 *          guess if libxml2 is in use in the application, some libraries
Packit Bot 06c835
 *          or plugins may use it without notice. In case of doubt abstain
Packit Bot 06c835
 *          from calling this function or do it just before calling exit()
Packit Bot 06c835
 *          to avoid leak reports from valgrind !
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
xmlCleanupThreads(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef DEBUG_THREADS
Packit Bot 06c835
    xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
Packit Bot 06c835
#endif
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    if ((libxml_is_threaded)  && (pthread_key_delete != NULL))
Packit Bot 06c835
        pthread_key_delete(globalkey);
Packit Bot 06c835
    once_control = once_control_init;
Packit Bot 06c835
#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Packit Bot 06c835
    if (globalkey != TLS_OUT_OF_INDEXES) {
Packit Bot 06c835
        xmlGlobalStateCleanupHelperParams *p;
Packit Bot 06c835
Packit Bot 06c835
        EnterCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
        p = cleanup_helpers_head;
Packit Bot 06c835
        while (p != NULL) {
Packit Bot 06c835
            xmlGlobalStateCleanupHelperParams *temp = p;
Packit Bot 06c835
Packit Bot 06c835
            p = p->next;
Packit Bot 06c835
            xmlFreeGlobalState(temp->memory);
Packit Bot 06c835
            free(temp);
Packit Bot 06c835
        }
Packit Bot 06c835
        cleanup_helpers_head = 0;
Packit Bot 06c835
        LeaveCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
        TlsFree(globalkey);
Packit Bot 06c835
        globalkey = TLS_OUT_OF_INDEXES;
Packit Bot 06c835
    }
Packit Bot 06c835
    DeleteCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
#ifdef LIBXML_THREAD_ENABLED
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * xmlOnceInit
Packit Bot 06c835
 *
Packit Bot 06c835
 * xmlOnceInit() is used to initialize the value of mainthread for use
Packit Bot 06c835
 * in other routines. This function should only be called using
Packit Bot 06c835
 * pthread_once() in association with the once_control variable to ensure
Packit Bot 06c835
 * that the function is only called once. See man pthread_once for more
Packit Bot 06c835
 * details.
Packit Bot 06c835
 */
Packit Bot 06c835
static void
Packit Bot 06c835
xmlOnceInit(void)
Packit Bot 06c835
{
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
    (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
Packit Bot 06c835
    mainthread = pthread_self();
Packit Bot 06c835
    __xmlInitializeDict();
Packit Bot 06c835
#elif defined(HAVE_WIN32_THREADS)
Packit Bot 06c835
    if (!run_once.done) {
Packit Bot 06c835
        if (InterlockedIncrement(&run_once.control) == 1) {
Packit Bot 06c835
#if !defined(HAVE_COMPILER_TLS)
Packit Bot 06c835
            globalkey = TlsAlloc();
Packit Bot 06c835
#endif
Packit Bot 06c835
            mainthread = GetCurrentThreadId();
Packit Bot 06c835
	    __xmlInitializeDict();
Packit Bot 06c835
            run_once.done = 1;
Packit Bot 06c835
        } else {
Packit Bot 06c835
            /* Another thread is working; give up our slice and
Packit Bot 06c835
             * wait until they're done. */
Packit Bot 06c835
            while (!run_once.done)
Packit Bot 06c835
                Sleep(0);
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
#elif defined HAVE_BEOS_THREADS
Packit Bot 06c835
    if (atomic_add(&run_once_init, 1) == 0) {
Packit Bot 06c835
        globalkey = tls_allocate();
Packit Bot 06c835
        tls_set(globalkey, NULL);
Packit Bot 06c835
        mainthread = find_thread(NULL);
Packit Bot 06c835
	__xmlInitializeDict();
Packit Bot 06c835
    } else
Packit Bot 06c835
        atomic_add(&run_once_init, -1);
Packit Bot 06c835
#endif
Packit Bot 06c835
}
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * DllMain:
Packit Bot 06c835
 * @hinstDLL: handle to DLL instance
Packit Bot 06c835
 * @fdwReason: Reason code for entry
Packit Bot 06c835
 * @lpvReserved: generic pointer (depends upon reason code)
Packit Bot 06c835
 *
Packit Bot 06c835
 * Entry point for Windows library. It is being used to free thread-specific
Packit Bot 06c835
 * storage.
Packit Bot 06c835
 *
Packit Bot 06c835
 * Returns TRUE always
Packit Bot 06c835
 */
Packit Bot 06c835
#ifdef HAVE_PTHREAD_H
Packit Bot 06c835
#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
Packit Bot 06c835
#if defined(LIBXML_STATIC_FOR_DLL)
Packit Bot 06c835
BOOL XMLCALL
Packit Bot 06c835
xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Packit Bot 06c835
#else
Packit Bot 06c835
BOOL WINAPI
Packit Bot 06c835
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Packit Bot 06c835
#endif
Packit Bot 06c835
{
Packit Bot 06c835
    switch (fdwReason) {
Packit Bot 06c835
        case DLL_THREAD_DETACH:
Packit Bot 06c835
            if (globalkey != TLS_OUT_OF_INDEXES) {
Packit Bot 06c835
                xmlGlobalState *globalval = NULL;
Packit Bot 06c835
                xmlGlobalStateCleanupHelperParams *p =
Packit Bot 06c835
                    (xmlGlobalStateCleanupHelperParams *)
Packit Bot 06c835
                    TlsGetValue(globalkey);
Packit Bot 06c835
                globalval = (xmlGlobalState *) (p ? p->memory : NULL);
Packit Bot 06c835
                if (globalval) {
Packit Bot 06c835
                    xmlFreeGlobalState(globalval);
Packit Bot 06c835
                    TlsSetValue(globalkey, NULL);
Packit Bot 06c835
                }
Packit Bot 06c835
                if (p) {
Packit Bot 06c835
                    EnterCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
                    if (p == cleanup_helpers_head)
Packit Bot 06c835
                        cleanup_helpers_head = p->next;
Packit Bot 06c835
                    else
Packit Bot 06c835
                        p->prev->next = p->next;
Packit Bot 06c835
                    if (p->next != NULL)
Packit Bot 06c835
                        p->next->prev = p->prev;
Packit Bot 06c835
                    LeaveCriticalSection(&cleanup_helpers_cs);
Packit Bot 06c835
                    free(p);
Packit Bot 06c835
                }
Packit Bot 06c835
            }
Packit Bot 06c835
            break;
Packit Bot 06c835
    }
Packit Bot 06c835
    return TRUE;
Packit Bot 06c835
}
Packit Bot 06c835
#endif
Packit Bot 06c835
#define bottom_threads
Packit Bot 06c835
#include "elfgcchack.h"