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