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