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 */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 95 | static DWORD globalkey; |
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 | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 102 | static void xmlOnceInit(void); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 105 | * xmlNewMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 106 | * |
| 107 | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
| 108 | * synchronizing access to data. |
| 109 | * |
| 110 | * Returns a new simple mutex pointer or NULL in case of error |
| 111 | */ |
| 112 | xmlMutexPtr |
| 113 | xmlNewMutex(void) |
| 114 | { |
| 115 | xmlMutexPtr tok; |
| 116 | |
| 117 | if ((tok = malloc(sizeof(xmlMutex))) == NULL) |
| 118 | return (NULL); |
| 119 | #ifdef HAVE_PTHREAD_H |
| 120 | pthread_mutex_init(&tok->lock, NULL); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 121 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 122 | tok->mutex = CreateMutex(NULL, FALSE, NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 123 | #endif |
| 124 | return (tok); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * xmlFreeMutex: |
| 129 | * @tok: the simple mutex |
| 130 | * |
| 131 | * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token |
| 132 | * struct. |
| 133 | */ |
| 134 | void |
| 135 | xmlFreeMutex(xmlMutexPtr tok) |
| 136 | { |
| 137 | #ifdef HAVE_PTHREAD_H |
| 138 | pthread_mutex_destroy(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 139 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 140 | CloseHandle(tok->mutex); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 141 | #endif |
| 142 | free(tok); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * xmlMutexLock: |
| 147 | * @tok: the simple mutex |
| 148 | * |
| 149 | * xmlMutexLock() is used to lock a libxml2 token. |
| 150 | */ |
| 151 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 152 | xmlMutexLock(xmlMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 153 | { |
| 154 | #ifdef HAVE_PTHREAD_H |
| 155 | pthread_mutex_lock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 156 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 157 | WaitForSingleObject(tok->mutex, INFINITE); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * xmlMutexUnlock: |
| 164 | * @tok: the simple mutex |
| 165 | * |
| 166 | * xmlMutexUnlock() is used to unlock a libxml2 token. |
| 167 | */ |
| 168 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 169 | xmlMutexUnlock(xmlMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 170 | { |
| 171 | #ifdef HAVE_PTHREAD_H |
| 172 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 173 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 174 | ReleaseMutex(tok->mutex); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 175 | #endif |
| 176 | } |
| 177 | |
| 178 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 179 | * xmlNewRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 180 | * |
| 181 | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
| 182 | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
| 183 | * for synchronizing access to data structures that may be manipulated in a |
| 184 | * recursive fashion. |
| 185 | * |
| 186 | * Returns the new reentrant mutex pointer or NULL in case of error |
| 187 | */ |
| 188 | xmlRMutexPtr |
| 189 | xmlNewRMutex(void) |
| 190 | { |
| 191 | xmlRMutexPtr tok; |
| 192 | |
| 193 | if ((tok = malloc(sizeof(xmlRMutex))) == NULL) |
| 194 | return (NULL); |
| 195 | #ifdef HAVE_PTHREAD_H |
| 196 | pthread_mutex_init(&tok->lock, NULL); |
| 197 | tok->held = 0; |
| 198 | tok->waiters = 0; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 199 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 200 | InitializeCriticalSection(&tok->cs); |
| 201 | tok->count = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 202 | #endif |
| 203 | return (tok); |
| 204 | } |
| 205 | |
| 206 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 207 | * xmlFreeRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 208 | * @tok: the reentrant mutex |
| 209 | * |
| 210 | * xmlRFreeMutex() is used to reclaim resources associated with a |
| 211 | * reentrant mutex. |
| 212 | */ |
| 213 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 214 | xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 215 | { |
| 216 | #ifdef HAVE_PTHREAD_H |
| 217 | pthread_mutex_destroy(&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 | DeleteCriticalSection(&tok->cs); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 220 | #endif |
| 221 | free(tok); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * xmlRMutexLock: |
| 226 | * @tok: the reentrant mutex |
| 227 | * |
| 228 | * xmlRMutexLock() is used to lock a libxml2 token_r. |
| 229 | */ |
| 230 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 231 | xmlRMutexLock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 232 | { |
| 233 | #ifdef HAVE_PTHREAD_H |
| 234 | pthread_mutex_lock(&tok->lock); |
| 235 | if (tok->held) { |
| 236 | if (pthread_equal(tok->tid, pthread_self())) { |
| 237 | tok->held++; |
| 238 | pthread_mutex_unlock(&tok->lock); |
| 239 | return; |
| 240 | } else { |
| 241 | tok->waiters++; |
| 242 | while (tok->held) |
| 243 | pthread_cond_wait(&tok->cv, &tok->lock); |
| 244 | tok->waiters--; |
| 245 | } |
| 246 | } |
| 247 | tok->tid = pthread_self(); |
| 248 | tok->held = 1; |
| 249 | pthread_mutex_unlock(&tok->lock); |
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 | EnterCriticalSection(&tok->cs); |
| 252 | ++tok->count; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 253 | #endif |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * xmlRMutexUnlock: |
| 258 | * @tok: the reentrant mutex |
| 259 | * |
| 260 | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
| 261 | */ |
| 262 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 263 | xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 264 | { |
| 265 | #ifdef HAVE_PTHREAD_H |
| 266 | pthread_mutex_lock(&tok->lock); |
| 267 | tok->held--; |
| 268 | if (tok->held == 0) { |
| 269 | if (tok->waiters) |
| 270 | pthread_cond_signal(&tok->cv); |
| 271 | tok->tid = 0; |
| 272 | } |
| 273 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 274 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 275 | if (!--tok->count) |
| 276 | LeaveCriticalSection(&tok->cs); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 277 | #endif |
| 278 | } |
| 279 | |
| 280 | /************************************************************************ |
| 281 | * * |
| 282 | * Per thread global state handling * |
| 283 | * * |
| 284 | ************************************************************************/ |
| 285 | |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 286 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 287 | /** |
| 288 | * xmlFreeGlobalState: |
| 289 | * @state: a thread global state |
| 290 | * |
| 291 | * xmlFreeGlobalState() is called when a thread terminates with a non-NULL |
| 292 | * global state. It is is used here to reclaim memory resources. |
| 293 | */ |
| 294 | static void |
| 295 | xmlFreeGlobalState(void *state) |
| 296 | { |
| 297 | free(state); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * xmlNewGlobalState: |
| 302 | * |
| 303 | * xmlNewGlobalState() allocates a global state. This structure is used to |
| 304 | * hold all data for use by a thread when supporting backwards compatibility |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 305 | * of libxml2 to pre-thread-safe behaviour. |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 306 | * |
| 307 | * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error |
| 308 | */ |
| 309 | static xmlGlobalStatePtr |
| 310 | xmlNewGlobalState(void) |
| 311 | { |
| 312 | xmlGlobalState *gs; |
| 313 | |
| 314 | gs = malloc(sizeof(xmlGlobalState)); |
| 315 | if (gs == NULL) |
| 316 | return(NULL); |
| 317 | |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 318 | memset(gs, 0, sizeof(xmlGlobalState)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 319 | xmlInitializeGlobalState(gs); |
| 320 | return (gs); |
| 321 | } |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 322 | #endif /* LIBXML_THREAD_ENABLED */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 323 | |
| 324 | |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 325 | #ifdef HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 326 | #if !defined(HAVE_COMPILER_TLS) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 327 | typedef struct _xmlGlobalStateCleanupHelperParams |
| 328 | { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 329 | HANDLE thread; |
| 330 | void *memory; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 331 | } xmlGlobalStateCleanupHelperParams; |
| 332 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 333 | static void xmlGlobalStateCleanupHelper (void *p) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 334 | { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 335 | xmlGlobalStateCleanupHelperParams *params = (xmlGlobalStateCleanupHelperParams *) p; |
| 336 | WaitForSingleObject(params->thread, INFINITE); |
| 337 | CloseHandle(params->thread); |
| 338 | xmlFreeGlobalState(params->memory); |
| 339 | free(params); |
| 340 | _endthread(); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 341 | } |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 342 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 343 | #endif /* HAVE_WIN32_THREADS */ |
| 344 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 345 | /** |
| 346 | * xmlGetGlobalState: |
| 347 | * |
| 348 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 349 | * |
| 350 | * Returns the thread global state or NULL in case of error |
| 351 | */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 352 | xmlGlobalStatePtr |
| 353 | xmlGetGlobalState(void) |
| 354 | { |
| 355 | #ifdef HAVE_PTHREAD_H |
| 356 | xmlGlobalState *globalval; |
| 357 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 358 | pthread_once(&once_control, xmlOnceInit); |
| 359 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 360 | if ((globalval = (xmlGlobalState *) |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 361 | pthread_getspecific(globalkey)) == NULL) { |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 362 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 363 | |
| 364 | pthread_setspecific(globalkey, tsd); |
| 365 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 366 | } |
| 367 | return (globalval); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 368 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 369 | #if defined(HAVE_COMPILER_TLS) |
| 370 | if (!tlstate_inited) { |
| 371 | tlstate_inited = 1; |
| 372 | xmlInitializeGlobalState(&tlstate); |
| 373 | } |
| 374 | return &tlstate; |
| 375 | #else /* HAVE_COMPILER_TLS */ |
| 376 | xmlGlobalState *globalval; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 377 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 378 | if (run_once_init) { |
| 379 | run_once_init = 0; |
| 380 | xmlOnceInit(); |
| 381 | } |
| 382 | if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) { |
| 383 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 384 | xmlGlobalStateCleanupHelperParams *p = |
| 385 | (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams)); |
| 386 | p->memory = tsd; |
| 387 | DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 388 | GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 389 | TlsSetValue(globalkey, tsd); |
| 390 | _beginthread(xmlGlobalStateCleanupHelper, 0, p); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 391 | |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 392 | return (tsd); |
| 393 | } |
| 394 | return (globalval); |
| 395 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 396 | #else |
| 397 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 398 | #endif |
| 399 | } |
| 400 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 401 | /************************************************************************ |
| 402 | * * |
| 403 | * Library wide thread interfaces * |
| 404 | * * |
| 405 | ************************************************************************/ |
| 406 | |
| 407 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 408 | * xmlGetThreadId: |
| 409 | * |
| 410 | * xmlGetThreadId() find the current thread ID number |
| 411 | * |
| 412 | * Returns the current thread ID number |
| 413 | */ |
| 414 | int |
| 415 | xmlGetThreadId(void) |
| 416 | { |
| 417 | #ifdef HAVE_PTHREAD_H |
| 418 | return((int) pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 419 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 420 | return GetCurrentThreadId(); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 421 | #else |
| 422 | return((int) 0); |
| 423 | #endif |
| 424 | } |
| 425 | |
| 426 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 427 | * xmlIsMainThread: |
| 428 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 429 | * xmlIsMainThread() check whether the current thread is the main thread. |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 430 | * |
| 431 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 432 | */ |
| 433 | int |
| 434 | xmlIsMainThread(void) |
| 435 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 436 | #ifdef HAVE_PTHREAD_H |
| 437 | pthread_once(&once_control, xmlOnceInit); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 438 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 439 | if (run_once_init) { |
| 440 | run_once_init = 0; |
| 441 | xmlOnceInit (); |
| 442 | } |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 443 | #endif |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 444 | |
| 445 | #ifdef DEBUG_THREADS |
| 446 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 447 | #endif |
| 448 | #ifdef HAVE_PTHREAD_H |
| 449 | return(mainthread == pthread_self()); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 450 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 451 | return(mainthread == GetCurrentThreadId ()); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 452 | #else |
| 453 | return(1); |
| 454 | #endif |
| 455 | } |
| 456 | |
| 457 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 458 | * xmlLockLibrary: |
| 459 | * |
| 460 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 461 | * library. |
| 462 | */ |
| 463 | void |
| 464 | xmlLockLibrary(void) |
| 465 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 466 | #ifdef DEBUG_THREADS |
| 467 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 468 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 469 | xmlRMutexLock(xmlLibraryLock); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * xmlUnlockLibrary: |
| 474 | * |
| 475 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 476 | * library. |
| 477 | */ |
| 478 | void |
| 479 | xmlUnlockLibrary(void) |
| 480 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 481 | #ifdef DEBUG_THREADS |
| 482 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 483 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 484 | xmlRMutexUnlock(xmlLibraryLock); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * xmlInitThreads: |
| 489 | * |
| 490 | * xmlInitThreads() is used to to initialize all the thread related |
| 491 | * data of the libxml2 library. |
| 492 | */ |
| 493 | void |
| 494 | xmlInitThreads(void) |
| 495 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 496 | #ifdef DEBUG_THREADS |
| 497 | xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n"); |
| 498 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * xmlCleanupThreads: |
| 503 | * |
| 504 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 505 | * data of the libxml2 library once processing has ended. |
| 506 | */ |
| 507 | void |
| 508 | xmlCleanupThreads(void) |
| 509 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 510 | #ifdef DEBUG_THREADS |
| 511 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 512 | #endif |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 513 | } |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 514 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 515 | /** |
| 516 | * xmlOnceInit |
| 517 | * |
| 518 | * xmlOnceInit() is used to initialize the value of mainthread for use |
| 519 | * in other routines. This function should only be called using |
| 520 | * pthread_once() in association with the once_control variable to ensure |
| 521 | * that the function is only called once. See man pthread_once for more |
| 522 | * details. |
| 523 | */ |
| 524 | static void |
| 525 | xmlOnceInit(void) { |
| 526 | #ifdef HAVE_PTHREAD_H |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 527 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 528 | mainthread = pthread_self(); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 529 | #endif |
| 530 | |
| 531 | #if defined(HAVE_WIN32_THREADS) |
| 532 | #if !defined(HAVE_COMPILER_TLS) |
| 533 | globalkey = TlsAlloc(); |
| 534 | #endif |
| 535 | mainthread = GetCurrentThreadId(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 536 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 537 | } |