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