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 |
William M. Brack | b1d5316 | 2003-11-18 06:54:40 +0000 | [diff] [blame] | 435 | /** |
| 436 | * xmlGlobalStateCleanup: |
| 437 | * @data: unused parameter |
| 438 | * |
| 439 | * Used for Beos only |
| 440 | */ |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 441 | void xmlGlobalStateCleanup(void *data) |
| 442 | { |
| 443 | void *globalval = tls_get(globalkey); |
| 444 | if (globalval != NULL) |
| 445 | xmlFreeGlobalState(globalval); |
| 446 | } |
| 447 | #endif |
| 448 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 449 | /** |
| 450 | * xmlGetGlobalState: |
| 451 | * |
| 452 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 453 | * |
| 454 | * Returns the thread global state or NULL in case of error |
| 455 | */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 456 | xmlGlobalStatePtr |
| 457 | xmlGetGlobalState(void) |
| 458 | { |
| 459 | #ifdef HAVE_PTHREAD_H |
| 460 | xmlGlobalState *globalval; |
| 461 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 462 | pthread_once(&once_control, xmlOnceInit); |
| 463 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 464 | if ((globalval = (xmlGlobalState *) |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 465 | pthread_getspecific(globalkey)) == NULL) { |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 466 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 467 | |
| 468 | pthread_setspecific(globalkey, tsd); |
| 469 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 470 | } |
| 471 | return (globalval); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 472 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 473 | #if defined(HAVE_COMPILER_TLS) |
| 474 | if (!tlstate_inited) { |
| 475 | tlstate_inited = 1; |
| 476 | xmlInitializeGlobalState(&tlstate); |
| 477 | } |
| 478 | return &tlstate; |
| 479 | #else /* HAVE_COMPILER_TLS */ |
| 480 | xmlGlobalState *globalval; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 481 | xmlGlobalStateCleanupHelperParams * p; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 482 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 483 | if (run_once_init) { |
| 484 | run_once_init = 0; |
| 485 | xmlOnceInit(); |
| 486 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 487 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
| 488 | globalval = (xmlGlobalState *)TlsGetValue(globalkey); |
| 489 | #else |
| 490 | p = (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); |
| 491 | globalval = (xmlGlobalState *)(p ? p->memory : NULL); |
| 492 | #endif |
| 493 | if (globalval == NULL) { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 494 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 495 | p = (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams)); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 496 | p->memory = tsd; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 497 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 498 | DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 499 | GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 500 | TlsSetValue(globalkey, tsd); |
| 501 | _beginthread(xmlGlobalStateCleanupHelper, 0, p); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 502 | #else |
| 503 | EnterCriticalSection(&cleanup_helpers_cs); |
| 504 | if (cleanup_helpers_head != NULL) { |
| 505 | cleanup_helpers_head->prev = p; |
| 506 | } |
| 507 | p->next = cleanup_helpers_head; |
| 508 | p->prev = NULL; |
| 509 | cleanup_helpers_head = p; |
| 510 | TlsSetValue(globalkey, p); |
| 511 | LeaveCriticalSection(&cleanup_helpers_cs); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 512 | #endif |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 513 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 514 | return (tsd); |
| 515 | } |
| 516 | return (globalval); |
| 517 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 518 | #elif defined HAVE_BEOS_THREADS |
| 519 | xmlGlobalState *globalval; |
| 520 | |
| 521 | xmlOnceInit(); |
| 522 | |
| 523 | if ((globalval = (xmlGlobalState *) |
| 524 | tls_get(globalkey)) == NULL) { |
| 525 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 526 | |
| 527 | tls_set(globalkey, tsd); |
| 528 | on_exit_thread(xmlGlobalStateCleanup, NULL); |
| 529 | return (tsd); |
| 530 | } |
| 531 | return (globalval); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 532 | #else |
| 533 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 534 | #endif |
| 535 | } |
| 536 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 537 | /************************************************************************ |
| 538 | * * |
| 539 | * Library wide thread interfaces * |
| 540 | * * |
| 541 | ************************************************************************/ |
| 542 | |
| 543 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 544 | * xmlGetThreadId: |
| 545 | * |
| 546 | * xmlGetThreadId() find the current thread ID number |
| 547 | * |
| 548 | * Returns the current thread ID number |
| 549 | */ |
| 550 | int |
| 551 | xmlGetThreadId(void) |
| 552 | { |
| 553 | #ifdef HAVE_PTHREAD_H |
| 554 | return((int) pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 555 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 556 | return GetCurrentThreadId(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 557 | #elif defined HAVE_BEOS_THREADS |
| 558 | return find_thread(NULL); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 559 | #else |
| 560 | return((int) 0); |
| 561 | #endif |
| 562 | } |
| 563 | |
| 564 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 565 | * xmlIsMainThread: |
| 566 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 567 | * xmlIsMainThread() check whether the current thread is the main thread. |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 568 | * |
| 569 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 570 | */ |
| 571 | int |
| 572 | xmlIsMainThread(void) |
| 573 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 574 | #ifdef HAVE_PTHREAD_H |
| 575 | pthread_once(&once_control, xmlOnceInit); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 576 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 577 | if (run_once_init) { |
| 578 | run_once_init = 0; |
| 579 | xmlOnceInit (); |
| 580 | } |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 581 | #elif defined HAVE_BEOS_THREADS |
| 582 | xmlOnceInit(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 583 | #endif |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 584 | |
| 585 | #ifdef DEBUG_THREADS |
| 586 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 587 | #endif |
| 588 | #ifdef HAVE_PTHREAD_H |
| 589 | return(mainthread == pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 590 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 591 | return(mainthread == GetCurrentThreadId ()); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 592 | #elif defined HAVE_BEOS_THREADS |
| 593 | return(mainthread == find_thread(NULL)); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 594 | #else |
| 595 | return(1); |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 600 | * xmlLockLibrary: |
| 601 | * |
| 602 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 603 | * library. |
| 604 | */ |
| 605 | void |
| 606 | xmlLockLibrary(void) |
| 607 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 608 | #ifdef DEBUG_THREADS |
| 609 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 610 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 611 | xmlRMutexLock(xmlLibraryLock); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * xmlUnlockLibrary: |
| 616 | * |
| 617 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 618 | * library. |
| 619 | */ |
| 620 | void |
| 621 | xmlUnlockLibrary(void) |
| 622 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 623 | #ifdef DEBUG_THREADS |
| 624 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 625 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 626 | xmlRMutexUnlock(xmlLibraryLock); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * xmlInitThreads: |
| 631 | * |
| 632 | * xmlInitThreads() is used to to initialize all the thread related |
| 633 | * data of the libxml2 library. |
| 634 | */ |
| 635 | void |
| 636 | xmlInitThreads(void) |
| 637 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 638 | #ifdef DEBUG_THREADS |
| 639 | xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n"); |
| 640 | #endif |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 641 | #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] | 642 | InitializeCriticalSection(&cleanup_helpers_cs); |
| 643 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /** |
| 647 | * xmlCleanupThreads: |
| 648 | * |
| 649 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 650 | * data of the libxml2 library once processing has ended. |
| 651 | */ |
| 652 | void |
| 653 | xmlCleanupThreads(void) |
| 654 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 655 | #ifdef DEBUG_THREADS |
| 656 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 657 | #endif |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 658 | #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] | 659 | if (globalkey != TLS_OUT_OF_INDEXES) { |
| 660 | xmlGlobalStateCleanupHelperParams * p; |
| 661 | EnterCriticalSection(&cleanup_helpers_cs); |
| 662 | p = cleanup_helpers_head; |
| 663 | while (p != NULL) { |
| 664 | xmlGlobalStateCleanupHelperParams * temp = p; |
| 665 | p = p->next; |
| 666 | xmlFreeGlobalState(temp->memory); |
| 667 | free(temp); |
| 668 | } |
| 669 | cleanup_helpers_head = 0; |
| 670 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 671 | TlsFree(globalkey); |
| 672 | globalkey = TLS_OUT_OF_INDEXES; |
| 673 | } |
| 674 | DeleteCriticalSection(&cleanup_helpers_cs); |
| 675 | #endif |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 676 | } |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 677 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 678 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 679 | /** |
| 680 | * xmlOnceInit |
| 681 | * |
| 682 | * xmlOnceInit() is used to initialize the value of mainthread for use |
| 683 | * in other routines. This function should only be called using |
| 684 | * pthread_once() in association with the once_control variable to ensure |
| 685 | * that the function is only called once. See man pthread_once for more |
| 686 | * details. |
| 687 | */ |
| 688 | static void |
| 689 | xmlOnceInit(void) { |
| 690 | #ifdef HAVE_PTHREAD_H |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 691 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 692 | mainthread = pthread_self(); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 693 | #endif |
| 694 | |
| 695 | #if defined(HAVE_WIN32_THREADS) |
| 696 | #if !defined(HAVE_COMPILER_TLS) |
| 697 | globalkey = TlsAlloc(); |
| 698 | #endif |
| 699 | mainthread = GetCurrentThreadId(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 700 | #endif |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 701 | |
| 702 | #ifdef HAVE_BEOS_THREADS |
| 703 | if (atomic_add(&run_once_init, 1) == 0) { |
| 704 | globalkey = tls_allocate(); |
| 705 | tls_set(globalkey, NULL); |
| 706 | mainthread = find_thread(NULL); |
| 707 | } else |
| 708 | atomic_add(&run_once_init, -1); |
| 709 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 710 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 711 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 712 | |
| 713 | /** |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 714 | * DllMain: |
| 715 | * @hinstDLL: handle to DLL instance |
| 716 | * @fdwReason: Reason code for entry |
| 717 | * @lpvReserved: generic pointer (depends upon reason code) |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 718 | * |
| 719 | * Entry point for Windows library. It is being used to free thread-specific |
| 720 | * storage. |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 721 | * |
| 722 | * Returns TRUE always |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 723 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 724 | #if defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
| 725 | #if defined(LIBXML_STATIC_FOR_DLL) |
| 726 | BOOL WINAPI xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 727 | #else |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 728 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 729 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 730 | { |
| 731 | switch(fdwReason) { |
| 732 | case DLL_THREAD_DETACH: |
| 733 | if (globalkey != TLS_OUT_OF_INDEXES) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 734 | xmlGlobalState *globalval = NULL; |
| 735 | xmlGlobalStateCleanupHelperParams * p = |
| 736 | (xmlGlobalStateCleanupHelperParams*)TlsGetValue(globalkey); |
| 737 | globalval = (xmlGlobalState *)(p ? p->memory : NULL); |
| 738 | if (globalval) { |
| 739 | xmlFreeGlobalState(globalval); |
| 740 | TlsSetValue(globalkey,NULL); |
| 741 | } |
| 742 | if (p) |
| 743 | { |
| 744 | EnterCriticalSection(&cleanup_helpers_cs); |
| 745 | if (p == cleanup_helpers_head) |
| 746 | cleanup_helpers_head = p->next; |
| 747 | else |
| 748 | p->prev->next = p->next; |
| 749 | if (p->next != NULL) |
| 750 | p->next->prev = p->prev; |
| 751 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 752 | free(p); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | break; |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 756 | } |
| 757 | return TRUE; |
| 758 | } |
| 759 | #endif |