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 | |
| 10 | #include "libxml.h" |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <libxml/threads.h> |
| 15 | #include <libxml/globals.h> |
| 16 | |
| 17 | #ifdef HAVE_SYS_TYPES_H |
| 18 | #include <sys/types.h> |
| 19 | #endif |
| 20 | #ifdef HAVE_UNISTD_H |
| 21 | #include <unistd.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_STDLIB_H |
| 24 | #include <stdlib.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_PTHREAD_H |
| 27 | #include <pthread.h> |
| 28 | #endif |
| 29 | |
| 30 | #if defined(SOLARIS) |
| 31 | #include <note.h> |
| 32 | #endif |
| 33 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 34 | /* #define DEBUG_THREADS */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree |
| 38 | * to avoid some crazyness since xmlMalloc/xmlFree may actually |
| 39 | * be hosted on allocated blocks needing them for the allocation ... |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * xmlMutex are a simple mutual exception locks |
| 44 | */ |
| 45 | struct _xmlMutex { |
| 46 | #ifdef HAVE_PTHREAD_H |
| 47 | pthread_mutex_t lock; |
| 48 | #else |
| 49 | int empty; |
| 50 | #endif |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * xmlRMutex are reentrant mutual exception locks |
| 55 | */ |
| 56 | struct _xmlRMutex { |
| 57 | #ifdef HAVE_PTHREAD_H |
| 58 | pthread_mutex_t lock; |
| 59 | unsigned int held; |
| 60 | unsigned int waiters; |
| 61 | pthread_t tid; |
| 62 | pthread_cond_t cv; |
| 63 | #else |
| 64 | int empty; |
| 65 | #endif |
| 66 | }; |
| 67 | /* |
| 68 | * This module still has some internal static data. |
| 69 | * - xmlLibraryLock a global lock |
| 70 | * - globalkey used for per-thread data |
| 71 | * - keylock protecting globalkey |
| 72 | * - keyonce to mark initialization of globalkey |
| 73 | */ |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 74 | |
| 75 | static int initialized = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 76 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | 5ee57fc | 2001-10-15 10:46:16 +0000 | [diff] [blame] | 77 | static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 78 | static pthread_key_t globalkey; |
| 79 | static int keyonce = 0; |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 80 | static pthread_t mainthread; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 81 | #endif |
| 82 | static xmlRMutexPtr xmlLibraryLock = NULL; |
| 83 | |
| 84 | #if defined(SOLARIS) |
| 85 | NOTE(DATA_READABLE_WITHOUT_LOCK(keyonce)) |
| 86 | #endif |
| 87 | |
| 88 | /** |
| 89 | * xmlMutexPtr: |
| 90 | * |
| 91 | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
| 92 | * synchronizing access to data. |
| 93 | * |
| 94 | * Returns a new simple mutex pointer or NULL in case of error |
| 95 | */ |
| 96 | xmlMutexPtr |
| 97 | xmlNewMutex(void) |
| 98 | { |
| 99 | xmlMutexPtr tok; |
| 100 | |
| 101 | if ((tok = malloc(sizeof(xmlMutex))) == NULL) |
| 102 | return (NULL); |
| 103 | #ifdef HAVE_PTHREAD_H |
| 104 | pthread_mutex_init(&tok->lock, NULL); |
| 105 | #endif |
| 106 | return (tok); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * xmlFreeMutex: |
| 111 | * @tok: the simple mutex |
| 112 | * |
| 113 | * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token |
| 114 | * struct. |
| 115 | */ |
| 116 | void |
| 117 | xmlFreeMutex(xmlMutexPtr tok) |
| 118 | { |
| 119 | #ifdef HAVE_PTHREAD_H |
| 120 | pthread_mutex_destroy(&tok->lock); |
| 121 | #endif |
| 122 | free(tok); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * xmlMutexLock: |
| 127 | * @tok: the simple mutex |
| 128 | * |
| 129 | * xmlMutexLock() is used to lock a libxml2 token. |
| 130 | */ |
| 131 | void |
| 132 | xmlMutexLock(xmlMutexPtr tok) |
| 133 | { |
| 134 | #ifdef HAVE_PTHREAD_H |
| 135 | pthread_mutex_lock(&tok->lock); |
| 136 | #endif |
| 137 | |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * xmlMutexUnlock: |
| 142 | * @tok: the simple mutex |
| 143 | * |
| 144 | * xmlMutexUnlock() is used to unlock a libxml2 token. |
| 145 | */ |
| 146 | void |
| 147 | xmlMutexUnlock(xmlMutexPtr tok) |
| 148 | { |
| 149 | #ifdef HAVE_PTHREAD_H |
| 150 | pthread_mutex_unlock(&tok->lock); |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * xmlRNewMutex: |
| 156 | * |
| 157 | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
| 158 | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
| 159 | * for synchronizing access to data structures that may be manipulated in a |
| 160 | * recursive fashion. |
| 161 | * |
| 162 | * Returns the new reentrant mutex pointer or NULL in case of error |
| 163 | */ |
| 164 | xmlRMutexPtr |
| 165 | xmlNewRMutex(void) |
| 166 | { |
| 167 | xmlRMutexPtr tok; |
| 168 | |
| 169 | if ((tok = malloc(sizeof(xmlRMutex))) == NULL) |
| 170 | return (NULL); |
| 171 | #ifdef HAVE_PTHREAD_H |
| 172 | pthread_mutex_init(&tok->lock, NULL); |
| 173 | tok->held = 0; |
| 174 | tok->waiters = 0; |
| 175 | #endif |
| 176 | return (tok); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * xmlRFreeMutex: |
| 181 | * @tok: the reentrant mutex |
| 182 | * |
| 183 | * xmlRFreeMutex() is used to reclaim resources associated with a |
| 184 | * reentrant mutex. |
| 185 | */ |
| 186 | void |
| 187 | xmlFreeRMutex(xmlRMutexPtr tok) |
| 188 | { |
| 189 | #ifdef HAVE_PTHREAD_H |
| 190 | pthread_mutex_destroy(&tok->lock); |
| 191 | #endif |
| 192 | free(tok); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * xmlRMutexLock: |
| 197 | * @tok: the reentrant mutex |
| 198 | * |
| 199 | * xmlRMutexLock() is used to lock a libxml2 token_r. |
| 200 | */ |
| 201 | void |
| 202 | xmlRMutexLock(xmlRMutexPtr tok) |
| 203 | { |
| 204 | #ifdef HAVE_PTHREAD_H |
| 205 | pthread_mutex_lock(&tok->lock); |
| 206 | if (tok->held) { |
| 207 | if (pthread_equal(tok->tid, pthread_self())) { |
| 208 | tok->held++; |
| 209 | pthread_mutex_unlock(&tok->lock); |
| 210 | return; |
| 211 | } else { |
| 212 | tok->waiters++; |
| 213 | while (tok->held) |
| 214 | pthread_cond_wait(&tok->cv, &tok->lock); |
| 215 | tok->waiters--; |
| 216 | } |
| 217 | } |
| 218 | tok->tid = pthread_self(); |
| 219 | tok->held = 1; |
| 220 | pthread_mutex_unlock(&tok->lock); |
| 221 | #endif |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * xmlRMutexUnlock: |
| 226 | * @tok: the reentrant mutex |
| 227 | * |
| 228 | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
| 229 | */ |
| 230 | void |
| 231 | xmlRMutexUnlock(xmlRMutexPtr tok) |
| 232 | { |
| 233 | #ifdef HAVE_PTHREAD_H |
| 234 | pthread_mutex_lock(&tok->lock); |
| 235 | tok->held--; |
| 236 | if (tok->held == 0) { |
| 237 | if (tok->waiters) |
| 238 | pthread_cond_signal(&tok->cv); |
| 239 | tok->tid = 0; |
| 240 | } |
| 241 | pthread_mutex_unlock(&tok->lock); |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | /************************************************************************ |
| 246 | * * |
| 247 | * Per thread global state handling * |
| 248 | * * |
| 249 | ************************************************************************/ |
| 250 | |
| 251 | /** |
| 252 | * xmlFreeGlobalState: |
| 253 | * @state: a thread global state |
| 254 | * |
| 255 | * xmlFreeGlobalState() is called when a thread terminates with a non-NULL |
| 256 | * global state. It is is used here to reclaim memory resources. |
| 257 | */ |
| 258 | static void |
| 259 | xmlFreeGlobalState(void *state) |
| 260 | { |
| 261 | free(state); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * xmlNewGlobalState: |
| 266 | * |
| 267 | * xmlNewGlobalState() allocates a global state. This structure is used to |
| 268 | * hold all data for use by a thread when supporting backwards compatibility |
| 269 | * of libmxml2 to pre-thread-safe behaviour. |
| 270 | * |
| 271 | * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error |
| 272 | */ |
| 273 | static xmlGlobalStatePtr |
| 274 | xmlNewGlobalState(void) |
| 275 | { |
| 276 | xmlGlobalState *gs; |
| 277 | |
| 278 | gs = malloc(sizeof(xmlGlobalState)); |
| 279 | if (gs == NULL) |
| 280 | return(NULL); |
| 281 | |
| 282 | memset(gs, 0, sizeof(gs)); |
| 283 | xmlInitializeGlobalState(gs); |
| 284 | return (gs); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * xmlGetGlobalState: |
| 290 | * |
| 291 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 292 | * keyonce will only be set once during a library invocation and is used |
| 293 | * to create globalkey, the key used to store each thread's TSD. |
| 294 | * |
| 295 | * Note: it should not be called for the "main" thread as this thread uses |
| 296 | * the existing global variables defined in the library. |
| 297 | * |
| 298 | * Returns the thread global state or NULL in case of error |
| 299 | */ |
| 300 | xmlGlobalStatePtr |
| 301 | xmlGetGlobalState(void) |
| 302 | { |
| 303 | #ifdef HAVE_PTHREAD_H |
| 304 | xmlGlobalState *globalval; |
| 305 | |
| 306 | if (keyonce == 0) { |
| 307 | (void) pthread_mutex_lock(&keylock); |
| 308 | if (keyonce == 0) { |
| 309 | keyonce++; |
| 310 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
| 311 | } |
| 312 | (void) pthread_mutex_unlock(&keylock); |
| 313 | } |
| 314 | if ((globalval = (xmlGlobalState *) |
| 315 | pthread_getspecific(globalkey)) == NULL) { |
| 316 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 317 | |
| 318 | pthread_setspecific(globalkey, tsd); |
| 319 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 320 | } |
| 321 | return (globalval); |
| 322 | #else |
| 323 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 324 | #endif |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /************************************************************************ |
| 329 | * * |
| 330 | * Library wide thread interfaces * |
| 331 | * * |
| 332 | ************************************************************************/ |
| 333 | |
| 334 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 335 | * xmlGetThreadId: |
| 336 | * |
| 337 | * xmlGetThreadId() find the current thread ID number |
| 338 | * |
| 339 | * Returns the current thread ID number |
| 340 | */ |
| 341 | int |
| 342 | xmlGetThreadId(void) |
| 343 | { |
| 344 | #ifdef HAVE_PTHREAD_H |
| 345 | return((int) pthread_self()); |
| 346 | #else |
| 347 | return((int) 0); |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 352 | * xmlIsMainThread: |
| 353 | * |
| 354 | * xmlIsMainThread() check wether the current thread is the main thread. |
| 355 | * |
| 356 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 357 | */ |
| 358 | int |
| 359 | xmlIsMainThread(void) |
| 360 | { |
| 361 | if (!initialized) |
| 362 | xmlInitThreads(); |
| 363 | |
| 364 | #ifdef DEBUG_THREADS |
| 365 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 366 | #endif |
| 367 | #ifdef HAVE_PTHREAD_H |
| 368 | return(mainthread == pthread_self()); |
| 369 | #else |
| 370 | return(1); |
| 371 | #endif |
| 372 | } |
| 373 | |
| 374 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 375 | * xmlLockLibrary: |
| 376 | * |
| 377 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 378 | * library. |
| 379 | */ |
| 380 | void |
| 381 | xmlLockLibrary(void) |
| 382 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 383 | #ifdef DEBUG_THREADS |
| 384 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 385 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 386 | xmlRMutexLock(xmlLibraryLock); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * xmlUnlockLibrary: |
| 391 | * |
| 392 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 393 | * library. |
| 394 | */ |
| 395 | void |
| 396 | xmlUnlockLibrary(void) |
| 397 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 398 | #ifdef DEBUG_THREADS |
| 399 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 400 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 401 | xmlRMutexUnlock(xmlLibraryLock); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * xmlInitThreads: |
| 406 | * |
| 407 | * xmlInitThreads() is used to to initialize all the thread related |
| 408 | * data of the libxml2 library. |
| 409 | */ |
| 410 | void |
| 411 | xmlInitThreads(void) |
| 412 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 413 | if (initialized != 0) |
| 414 | return; |
| 415 | |
| 416 | #ifdef DEBUG_THREADS |
| 417 | xmlGenericError(xmlGenericErrorContext, "xmlInitThreads()\n"); |
| 418 | #endif |
| 419 | |
| 420 | #ifdef HAVE_PTHREAD_H |
| 421 | mainthread = pthread_self(); |
| 422 | #endif |
| 423 | |
| 424 | initialized = 1; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * xmlCleanupThreads: |
| 429 | * |
| 430 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 431 | * data of the libxml2 library once processing has ended. |
| 432 | */ |
| 433 | void |
| 434 | xmlCleanupThreads(void) |
| 435 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 436 | if (initialized == 0) |
| 437 | return; |
| 438 | |
| 439 | #ifdef DEBUG_THREADS |
| 440 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 441 | #endif |
| 442 | |
| 443 | initialized = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 444 | } |