summaryrefslogtreecommitdiff
path: root/chromium/third_party/libxml/src/threads.c
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/third_party/libxml/src/threads.c')
-rw-r--r--chromium/third_party/libxml/src/threads.c260
1 files changed, 64 insertions, 196 deletions
diff --git a/chromium/third_party/libxml/src/threads.c b/chromium/third_party/libxml/src/threads.c
index 5e4db16c9ac..9fa477e53b6 100644
--- a/chromium/third_party/libxml/src/threads.c
+++ b/chromium/third_party/libxml/src/threads.c
@@ -16,37 +16,32 @@
#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_PTHREAD_H
-#include <pthread.h>
-#elif defined HAVE_WIN32_THREADS
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#ifndef HAVE_COMPILER_TLS
-#include <process.h>
-#endif
-#endif
-
-#ifdef HAVE_BEOS_THREADS
-#include <OS.h>
-#include <TLS.h>
+#ifdef LIBXML_THREAD_ENABLED
+ #ifdef HAVE_PTHREAD_H
+ #include <pthread.h>
+ #define HAVE_POSIX_THREADS
+ #elif defined(_WIN32)
+ #define WIN32_LEAN_AND_MEAN
+ #include <windows.h>
+ #ifndef HAVE_COMPILER_TLS
+ #include <process.h>
+ #endif
+ #define HAVE_WIN32_THREADS
+ #endif
#endif
#if defined(SOLARIS)
#include <note.h>
#endif
+#include "private/dict.h"
+#include "private/threads.h"
+
/* #define DEBUG_THREADS */
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
-#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 303) && \
- defined(__GLIBC__) && defined(__linux__)
+#if defined(__GNUC__) && defined(__linux__)
static int libxml_is_threaded = -1;
@@ -76,7 +71,7 @@ static int libxml_is_threaded = 1;
#endif /* __GNUC__, __GLIBC__, __linux__ */
-#endif /* HAVE_PTHREAD_H */
+#endif /* HAVE_POSIX_THREADS */
/*
* TODO: this module still uses malloc/free and not xmlMalloc/xmlFree
@@ -88,13 +83,10 @@ static int libxml_is_threaded = 1;
* xmlMutex are a simple mutual exception locks
*/
struct _xmlMutex {
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
pthread_mutex_t lock;
#elif defined HAVE_WIN32_THREADS
- HANDLE mutex;
-#elif defined HAVE_BEOS_THREADS
- sem_id sem;
- thread_id tid;
+ CRITICAL_SECTION cs;
#else
int empty;
#endif
@@ -104,7 +96,7 @@ struct _xmlMutex {
* xmlRMutex are reentrant mutual exception locks
*/
struct _xmlRMutex {
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
pthread_mutex_t lock;
unsigned int held;
unsigned int waiters;
@@ -112,11 +104,6 @@ struct _xmlRMutex {
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
@@ -128,7 +115,7 @@ struct _xmlRMutex {
* - globalkey used for per-thread data
*/
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
static pthread_key_t globalkey;
static pthread_t mainthread;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
@@ -147,14 +134,6 @@ static struct {
LONG control;
} run_once = { 0, 0};
static volatile LPCRITICAL_SECTION global_init_lock = NULL;
-
-/* endif HAVE_WIN32_THREADS */
-#elif defined HAVE_BEOS_THREADS
-int32 globalkey = 0;
-thread_id mainthread = 0;
-int32 run_once_init = 0;
-static int32 global_init_lock = -1;
-static vint32 global_init_count = 0;
#endif
static xmlRMutexPtr xmlLibraryLock = NULL;
@@ -178,17 +157,11 @@ xmlNewMutex(void)
if ((tok = malloc(sizeof(xmlMutex))) == NULL)
return (NULL);
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
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;
+ InitializeCriticalSection(&tok->cs);
#endif
return (tok);
}
@@ -206,13 +179,11 @@ xmlFreeMutex(xmlMutexPtr tok)
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
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);
+ DeleteCriticalSection(&tok->cs);
#endif
free(tok);
}
@@ -228,19 +199,11 @@ xmlMutexLock(xmlMutexPtr tok)
{
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
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 acquire semaphore\n");
-#endif
- }
- tok->tid = find_thread(NULL);
+ EnterCriticalSection(&tok->cs);
#endif
}
@@ -256,16 +219,11 @@ xmlMutexUnlock(xmlMutexPtr tok)
{
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
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);
- }
+ LeaveCriticalSection(&tok->cs);
#endif
}
@@ -286,7 +244,7 @@ xmlNewRMutex(void)
if ((tok = malloc(sizeof(xmlRMutex))) == NULL)
return (NULL);
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
if (libxml_is_threaded != 0) {
pthread_mutex_init(&tok->lock, NULL);
tok->held = 0;
@@ -295,13 +253,6 @@ xmlNewRMutex(void)
}
#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);
}
@@ -318,15 +269,13 @@ xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
{
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
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);
}
@@ -342,7 +291,7 @@ xmlRMutexLock(xmlRMutexPtr tok)
{
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
if (libxml_is_threaded == 0)
return;
@@ -364,15 +313,6 @@ xmlRMutexLock(xmlRMutexPtr tok)
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
}
@@ -387,7 +327,7 @@ xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
{
if (tok == NULL)
return;
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
if (libxml_is_threaded == 0)
return;
@@ -400,18 +340,7 @@ xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED)
}
pthread_mutex_unlock(&tok->lock);
#elif defined HAVE_WIN32_THREADS
- if (tok->count > 0) {
- 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;
- }
+ LeaveCriticalSection(&tok->cs);
#endif
}
@@ -425,7 +354,7 @@ void
__xmlGlobalInitMutexLock(void)
{
/* Make sure the global init lock is initialized and then lock it. */
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
/* The mutex is statically initialized, so we just lock it. */
#ifdef XML_PTHREAD_WEAK
if (pthread_mutex_lock == NULL)
@@ -465,41 +394,13 @@ __xmlGlobalInitMutexLock(void)
/* 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)
- delete_sem(sem);
-
- /* Acquire the chosen semaphore */
- if (acquire_sem(global_init_lock) != B_NO_ERROR) {
-#ifdef DEBUG_THREADS
- xmlGenericError(xmlGenericErrorContext,
- "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n");
-#endif
- }
#endif
}
void
__xmlGlobalInitMutexUnlock(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
#ifdef XML_PTHREAD_WEAK
if (pthread_mutex_unlock == NULL)
return;
@@ -509,8 +410,6 @@ __xmlGlobalInitMutexUnlock(void)
if (global_init_lock != NULL) {
LeaveCriticalSection(global_init_lock);
}
-#elif defined HAVE_BEOS_THREADS
- release_sem(global_init_lock);
#endif
}
@@ -523,7 +422,7 @@ __xmlGlobalInitMutexUnlock(void)
void
__xmlGlobalInitMutexDestroy(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
#elif defined HAVE_WIN32_THREADS
if (global_init_lock != NULL) {
DeleteCriticalSection(global_init_lock);
@@ -588,7 +487,7 @@ xmlNewGlobalState(void)
}
#endif /* LIBXML_THREAD_ENABLED */
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
#elif defined HAVE_WIN32_THREADS
#if !defined(HAVE_COMPILER_TLS)
#if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL)
@@ -623,24 +522,6 @@ static CRITICAL_SECTION cleanup_helpers_cs;
#endif /* HAVE_COMPILER_TLS */
#endif /* HAVE_WIN32_THREADS */
-#if defined HAVE_BEOS_THREADS
-
-/**
- * xmlGlobalStateCleanup:
- * @data: unused parameter
- *
- * Used for Beos only
- */
-void
-xmlGlobalStateCleanup(void *data)
-{
- void *globalval = tls_get(globalkey);
-
- if (globalval != NULL)
- xmlFreeGlobalState(globalval);
-}
-#endif
-
/**
* xmlGetGlobalState:
*
@@ -651,7 +532,7 @@ xmlGlobalStateCleanup(void *data)
xmlGlobalStatePtr
xmlGetGlobalState(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
xmlGlobalState *globalval;
if (libxml_is_threaded == 0)
@@ -723,21 +604,6 @@ xmlGetGlobalState(void)
}
return (globalval);
#endif /* HAVE_COMPILER_TLS */
-#elif defined HAVE_BEOS_THREADS
- xmlGlobalState *globalval;
-
- xmlOnceInit();
-
- if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) {
- xmlGlobalState *tsd = xmlNewGlobalState();
- if (tsd == NULL)
- return (NULL);
-
- tls_set(globalkey, tsd);
- on_exit_thread(xmlGlobalStateCleanup, NULL);
- return (tsd);
- }
- return (globalval);
#else
return (NULL);
#endif
@@ -761,7 +627,7 @@ xmlGetGlobalState(void)
int
xmlGetThreadId(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
pthread_t id;
int ret;
@@ -773,8 +639,6 @@ xmlGetThreadId(void)
return (ret);
#elif defined HAVE_WIN32_THREADS
return GetCurrentThreadId();
-#elif defined HAVE_BEOS_THREADS
- return find_thread(NULL);
#else
return ((int) 0);
#endif
@@ -790,7 +654,7 @@ xmlGetThreadId(void)
int
xmlIsMainThread(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
if (libxml_is_threaded == -1)
xmlInitThreads();
if (libxml_is_threaded == 0)
@@ -798,19 +662,15 @@ xmlIsMainThread(void)
pthread_once(&once_control, xmlOnceInit);
#elif defined HAVE_WIN32_THREADS
xmlOnceInit();
-#elif defined HAVE_BEOS_THREADS
- xmlOnceInit();
#endif
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n");
#endif
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
return (pthread_equal(mainthread,pthread_self()));
#elif defined HAVE_WIN32_THREADS
return (mainthread == GetCurrentThreadId());
-#elif defined HAVE_BEOS_THREADS
- return (mainthread == find_thread(NULL));
#else
return (1);
#endif
@@ -849,13 +709,16 @@ xmlUnlockLibrary(void)
/**
* xmlInitThreads:
*
+ * DEPRECATED: This function will be made private. Call xmlInitParser to
+ * initialize the library.
+ *
* xmlInitThreads() is used to to initialize all the thread related
* data of the libxml2 library.
*/
void
xmlInitThreads(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
#ifdef XML_PTHREAD_WEAK
if (libxml_is_threaded == -1) {
if ((pthread_once != NULL) &&
@@ -889,6 +752,11 @@ xmlInitThreads(void)
/**
* xmlCleanupThreads:
*
+ * DEPRECATED: This function will be made private. Call xmlCleanupParser
+ * to free global state but see the warnings there. xmlCleanupParser
+ * should be only called once at program exit. In most cases, you don't
+ * have call cleanup functions at all.
+ *
* xmlCleanupThreads() is used to to cleanup all the thread related
* data of the libxml2 library once processing has ended.
*
@@ -906,12 +774,14 @@ xmlCleanupThreads(void)
#ifdef DEBUG_THREADS
xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
#endif
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
if (libxml_is_threaded != 0)
pthread_key_delete(globalkey);
once_control = once_control_init;
-#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
+#elif defined(HAVE_WIN32_THREADS)
+#if !defined(HAVE_COMPILER_TLS)
if (globalkey != TLS_OUT_OF_INDEXES) {
+#if !defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)
xmlGlobalStateCleanupHelperParams *p;
EnterCriticalSection(&cleanup_helpers_cs);
@@ -925,11 +795,17 @@ xmlCleanupThreads(void)
}
cleanup_helpers_head = 0;
LeaveCriticalSection(&cleanup_helpers_cs);
+#endif
TlsFree(globalkey);
globalkey = TLS_OUT_OF_INDEXES;
}
+#if !defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)
DeleteCriticalSection(&cleanup_helpers_cs);
#endif
+#endif
+ run_once.done = 0;
+ run_once.control = 0;
+#endif
}
#ifdef LIBXML_THREAD_ENABLED
@@ -946,7 +822,7 @@ xmlCleanupThreads(void)
static void
xmlOnceInit(void)
{
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
(void) pthread_key_create(&globalkey, xmlFreeGlobalState);
mainthread = pthread_self();
__xmlInitializeDict();
@@ -969,14 +845,6 @@ xmlOnceInit(void)
Sleep(0);
}
}
-#elif defined HAVE_BEOS_THREADS
- if (atomic_add(&run_once_init, 1) == 0) {
- globalkey = tls_allocate();
- tls_set(globalkey, NULL);
- mainthread = find_thread(NULL);
- __xmlInitializeDict();
- } else
- atomic_add(&run_once_init, -1);
#endif
}
#endif
@@ -992,7 +860,7 @@ xmlOnceInit(void)
*
* Returns TRUE always
*/
-#ifdef HAVE_PTHREAD_H
+#ifdef HAVE_POSIX_THREADS
#elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
#if defined(LIBXML_STATIC_FOR_DLL)
int XMLCALL