Blame threads.c

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