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