📄 threads.c.svn-base
字号:
/** * threads.c: set of generic threading related routines * * See Copyright for the status of this software. * * Gary Pennington <Gary.Pennington@uk.sun.com> * daniel@veillard.com */#define IN_LIBXML#include "libxml.h"#include <string.h>#include <libxml/threads.h>#include <libxml/globals.h>#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_PTHREAD_H#include <pthread.h>#endif#ifdef HAVE_WIN32_THREADS#include <windows.h>#ifndef HAVE_COMPILER_TLS#include <process.h>#endif#endif#ifdef HAVE_BEOS_THREADS#include <OS.h>#include <TLS.h>#endif#if defined(SOLARIS)#include <note.h>#endif/* #define DEBUG_THREADS *//* * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree * to avoid some crazyness since xmlMalloc/xmlFree may actually * be hosted on allocated blocks needing them for the allocation ... *//* * xmlMutex are a simple mutual exception locks */struct _xmlMutex {#ifdef HAVE_PTHREAD_H pthread_mutex_t lock;#elif defined HAVE_WIN32_THREADS HANDLE mutex;#elif defined HAVE_BEOS_THREADS sem_id sem; thread_id tid;#else int empty;#endif};/* * xmlRMutex are reentrant mutual exception locks */struct _xmlRMutex {#ifdef HAVE_PTHREAD_H pthread_mutex_t lock; unsigned int held; unsigned int waiters; pthread_t tid; pthread_cond_t cv;#elif defined HAVE_WIN32_THREADS CRITICAL_SECTION cs; unsigned int count;#elif defined HAVE_BEOS_THREADS xmlMutexPtr lock; thread_id tid; int32 count;#else int empty;#endif};/* * This module still has some internal static data. * - xmlLibraryLock a global lock * - globalkey used for per-thread data */#ifdef HAVE_PTHREAD_Hstatic pthread_key_t globalkey;static pthread_t mainthread;static pthread_once_t once_control = PTHREAD_ONCE_INIT;#elif defined HAVE_WIN32_THREADS#if defined(HAVE_COMPILER_TLS)static __declspec(thread) xmlGlobalState tlstate;static __declspec(thread) int tlstate_inited = 0;#else /* HAVE_COMPILER_TLS */static DWORD globalkey = TLS_OUT_OF_INDEXES;#endif /* HAVE_COMPILER_TLS */static DWORD mainthread;static int run_once_init = 1;/* endif HAVE_WIN32_THREADS */#elif defined HAVE_BEOS_THREADSint32 globalkey = 0;thread_id mainthread = 0;int32 run_once_init = 0;#endifstatic xmlRMutexPtr xmlLibraryLock = NULL;#ifdef LIBXML_THREAD_ENABLEDstatic void xmlOnceInit(void);#endif/** * xmlNewMutex: * * xmlNewMutex() is used to allocate a libxml2 token struct for use in * synchronizing access to data. * * Returns a new simple mutex pointer or NULL in case of error */xmlMutexPtrxmlNewMutex(void){ xmlMutexPtr tok; if ((tok = malloc(sizeof(xmlMutex))) == NULL) return (NULL);#ifdef HAVE_PTHREAD_H pthread_mutex_init(&tok->lock, NULL);#elif defined HAVE_WIN32_THREADS tok->mutex = CreateMutex(NULL, FALSE, NULL);#elif defined HAVE_BEOS_THREADS if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) { free(tok); return NULL; } tok->tid = -1;#endif return (tok);}/** * xmlFreeMutex: * @tok: the simple mutex * * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token * struct. */voidxmlFreeMutex(xmlMutexPtr tok){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H pthread_mutex_destroy(&tok->lock);#elif defined HAVE_WIN32_THREADS CloseHandle(tok->mutex);#elif defined HAVE_BEOS_THREADS delete_sem(tok->sem);#endif free(tok);}/** * xmlMutexLock: * @tok: the simple mutex * * xmlMutexLock() is used to lock a libxml2 token. */voidxmlMutexLock(xmlMutexPtr tok){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&tok->lock);#elif defined HAVE_WIN32_THREADS WaitForSingleObject(tok->mutex, INFINITE);#elif defined HAVE_BEOS_THREADS if (acquire_sem(tok->sem) != B_NO_ERROR) {#ifdef DEBUG_THREADS xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n"); exit();#endif } tok->tid = find_thread(NULL);#endif}/** * xmlMutexUnlock: * @tok: the simple mutex * * xmlMutexUnlock() is used to unlock a libxml2 token. */voidxmlMutexUnlock(xmlMutexPtr tok){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H pthread_mutex_unlock(&tok->lock);#elif defined HAVE_WIN32_THREADS ReleaseMutex(tok->mutex);#elif defined HAVE_BEOS_THREADS if (tok->tid == find_thread(NULL)) { tok->tid = -1; release_sem(tok->sem); }#endif}/** * xmlNewRMutex: * * xmlRNewMutex() is used to allocate a reentrant mutex for use in * synchronizing access to data. token_r is a re-entrant lock and thus useful * for synchronizing access to data structures that may be manipulated in a * recursive fashion. * * Returns the new reentrant mutex pointer or NULL in case of error */xmlRMutexPtrxmlNewRMutex(void){ xmlRMutexPtr tok; if ((tok = malloc(sizeof(xmlRMutex))) == NULL) return (NULL);#ifdef HAVE_PTHREAD_H pthread_mutex_init(&tok->lock, NULL); tok->held = 0; tok->waiters = 0; pthread_cond_init(&tok->cv, NULL);#elif defined HAVE_WIN32_THREADS InitializeCriticalSection(&tok->cs); tok->count = 0;#elif defined HAVE_BEOS_THREADS if ((tok->lock = xmlNewMutex()) == NULL) { free(tok); return NULL; } tok->count = 0;#endif return (tok);}/** * xmlFreeRMutex: * @tok: the reentrant mutex * * xmlRFreeMutex() is used to reclaim resources associated with a * reentrant mutex. */voidxmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED){#ifdef HAVE_PTHREAD_H pthread_mutex_destroy(&tok->lock);#elif defined HAVE_WIN32_THREADS DeleteCriticalSection(&tok->cs);#elif defined HAVE_BEOS_THREADS xmlFreeMutex(tok->lock);#endif free(tok);}/** * xmlRMutexLock: * @tok: the reentrant mutex * * xmlRMutexLock() is used to lock a libxml2 token_r. */voidxmlRMutexLock(xmlRMutexPtr tok){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&tok->lock); if (tok->held) { if (pthread_equal(tok->tid, pthread_self())) { tok->held++; pthread_mutex_unlock(&tok->lock); return; } else { tok->waiters++; while (tok->held) pthread_cond_wait(&tok->cv, &tok->lock); tok->waiters--; } } tok->tid = pthread_self(); tok->held = 1; pthread_mutex_unlock(&tok->lock);#elif defined HAVE_WIN32_THREADS EnterCriticalSection(&tok->cs); ++tok->count;#elif defined HAVE_BEOS_THREADS if (tok->lock->tid == find_thread(NULL)) { tok->count++; return; } else { xmlMutexLock(tok->lock); tok->count = 1; }#endif}/** * xmlRMutexUnlock: * @tok: the reentrant mutex * * xmlRMutexUnlock() is used to unlock a libxml2 token_r. */voidxmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H pthread_mutex_lock(&tok->lock); tok->held--; if (tok->held == 0) { if (tok->waiters) pthread_cond_signal(&tok->cv); tok->tid = 0; } pthread_mutex_unlock(&tok->lock);#elif defined HAVE_WIN32_THREADS if (!--tok->count) LeaveCriticalSection(&tok->cs);#elif defined HAVE_BEOS_THREADS if (tok->lock->tid == find_thread(NULL)) { tok->count--; if (tok->count == 0) { xmlMutexUnlock(tok->lock); } return; }#endif}/************************************************************************ * * * Per thread global state handling * * * ************************************************************************/#ifdef LIBXML_THREAD_ENABLED/** * xmlFreeGlobalState: * @state: a thread global state * * xmlFreeGlobalState() is called when a thread terminates with a non-NULL * global state. It is is used here to reclaim memory resources. */static voidxmlFreeGlobalState(void *state){ free(state);}/** * xmlNewGlobalState: * * xmlNewGlobalState() allocates a global state. This structure is used to * hold all data for use by a thread when supporting backwards compatibility * of libxml2 to pre-thread-safe behaviour.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -