Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 1 | /** |
Daniel Veillard | dee2348 | 2008-04-11 12:58:43 +0000 | [diff] [blame] | 2 | * threads.c: set of generic threading related routines |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 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> |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 29 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 30 | #include <windows.h> |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 31 | #ifndef HAVE_COMPILER_TLS |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 32 | #include <process.h> |
| 33 | #endif |
| 34 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 35 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 36 | #ifdef HAVE_BEOS_THREADS |
| 37 | #include <OS.h> |
| 38 | #include <TLS.h> |
| 39 | #endif |
| 40 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 41 | #if defined(SOLARIS) |
| 42 | #include <note.h> |
| 43 | #endif |
| 44 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 45 | /* #define DEBUG_THREADS */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 46 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 47 | #ifdef HAVE_PTHREAD_H |
| 48 | |
| 49 | static int libxml_is_threaded = -1; |
Michael Heimpold | fff8a6b | 2014-12-22 11:12:12 +0800 | [diff] [blame] | 50 | #if defined(__GNUC__) && defined(__GLIBC__) |
Nick Wellnhofer | 2cdaaab | 2017-09-14 21:30:51 +0200 | [diff] [blame^] | 51 | #ifdef __linux__ |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 52 | #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) || (__GNUC__ > 3) |
Nick Wellnhofer | 1f09aea | 2017-06-17 15:05:34 +0200 | [diff] [blame] | 53 | #pragma weak pthread_once |
| 54 | #pragma weak pthread_getspecific |
| 55 | #pragma weak pthread_setspecific |
| 56 | #pragma weak pthread_key_create |
| 57 | #pragma weak pthread_key_delete |
| 58 | #pragma weak pthread_mutex_init |
| 59 | #pragma weak pthread_mutex_destroy |
| 60 | #pragma weak pthread_mutex_lock |
| 61 | #pragma weak pthread_mutex_unlock |
| 62 | #pragma weak pthread_cond_init |
| 63 | #pragma weak pthread_cond_destroy |
| 64 | #pragma weak pthread_cond_wait |
| 65 | #pragma weak pthread_equal |
| 66 | #pragma weak pthread_self |
| 67 | #pragma weak pthread_key_create |
| 68 | #pragma weak pthread_key_delete |
| 69 | #pragma weak pthread_cond_signal |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 70 | #endif |
Nick Wellnhofer | 2cdaaab | 2017-09-14 21:30:51 +0200 | [diff] [blame^] | 71 | #endif /* __linux__ */ |
Michael Heimpold | fff8a6b | 2014-12-22 11:12:12 +0800 | [diff] [blame] | 72 | #endif /* defined(__GNUC__) && defined(__GLIBC__) */ |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 73 | #endif /* HAVE_PTHREAD_H */ |
| 74 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 75 | /* |
| 76 | * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree |
| 77 | * to avoid some crazyness since xmlMalloc/xmlFree may actually |
| 78 | * be hosted on allocated blocks needing them for the allocation ... |
| 79 | */ |
| 80 | |
| 81 | /* |
| 82 | * xmlMutex are a simple mutual exception locks |
| 83 | */ |
| 84 | struct _xmlMutex { |
| 85 | #ifdef HAVE_PTHREAD_H |
| 86 | pthread_mutex_t lock; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 87 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 88 | HANDLE mutex; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 89 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 90 | sem_id sem; |
| 91 | thread_id tid; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 92 | #else |
| 93 | int empty; |
| 94 | #endif |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * xmlRMutex are reentrant mutual exception locks |
| 99 | */ |
| 100 | struct _xmlRMutex { |
| 101 | #ifdef HAVE_PTHREAD_H |
| 102 | pthread_mutex_t lock; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 103 | unsigned int held; |
| 104 | unsigned int waiters; |
| 105 | pthread_t tid; |
| 106 | pthread_cond_t cv; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 107 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 108 | CRITICAL_SECTION cs; |
| 109 | unsigned int count; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 110 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 111 | xmlMutexPtr lock; |
| 112 | thread_id tid; |
| 113 | int32 count; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 114 | #else |
| 115 | int empty; |
| 116 | #endif |
| 117 | }; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 118 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 119 | /* |
| 120 | * This module still has some internal static data. |
| 121 | * - xmlLibraryLock a global lock |
| 122 | * - globalkey used for per-thread data |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 123 | */ |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 124 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 125 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 126 | static pthread_key_t globalkey; |
| 127 | static pthread_t mainthread; |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 128 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
Friedrich Haubensak | 3f6cfbd | 2012-09-12 17:34:53 +0200 | [diff] [blame] | 129 | static pthread_once_t once_control_init = PTHREAD_ONCE_INIT; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 130 | static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 131 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 132 | #if defined(HAVE_COMPILER_TLS) |
| 133 | static __declspec(thread) xmlGlobalState tlstate; |
| 134 | static __declspec(thread) int tlstate_inited = 0; |
| 135 | #else /* HAVE_COMPILER_TLS */ |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 136 | static DWORD globalkey = TLS_OUT_OF_INDEXES; |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 137 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 138 | static DWORD mainthread; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 139 | static struct { |
Daniel Veillard | 36616dd | 2005-02-25 07:31:49 +0000 | [diff] [blame] | 140 | DWORD done; |
| 141 | DWORD control; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 142 | } run_once = { 0, 0}; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 143 | static volatile LPCRITICAL_SECTION global_init_lock = NULL; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 144 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 145 | /* endif HAVE_WIN32_THREADS */ |
| 146 | #elif defined HAVE_BEOS_THREADS |
| 147 | int32 globalkey = 0; |
| 148 | thread_id mainthread = 0; |
| 149 | int32 run_once_init = 0; |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 150 | static int32 global_init_lock = -1; |
| 151 | static vint32 global_init_count = 0; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 152 | #endif |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 153 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 154 | static xmlRMutexPtr xmlLibraryLock = NULL; |
| 155 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 156 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 157 | static void xmlOnceInit(void); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 158 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 159 | |
| 160 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 161 | * xmlNewMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 162 | * |
| 163 | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
| 164 | * synchronizing access to data. |
| 165 | * |
| 166 | * Returns a new simple mutex pointer or NULL in case of error |
| 167 | */ |
| 168 | xmlMutexPtr |
| 169 | xmlNewMutex(void) |
| 170 | { |
| 171 | xmlMutexPtr tok; |
| 172 | |
| 173 | if ((tok = malloc(sizeof(xmlMutex))) == NULL) |
| 174 | return (NULL); |
| 175 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 176 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 177 | pthread_mutex_init(&tok->lock, NULL); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 178 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 179 | tok->mutex = CreateMutex(NULL, FALSE, NULL); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 180 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 181 | if ((tok->sem = create_sem(1, "xmlMutex")) < B_OK) { |
| 182 | free(tok); |
| 183 | return NULL; |
| 184 | } |
| 185 | tok->tid = -1; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 186 | #endif |
| 187 | return (tok); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * xmlFreeMutex: |
| 192 | * @tok: the simple mutex |
| 193 | * |
| 194 | * xmlFreeMutex() is used to reclaim resources associated with a libxml2 token |
| 195 | * struct. |
| 196 | */ |
| 197 | void |
| 198 | xmlFreeMutex(xmlMutexPtr tok) |
| 199 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 200 | if (tok == NULL) |
| 201 | return; |
Daniel Veillard | df101d8 | 2003-07-08 14:03:36 +0000 | [diff] [blame] | 202 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 203 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 204 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 205 | pthread_mutex_destroy(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 206 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 207 | CloseHandle(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 208 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 209 | delete_sem(tok->sem); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 210 | #endif |
| 211 | free(tok); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * xmlMutexLock: |
| 216 | * @tok: the simple mutex |
| 217 | * |
| 218 | * xmlMutexLock() is used to lock a libxml2 token. |
| 219 | */ |
| 220 | void |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 221 | xmlMutexLock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 222 | { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 223 | if (tok == NULL) |
| 224 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 225 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 226 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 227 | pthread_mutex_lock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 228 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 229 | WaitForSingleObject(tok->mutex, INFINITE); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 230 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 231 | if (acquire_sem(tok->sem) != B_NO_ERROR) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 232 | #ifdef DEBUG_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 233 | xmlGenericError(xmlGenericErrorContext, |
| 234 | "xmlMutexLock():BeOS:Couldn't aquire semaphore\n"); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 235 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 236 | } |
| 237 | tok->tid = find_thread(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 238 | #endif |
| 239 | |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * xmlMutexUnlock: |
| 244 | * @tok: the simple mutex |
| 245 | * |
| 246 | * xmlMutexUnlock() is used to unlock a libxml2 token. |
| 247 | */ |
| 248 | void |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 249 | xmlMutexUnlock(xmlMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 250 | { |
Daniel Veillard | 5805be2 | 2003-08-28 08:03:23 +0000 | [diff] [blame] | 251 | if (tok == NULL) |
| 252 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 253 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 254 | if (libxml_is_threaded != 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 255 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 256 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 257 | ReleaseMutex(tok->mutex); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 258 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 259 | if (tok->tid == find_thread(NULL)) { |
| 260 | tok->tid = -1; |
| 261 | release_sem(tok->sem); |
| 262 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 263 | #endif |
| 264 | } |
| 265 | |
| 266 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 267 | * xmlNewRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 268 | * |
| 269 | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
| 270 | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
| 271 | * for synchronizing access to data structures that may be manipulated in a |
| 272 | * recursive fashion. |
| 273 | * |
| 274 | * Returns the new reentrant mutex pointer or NULL in case of error |
| 275 | */ |
| 276 | xmlRMutexPtr |
| 277 | xmlNewRMutex(void) |
| 278 | { |
| 279 | xmlRMutexPtr tok; |
| 280 | |
| 281 | if ((tok = malloc(sizeof(xmlRMutex))) == NULL) |
| 282 | return (NULL); |
| 283 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 284 | if (libxml_is_threaded != 0) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 285 | pthread_mutex_init(&tok->lock, NULL); |
| 286 | tok->held = 0; |
| 287 | tok->waiters = 0; |
| 288 | pthread_cond_init(&tok->cv, NULL); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 289 | } |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 290 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 291 | InitializeCriticalSection(&tok->cs); |
| 292 | tok->count = 0; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 293 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 294 | if ((tok->lock = xmlNewMutex()) == NULL) { |
| 295 | free(tok); |
| 296 | return NULL; |
| 297 | } |
| 298 | tok->count = 0; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 299 | #endif |
| 300 | return (tok); |
| 301 | } |
| 302 | |
| 303 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 304 | * xmlFreeRMutex: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 305 | * @tok: the reentrant mutex |
| 306 | * |
| 307 | * xmlRFreeMutex() is used to reclaim resources associated with a |
| 308 | * reentrant mutex. |
| 309 | */ |
| 310 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 311 | xmlFreeRMutex(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 312 | { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 313 | if (tok == NULL) |
| 314 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 315 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | a8b5413 | 2006-06-29 11:50:18 +0000 | [diff] [blame] | 316 | if (libxml_is_threaded != 0) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 317 | pthread_mutex_destroy(&tok->lock); |
| 318 | pthread_cond_destroy(&tok->cv); |
Daniel Veillard | a8b5413 | 2006-06-29 11:50:18 +0000 | [diff] [blame] | 319 | } |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 320 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 321 | DeleteCriticalSection(&tok->cs); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 322 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 323 | xmlFreeMutex(tok->lock); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 324 | #endif |
| 325 | free(tok); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * xmlRMutexLock: |
| 330 | * @tok: the reentrant mutex |
| 331 | * |
| 332 | * xmlRMutexLock() is used to lock a libxml2 token_r. |
| 333 | */ |
| 334 | void |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 335 | xmlRMutexLock(xmlRMutexPtr tok) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 336 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 337 | if (tok == NULL) |
| 338 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 339 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 340 | if (libxml_is_threaded == 0) |
| 341 | return; |
| 342 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 343 | pthread_mutex_lock(&tok->lock); |
| 344 | if (tok->held) { |
| 345 | if (pthread_equal(tok->tid, pthread_self())) { |
| 346 | tok->held++; |
| 347 | pthread_mutex_unlock(&tok->lock); |
| 348 | return; |
| 349 | } else { |
| 350 | tok->waiters++; |
| 351 | while (tok->held) |
| 352 | pthread_cond_wait(&tok->cv, &tok->lock); |
| 353 | tok->waiters--; |
| 354 | } |
| 355 | } |
| 356 | tok->tid = pthread_self(); |
| 357 | tok->held = 1; |
| 358 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 359 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 360 | EnterCriticalSection(&tok->cs); |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 361 | tok->count++; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 362 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 363 | if (tok->lock->tid == find_thread(NULL)) { |
| 364 | tok->count++; |
| 365 | return; |
| 366 | } else { |
| 367 | xmlMutexLock(tok->lock); |
| 368 | tok->count = 1; |
| 369 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 370 | #endif |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * xmlRMutexUnlock: |
| 375 | * @tok: the reentrant mutex |
| 376 | * |
| 377 | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
| 378 | */ |
| 379 | void |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 380 | xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 381 | { |
Daniel Veillard | 5f1e1f8 | 2003-09-11 23:35:09 +0000 | [diff] [blame] | 382 | if (tok == NULL) |
| 383 | return; |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 384 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 385 | if (libxml_is_threaded == 0) |
| 386 | return; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 387 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 388 | pthread_mutex_lock(&tok->lock); |
| 389 | tok->held--; |
| 390 | if (tok->held == 0) { |
| 391 | if (tok->waiters) |
| 392 | pthread_cond_signal(&tok->cv); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 393 | memset(&tok->tid, 0, sizeof(tok->tid)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 394 | } |
| 395 | pthread_mutex_unlock(&tok->lock); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 396 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 397 | if (tok->count > 0) { |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 398 | tok->count--; |
Steve Nairn | 620a706 | 2015-03-03 19:40:06 +0800 | [diff] [blame] | 399 | LeaveCriticalSection(&tok->cs); |
Daniel Veillard | 8854e46 | 2014-10-13 15:03:58 +0800 | [diff] [blame] | 400 | } |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 401 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 402 | if (tok->lock->tid == find_thread(NULL)) { |
| 403 | tok->count--; |
| 404 | if (tok->count == 0) { |
| 405 | xmlMutexUnlock(tok->lock); |
| 406 | } |
| 407 | return; |
| 408 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 409 | #endif |
| 410 | } |
| 411 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 412 | /** |
| 413 | * xmlGlobalInitMutexLock |
| 414 | * |
| 415 | * Makes sure that the global initialization mutex is initialized and |
| 416 | * locks it. |
| 417 | */ |
| 418 | void |
| 419 | __xmlGlobalInitMutexLock(void) |
| 420 | { |
| 421 | /* Make sure the global init lock is initialized and then lock it. */ |
| 422 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 423 | /* The mutex is statically initialized, so we just lock it. */ |
Daniel Richard G | 5706b6d | 2012-08-06 11:32:54 +0800 | [diff] [blame] | 424 | if (pthread_mutex_lock != NULL) |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 425 | pthread_mutex_lock(&global_init_lock); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 426 | #elif defined HAVE_WIN32_THREADS |
| 427 | LPCRITICAL_SECTION cs; |
| 428 | |
| 429 | /* Create a new critical section */ |
| 430 | if (global_init_lock == NULL) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 431 | cs = malloc(sizeof(CRITICAL_SECTION)); |
| 432 | if (cs == NULL) { |
| 433 | xmlGenericError(xmlGenericErrorContext, |
| 434 | "xmlGlobalInitMutexLock: out of memory\n"); |
| 435 | return; |
| 436 | } |
| 437 | InitializeCriticalSection(cs); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 438 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 439 | /* Swap it into the global_init_lock */ |
Rob Richards | e967f0b | 2007-06-08 19:36:04 +0000 | [diff] [blame] | 440 | #ifdef InterlockedCompareExchangePointer |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 441 | InterlockedCompareExchangePointer(&global_init_lock, cs, NULL); |
| 442 | #else /* Use older void* version */ |
| 443 | InterlockedCompareExchange((void **) &global_init_lock, |
| 444 | (void *) cs, NULL); |
Rob Richards | e967f0b | 2007-06-08 19:36:04 +0000 | [diff] [blame] | 445 | #endif /* InterlockedCompareExchangePointer */ |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 446 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 447 | /* If another thread successfully recorded its critical |
| 448 | * section in the global_init_lock then discard the one |
| 449 | * allocated by this thread. */ |
| 450 | if (global_init_lock != cs) { |
| 451 | DeleteCriticalSection(cs); |
| 452 | free(cs); |
| 453 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Lock the chosen critical section */ |
| 457 | EnterCriticalSection(global_init_lock); |
| 458 | #elif defined HAVE_BEOS_THREADS |
| 459 | int32 sem; |
| 460 | |
| 461 | /* Allocate a new semaphore */ |
| 462 | sem = create_sem(1, "xmlGlobalinitMutex"); |
| 463 | |
| 464 | while (global_init_lock == -1) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 465 | if (atomic_add(&global_init_count, 1) == 0) { |
| 466 | global_init_lock = sem; |
| 467 | } else { |
| 468 | snooze(1); |
| 469 | atomic_add(&global_init_count, -1); |
| 470 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* If another thread successfully recorded its critical |
| 474 | * section in the global_init_lock then discard the one |
| 475 | * allocated by this thread. */ |
| 476 | if (global_init_lock != sem) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 477 | delete_sem(sem); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 478 | |
| 479 | /* Acquire the chosen semaphore */ |
| 480 | if (acquire_sem(global_init_lock) != B_NO_ERROR) { |
| 481 | #ifdef DEBUG_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 482 | xmlGenericError(xmlGenericErrorContext, |
| 483 | "xmlGlobalInitMutexLock():BeOS:Couldn't acquire semaphore\n"); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 484 | #endif |
| 485 | } |
| 486 | #endif |
| 487 | } |
| 488 | |
| 489 | void |
| 490 | __xmlGlobalInitMutexUnlock(void) |
| 491 | { |
| 492 | #ifdef HAVE_PTHREAD_H |
Daniel Richard G | 5706b6d | 2012-08-06 11:32:54 +0800 | [diff] [blame] | 493 | if (pthread_mutex_unlock != NULL) |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 494 | pthread_mutex_unlock(&global_init_lock); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 495 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 496 | if (global_init_lock != NULL) { |
| 497 | LeaveCriticalSection(global_init_lock); |
| 498 | } |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 499 | #elif defined HAVE_BEOS_THREADS |
| 500 | release_sem(global_init_lock); |
| 501 | #endif |
| 502 | } |
| 503 | |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 504 | /** |
| 505 | * xmlGlobalInitMutexDestroy |
| 506 | * |
| 507 | * Makes sure that the global initialization mutex is destroyed before |
| 508 | * application termination. |
| 509 | */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 510 | void |
| 511 | __xmlGlobalInitMutexDestroy(void) |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 512 | { |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 513 | #ifdef HAVE_PTHREAD_H |
| 514 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 515 | if (global_init_lock != NULL) { |
| 516 | DeleteCriticalSection(global_init_lock); |
| 517 | free(global_init_lock); |
| 518 | global_init_lock = NULL; |
Rob Richards | 91eb560 | 2007-11-16 10:54:59 +0000 | [diff] [blame] | 519 | } |
| 520 | #endif |
| 521 | } |
| 522 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 523 | /************************************************************************ |
| 524 | * * |
| 525 | * Per thread global state handling * |
| 526 | * * |
| 527 | ************************************************************************/ |
| 528 | |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 529 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | 01c3bd5 | 2004-10-22 13:16:10 +0000 | [diff] [blame] | 530 | #ifdef xmlLastError |
| 531 | #undef xmlLastError |
| 532 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 533 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 534 | /** |
| 535 | * xmlFreeGlobalState: |
| 536 | * @state: a thread global state |
| 537 | * |
| 538 | * xmlFreeGlobalState() is called when a thread terminates with a non-NULL |
| 539 | * global state. It is is used here to reclaim memory resources. |
| 540 | */ |
| 541 | static void |
| 542 | xmlFreeGlobalState(void *state) |
| 543 | { |
Daniel Veillard | 01c3bd5 | 2004-10-22 13:16:10 +0000 | [diff] [blame] | 544 | xmlGlobalState *gs = (xmlGlobalState *) state; |
| 545 | |
| 546 | /* free any memory allocated in the thread's xmlLastError */ |
| 547 | xmlResetError(&(gs->xmlLastError)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 548 | free(state); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * xmlNewGlobalState: |
| 553 | * |
| 554 | * xmlNewGlobalState() allocates a global state. This structure is used to |
| 555 | * hold all data for use by a thread when supporting backwards compatibility |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 556 | * of libxml2 to pre-thread-safe behaviour. |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 557 | * |
| 558 | * Returns the newly allocated xmlGlobalStatePtr or NULL in case of error |
| 559 | */ |
| 560 | static xmlGlobalStatePtr |
| 561 | xmlNewGlobalState(void) |
| 562 | { |
| 563 | xmlGlobalState *gs; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 564 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 565 | gs = malloc(sizeof(xmlGlobalState)); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 566 | if (gs == NULL) { |
| 567 | xmlGenericError(xmlGenericErrorContext, |
| 568 | "xmlGetGlobalState: out of memory\n"); |
| 569 | return (NULL); |
| 570 | } |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 571 | |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 572 | memset(gs, 0, sizeof(xmlGlobalState)); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 573 | xmlInitializeGlobalState(gs); |
| 574 | return (gs); |
| 575 | } |
Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 576 | #endif /* LIBXML_THREAD_ENABLED */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 577 | |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 578 | #ifdef HAVE_PTHREAD_H |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 579 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 580 | #if !defined(HAVE_COMPILER_TLS) |
| 581 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 582 | typedef struct _xmlGlobalStateCleanupHelperParams { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 583 | HANDLE thread; |
| 584 | void *memory; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 585 | } xmlGlobalStateCleanupHelperParams; |
| 586 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 587 | static void XMLCDECL |
| 588 | xmlGlobalStateCleanupHelper(void *p) |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 589 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 590 | xmlGlobalStateCleanupHelperParams *params = |
| 591 | (xmlGlobalStateCleanupHelperParams *) p; |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 592 | WaitForSingleObject(params->thread, INFINITE); |
| 593 | CloseHandle(params->thread); |
| 594 | xmlFreeGlobalState(params->memory); |
| 595 | free(params); |
| 596 | _endthread(); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 597 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 598 | #else /* LIBXML_STATIC && !LIBXML_STATIC_FOR_DLL */ |
| 599 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 600 | typedef struct _xmlGlobalStateCleanupHelperParams { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 601 | void *memory; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 602 | struct _xmlGlobalStateCleanupHelperParams *prev; |
| 603 | struct _xmlGlobalStateCleanupHelperParams *next; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 604 | } xmlGlobalStateCleanupHelperParams; |
| 605 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 606 | static xmlGlobalStateCleanupHelperParams *cleanup_helpers_head = NULL; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 607 | static CRITICAL_SECTION cleanup_helpers_cs; |
| 608 | |
| 609 | #endif /* LIBXMLSTATIC && !LIBXML_STATIC_FOR_DLL */ |
| 610 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 611 | #endif /* HAVE_WIN32_THREADS */ |
| 612 | |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 613 | #if defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 614 | |
William M. Brack | b1d5316 | 2003-11-18 06:54:40 +0000 | [diff] [blame] | 615 | /** |
| 616 | * xmlGlobalStateCleanup: |
| 617 | * @data: unused parameter |
| 618 | * |
| 619 | * Used for Beos only |
| 620 | */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 621 | void |
| 622 | xmlGlobalStateCleanup(void *data) |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 623 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 624 | void *globalval = tls_get(globalkey); |
| 625 | |
| 626 | if (globalval != NULL) |
| 627 | xmlFreeGlobalState(globalval); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 628 | } |
| 629 | #endif |
| 630 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 631 | /** |
| 632 | * xmlGetGlobalState: |
| 633 | * |
| 634 | * xmlGetGlobalState() is called to retrieve the global state for a thread. |
| 635 | * |
| 636 | * Returns the thread global state or NULL in case of error |
| 637 | */ |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 638 | xmlGlobalStatePtr |
| 639 | xmlGetGlobalState(void) |
| 640 | { |
| 641 | #ifdef HAVE_PTHREAD_H |
| 642 | xmlGlobalState *globalval; |
| 643 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 644 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 645 | return (NULL); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 646 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 647 | pthread_once(&once_control, xmlOnceInit); |
| 648 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 649 | if ((globalval = (xmlGlobalState *) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 650 | pthread_getspecific(globalkey)) == NULL) { |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 651 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 652 | if (tsd == NULL) |
| 653 | return(NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 654 | |
| 655 | pthread_setspecific(globalkey, tsd); |
| 656 | return (tsd); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 657 | } |
| 658 | return (globalval); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 659 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 660 | #if defined(HAVE_COMPILER_TLS) |
| 661 | if (!tlstate_inited) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 662 | tlstate_inited = 1; |
| 663 | xmlInitializeGlobalState(&tlstate); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 664 | } |
| 665 | return &tlstate; |
| 666 | #else /* HAVE_COMPILER_TLS */ |
| 667 | xmlGlobalState *globalval; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 668 | xmlGlobalStateCleanupHelperParams *p; |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 669 | |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 670 | xmlOnceInit(); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 671 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 672 | globalval = (xmlGlobalState *) TlsGetValue(globalkey); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 673 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 674 | p = (xmlGlobalStateCleanupHelperParams *) TlsGetValue(globalkey); |
| 675 | globalval = (xmlGlobalState *) (p ? p->memory : NULL); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 676 | #endif |
| 677 | if (globalval == NULL) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 678 | xmlGlobalState *tsd = xmlNewGlobalState(); |
| 679 | |
| 680 | if (tsd == NULL) |
| 681 | return(NULL); |
| 682 | p = (xmlGlobalStateCleanupHelperParams *) |
| 683 | malloc(sizeof(xmlGlobalStateCleanupHelperParams)); |
| 684 | if (p == NULL) { |
| 685 | xmlGenericError(xmlGenericErrorContext, |
| 686 | "xmlGetGlobalState: out of memory\n"); |
Daniel Veillard | bf2ebff | 2009-01-18 14:57:04 +0000 | [diff] [blame] | 687 | xmlFreeGlobalState(tsd); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 688 | return(NULL); |
| 689 | } |
| 690 | p->memory = tsd; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 691 | #if defined(LIBXML_STATIC) && !defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 692 | DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 693 | GetCurrentProcess(), &p->thread, 0, TRUE, |
| 694 | DUPLICATE_SAME_ACCESS); |
| 695 | TlsSetValue(globalkey, tsd); |
| 696 | _beginthread(xmlGlobalStateCleanupHelper, 0, p); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 697 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 698 | EnterCriticalSection(&cleanup_helpers_cs); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 699 | if (cleanup_helpers_head != NULL) { |
| 700 | cleanup_helpers_head->prev = p; |
| 701 | } |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 702 | p->next = cleanup_helpers_head; |
| 703 | p->prev = NULL; |
| 704 | cleanup_helpers_head = p; |
| 705 | TlsSetValue(globalkey, p); |
| 706 | LeaveCriticalSection(&cleanup_helpers_cs); |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 707 | #endif |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 708 | |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 709 | return (tsd); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 710 | } |
| 711 | return (globalval); |
| 712 | #endif /* HAVE_COMPILER_TLS */ |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 713 | #elif defined HAVE_BEOS_THREADS |
| 714 | xmlGlobalState *globalval; |
| 715 | |
| 716 | xmlOnceInit(); |
| 717 | |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 718 | if ((globalval = (xmlGlobalState *) tls_get(globalkey)) == NULL) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 719 | xmlGlobalState *tsd = xmlNewGlobalState(); |
Daniel Veillard | 14d465d | 2008-03-24 11:12:55 +0000 | [diff] [blame] | 720 | if (tsd == NULL) |
| 721 | return (NULL); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 722 | |
| 723 | tls_set(globalkey, tsd); |
| 724 | on_exit_thread(xmlGlobalStateCleanup, NULL); |
| 725 | return (tsd); |
| 726 | } |
| 727 | return (globalval); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 728 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 729 | return (NULL); |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 730 | #endif |
| 731 | } |
| 732 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 733 | /************************************************************************ |
| 734 | * * |
| 735 | * Library wide thread interfaces * |
| 736 | * * |
| 737 | ************************************************************************/ |
| 738 | |
| 739 | /** |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 740 | * xmlGetThreadId: |
| 741 | * |
| 742 | * xmlGetThreadId() find the current thread ID number |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 743 | * Note that this is likely to be broken on some platforms using pthreads |
| 744 | * as the specification doesn't mandate pthread_t to be an integer type |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 745 | * |
| 746 | * Returns the current thread ID number |
| 747 | */ |
| 748 | int |
| 749 | xmlGetThreadId(void) |
| 750 | { |
| 751 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 752 | pthread_t id; |
| 753 | int ret; |
| 754 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 755 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 756 | return (0); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 757 | id = pthread_self(); |
| 758 | /* horrible but preserves compat, see warning above */ |
| 759 | memcpy(&ret, &id, sizeof(ret)); |
| 760 | return (ret); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 761 | #elif defined HAVE_WIN32_THREADS |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 762 | return GetCurrentThreadId(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 763 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 764 | return find_thread(NULL); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 765 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 766 | return ((int) 0); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 767 | #endif |
| 768 | } |
| 769 | |
| 770 | /** |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 771 | * xmlIsMainThread: |
| 772 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 773 | * xmlIsMainThread() check whether the current thread is the main thread. |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 774 | * |
| 775 | * Returns 1 if the current thread is the main thread, 0 otherwise |
| 776 | */ |
| 777 | int |
| 778 | xmlIsMainThread(void) |
| 779 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 780 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 781 | if (libxml_is_threaded == -1) |
| 782 | xmlInitThreads(); |
| 783 | if (libxml_is_threaded == 0) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 784 | return (1); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 785 | pthread_once(&once_control, xmlOnceInit); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 786 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 787 | xmlOnceInit(); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 788 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 789 | xmlOnceInit(); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 790 | #endif |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 791 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 792 | #ifdef DEBUG_THREADS |
| 793 | xmlGenericError(xmlGenericErrorContext, "xmlIsMainThread()\n"); |
| 794 | #endif |
| 795 | #ifdef HAVE_PTHREAD_H |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 796 | return (pthread_equal(mainthread,pthread_self())); |
Daniel Veillard | db0eb8d | 2002-01-13 13:35:00 +0000 | [diff] [blame] | 797 | #elif defined HAVE_WIN32_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 798 | return (mainthread == GetCurrentThreadId()); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 799 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 800 | return (mainthread == find_thread(NULL)); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 801 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 802 | return (1); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 803 | #endif |
| 804 | } |
| 805 | |
| 806 | /** |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 807 | * xmlLockLibrary: |
| 808 | * |
| 809 | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
| 810 | * library. |
| 811 | */ |
| 812 | void |
| 813 | xmlLockLibrary(void) |
| 814 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 815 | #ifdef DEBUG_THREADS |
| 816 | xmlGenericError(xmlGenericErrorContext, "xmlLockLibrary()\n"); |
| 817 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 818 | xmlRMutexLock(xmlLibraryLock); |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * xmlUnlockLibrary: |
| 823 | * |
| 824 | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
| 825 | * library. |
| 826 | */ |
| 827 | void |
| 828 | xmlUnlockLibrary(void) |
| 829 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 830 | #ifdef DEBUG_THREADS |
| 831 | xmlGenericError(xmlGenericErrorContext, "xmlUnlockLibrary()\n"); |
| 832 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 833 | xmlRMutexUnlock(xmlLibraryLock); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * xmlInitThreads: |
| 838 | * |
| 839 | * xmlInitThreads() is used to to initialize all the thread related |
| 840 | * data of the libxml2 library. |
| 841 | */ |
| 842 | void |
| 843 | xmlInitThreads(void) |
| 844 | { |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 845 | #ifdef HAVE_PTHREAD_H |
| 846 | if (libxml_is_threaded == -1) { |
| 847 | if ((pthread_once != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 848 | (pthread_getspecific != NULL) && |
| 849 | (pthread_setspecific != NULL) && |
| 850 | (pthread_key_create != NULL) && |
Daniel Veillard | d4a3f24 | 2009-01-18 15:41:30 +0000 | [diff] [blame] | 851 | (pthread_key_delete != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 852 | (pthread_mutex_init != NULL) && |
| 853 | (pthread_mutex_destroy != NULL) && |
| 854 | (pthread_mutex_lock != NULL) && |
| 855 | (pthread_mutex_unlock != NULL) && |
| 856 | (pthread_cond_init != NULL) && |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 857 | (pthread_cond_destroy != NULL) && |
| 858 | (pthread_cond_wait != NULL) && |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 859 | (pthread_equal != NULL) && |
| 860 | (pthread_self != NULL) && |
| 861 | (pthread_cond_signal != NULL)) { |
| 862 | libxml_is_threaded = 1; |
| 863 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 864 | /* fprintf(stderr, "Running multithreaded\n"); */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 865 | } else { |
| 866 | |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 867 | /* fprintf(stderr, "Running without multithread\n"); */ |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 868 | libxml_is_threaded = 0; |
| 869 | } |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 870 | } |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 871 | #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) |
| 872 | InitializeCriticalSection(&cleanup_helpers_cs); |
Daniel Veillard | dbfe05a | 2005-05-04 09:18:00 +0000 | [diff] [blame] | 873 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /** |
| 877 | * xmlCleanupThreads: |
| 878 | * |
| 879 | * xmlCleanupThreads() is used to to cleanup all the thread related |
| 880 | * data of the libxml2 library once processing has ended. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 881 | * |
| 882 | * WARNING: if your application is multithreaded or has plugin support |
| 883 | * calling this may crash the application if another thread or |
| 884 | * a plugin is still using libxml2. It's sometimes very hard to |
| 885 | * guess if libxml2 is in use in the application, some libraries |
| 886 | * or plugins may use it without notice. In case of doubt abstain |
| 887 | * from calling this function or do it just before calling exit() |
| 888 | * to avoid leak reports from valgrind ! |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 889 | */ |
| 890 | void |
| 891 | xmlCleanupThreads(void) |
| 892 | { |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 893 | #ifdef DEBUG_THREADS |
| 894 | xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n"); |
| 895 | #endif |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 896 | #ifdef HAVE_PTHREAD_H |
| 897 | if ((libxml_is_threaded) && (pthread_key_delete != NULL)) |
| 898 | pthread_key_delete(globalkey); |
Friedrich Haubensak | 3f6cfbd | 2012-09-12 17:34:53 +0200 | [diff] [blame] | 899 | once_control = once_control_init; |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 900 | #elif 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] | 901 | if (globalkey != TLS_OUT_OF_INDEXES) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 902 | xmlGlobalStateCleanupHelperParams *p; |
| 903 | |
| 904 | EnterCriticalSection(&cleanup_helpers_cs); |
| 905 | p = cleanup_helpers_head; |
| 906 | while (p != NULL) { |
| 907 | xmlGlobalStateCleanupHelperParams *temp = p; |
| 908 | |
| 909 | p = p->next; |
| 910 | xmlFreeGlobalState(temp->memory); |
| 911 | free(temp); |
| 912 | } |
| 913 | cleanup_helpers_head = 0; |
| 914 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 915 | TlsFree(globalkey); |
| 916 | globalkey = TLS_OUT_OF_INDEXES; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 917 | } |
| 918 | DeleteCriticalSection(&cleanup_helpers_cs); |
| 919 | #endif |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 920 | } |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 921 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 922 | #ifdef LIBXML_THREAD_ENABLED |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 923 | |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 924 | /** |
| 925 | * xmlOnceInit |
| 926 | * |
| 927 | * xmlOnceInit() is used to initialize the value of mainthread for use |
| 928 | * in other routines. This function should only be called using |
| 929 | * pthread_once() in association with the once_control variable to ensure |
| 930 | * that the function is only called once. See man pthread_once for more |
| 931 | * details. |
| 932 | */ |
| 933 | static void |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 934 | xmlOnceInit(void) |
| 935 | { |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 936 | #ifdef HAVE_PTHREAD_H |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 937 | (void) pthread_key_create(&globalkey, xmlFreeGlobalState); |
Daniel Veillard | e28313b | 2001-12-06 14:08:31 +0000 | [diff] [blame] | 938 | mainthread = pthread_self(); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 939 | __xmlInitializeDict(); |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 940 | #elif defined(HAVE_WIN32_THREADS) |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 941 | if (!run_once.done) { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 942 | if (InterlockedIncrement(&run_once.control) == 1) { |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 943 | #if !defined(HAVE_COMPILER_TLS) |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 944 | globalkey = TlsAlloc(); |
Igor Zlatkovic | f2160a0 | 2002-10-31 15:58:42 +0000 | [diff] [blame] | 945 | #endif |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 946 | mainthread = GetCurrentThreadId(); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 947 | __xmlInitializeDict(); |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 948 | run_once.done = 1; |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 949 | } else { |
Daniel Veillard | 62121e2 | 2005-02-24 15:38:52 +0000 | [diff] [blame] | 950 | /* Another thread is working; give up our slice and |
| 951 | * wait until they're done. */ |
| 952 | while (!run_once.done) |
| 953 | Sleep(0); |
| 954 | } |
| 955 | } |
Eric Zurcher | 243b034 | 2009-10-01 00:13:07 +0200 | [diff] [blame] | 956 | #elif defined HAVE_BEOS_THREADS |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 957 | if (atomic_add(&run_once_init, 1) == 0) { |
| 958 | globalkey = tls_allocate(); |
| 959 | tls_set(globalkey, NULL); |
| 960 | mainthread = find_thread(NULL); |
Daniel Veillard | 5fe9e9e | 2013-04-05 23:10:41 +0800 | [diff] [blame] | 961 | __xmlInitializeDict(); |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 962 | } else |
| 963 | atomic_add(&run_once_init, -1); |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 964 | #endif |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 965 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 966 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 967 | |
| 968 | /** |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 969 | * DllMain: |
| 970 | * @hinstDLL: handle to DLL instance |
| 971 | * @fdwReason: Reason code for entry |
| 972 | * @lpvReserved: generic pointer (depends upon reason code) |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 973 | * |
| 974 | * Entry point for Windows library. It is being used to free thread-specific |
| 975 | * storage. |
William M. Brack | 7a82165 | 2003-08-15 07:27:40 +0000 | [diff] [blame] | 976 | * |
| 977 | * Returns TRUE always |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 978 | */ |
Daniel Veillard | d87c5d1 | 2009-09-10 17:46:07 +0200 | [diff] [blame] | 979 | #ifdef HAVE_PTHREAD_H |
| 980 | #elif 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] | 981 | #if defined(LIBXML_STATIC_FOR_DLL) |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 982 | BOOL XMLCALL |
| 983 | xmlDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 984 | #else |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 985 | BOOL WINAPI |
| 986 | DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 987 | #endif |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 988 | { |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 989 | switch (fdwReason) { |
| 990 | case DLL_THREAD_DETACH: |
| 991 | if (globalkey != TLS_OUT_OF_INDEXES) { |
| 992 | xmlGlobalState *globalval = NULL; |
| 993 | xmlGlobalStateCleanupHelperParams *p = |
| 994 | (xmlGlobalStateCleanupHelperParams *) |
| 995 | TlsGetValue(globalkey); |
| 996 | globalval = (xmlGlobalState *) (p ? p->memory : NULL); |
| 997 | if (globalval) { |
| 998 | xmlFreeGlobalState(globalval); |
| 999 | TlsSetValue(globalkey, NULL); |
| 1000 | } |
| 1001 | if (p) { |
| 1002 | EnterCriticalSection(&cleanup_helpers_cs); |
| 1003 | if (p == cleanup_helpers_head) |
| 1004 | cleanup_helpers_head = p->next; |
| 1005 | else |
| 1006 | p->prev->next = p->next; |
| 1007 | if (p->next != NULL) |
| 1008 | p->next->prev = p->prev; |
| 1009 | LeaveCriticalSection(&cleanup_helpers_cs); |
| 1010 | free(p); |
| 1011 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1012 | } |
Daniel Veillard | ddbe38b | 2008-03-18 08:24:25 +0000 | [diff] [blame] | 1013 | break; |
Igor Zlatkovic | d58a42d | 2003-05-17 10:55:15 +0000 | [diff] [blame] | 1014 | } |
| 1015 | return TRUE; |
| 1016 | } |
| 1017 | #endif |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1018 | #define bottom_threads |
| 1019 | #include "elfgcchack.h" |