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