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