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