globals: Move xmlIsMainThread to globals.c
xmlIsMainThread is mainly needed for global variables.
diff --git a/globals.c b/globals.c
index 4293eaf..8c3778f 100644
--- a/globals.c
+++ b/globals.c
@@ -29,11 +29,13 @@
* Helpful Macro
*/
#ifdef LIBXML_THREAD_ENABLED
-#define IS_MAIN_THREAD (xmlIsMainThread())
+#define IS_MAIN_THREAD (xmlIsMainThreadInternal())
#else
#define IS_MAIN_THREAD 1
#endif
+static int parserInitialized;
+
/*
* Mutex to protect "ForNewThreads" variables
*/
@@ -78,12 +80,14 @@
#endif
static pthread_key_t globalkey;
+static pthread_t mainthread;
#elif defined HAVE_WIN32_THREADS
#ifndef XML_THREAD_LOCAL
static DWORD globalkey = TLS_OUT_OF_INDEXES;
#endif
+static DWORD mainthread;
#endif /* HAVE_WIN32_THREADS */
@@ -522,10 +526,12 @@
return;
#endif /* XML_PTHREAD_WEAK */
pthread_key_create(&globalkey, xmlFreeGlobalState);
+ mainthread = pthread_self();
#elif defined(HAVE_WIN32_THREADS)
#ifndef XML_THREAD_LOCAL
globalkey = TlsAlloc();
#endif
+ mainthread = GetCurrentThreadId();
#endif
}
@@ -588,6 +594,40 @@
return(NULL);
}
+static int
+xmlIsMainThreadInternal(void) {
+ if (parserInitialized == 0) {
+ xmlInitParser();
+ parserInitialized = 1;
+ }
+
+#ifdef HAVE_POSIX_THREADS
+#ifdef XML_PTHREAD_WEAK
+ if (libxml_is_threaded == 0)
+ return (1);
+#endif
+ return (pthread_equal(mainthread, pthread_self()));
+#elif defined HAVE_WIN32_THREADS
+ return (mainthread == GetCurrentThreadId());
+#else
+ return (1);
+#endif
+}
+
+/**
+ * xmlIsMainThread:
+ *
+ * DEPRECATED: Internal function, do not use.
+ *
+ * Check whether the current thread is the main thread.
+ *
+ * Returns 1 if the current thread is the main thread, 0 otherwise
+ */
+int
+xmlIsMainThread(void) {
+ return(xmlIsMainThreadInternal());
+}
+
#ifdef LIBXML_THREAD_ENABLED
/**
* xmlFreeGlobalState:
@@ -781,7 +821,7 @@
int
xmlCheckThreadLocalStorage(void) {
#if defined(LIBXML_THREAD_ENABLED) && !defined(XML_THREAD_LOCAL_STORAGE)
- if ((!xmlIsMainThread()) && (xmlGetThreadLocalStorage() == NULL))
+ if ((!xmlIsMainThreadInternal()) && (xmlGetThreadLocalStorage() == NULL))
return(-1);
#endif
return(0);
diff --git a/threads.c b/threads.c
index c377c09..7100f0d 100644
--- a/threads.c
+++ b/threads.c
@@ -118,12 +118,6 @@
#endif
};
-#ifdef HAVE_POSIX_THREADS
-static pthread_t mainthread;
-#elif defined HAVE_WIN32_THREADS
-static DWORD mainthread;
-#endif
-
static xmlRMutexPtr xmlLibraryLock = NULL;
/**
@@ -397,31 +391,6 @@
}
/**
- * xmlIsMainThread:
- *
- * DEPRECATED: Internal function, do not use.
- *
- * xmlIsMainThread() check whether the current thread is the main thread.
- *
- * Returns 1 if the current thread is the main thread, 0 otherwise
- */
-int
-xmlIsMainThread(void)
-{
- xmlInitParser();
-
-#ifdef HAVE_POSIX_THREADS
- if (XML_IS_THREADED() == 0)
- return (1);
- return (pthread_equal(mainthread,pthread_self()));
-#elif defined HAVE_WIN32_THREADS
- return (mainthread == GetCurrentThreadId());
-#else
- return (1);
-#endif
-}
-
-/**
* xmlLockLibrary:
*
* xmlLockLibrary() is used to take out a re-entrant lock on the libxml2
@@ -457,22 +426,6 @@
}
/**
- * xmlInitThreadsInternal:
- *
- * Used to to initialize all the thread related data.
- */
-static void
-xmlInitThreadsInternal(void)
-{
-#ifdef HAVE_POSIX_THREADS
- if (XML_IS_NEVER_THREADED() == 0)
- mainthread = pthread_self();
-#elif defined(HAVE_WIN32_THREADS)
- mainthread = GetCurrentThreadId();
-#endif
-}
-
-/**
* xmlCleanupThreads:
*
* DEPRECATED: This function is a no-op. Call xmlCleanupParser
@@ -629,7 +582,6 @@
atexit(xmlCleanupParser);
#endif
- xmlInitThreadsInternal(); /* Must come first */
xmlInitMemoryInternal(); /* Should come second */
xmlInitGlobalsInternal();
xmlInitDictInternal();