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