📄 threads.c
字号:
/** * 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 */#ifdef HAVE_PTHREAD_Hstatic int libxml_is_threaded = -1;#ifdef __GNUC__#ifdef linux#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3)extern int pthread_once (pthread_once_t *__once_control, void (*__init_routine) (void)) __attribute((weak));extern void *pthread_getspecific (pthread_key_t __key) __attribute((weak));extern int pthread_setspecific (pthread_key_t __key, __const void *__pointer) __attribute((weak));extern int pthread_key_create (pthread_key_t *__key, void (*__destr_function) (void *)) __attribute((weak));extern int pthread_mutex_init () __attribute((weak));extern int pthread_mutex_destroy () __attribute((weak));extern int pthread_mutex_lock () __attribute((weak));extern int pthread_mutex_unlock () __attribute((weak));extern int pthread_cond_init () __attribute((weak));extern int pthread_equal () __attribute((weak));extern pthread_t pthread_self () __attribute((weak));extern int pthread_key_create () __attribute((weak));extern int pthread_cond_signal () __attribute((weak));#endif#endif /* linux */#endif /* __GNUC__ */#endif /* HAVE_PTHREAD_H *//* * 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;static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;#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 struct{ DWORD done; DWORD control;} run_once = { 0, 0 };static volatile LPCRITICAL_SECTION global_init_lock = NULL;/* endif HAVE_WIN32_THREADS */#elif defined HAVE_BEOS_THREADSint32 globalkey = 0;thread_id mainthread = 0;int32 run_once_init = 0;static int32 global_init_lock = -1;static vint32 global_init_count = 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 if (libxml_is_threaded != 0) 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 if (libxml_is_threaded != 0) 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 if (libxml_is_threaded != 0) 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 if (libxml_is_threaded != 0) 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 if (libxml_is_threaded != 0) { 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){ if (tok == NULL) return;#ifdef HAVE_PTHREAD_H if (libxml_is_threaded != 0) { pthread_mutex_destroy(&tok->lock); pthread_cond_destroy(&tok->cv); }#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 if (libxml_is_threaded == 0) return; 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 if (libxml_is_threaded == 0) return; 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}/** * xmlGlobalInitMutexLock * * Makes sure that the global initialization mutex is initialized and * locks it. */void__xmlGlobalInitMutexLock(void){ /* Make sure the global init lock is initialized and then lock it. */#ifdef HAVE_PTHREAD_H int err; /* The mutex is statically initialized, so we just lock it. */ pthread_mutex_lock(&global_init_lock);#elif defined HAVE_WIN32_THREADS LPCRITICAL_SECTION cs; /* Create a new critical section */ if (global_init_lock == NULL) { cs = malloc(sizeof(CRITICAL_SECTION)); InitializeCriticalSection(cs); /* Swap it into the global_init_lock */#ifdef InterlockedCompareExchangePointer InterlockedCompareExchangePointer(&global_init_lock, cs, NULL);#else /* Use older void* version */ InterlockedCompareExchange((void **)&global_init_lock, (void *)cs, NULL);#endif /* InterlockedCompareExchangePointer */ /* If another thread successfully recorded its critical * section in the global_init_lock then discard the one * allocated by this thread. */ if (global_init_lock != cs) { free(cs); } } /* Lock the chosen critical section */ EnterCriticalSection(global_init_lock);#elif defined HAVE_BEOS_THREADS int32 sem; /* Allocate a new semaphore */ sem = create_sem(1, "xmlGlobalinitMutex"); while (global_init_lock == -1) { if (atomic_add(&global_init_count, 1) == 0) { global_init_lock = sem; } else { snooze(1); atomic_add(&global_init_count, -1); } } /* If another thread successfully recorded its critical * section in the global_init_lock then discard the one * allocated by this thread. */ if (global_init_lock != sem)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -