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 | |
| 49 | /* |
| 50 | * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree |
| 51 | * to avoid some crazyness since xmlMalloc/xmlFree may actually |
| 52 | * be hosted on allocated blocks needing them for the allocation ... |
| 53 | */ |
| 54 | |
| 55 | /* |
| 56 | * xmlMutex are a simple mutual exception locks |
| 57 | */ |
| 58 | struct _xmlMutex { |
| 59 | #ifdef HAVE_PTHREAD_H |
| 60 | pthread_mutex_t lock; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 61 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 62 | HANDLE mutex; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 63 | #elif defined HAVE_BEOS_THREADS |
| 64 | sem_id sem; |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 65 | thread_id tid; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 66 | #else |
| 67 | int empty; |
| 68 | #endif |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * xmlRMutex are reentrant mutual exception locks |
| 73 | */ |
| 74 | struct _xmlRMutex { |
| 75 | #ifdef HAVE_PTHREAD_H |
| 76 | pthread_mutex_t lock; |
| 77 | unsigned int held; |
| 78 | unsigned int waiters; |
| 79 | pthread_t tid; |
| 80 | pthread_cond_t cv; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 81 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 82 | CRITICAL_SECTION cs; |
| 83 | unsigned int count; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 84 | #elif defined HAVE_BEOS_THREADS |
| 85 | xmlMutexPtr lock; |
| 86 | thread_id tid; |
| 87 | int32 count; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 88 | #else |
| 89 | int empty; |
| 90 | #endif |
| 91 | }; |
| 92 | /* |
| 93 | * This module still has some internal static data. |
| 94 | * - xmlLibraryLock a global lock |
| 95 | * - globalkey used for per-thread data |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 96 | */ |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 97 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 98 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 99 | static pthread_key_t globalkey; |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 100 | static pthread_t mainthread; |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 101 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 102 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 103 | #if defined(HAVE_COMPILER_TLS) |
| 104 | static __declspec(thread) xmlGlobalState tlstate; |
| 105 | static __declspec(thread) int tlstate_inited = 0; |
| 106 | #else /* HAVE_COMPILER_TLS */ |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 107 | static DWORD globalkey = TLS_OUT_OF_INDEXES; |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 108 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 109 | static DWORD mainthread; |
| 110 | static int run_once_init = 1; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 111 | /* endif HAVE_WIN32_THREADS */ |
| 112 | #elif defined HAVE_BEOS_THREADS |
| 113 | int32 globalkey = 0; |
| 114 | thread_id mainthread = 0; |
| 115 | int32 run_once_init = 0; |
| 116 | #endif |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 117 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 118 | static xmlRMutexPtr xmlLibraryLock = NULL; |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 119 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 120 | static void xmlOnceInit(void); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 121 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 124 | * xmlNewMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 125 | * |
| 126 | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
| 127 | * synchronizing access to data. |
| 128 | * |
| 129 | * Returns a new simple mutex pointer or NULL in case of error |
| 130 | */ |
| 131 | xmlMutexPtr |
| 132 | xmlNewMutex(void) |
| 133 | { |
| 134 | xmlMutexPtr tok; |
| 135 | |
| 136 | if ((tok = malloc(sizeof(xmlMutex))) == NULL) |
| 137 | return (NULL); |
| 138 | #ifdef HAVE_PTHREAD_H |
| 139 | pthread_mutex_init(&tok->lock, NULL); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 140 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 141 | tok->mutex = CreateMutex(NULL, FALSE, NULL); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 142 | #elif defined HAVE_BEOS_THREADS |
| 143 | if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) { |
| 144 | free(tok); |
| 145 | return NULL; |
| 146 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 147 | tok->tid = -1; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 148 | #endif |
| 149 | return (tok); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * xmlFreeMutex: |
| 154 | * @tok: the simple mutex |
| 155 | * |
| 156 | * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token |
| 157 | * struct. |
| 158 | */ |
| 159 | void |
| 160 | xmlFreeMutex(xmlMutexPtr tok) |
| 161 | { |
Daniel Veillard | df101d8 | 2003-07-08 14:03:36 +0000 | [diff] [blame] | 162 | if (tok == NULL) return; |
| 163 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 164 | #ifdef HAVE_PTHREAD_H |
| 165 | pthread_mutex_destroy(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 166 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 167 | CloseHandle(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 168 | #elif defined HAVE_BEOS_THREADS |
| 169 | delete_sem(tok->sem); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 170 | #endif |
| 171 | free(tok); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * xmlMutexLock: |
| 176 | * @tok: the simple mutex |
| 177 | * |
| 178 | * xmlMutexLock() is used to lock a libxml2 token. |
| 179 | */ |
| 180 | void |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 181 | xmlMutexLock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 182 | { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 183 | if (tok == NULL) |
| 184 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 185 | #ifdef HAVE_PTHREAD_H |
| 186 | pthread_mutex_lock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 187 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 188 | WaitForSingleObject(tok->mutex, INFINITE); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 189 | #elif defined HAVE_BEOS_THREADS |
| 190 | if (acquire_sem(tok->sem) != B_NO_ERROR) { |
| 191 | #ifdef DEBUG_THREADS |
| 192 | xmlGenericError(xmlGenericErrorContext, "xmlMutexLock():BeOS:Couldn't aquire semaphore\n"); |
| 193 | exit(); |
| 194 | #endif |
| 195 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 196 | tok->tid = find_thread(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 197 | #endif |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * xmlMutexUnlock: |
| 203 | * @tok: the simple mutex |
| 204 | * |
| 205 | * xmlMutexUnlock() is used to unlock a libxml2 token. |
| 206 | */ |
| 207 | void |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 208 | xmlMutexUnlock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 209 | { |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 210 | if (tok == NULL) |
| 211 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 212 | #ifdef HAVE_PTHREAD_H |
| 213 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 214 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 215 | ReleaseMutex(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 216 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 217 | if (tok->tid == find_thread(NULL)) { |
| 218 | tok->tid = -1; |
| 219 | release_sem(tok->sem); |
| 220 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 221 | #endif |
| 222 | } |
| 223 | |
| 224 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 225 | * xmlNewRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 226 | * |
| 227 | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
| 228 | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
| 229 | * for synchronizing access to data structures that may be manipulated in a |
| 230 | * recursive fashion. |
| 231 | * |
| 232 | * Returns the new reentrant mutex pointer or NULL in case of error |
| 233 | */ |
| 234 | xmlRMutexPtr |
| 235 | xmlNewRMutex(void) |
| 236 | { |
| 237 | xmlRMutexPtr tok; |
| 238 | |
| 239 | if ((tok = malloc(sizeof(xmlRMutex))) == NULL) |
| 240 | return (NULL); |
| 241 | #ifdef HAVE_PTHREAD_H |
| 242 | pthread_mutex_init(&tok->lock, NULL); |
| 243 | tok->held = 0; |
| 244 | tok->waiters = 0; |
William M. Brack | 59002e7 | 2003-07-04 17:01:59 +0000 | [diff] [blame] | 245 | pthread_cond_init(&tok->cv, NULL); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 246 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 247 | InitializeCriticalSection(&tok->cs); |
| 248 | tok->count = 0; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 249 | #elif defined HAVE_BEOS_THREADS |
| 250 | if ((tok->lock = xmlNewMutex()) == NULL) { |
| 251 | free(tok); |
| 252 | return NULL; |
| 253 | } |
| 254 | tok->count = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 255 | #endif |
| 256 | return (tok); |
| 257 | } |
| 258 | |
| 259 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 260 | * xmlFreeRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 261 | * @tok: the reentrant mutex |
| 262 | * |
| 263 | * xmlRFreeMutex() is used to reclaim resources associated with a |
| 264 | * reentrant mutex. |
| 265 | */ |
| 266 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 267 | xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 268 | { |
| 269 | #ifdef HAVE_PTHREAD_H |
| 270 | pthread_mutex_destroy(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 271 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 272 | DeleteCriticalSection(&tok->cs); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 273 | #elif defined HAVE_BEOS_THREADS |
| 274 | xmlFreeMutex(tok->lock); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 275 | #endif |
| 276 | free(tok); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * xmlRMutexLock: |
| 281 | * @tok: the reentrant mutex |
| 282 | * |
| 283 | * xmlRMutexLock() is used to lock a libxml2 token_r. |
| 284 | */ |
| 285 | void |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 286 | xmlRMutexLock(xmlRMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 287 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 288 | if (tok == NULL) |
| 289 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 290 | #ifdef HAVE_PTHREAD_H |
| 291 | pthread_mutex_lock(&tok->lock); |
| 292 | if (tok->held) { |
| 293 | if (pthread_equal(tok->tid, pthread_self())) { |
| 294 | tok->held++; |
| 295 | pthread_mutex_unlock(&tok->lock); |
| 296 | return; |
| 297 | } else { |
| 298 | tok->waiters++; |
| 299 | while (tok->held) |
| 300 | pthread_cond_wait(&tok->cv, &tok->lock); |
| 301 | tok->waiters--; |
| 302 | } |
| 303 | } |
| 304 | tok->tid = pthread_self(); |
| 305 | tok->held = 1; |
| 306 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 307 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 308 | EnterCriticalSection(&tok->cs); |
| 309 | ++tok->count; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 310 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 311 | if (tok->lock->tid == find_thread(NULL)) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 312 | tok->count++; |
| 313 | return; |
| 314 | } else { |
| 315 | xmlMutexLock(tok->lock); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 316 | tok->count = 1; |
| 317 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 318 | #endif |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * xmlRMutexUnlock: |
| 323 | * @tok: the reentrant mutex |
| 324 | * |
| 325 | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
| 326 | */ |
| 327 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 328 | xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 329 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 330 | if (tok == NULL) |
| 331 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 332 | #ifdef HAVE_PTHREAD_H |
| 333 | pthread_mutex_lock(&tok->lock); |
| 334 | tok->held--; |
| 335 | if (tok->held == 0) { |
| 336 | if (tok->waiters) |
| 337 | pthread_cond_signal(&tok->cv); |
| 338 | tok->tid = 0; |
| 339 | } |
| 340 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 341 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 342 | if (!--tok->count) |
| 343 | LeaveCriticalSection(&tok->cs); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 344 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame^] | 345 | if (tok->lock->tid == find_thread(NULL)) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 346 | tok->count--; |
| 347 | if (tok->count == 0) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 348 | xmlMutexUnlock(tok->lock); |
| 349 | } |
| 350 | return; |
| 351 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 352 | #endif |
| 353 | } |
| 354 | |
| 355 | /************************************************************************ |
| 356 | * * |
| 357 | * Per thread global state handling * |
| 358 | * * |
| 359 | ************************************************************************/ |
| 360 | |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 361 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 362 | /** |
| 363 | * xmlFreeGlobalState: |
| 364 | * @state: a thread global state |
| 365 | * |
| 366 | * xmlFreeGlobalState() is called when a thread terminates with a non-NULL |
| 367 | * global state. It is is used here to reclaim memory resources. |
| 368 | */ |
| 369 | static void |
| 370 | xmlFreeGlobalState(void *state) |
| 371 | { |
| 372 | free(state); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * xmlNewGlobalState: |
| 377 | * |
| 378 | * xmlNewGlobalState() allocates a global state. This structure is used to |
| 379 | * hold all data for use by a thread when supporting backwards compatibility |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 380 | * of libxml2 to pre-thread-safe behaviour. |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 381 | * |
| 382 | * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error |
| 383 | */ |
| 384 | static xmlGlobalStatePtr |
| 385 | xmlNewGlobalState(void) |
| 386 | { |
| 387 | xmlGlobalState *gs; |
| 388 | |
| 389 | gs = malloc(sizeof(xmlGlobalState)); |
| 390 | if (gs == NULL) |
| 391 | return(NULL); |
| 392 | |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 393 | memset(gs, 0, sizeof(xmlGlobalState)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 394 | xmlInitializeGlobalState(gs); |
| 395 | return (gs); |
| 396 | } |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 397 | #endif /* LIBXML_THREAD_ENABLED */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 398 | |
| 399 | |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 400 | #ifdef HAVE_WIN32_THREADS |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 401 | #if !defined(HAVE_COMPILER_TLS) |
| 402 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 403 | typedef struct _xmlGlobalStateCleanupHelperParams |
| 404 | { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 405 | HANDLE thread; |
| 406 | void *memory; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 407 | } xmlGlobalStateCleanupHelperParams; |
| 408 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 409 | static void xmlGlobalStateCleanupHelper (void *p) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 410 | { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 411 | xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p; |
| 412 | WaitForSingleObject(params->thread, INFINITE); |
| 413 | CloseHandle(params->thread); |
| 414 | xmlFreeGlobalState(params->memory); |
| 415 | free(params); |
| 416 | _endthread(); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 417 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 418 | #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */ |
| 419 | |
| 420 | typedef struct _xmlGlobalStateCleanupHelperParams |
| 421 | { |
| 422 | void *memory; |
| 423 | struct _xmlGlobalStateCleanupHelperParams * prev; |
| 424 | struct _xmlGlobalStateCleanupHelperParams * next; |
| 425 | } xmlGlobalStateCleanupHelperParams; |
| 426 | |
| 427 | static xmlGlobalStateCleanupHelperParams * cleanup_helpers_head = NULL; |
| 428 | static CRITICAL_SECTION cleanup_helpers_cs; |
| 429 | |
| 430 | #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */ |
| 431 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 432 | #endif /* HAVE_WIN32_THREADS */ |
| 433 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 434 | #if defined HAVE_BEOS_THREADS |
| 435 | void xmlGlobalStateCleanup(void *data) |
| 436 | { |
| 437 | void *globalval = tls_get(globalkey); |
| 438 | if (globalval != NULL) |
| 439 | xmlFreeGlobalState(globalval); |
| 440 | } |
| 441 | #endif |
| 442 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 443 | /** |
| 444 | * xmlGetGlobalState: |
| 445 | * |
| 446 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 447 | * |
| 448 | * Returns the thread global state or NULL in case of error |
| 449 | */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 450 | xmlGlobalStatePtr |
| 451 | xmlGetGlobalState(void) |
| 452 | { |
| 453 | #ifdef HAVE_PTHREAD_H |
| 454 | xmlGlobalState *globalval; |
| 455 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 456 | pthread_once(&once_control, xmlOnceInit); |
| 457 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 458 | if ((globalval = (xmlGlobalState *) |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 459 | pthread_getspecific(globalkey)) == NULL) { |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 460 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 461 | |
| 462 | pthread_setspecific(globalkey, tsd); |
| 463 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 464 | } |
| 465 | return (globalval); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 466 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 467 | #if defined(HAVE_COMPILER_TLS) |
| 468 | if (!tlstate_inited) { |
| 469 | tlstate_inited = 1; |
| 470 | xmlInitializeGlobalState(&tlstate); |
| 471 | } |
| 472 | return &tlstate; |
| 473 | #else /* HAVE_COMPILER_TLS */ |
| 474 | xmlGlobalState *globalval; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 475 | xmlGlobalStateCleanupHelperParams * p; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 476 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 477 | if (run_once_init) { |
| 478 | run_once_init = 0; |
| 479 | xmlOnceInit(); |
| 480 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 481 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
| 482 | globalval = (xmlGlobalState *)TlsGetValue(globalkey); |
| 483 | #else |
| 484 | p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); |
| 485 | globalval = (xmlGlobalState *)(p ? p->memory : NULL); |
| 486 | #endif |
| 487 | if (globalval == NULL) { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 488 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 489 | p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams)); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 490 | p->memory = tsd; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 491 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 492 | DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 493 | GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 494 | TlsSetValue(globalkey, tsd); |
| 495 | _beginthread(xmlGlobalStateCleanupHelper, 0, p); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 496 | #else |
| 497 | EnterCriticalSection(&cleanup_helpers_cs); |
| 498 | if (cleanup_helpers_head != NULL) { |
| 499 | cleanup_helpers_head->prev = p; |
| 500 | } |
| 501 | p->next = cleanup_helpers_head; |
| 502 | p->prev = NULL; |
| 503 | cleanup_helpers_head = p; |
| 504 | TlsSetValue(globalkey, p); |
| 505 | LeaveCriticalSection(&cleanup_helpers_cs); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 506 | #endif |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 507 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 508 | return (tsd); |
| 509 | } |
| 510 | return (globalval); |
| 511 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 512 | #elif defined HAVE_BEOS_THREADS |
| 513 | xmlGlobalState *globalval; |
| 514 | |
| 515 | xmlOnceInit(); |
| 516 | |
| 517 | if ((globalval = (xmlGlobalState *) |
| 518 | tls_get(globalkey)) == NULL) { |
| 519 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 520 | |
| 521 | tls_set(globalkey, tsd); |
| 522 | on_exit_thread(xmlGlobalStateCleanup, NULL); |
| 523 | return (tsd); |
| 524 | } |
| 525 | return (globalval); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 526 | #else |
| 527 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 528 | #endif |
| 529 | } |
| 530 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 531 | /************************************************************************ |
| 532 | * * |
| 533 | * Library wide thread interfaces * |
| 534 | * * |
| 535 | ************************************************************************/ |
| 536 | |
| 537 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 538 | * xmlGetThreadId: |
| 539 | * |
| 540 | * xmlGetThreadId() find the current thread ID number |
| 541 | * |
| 542 | * Returns the current thread ID number |
| 543 | */ |
| 544 | int |
| 545 | xmlGetThreadId(void) |
| 546 | { |
| 547 | #ifdef HAVE_PTHREAD_H |
| 548 | return((int) pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 549 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 550 | return GetCurrentThreadId(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 551 | #elif defined HAVE_BEOS_THREADS |
| 552 | return find_thread(NULL); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 553 | #else |
| 554 | return((int) 0); |
| 555 | #endif |
| 556 | } |
| 557 | |
| 558 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 559 | * xmlIsMainThread: |
| 560 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 561 | * xmlIsMainThread() check whether the current thread is the main thread. |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 562 | * |
| 563 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 564 | */ |
| 565 | int |
| 566 | xmlIsMainThread(void) |
| 567 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 568 | #ifdef HAVE_PTHREAD_H |
| 569 | pthread_once(&once_control, xmlOnceInit); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 570 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 571 | if (run_once_init) { |
| 572 | run_once_init = 0; |
| 573 | xmlOnceInit (); |
| 574 | } |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 575 | #elif defined HAVE_BEOS_THREADS |
| 576 | xmlOnceInit(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 577 | #endif |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 578 | |
| 579 | #ifdef DEBUG_THREADS |
| 580 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 581 | #endif |
| 582 | #ifdef HAVE_PTHREAD_H |
| 583 | return(mainthread == pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 584 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 585 | return(mainthread == GetCurrentThreadId ()); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 586 | #elif defined HAVE_BEOS_THREADS |
| 587 | return(mainthread == find_thread(NULL)); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 588 | #else |
| 589 | return(1); |
| 590 | #endif |
| 591 | } |
| 592 | |
| 593 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 594 | * xmlLockLibrary: |
| 595 | * |
| 596 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 597 | * library. |
| 598 | */ |
| 599 | void |
| 600 | xmlLockLibrary(void) |
| 601 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 602 | #ifdef DEBUG_THREADS |
| 603 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 604 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 605 | xmlRMutexLock(xmlLibraryLock); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * xmlUnlockLibrary: |
| 610 | * |
| 611 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 612 | * library. |
| 613 | */ |
| 614 | void |
| 615 | xmlUnlockLibrary(void) |
| 616 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 617 | #ifdef DEBUG_THREADS |
| 618 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 619 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 620 | xmlRMutexUnlock(xmlLibraryLock); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * xmlInitThreads: |
| 625 | * |
| 626 | * xmlInitThreads() is used to to initialize all the thread related |
| 627 | * data of the libxml2 library. |
| 628 | */ |
| 629 | void |
| 630 | xmlInitThreads(void) |
| 631 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 632 | #ifdef DEBUG_THREADS |
| 633 | xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n"); |
| 634 | #endif |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 635 | #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] | 636 | InitializeCriticalSection(&cleanup_helpers_cs); |
| 637 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * xmlCleanupThreads: |
| 642 | * |
| 643 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 644 | * data of the libxml2 library once processing has ended. |
| 645 | */ |
| 646 | void |
| 647 | xmlCleanupThreads(void) |
| 648 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 649 | #ifdef DEBUG_THREADS |
| 650 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 651 | #endif |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 652 | #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] | 653 | if (globalkey != TLS_OUT_OF_INDEXES) { |
| 654 | xmlGlobalStateCleanupHelperParams * p; |
| 655 | EnterCriticalSection(&cleanup_helpers_cs); |
| 656 | p = cleanup_helpers_head; |
| 657 | while (p != NULL) { |
| 658 | xmlGlobalStateCleanupHelperParams * temp = p; |
| 659 | p = p->next; |
| 660 | xmlFreeGlobalState(temp->memory); |
| 661 | free(temp); |
| 662 | } |
| 663 | cleanup_helpers_head = 0; |
| 664 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 665 | TlsFree(globalkey); |
| 666 | globalkey = TLS_OUT_OF_INDEXES; |
| 667 | } |
| 668 | DeleteCriticalSection(&cleanup_helpers_cs); |
| 669 | #endif |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 670 | } |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 671 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 672 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 673 | /** |
| 674 | * xmlOnceInit |
| 675 | * |
| 676 | * xmlOnceInit() is used to initialize the value of mainthread for use |
| 677 | * in other routines. This function should only be called using |
| 678 | * pthread_once() in association with the once_control variable to ensure |
| 679 | * that the function is only called once. See man pthread_once for more |
| 680 | * details. |
| 681 | */ |
| 682 | static void |
| 683 | xmlOnceInit(void) { |
| 684 | #ifdef HAVE_PTHREAD_H |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 685 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 686 | mainthread = pthread_self(); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 687 | #endif |
| 688 | |
| 689 | #if defined(HAVE_WIN32_THREADS) |
| 690 | #if !defined(HAVE_COMPILER_TLS) |
| 691 | globalkey = TlsAlloc(); |
| 692 | #endif |
| 693 | mainthread = GetCurrentThreadId(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 694 | #endif |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 695 | |
| 696 | #ifdef HAVE_BEOS_THREADS |
| 697 | if (atomic_add(&run_once_init, 1) == 0) { |
| 698 | globalkey = tls_allocate(); |
| 699 | tls_set(globalkey, NULL); |
| 700 | mainthread = find_thread(NULL); |
| 701 | } else |
| 702 | atomic_add(&run_once_init, -1); |
| 703 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 704 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 705 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 706 | |
| 707 | /** |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 708 | * DllMain: |
| 709 | * @hinstDLL: handle to DLL instance |
| 710 | * @fdwReason: Reason code for entry |
| 711 | * @lpvReserved: generic pointer (depends upon reason code) |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 712 | * |
| 713 | * Entry point for Windows library. It is being used to free thread-specific |
| 714 | * storage. |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 715 | * |
| 716 | * Returns TRUE always |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 717 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 718 | #if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
| 719 | #if defined(LIBXML_STATIC_FOR_DLL) |
| 720 | BOOL WINAPI xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 721 | #else |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 722 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 723 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 724 | { |
| 725 | switch(fdwReason) { |
| 726 | case DLL_THREAD_DETACH: |
| 727 | if (globalkey != TLS_OUT_OF_INDEXES) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 728 | xmlGlobalState *globalval = NULL; |
| 729 | xmlGlobalStateCleanupHelperParams * p = |
| 730 | (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); |
| 731 | globalval = (xmlGlobalState *)(p ? p->memory : NULL); |
| 732 | if (globalval) { |
| 733 | xmlFreeGlobalState(globalval); |
| 734 | TlsSetValue(globalkey,NULL); |
| 735 | } |
| 736 | if (p) |
| 737 | { |
| 738 | EnterCriticalSection(&cleanup_helpers_cs); |
| 739 | if (p == cleanup_helpers_head) |
| 740 | cleanup_helpers_head = p->next; |
| 741 | else |
| 742 | p->prev->next = p->next; |
| 743 | if (p->next != NULL) |
| 744 | p->next->prev = p->prev; |
| 745 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 746 | free(p); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | break; |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 750 | } |
| 751 | return TRUE; |
| 752 | } |
| 753 | #endif |