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