Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 1 | /** |
Daniel Veillard | dee2348 | 2008-04-11 12:58:43 +0000 | [diff] [blame] | 2 | * threads.c: set of generic threading related routines |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Gary Pennington <Gary.Pennington@uk.sun.com> |
| 7 | * daniel@veillard.com |
| 8 | */ |
| 9 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 10 | #define IN_LIBXML |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 11 | #include "libxml.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <libxml/threads.h> |
| 16 | #include <libxml/globals.h> |
| 17 | |
| 18 | #ifdef HAVE_SYS_TYPES_H |
| 19 | #include <sys/types.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_UNISTD_H |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_STDLIB_H |
| 25 | #include <stdlib.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_PTHREAD_H |
| 28 | #include <pthread.h> |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 29 | #elif defined HAVE_WIN32_THREADS |
Nick Wellnhofer | e389054 | 2017-10-09 00:20:01 +0200 | [diff] [blame] | 30 | #define WIN32_LEAN_AND_MEAN |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 31 | #include <windows.h> |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 32 | #ifndef HAVE_COMPILER_TLS |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 33 | #include <process.h> |
| 34 | #endif |
| 35 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 37 | #ifdef HAVE_BEOS_THREADS |
| 38 | #include <OS.h> |
| 39 | #include <TLS.h> |
| 40 | #endif |
| 41 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 42 | #if defined(SOLARIS) |
| 43 | #include <note.h> |
| 44 | #endif |
| 45 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 46 | /* #define DEBUG_THREADS */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 47 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 48 | #ifdef HAVE_PTHREAD_H |
| 49 | |
| 50 | static int libxml_is_threaded = -1; |
Michael Heimpold | fff8a6b | 2014-12-22 11:12:12 +0800 | [diff] [blame] | 51 | #if defined(__GNUC__) && defined(__GLIBC__) |
Nick Wellnhofer | 2cdaaab | 2017-09-14 21:30:51 +0200 | [diff] [blame] | 52 | #ifdef __linux__ |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 53 | #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3) |
Nick Wellnhofer | 1f09aea | 2017-06-17 15:05:34 +0200 | [diff] [blame] | 54 | #pragma weak pthread_once |
| 55 | #pragma weak pthread_getspecific |
| 56 | #pragma weak pthread_setspecific |
| 57 | #pragma weak pthread_key_create |
| 58 | #pragma weak pthread_key_delete |
| 59 | #pragma weak pthread_mutex_init |
| 60 | #pragma weak pthread_mutex_destroy |
| 61 | #pragma weak pthread_mutex_lock |
| 62 | #pragma weak pthread_mutex_unlock |
| 63 | #pragma weak pthread_cond_init |
| 64 | #pragma weak pthread_cond_destroy |
| 65 | #pragma weak pthread_cond_wait |
| 66 | #pragma weak pthread_equal |
| 67 | #pragma weak pthread_self |
| 68 | #pragma weak pthread_key_create |
| 69 | #pragma weak pthread_key_delete |
| 70 | #pragma weak pthread_cond_signal |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 71 | #endif |
Nick Wellnhofer | 2cdaaab | 2017-09-14 21:30:51 +0200 | [diff] [blame] | 72 | #endif /* __linux__ */ |
Michael Heimpold | fff8a6b | 2014-12-22 11:12:12 +0800 | [diff] [blame] | 73 | #endif /* defined(__GNUC__) && defined(__GLIBC__) */ |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 74 | #endif /* HAVE_PTHREAD_H */ |
| 75 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 76 | /* |
| 77 | * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree |
| 78 | * to avoid some crazyness since xmlMalloc/xmlFree may actually |
| 79 | * be hosted on allocated blocks needing them for the allocation ... |
| 80 | */ |
| 81 | |
| 82 | /* |
| 83 | * xmlMutex are a simple mutual exception locks |
| 84 | */ |
| 85 | struct _xmlMutex { |
| 86 | #ifdef HAVE_PTHREAD_H |
| 87 | pthread_mutex_t lock; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 88 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 89 | HANDLE mutex; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 90 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 91 | sem_id sem; |
| 92 | thread_id tid; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 93 | #else |
| 94 | int empty; |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * xmlRMutex are reentrant mutual exception locks |
| 100 | */ |
| 101 | struct _xmlRMutex { |
| 102 | #ifdef HAVE_PTHREAD_H |
| 103 | pthread_mutex_t lock; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 104 | unsigned int held; |
| 105 | unsigned int waiters; |
| 106 | pthread_t tid; |
| 107 | pthread_cond_t cv; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 108 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 109 | CRITICAL_SECTION cs; |
| 110 | unsigned int count; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 111 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 112 | xmlMutexPtr lock; |
| 113 | thread_id tid; |
| 114 | int32 count; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 115 | #else |
| 116 | int empty; |
| 117 | #endif |
| 118 | }; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 119 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 120 | /* |
| 121 | * This module still has some internal static data. |
| 122 | * - xmlLibraryLock a global lock |
| 123 | * - globalkey used for per-thread data |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 124 | */ |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 125 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 126 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 127 | static pthread_key_t globalkey; |
| 128 | static pthread_t mainthread; |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 129 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
Friedrich Haubensak | 3f6cfbd | 2012-09-12 17:34:53 +0200 | [diff] [blame] | 130 | static pthread_once_t once_control_init = PTHREAD_ONCE_INIT; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 131 | static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 132 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 133 | #if defined(HAVE_COMPILER_TLS) |
| 134 | static __declspec(thread) xmlGlobalState tlstate; |
| 135 | static __declspec(thread) int tlstate_inited = 0; |
| 136 | #else /* HAVE_COMPILER_TLS */ |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 137 | static DWORD globalkey = TLS_OUT_OF_INDEXES; |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 138 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 139 | static DWORD mainthread; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 140 | static struct { |
Daniel Veillard | 36616dd | 2005-02-25 07:31:49 +0000 | [diff] [blame] | 141 | DWORD done; |
| 142 | DWORD control; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 143 | } run_once = { 0, 0}; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 144 | static volatile LPCRITICAL_SECTION global_init_lock = NULL; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 145 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 146 | /* endif HAVE_WIN32_THREADS */ |
| 147 | #elif defined HAVE_BEOS_THREADS |
| 148 | int32 globalkey = 0; |
| 149 | thread_id mainthread = 0; |
| 150 | int32 run_once_init = 0; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 151 | static int32 global_init_lock = -1; |
| 152 | static vint32 global_init_count = 0; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 153 | #endif |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 154 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 155 | static xmlRMutexPtr xmlLibraryLock = NULL; |
| 156 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 157 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 158 | static void xmlOnceInit(void); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 159 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 162 | * xmlNewMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 163 | * |
| 164 | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
| 165 | * synchronizing access to data. |
| 166 | * |
| 167 | * Returns a new simple mutex pointer or NULL in case of error |
| 168 | */ |
| 169 | xmlMutexPtr |
| 170 | xmlNewMutex(void) |
| 171 | { |
| 172 | xmlMutexPtr tok; |
| 173 | |
| 174 | if ((tok = malloc(sizeof(xmlMutex))) == NULL) |
| 175 | return (NULL); |
| 176 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 177 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 178 | pthread_mutex_init(&tok->lock, NULL); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 179 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 180 | tok->mutex = CreateMutex(NULL, FALSE, NULL); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 181 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 182 | if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) { |
| 183 | free(tok); |
| 184 | return NULL; |
| 185 | } |
| 186 | tok->tid = -1; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 187 | #endif |
| 188 | return (tok); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * xmlFreeMutex: |
| 193 | * @tok: the simple mutex |
| 194 | * |
| 195 | * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token |
| 196 | * struct. |
| 197 | */ |
| 198 | void |
| 199 | xmlFreeMutex(xmlMutexPtr tok) |
| 200 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 201 | if (tok == NULL) |
| 202 | return; |
Daniel Veillard | df101d8 | 2003-07-08 14:03:36 +0000 | [diff] [blame] | 203 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 204 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 205 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 206 | pthread_mutex_destroy(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 207 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 208 | CloseHandle(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 209 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 210 | delete_sem(tok->sem); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 211 | #endif |
| 212 | free(tok); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * xmlMutexLock: |
| 217 | * @tok: the simple mutex |
| 218 | * |
| 219 | * xmlMutexLock() is used to lock a libxml2 token. |
| 220 | */ |
| 221 | void |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 222 | xmlMutexLock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 223 | { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 224 | if (tok == NULL) |
| 225 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 226 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 227 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 228 | pthread_mutex_lock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 229 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 230 | WaitForSingleObject(tok->mutex, INFINITE); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 231 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 232 | if (acquire_sem(tok->sem) != B_NO_ERROR) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 233 | #ifdef DEBUG_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 234 | xmlGenericError(xmlGenericErrorContext, |
| 235 | "xmlMutexLock():BeOS:Couldn't aquire semaphore\n"); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 236 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 237 | } |
| 238 | tok->tid = find_thread(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 239 | #endif |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * xmlMutexUnlock: |
| 245 | * @tok: the simple mutex |
| 246 | * |
| 247 | * xmlMutexUnlock() is used to unlock a libxml2 token. |
| 248 | */ |
| 249 | void |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 250 | xmlMutexUnlock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 251 | { |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 252 | if (tok == NULL) |
| 253 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 254 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 255 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 256 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 257 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 258 | ReleaseMutex(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 259 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 260 | if (tok->tid == find_thread(NULL)) { |
| 261 | tok->tid = -1; |
| 262 | release_sem(tok->sem); |
| 263 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
| 267 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 268 | * xmlNewRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 269 | * |
| 270 | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
| 271 | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
| 272 | * for synchronizing access to data structures that may be manipulated in a |
| 273 | * recursive fashion. |
| 274 | * |
| 275 | * Returns the new reentrant mutex pointer or NULL in case of error |
| 276 | */ |
| 277 | xmlRMutexPtr |
| 278 | xmlNewRMutex(void) |
| 279 | { |
| 280 | xmlRMutexPtr tok; |
| 281 | |
| 282 | if ((tok = malloc(sizeof(xmlRMutex))) == NULL) |
| 283 | return (NULL); |
| 284 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 285 | if (libxml_is_threaded != 0) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 286 | pthread_mutex_init(&tok->lock, NULL); |
| 287 | tok->held = 0; |
| 288 | tok->waiters = 0; |
| 289 | pthread_cond_init(&tok->cv, NULL); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 290 | } |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 291 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 292 | InitializeCriticalSection(&tok->cs); |
| 293 | tok->count = 0; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 294 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 295 | if ((tok->lock = xmlNewMutex()) == NULL) { |
| 296 | free(tok); |
| 297 | return NULL; |
| 298 | } |
| 299 | tok->count = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 300 | #endif |
| 301 | return (tok); |
| 302 | } |
| 303 | |
| 304 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 305 | * xmlFreeRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 306 | * @tok: the reentrant mutex |
| 307 | * |
| 308 | * xmlRFreeMutex() is used to reclaim resources associated with a |
| 309 | * reentrant mutex. |
| 310 | */ |
| 311 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 312 | xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 313 | { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 314 | if (tok == NULL) |
| 315 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 316 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | a8b5413 | 2006-06-29 11:50:18 +0000 | [diff] [blame] | 317 | if (libxml_is_threaded != 0) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 318 | pthread_mutex_destroy(&tok->lock); |
| 319 | pthread_cond_destroy(&tok->cv); |
Daniel Veillard | a8b5413 | 2006-06-29 11:50:18 +0000 | [diff] [blame] | 320 | } |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 321 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 322 | DeleteCriticalSection(&tok->cs); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 323 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 324 | xmlFreeMutex(tok->lock); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 325 | #endif |
| 326 | free(tok); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * xmlRMutexLock: |
| 331 | * @tok: the reentrant mutex |
| 332 | * |
| 333 | * xmlRMutexLock() is used to lock a libxml2 token_r. |
| 334 | */ |
| 335 | void |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 336 | xmlRMutexLock(xmlRMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 337 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 338 | if (tok == NULL) |
| 339 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 340 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 341 | if (libxml_is_threaded == 0) |
| 342 | return; |
| 343 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 344 | pthread_mutex_lock(&tok->lock); |
| 345 | if (tok->held) { |
| 346 | if (pthread_equal(tok->tid, pthread_self())) { |
| 347 | tok->held++; |
| 348 | pthread_mutex_unlock(&tok->lock); |
| 349 | return; |
| 350 | } else { |
| 351 | tok->waiters++; |
| 352 | while (tok->held) |
| 353 | pthread_cond_wait(&tok->cv, &tok->lock); |
| 354 | tok->waiters--; |
| 355 | } |
| 356 | } |
| 357 | tok->tid = pthread_self(); |
| 358 | tok->held = 1; |
| 359 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 360 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 361 | EnterCriticalSection(&tok->cs); |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 362 | tok->count++; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 363 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 364 | if (tok->lock->tid == find_thread(NULL)) { |
| 365 | tok->count++; |
| 366 | return; |
| 367 | } else { |
| 368 | xmlMutexLock(tok->lock); |
| 369 | tok->count = 1; |
| 370 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 371 | #endif |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * xmlRMutexUnlock: |
| 376 | * @tok: the reentrant mutex |
| 377 | * |
| 378 | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
| 379 | */ |
| 380 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 381 | xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 382 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 383 | if (tok == NULL) |
| 384 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 385 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 386 | if (libxml_is_threaded == 0) |
| 387 | return; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 388 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 389 | pthread_mutex_lock(&tok->lock); |
| 390 | tok->held--; |
| 391 | if (tok->held == 0) { |
| 392 | if (tok->waiters) |
| 393 | pthread_cond_signal(&tok->cv); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 394 | memset(&tok->tid, 0, sizeof(tok->tid)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 395 | } |
| 396 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 397 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 398 | if (tok->count > 0) { |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 399 | tok->count--; |
Steve Nairn | 620a706 | 2015-03-03 19:40:06 +0800 | [diff] [blame] | 400 | LeaveCriticalSection(&tok->cs); |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 401 | } |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 402 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 403 | if (tok->lock->tid == find_thread(NULL)) { |
| 404 | tok->count--; |
| 405 | if (tok->count == 0) { |
| 406 | xmlMutexUnlock(tok->lock); |
| 407 | } |
| 408 | return; |
| 409 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 410 | #endif |
| 411 | } |
| 412 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 413 | /** |
| 414 | * xmlGlobalInitMutexLock |
| 415 | * |
| 416 | * Makes sure that the global initialization mutex is initialized and |
| 417 | * locks it. |
| 418 | */ |
| 419 | void |
| 420 | __xmlGlobalInitMutexLock(void) |
| 421 | { |
| 422 | /* Make sure the global init lock is initialized and then lock it. */ |
| 423 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 424 | /* The mutex is statically initialized, so we just lock it. */ |
Daniel Richard G | 5706b6d | 2012-08-06 11:32:54 +0800 | [diff] [blame] | 425 | if (pthread_mutex_lock != NULL) |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 426 | pthread_mutex_lock(&global_init_lock); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 427 | #elif defined HAVE_WIN32_THREADS |
| 428 | LPCRITICAL_SECTION cs; |
| 429 | |
| 430 | /* Create a new critical section */ |
| 431 | if (global_init_lock == NULL) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 432 | cs = malloc(sizeof(CRITICAL_SECTION)); |
| 433 | if (cs == NULL) { |
| 434 | xmlGenericError(xmlGenericErrorContext, |
| 435 | "xmlGlobalInitMutexLock: out of memory\n"); |
| 436 | return; |
| 437 | } |
| 438 | InitializeCriticalSection(cs); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 439 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 440 | /* Swap it into the global_init_lock */ |
Rob Richards | e967f0b | 2007-06-08 19:36:04 +0000 | [diff] [blame] | 441 | #ifdef InterlockedCompareExchangePointer |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 442 | InterlockedCompareExchangePointer(&global_init_lock, cs, NULL); |
| 443 | #else /* Use older void* version */ |
| 444 | InterlockedCompareExchange((void **) &global_init_lock, |
| 445 | (void *) cs, NULL); |
Rob Richards | e967f0b | 2007-06-08 19:36:04 +0000 | [diff] [blame] | 446 | #endif /* InterlockedCompareExchangePointer */ |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 447 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 448 | /* If another thread successfully recorded its critical |
| 449 | * section in the global_init_lock then discard the one |
| 450 | * allocated by this thread. */ |
| 451 | if (global_init_lock != cs) { |
| 452 | DeleteCriticalSection(cs); |
| 453 | free(cs); |
| 454 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Lock the chosen critical section */ |
| 458 | EnterCriticalSection(global_init_lock); |
| 459 | #elif defined HAVE_BEOS_THREADS |
| 460 | int32 sem; |
| 461 | |
| 462 | /* Allocate a new semaphore */ |
| 463 | sem = create_sem(1, "xmlGlobalinitMutex"); |
| 464 | |
| 465 | while (global_init_lock == -1) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 466 | if (atomic_add(&global_init_count, 1) == 0) { |
| 467 | global_init_lock = sem; |
| 468 | } else { |
| 469 | snooze(1); |
| 470 | atomic_add(&global_init_count, -1); |
| 471 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* If another thread successfully recorded its critical |
| 475 | * section in the global_init_lock then discard the one |
| 476 | * allocated by this thread. */ |
| 477 | if (global_init_lock != sem) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 478 | delete_sem(sem); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 479 | |
| 480 | /* Acquire the chosen semaphore */ |
| 481 | if (acquire_sem(global_init_lock) != B_NO_ERROR) { |
| 482 | #ifdef DEBUG_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 483 | xmlGenericError(xmlGenericErrorContext, |
| 484 | "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n"); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 485 | #endif |
| 486 | } |
| 487 | #endif |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | __xmlGlobalInitMutexUnlock(void) |
| 492 | { |
| 493 | #ifdef HAVE_PTHREAD_H |
Daniel Richard G | 5706b6d | 2012-08-06 11:32:54 +0800 | [diff] [blame] | 494 | if (pthread_mutex_unlock != NULL) |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 495 | pthread_mutex_unlock(&global_init_lock); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 496 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 497 | if (global_init_lock != NULL) { |
| 498 | LeaveCriticalSection(global_init_lock); |
| 499 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 500 | #elif defined HAVE_BEOS_THREADS |
| 501 | release_sem(global_init_lock); |
| 502 | #endif |
| 503 | } |
| 504 | |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 505 | /** |
| 506 | * xmlGlobalInitMutexDestroy |
| 507 | * |
| 508 | * Makes sure that the global initialization mutex is destroyed before |
| 509 | * application termination. |
| 510 | */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 511 | void |
| 512 | __xmlGlobalInitMutexDestroy(void) |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 513 | { |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 514 | #ifdef HAVE_PTHREAD_H |
| 515 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 516 | if (global_init_lock != NULL) { |
| 517 | DeleteCriticalSection(global_init_lock); |
| 518 | free(global_init_lock); |
| 519 | global_init_lock = NULL; |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 520 | } |
| 521 | #endif |
| 522 | } |
| 523 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 524 | /************************************************************************ |
| 525 | * * |
| 526 | * Per thread global state handling * |
| 527 | * * |
| 528 | ************************************************************************/ |
| 529 | |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 530 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | 01c3bd5 | 2004-10-22 13:16:10 +0000 | [diff] [blame] | 531 | #ifdef xmlLastError |
| 532 | #undef xmlLastError |
| 533 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 534 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 535 | /** |
| 536 | * xmlFreeGlobalState: |
| 537 | * @state: a thread global state |
| 538 | * |
| 539 | * xmlFreeGlobalState() is called when a thread terminates with a non-NULL |
| 540 | * global state. It is is used here to reclaim memory resources. |
| 541 | */ |
| 542 | static void |
| 543 | xmlFreeGlobalState(void *state) |
| 544 | { |
Daniel Veillard | 01c3bd5 | 2004-10-22 13:16:10 +0000 | [diff] [blame] | 545 | xmlGlobalState *gs = (xmlGlobalState *) state; |
| 546 | |
| 547 | /* free any memory allocated in the thread's xmlLastError */ |
| 548 | xmlResetError(&(gs->xmlLastError)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 549 | free(state); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * xmlNewGlobalState: |
| 554 | * |
| 555 | * xmlNewGlobalState() allocates a global state. This structure is used to |
| 556 | * hold all data for use by a thread when supporting backwards compatibility |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 557 | * of libxml2 to pre-thread-safe behaviour. |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 558 | * |
| 559 | * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error |
| 560 | */ |
| 561 | static xmlGlobalStatePtr |
| 562 | xmlNewGlobalState(void) |
| 563 | { |
| 564 | xmlGlobalState *gs; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 565 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 566 | gs = malloc(sizeof(xmlGlobalState)); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 567 | if (gs == NULL) { |
| 568 | xmlGenericError(xmlGenericErrorContext, |
| 569 | "xmlGetGlobalState: out of memory\n"); |
| 570 | return (NULL); |
| 571 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 572 | |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 573 | memset(gs, 0, sizeof(xmlGlobalState)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 574 | xmlInitializeGlobalState(gs); |
| 575 | return (gs); |
| 576 | } |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 577 | #endif /* LIBXML_THREAD_ENABLED */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 578 | |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 579 | #ifdef HAVE_PTHREAD_H |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 580 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 581 | #if !defined(HAVE_COMPILER_TLS) |
| 582 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 583 | typedef struct _xmlGlobalStateCleanupHelperParams { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 584 | HANDLE thread; |
| 585 | void *memory; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 586 | } xmlGlobalStateCleanupHelperParams; |
| 587 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 588 | static void XMLCDECL |
| 589 | xmlGlobalStateCleanupHelper(void *p) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 590 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 591 | xmlGlobalStateCleanupHelperParams *params = |
| 592 | (xmlGlobalStateCleanupHelperParams *) p; |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 593 | WaitForSingleObject(params->thread, INFINITE); |
| 594 | CloseHandle(params->thread); |
| 595 | xmlFreeGlobalState(params->memory); |
| 596 | free(params); |
| 597 | _endthread(); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 598 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 599 | #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */ |
| 600 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 601 | typedef struct _xmlGlobalStateCleanupHelperParams { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 602 | void *memory; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 603 | struct _xmlGlobalStateCleanupHelperParams *prev; |
| 604 | struct _xmlGlobalStateCleanupHelperParams *next; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 605 | } xmlGlobalStateCleanupHelperParams; |
| 606 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 607 | static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 608 | static CRITICAL_SECTION cleanup_helpers_cs; |
| 609 | |
| 610 | #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */ |
| 611 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 612 | #endif /* HAVE_WIN32_THREADS */ |
| 613 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 614 | #if defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 615 | |
William M. Brack | b1d5316 | 2003-11-18 06:54:40 +0000 | [diff] [blame] | 616 | /** |
| 617 | * xmlGlobalStateCleanup: |
| 618 | * @data: unused parameter |
| 619 | * |
| 620 | * Used for Beos only |
| 621 | */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 622 | void |
| 623 | xmlGlobalStateCleanup(void *data) |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 624 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 625 | void *globalval = tls_get(globalkey); |
| 626 | |
| 627 | if (globalval != NULL) |
| 628 | xmlFreeGlobalState(globalval); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 629 | } |
| 630 | #endif |
| 631 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 632 | /** |
| 633 | * xmlGetGlobalState: |
| 634 | * |
| 635 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 636 | * |
| 637 | * Returns the thread global state or NULL in case of error |
| 638 | */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 639 | xmlGlobalStatePtr |
| 640 | xmlGetGlobalState(void) |
| 641 | { |
| 642 | #ifdef HAVE_PTHREAD_H |
| 643 | xmlGlobalState *globalval; |
| 644 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 645 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 646 | return (NULL); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 647 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 648 | pthread_once(&once_control, xmlOnceInit); |
| 649 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 650 | if ((globalval = (xmlGlobalState *) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 651 | pthread_getspecific(globalkey)) == NULL) { |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 652 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 653 | if (tsd == NULL) |
| 654 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 655 | |
| 656 | pthread_setspecific(globalkey, tsd); |
| 657 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 658 | } |
| 659 | return (globalval); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 660 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 661 | #if defined(HAVE_COMPILER_TLS) |
| 662 | if (!tlstate_inited) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 663 | tlstate_inited = 1; |
| 664 | xmlInitializeGlobalState(&tlstate); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 665 | } |
| 666 | return &tlstate; |
| 667 | #else /* HAVE_COMPILER_TLS */ |
| 668 | xmlGlobalState *globalval; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 669 | xmlGlobalStateCleanupHelperParams *p; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 670 | |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 671 | xmlOnceInit(); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 672 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 673 | globalval = (xmlGlobalState *) TlsGetValue(globalkey); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 674 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 675 | p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey); |
| 676 | globalval = (xmlGlobalState *) (p ? p->memory : NULL); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 677 | #endif |
| 678 | if (globalval == NULL) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 679 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 680 | |
| 681 | if (tsd == NULL) |
| 682 | return(NULL); |
| 683 | p = (xmlGlobalStateCleanupHelperParams *) |
| 684 | malloc(sizeof(xmlGlobalStateCleanupHelperParams)); |
| 685 | if (p == NULL) { |
| 686 | xmlGenericError(xmlGenericErrorContext, |
| 687 | "xmlGetGlobalState: out of memory\n"); |
Daniel Veillard | bf2ebff | 2009-01-18 14:57:04 +0000 | [diff] [blame] | 688 | xmlFreeGlobalState(tsd); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 689 | return(NULL); |
| 690 | } |
| 691 | p->memory = tsd; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 692 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 693 | DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 694 | GetCurrentProcess(), &p->thread, 0, TRUE, |
| 695 | DUPLICATE_SAME_ACCESS); |
| 696 | TlsSetValue(globalkey, tsd); |
| 697 | _beginthread(xmlGlobalStateCleanupHelper, 0, p); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 698 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 699 | EnterCriticalSection(&cleanup_helpers_cs); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 700 | if (cleanup_helpers_head != NULL) { |
| 701 | cleanup_helpers_head->prev = p; |
| 702 | } |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 703 | p->next = cleanup_helpers_head; |
| 704 | p->prev = NULL; |
| 705 | cleanup_helpers_head = p; |
| 706 | TlsSetValue(globalkey, p); |
| 707 | LeaveCriticalSection(&cleanup_helpers_cs); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 708 | #endif |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 709 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 710 | return (tsd); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 711 | } |
| 712 | return (globalval); |
| 713 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 714 | #elif defined HAVE_BEOS_THREADS |
| 715 | xmlGlobalState *globalval; |
| 716 | |
| 717 | xmlOnceInit(); |
| 718 | |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 719 | if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 720 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 721 | if (tsd == NULL) |
| 722 | return (NULL); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 723 | |
| 724 | tls_set(globalkey, tsd); |
| 725 | on_exit_thread(xmlGlobalStateCleanup, NULL); |
| 726 | return (tsd); |
| 727 | } |
| 728 | return (globalval); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 729 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 730 | return (NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 731 | #endif |
| 732 | } |
| 733 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 734 | /************************************************************************ |
| 735 | * * |
| 736 | * Library wide thread interfaces * |
| 737 | * * |
| 738 | ************************************************************************/ |
| 739 | |
| 740 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 741 | * xmlGetThreadId: |
| 742 | * |
| 743 | * xmlGetThreadId() find the current thread ID number |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 744 | * Note that this is likely to be broken on some platforms using pthreads |
| 745 | * as the specification doesn't mandate pthread_t to be an integer type |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 746 | * |
| 747 | * Returns the current thread ID number |
| 748 | */ |
| 749 | int |
| 750 | xmlGetThreadId(void) |
| 751 | { |
| 752 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 753 | pthread_t id; |
| 754 | int ret; |
| 755 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 756 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 757 | return (0); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 758 | id = pthread_self(); |
| 759 | /* horrible but preserves compat, see warning above */ |
| 760 | memcpy(&ret, &id, sizeof(ret)); |
| 761 | return (ret); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 762 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 763 | return GetCurrentThreadId(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 764 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 765 | return find_thread(NULL); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 766 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 767 | return ((int) 0); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 768 | #endif |
| 769 | } |
| 770 | |
| 771 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 772 | * xmlIsMainThread: |
| 773 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 774 | * xmlIsMainThread() check whether the current thread is the main thread. |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 775 | * |
| 776 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 777 | */ |
| 778 | int |
| 779 | xmlIsMainThread(void) |
| 780 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 781 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 782 | if (libxml_is_threaded == -1) |
| 783 | xmlInitThreads(); |
| 784 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 785 | return (1); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 786 | pthread_once(&once_control, xmlOnceInit); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 787 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 788 | xmlOnceInit(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 789 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 790 | xmlOnceInit(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 791 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 792 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 793 | #ifdef DEBUG_THREADS |
| 794 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 795 | #endif |
| 796 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 797 | return (pthread_equal(mainthread,pthread_self())); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 798 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 799 | return (mainthread == GetCurrentThreadId()); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 800 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 801 | return (mainthread == find_thread(NULL)); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 802 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 803 | return (1); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 804 | #endif |
| 805 | } |
| 806 | |
| 807 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 808 | * xmlLockLibrary: |
| 809 | * |
| 810 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 811 | * library. |
| 812 | */ |
| 813 | void |
| 814 | xmlLockLibrary(void) |
| 815 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 816 | #ifdef DEBUG_THREADS |
| 817 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 818 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 819 | xmlRMutexLock(xmlLibraryLock); |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * xmlUnlockLibrary: |
| 824 | * |
| 825 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 826 | * library. |
| 827 | */ |
| 828 | void |
| 829 | xmlUnlockLibrary(void) |
| 830 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 831 | #ifdef DEBUG_THREADS |
| 832 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 833 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 834 | xmlRMutexUnlock(xmlLibraryLock); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * xmlInitThreads: |
| 839 | * |
| 840 | * xmlInitThreads() is used to to initialize all the thread related |
| 841 | * data of the libxml2 library. |
| 842 | */ |
| 843 | void |
| 844 | xmlInitThreads(void) |
| 845 | { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 846 | #ifdef HAVE_PTHREAD_H |
| 847 | if (libxml_is_threaded == -1) { |
| 848 | if ((pthread_once != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 849 | (pthread_getspecific != NULL) && |
| 850 | (pthread_setspecific != NULL) && |
| 851 | (pthread_key_create != NULL) && |
Daniel Veillard | d4a3f24 | 2009-01-18 15:41:30 +0000 | [diff] [blame] | 852 | (pthread_key_delete != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 853 | (pthread_mutex_init != NULL) && |
| 854 | (pthread_mutex_destroy != NULL) && |
| 855 | (pthread_mutex_lock != NULL) && |
| 856 | (pthread_mutex_unlock != NULL) && |
| 857 | (pthread_cond_init != NULL) && |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 858 | (pthread_cond_destroy != NULL) && |
| 859 | (pthread_cond_wait != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 860 | (pthread_equal != NULL) && |
| 861 | (pthread_self != NULL) && |
| 862 | (pthread_cond_signal != NULL)) { |
| 863 | libxml_is_threaded = 1; |
| 864 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 865 | /* fprintf(stderr, "Running multithreaded\n"); */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 866 | } else { |
| 867 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 868 | /* fprintf(stderr, "Running without multithread\n"); */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 869 | libxml_is_threaded = 0; |
| 870 | } |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 871 | } |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 872 | #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
| 873 | InitializeCriticalSection(&cleanup_helpers_cs); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 874 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /** |
| 878 | * xmlCleanupThreads: |
| 879 | * |
| 880 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 881 | * data of the libxml2 library once processing has ended. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 882 | * |
| 883 | * WARNING: if your application is multithreaded or has plugin support |
| 884 | * calling this may crash the application if another thread or |
| 885 | * a plugin is still using libxml2. It's sometimes very hard to |
| 886 | * guess if libxml2 is in use in the application, some libraries |
| 887 | * or plugins may use it without notice. In case of doubt abstain |
| 888 | * from calling this function or do it just before calling exit() |
| 889 | * to avoid leak reports from valgrind ! |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 890 | */ |
| 891 | void |
| 892 | xmlCleanupThreads(void) |
| 893 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 894 | #ifdef DEBUG_THREADS |
| 895 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 896 | #endif |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 897 | #ifdef HAVE_PTHREAD_H |
| 898 | if ((libxml_is_threaded) && (pthread_key_delete != NULL)) |
| 899 | pthread_key_delete(globalkey); |
Friedrich Haubensak | 3f6cfbd | 2012-09-12 17:34:53 +0200 | [diff] [blame] | 900 | once_control = once_control_init; |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 901 | #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 902 | if (globalkey != TLS_OUT_OF_INDEXES) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 903 | xmlGlobalStateCleanupHelperParams *p; |
| 904 | |
| 905 | EnterCriticalSection(&cleanup_helpers_cs); |
| 906 | p = cleanup_helpers_head; |
| 907 | while (p != NULL) { |
| 908 | xmlGlobalStateCleanupHelperParams *temp = p; |
| 909 | |
| 910 | p = p->next; |
| 911 | xmlFreeGlobalState(temp->memory); |
| 912 | free(temp); |
| 913 | } |
| 914 | cleanup_helpers_head = 0; |
| 915 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 916 | TlsFree(globalkey); |
| 917 | globalkey = TLS_OUT_OF_INDEXES; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 918 | } |
| 919 | DeleteCriticalSection(&cleanup_helpers_cs); |
| 920 | #endif |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 921 | } |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 922 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 923 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 924 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 925 | /** |
| 926 | * xmlOnceInit |
| 927 | * |
| 928 | * xmlOnceInit() is used to initialize the value of mainthread for use |
| 929 | * in other routines. This function should only be called using |
| 930 | * pthread_once() in association with the once_control variable to ensure |
| 931 | * that the function is only called once. See man pthread_once for more |
| 932 | * details. |
| 933 | */ |
| 934 | static void |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 935 | xmlOnceInit(void) |
| 936 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 937 | #ifdef HAVE_PTHREAD_H |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 938 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 939 | mainthread = pthread_self(); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 940 | __xmlInitializeDict(); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 941 | #elif defined(HAVE_WIN32_THREADS) |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 942 | if (!run_once.done) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 943 | if (InterlockedIncrement(&run_once.control) == 1) { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 944 | #if !defined(HAVE_COMPILER_TLS) |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 945 | globalkey = TlsAlloc(); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 946 | #endif |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 947 | mainthread = GetCurrentThreadId(); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 948 | __xmlInitializeDict(); |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 949 | run_once.done = 1; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 950 | } else { |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 951 | /* Another thread is working; give up our slice and |
| 952 | * wait until they're done. */ |
| 953 | while (!run_once.done) |
| 954 | Sleep(0); |
| 955 | } |
| 956 | } |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 957 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 958 | if (atomic_add(&run_once_init, 1) == 0) { |
| 959 | globalkey = tls_allocate(); |
| 960 | tls_set(globalkey, NULL); |
| 961 | mainthread = find_thread(NULL); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 962 | __xmlInitializeDict(); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 963 | } else |
| 964 | atomic_add(&run_once_init, -1); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 965 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 966 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 967 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 968 | |
| 969 | /** |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 970 | * DllMain: |
| 971 | * @hinstDLL: handle to DLL instance |
| 972 | * @fdwReason: Reason code for entry |
| 973 | * @lpvReserved: generic pointer (depends upon reason code) |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 974 | * |
| 975 | * Entry point for Windows library. It is being used to free thread-specific |
| 976 | * storage. |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 977 | * |
| 978 | * Returns TRUE always |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 979 | */ |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 980 | #ifdef HAVE_PTHREAD_H |
| 981 | #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 982 | #if defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 983 | BOOL XMLCALL |
| 984 | xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 985 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 986 | BOOL WINAPI |
| 987 | DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 988 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 989 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 990 | switch (fdwReason) { |
| 991 | case DLL_THREAD_DETACH: |
| 992 | if (globalkey != TLS_OUT_OF_INDEXES) { |
| 993 | xmlGlobalState *globalval = NULL; |
| 994 | xmlGlobalStateCleanupHelperParams *p = |
| 995 | (xmlGlobalStateCleanupHelperParams *) |
| 996 | TlsGetValue(globalkey); |
| 997 | globalval = (xmlGlobalState *) (p ? p->memory : NULL); |
| 998 | if (globalval) { |
| 999 | xmlFreeGlobalState(globalval); |
| 1000 | TlsSetValue(globalkey, NULL); |
| 1001 | } |
| 1002 | if (p) { |
| 1003 | EnterCriticalSection(&cleanup_helpers_cs); |
| 1004 | if (p == cleanup_helpers_head) |
| 1005 | cleanup_helpers_head = p->next; |
| 1006 | else |
| 1007 | p->prev->next = p->next; |
| 1008 | if (p->next != NULL) |
| 1009 | p->next->prev = p->prev; |
| 1010 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 1011 | free(p); |
| 1012 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1013 | } |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 1014 | break; |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 1015 | } |
| 1016 | return TRUE; |
| 1017 | } |
| 1018 | #endif |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1019 | #define bottom_threads |
| 1020 | #include "elfgcchack.h" |