Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * threads.c: set of generic threading related routines |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * Gary Pennington <Gary.Pennington@uk.sun.com> |
7 | | * daniel@veillard.com |
8 | | */ |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | |
13 | | #include <string.h> |
14 | | #include <stdarg.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include <libxml/threads.h> |
18 | | #include <libxml/parser.h> |
19 | | #ifdef LIBXML_CATALOG_ENABLED |
20 | | #include <libxml/catalog.h> |
21 | | #endif |
22 | | #ifdef LIBXML_SCHEMAS_ENABLED |
23 | | #include <libxml/xmlschemastypes.h> |
24 | | #include <libxml/relaxng.h> |
25 | | #endif |
26 | | |
27 | | #if defined(SOLARIS) |
28 | | #include <note.h> |
29 | | #endif |
30 | | |
31 | | #include "private/cata.h" |
32 | | #include "private/dict.h" |
33 | | #include "private/enc.h" |
34 | | #include "private/error.h" |
35 | | #include "private/globals.h" |
36 | | #include "private/io.h" |
37 | | #include "private/memory.h" |
38 | | #include "private/threads.h" |
39 | | #include "private/xpath.h" |
40 | | |
41 | | /* |
42 | | * TODO: this module still uses malloc/free and not xmlMalloc/xmlFree |
43 | | * to avoid some craziness since xmlMalloc/xmlFree may actually |
44 | | * be hosted on allocated blocks needing them for the allocation ... |
45 | | */ |
46 | | |
47 | | static xmlRMutex xmlLibraryLock; |
48 | | |
49 | | /** |
50 | | * xmlInitMutex: |
51 | | * @mutex: the mutex |
52 | | * |
53 | | * Initialize a mutex. |
54 | | */ |
55 | | void |
56 | | xmlInitMutex(xmlMutexPtr mutex) |
57 | 39.5k | { |
58 | 39.5k | #ifdef HAVE_POSIX_THREADS |
59 | 39.5k | pthread_mutex_init(&mutex->lock, NULL); |
60 | | #elif defined HAVE_WIN32_THREADS |
61 | | InitializeCriticalSection(&mutex->cs); |
62 | | #else |
63 | | (void) mutex; |
64 | | #endif |
65 | 39.5k | } |
66 | | |
67 | | /** |
68 | | * xmlNewMutex: |
69 | | * |
70 | | * xmlNewMutex() is used to allocate a libxml2 token struct for use in |
71 | | * synchronizing access to data. |
72 | | * |
73 | | * Returns a new simple mutex pointer or NULL in case of error |
74 | | */ |
75 | | xmlMutexPtr |
76 | | xmlNewMutex(void) |
77 | 0 | { |
78 | 0 | xmlMutexPtr tok; |
79 | |
|
80 | 0 | tok = malloc(sizeof(xmlMutex)); |
81 | 0 | if (tok == NULL) |
82 | 0 | return (NULL); |
83 | 0 | xmlInitMutex(tok); |
84 | 0 | return (tok); |
85 | 0 | } |
86 | | |
87 | | /** |
88 | | * xmlCleanupMutex: |
89 | | * @mutex: the simple mutex |
90 | | * |
91 | | * Reclaim resources associated with a mutex. |
92 | | */ |
93 | | void |
94 | | xmlCleanupMutex(xmlMutexPtr mutex) |
95 | 39.5k | { |
96 | 39.5k | #ifdef HAVE_POSIX_THREADS |
97 | 39.5k | pthread_mutex_destroy(&mutex->lock); |
98 | | #elif defined HAVE_WIN32_THREADS |
99 | | DeleteCriticalSection(&mutex->cs); |
100 | | #else |
101 | | (void) mutex; |
102 | | #endif |
103 | 39.5k | } |
104 | | |
105 | | /** |
106 | | * xmlFreeMutex: |
107 | | * @tok: the simple mutex |
108 | | * |
109 | | * Free a mutex. |
110 | | */ |
111 | | void |
112 | | xmlFreeMutex(xmlMutexPtr tok) |
113 | 0 | { |
114 | 0 | if (tok == NULL) |
115 | 0 | return; |
116 | | |
117 | 0 | xmlCleanupMutex(tok); |
118 | 0 | free(tok); |
119 | 0 | } |
120 | | |
121 | | /** |
122 | | * xmlMutexLock: |
123 | | * @tok: the simple mutex |
124 | | * |
125 | | * xmlMutexLock() is used to lock a libxml2 token. |
126 | | */ |
127 | | void |
128 | | xmlMutexLock(xmlMutexPtr tok) |
129 | 104k | { |
130 | 104k | if (tok == NULL) |
131 | 0 | return; |
132 | 104k | #ifdef HAVE_POSIX_THREADS |
133 | | /* |
134 | | * This assumes that __libc_single_threaded won't change while the |
135 | | * lock is held. |
136 | | */ |
137 | 104k | pthread_mutex_lock(&tok->lock); |
138 | | #elif defined HAVE_WIN32_THREADS |
139 | | EnterCriticalSection(&tok->cs); |
140 | | #endif |
141 | | |
142 | 104k | } |
143 | | |
144 | | /** |
145 | | * xmlMutexUnlock: |
146 | | * @tok: the simple mutex |
147 | | * |
148 | | * xmlMutexUnlock() is used to unlock a libxml2 token. |
149 | | */ |
150 | | void |
151 | | xmlMutexUnlock(xmlMutexPtr tok) |
152 | 104k | { |
153 | 104k | if (tok == NULL) |
154 | 0 | return; |
155 | 104k | #ifdef HAVE_POSIX_THREADS |
156 | 104k | pthread_mutex_unlock(&tok->lock); |
157 | | #elif defined HAVE_WIN32_THREADS |
158 | | LeaveCriticalSection(&tok->cs); |
159 | | #endif |
160 | 104k | } |
161 | | |
162 | | void |
163 | 19.7k | xmlInitRMutex(xmlRMutexPtr tok) { |
164 | 19.7k | (void) tok; |
165 | | |
166 | 19.7k | #ifdef HAVE_POSIX_THREADS |
167 | 19.7k | pthread_mutex_init(&tok->lock, NULL); |
168 | 19.7k | tok->held = 0; |
169 | 19.7k | tok->waiters = 0; |
170 | 19.7k | pthread_cond_init(&tok->cv, NULL); |
171 | | #elif defined HAVE_WIN32_THREADS |
172 | | InitializeCriticalSection(&tok->cs); |
173 | | #endif |
174 | 19.7k | } |
175 | | |
176 | | /** |
177 | | * xmlNewRMutex: |
178 | | * |
179 | | * xmlRNewMutex() is used to allocate a reentrant mutex for use in |
180 | | * synchronizing access to data. token_r is a re-entrant lock and thus useful |
181 | | * for synchronizing access to data structures that may be manipulated in a |
182 | | * recursive fashion. |
183 | | * |
184 | | * Returns the new reentrant mutex pointer or NULL in case of error |
185 | | */ |
186 | | xmlRMutexPtr |
187 | | xmlNewRMutex(void) |
188 | 0 | { |
189 | 0 | xmlRMutexPtr tok; |
190 | |
|
191 | 0 | tok = malloc(sizeof(xmlRMutex)); |
192 | 0 | if (tok == NULL) |
193 | 0 | return (NULL); |
194 | 0 | xmlInitRMutex(tok); |
195 | 0 | return (tok); |
196 | 0 | } |
197 | | |
198 | | void |
199 | 19.7k | xmlCleanupRMutex(xmlRMutexPtr tok) { |
200 | 19.7k | (void) tok; |
201 | | |
202 | 19.7k | #ifdef HAVE_POSIX_THREADS |
203 | 19.7k | pthread_mutex_destroy(&tok->lock); |
204 | 19.7k | pthread_cond_destroy(&tok->cv); |
205 | | #elif defined HAVE_WIN32_THREADS |
206 | | DeleteCriticalSection(&tok->cs); |
207 | | #endif |
208 | 19.7k | } |
209 | | |
210 | | /** |
211 | | * xmlFreeRMutex: |
212 | | * @tok: the reentrant mutex |
213 | | * |
214 | | * xmlRFreeMutex() is used to reclaim resources associated with a |
215 | | * reentrant mutex. |
216 | | */ |
217 | | void |
218 | | xmlFreeRMutex(xmlRMutexPtr tok) |
219 | 0 | { |
220 | 0 | if (tok == NULL) |
221 | 0 | return; |
222 | 0 | xmlCleanupRMutex(tok); |
223 | 0 | free(tok); |
224 | 0 | } |
225 | | |
226 | | /** |
227 | | * xmlRMutexLock: |
228 | | * @tok: the reentrant mutex |
229 | | * |
230 | | * xmlRMutexLock() is used to lock a libxml2 token_r. |
231 | | */ |
232 | | void |
233 | | xmlRMutexLock(xmlRMutexPtr tok) |
234 | 9.89k | { |
235 | 9.89k | if (tok == NULL) |
236 | 0 | return; |
237 | 9.89k | #ifdef HAVE_POSIX_THREADS |
238 | 9.89k | pthread_mutex_lock(&tok->lock); |
239 | 9.89k | if (tok->held) { |
240 | 0 | if (pthread_equal(tok->tid, pthread_self())) { |
241 | 0 | tok->held++; |
242 | 0 | pthread_mutex_unlock(&tok->lock); |
243 | 0 | return; |
244 | 0 | } else { |
245 | 0 | tok->waiters++; |
246 | 0 | while (tok->held) |
247 | 0 | pthread_cond_wait(&tok->cv, &tok->lock); |
248 | 0 | tok->waiters--; |
249 | 0 | } |
250 | 0 | } |
251 | 9.89k | tok->tid = pthread_self(); |
252 | 9.89k | tok->held = 1; |
253 | 9.89k | pthread_mutex_unlock(&tok->lock); |
254 | | #elif defined HAVE_WIN32_THREADS |
255 | | EnterCriticalSection(&tok->cs); |
256 | | #endif |
257 | 9.89k | } |
258 | | |
259 | | /** |
260 | | * xmlRMutexUnlock: |
261 | | * @tok: the reentrant mutex |
262 | | * |
263 | | * xmlRMutexUnlock() is used to unlock a libxml2 token_r. |
264 | | */ |
265 | | void |
266 | | xmlRMutexUnlock(xmlRMutexPtr tok ATTRIBUTE_UNUSED) |
267 | 9.89k | { |
268 | 9.89k | if (tok == NULL) |
269 | 0 | return; |
270 | 9.89k | #ifdef HAVE_POSIX_THREADS |
271 | 9.89k | pthread_mutex_lock(&tok->lock); |
272 | 9.89k | tok->held--; |
273 | 9.89k | if (tok->held == 0) { |
274 | 9.89k | if (tok->waiters) |
275 | 0 | pthread_cond_signal(&tok->cv); |
276 | 9.89k | memset(&tok->tid, 0, sizeof(tok->tid)); |
277 | 9.89k | } |
278 | 9.89k | pthread_mutex_unlock(&tok->lock); |
279 | | #elif defined HAVE_WIN32_THREADS |
280 | | LeaveCriticalSection(&tok->cs); |
281 | | #endif |
282 | 9.89k | } |
283 | | |
284 | | /************************************************************************ |
285 | | * * |
286 | | * Library wide thread interfaces * |
287 | | * * |
288 | | ************************************************************************/ |
289 | | |
290 | | /** |
291 | | * xmlGetThreadId: |
292 | | * |
293 | | * DEPRECATED: Internal function, do not use. |
294 | | * |
295 | | * xmlGetThreadId() find the current thread ID number |
296 | | * Note that this is likely to be broken on some platforms using pthreads |
297 | | * as the specification doesn't mandate pthread_t to be an integer type |
298 | | * |
299 | | * Returns the current thread ID number |
300 | | */ |
301 | | int |
302 | | xmlGetThreadId(void) |
303 | 0 | { |
304 | 0 | #ifdef HAVE_POSIX_THREADS |
305 | 0 | pthread_t id; |
306 | 0 | int ret; |
307 | |
|
308 | 0 | id = pthread_self(); |
309 | | /* horrible but preserves compat, see warning above */ |
310 | 0 | memcpy(&ret, &id, sizeof(ret)); |
311 | 0 | return (ret); |
312 | | #elif defined HAVE_WIN32_THREADS |
313 | | return GetCurrentThreadId(); |
314 | | #else |
315 | | return ((int) 0); |
316 | | #endif |
317 | 0 | } |
318 | | |
319 | | /** |
320 | | * xmlLockLibrary: |
321 | | * |
322 | | * xmlLockLibrary() is used to take out a re-entrant lock on the libxml2 |
323 | | * library. |
324 | | */ |
325 | | void |
326 | | xmlLockLibrary(void) |
327 | 0 | { |
328 | 0 | xmlRMutexLock(&xmlLibraryLock); |
329 | 0 | } |
330 | | |
331 | | /** |
332 | | * xmlUnlockLibrary: |
333 | | * |
334 | | * xmlUnlockLibrary() is used to release a re-entrant lock on the libxml2 |
335 | | * library. |
336 | | */ |
337 | | void |
338 | | xmlUnlockLibrary(void) |
339 | 0 | { |
340 | 0 | xmlRMutexUnlock(&xmlLibraryLock); |
341 | 0 | } |
342 | | |
343 | | /** |
344 | | * xmlInitThreads: |
345 | | * |
346 | | * DEPRECATED: Alias for xmlInitParser. |
347 | | */ |
348 | | void |
349 | | xmlInitThreads(void) |
350 | 0 | { |
351 | 0 | xmlInitParser(); |
352 | 0 | } |
353 | | |
354 | | /** |
355 | | * xmlCleanupThreads: |
356 | | * |
357 | | * DEPRECATED: This function is a no-op. Call xmlCleanupParser |
358 | | * to free global state but see the warnings there. xmlCleanupParser |
359 | | * should be only called once at program exit. In most cases, you don't |
360 | | * have call cleanup functions at all. |
361 | | */ |
362 | | void |
363 | | xmlCleanupThreads(void) |
364 | 0 | { |
365 | 0 | } |
366 | | |
367 | | static void |
368 | 9.89k | xmlInitThreadsInternal(void) { |
369 | 9.89k | xmlInitRMutex(&xmlLibraryLock); |
370 | 9.89k | } |
371 | | |
372 | | static void |
373 | 9.89k | xmlCleanupThreadsInternal(void) { |
374 | 9.89k | xmlCleanupRMutex(&xmlLibraryLock); |
375 | 9.89k | } |
376 | | |
377 | | /************************************************************************ |
378 | | * * |
379 | | * Library wide initialization * |
380 | | * * |
381 | | ************************************************************************/ |
382 | | |
383 | | static int xmlParserInitialized = 0; |
384 | | |
385 | | #ifdef HAVE_POSIX_THREADS |
386 | | static pthread_once_t onceControl = PTHREAD_ONCE_INIT; |
387 | | #elif defined HAVE_WIN32_THREADS |
388 | | static INIT_ONCE onceControl = INIT_ONCE_STATIC_INIT; |
389 | | #else |
390 | | static int onceControl = 0; |
391 | | #endif |
392 | | |
393 | | static void |
394 | 9.89k | xmlInitParserInternal(void) { |
395 | | /* |
396 | | * Note that the initialization code must not make memory allocations. |
397 | | */ |
398 | 9.89k | xmlInitRandom(); /* Required by xmlInitGlobalsInternal */ |
399 | 9.89k | xmlInitMemoryInternal(); |
400 | 9.89k | xmlInitThreadsInternal(); |
401 | 9.89k | xmlInitGlobalsInternal(); |
402 | 9.89k | xmlInitDictInternal(); |
403 | 9.89k | xmlInitEncodingInternal(); |
404 | 9.89k | #if defined(LIBXML_XPATH_ENABLED) |
405 | 9.89k | xmlInitXPathInternal(); |
406 | 9.89k | #endif |
407 | 9.89k | xmlInitIOCallbacks(); |
408 | 9.89k | #ifdef LIBXML_CATALOG_ENABLED |
409 | 9.89k | xmlInitCatalogInternal(); |
410 | 9.89k | #endif |
411 | | |
412 | 9.89k | xmlParserInitialized = 1; |
413 | 9.89k | } |
414 | | |
415 | | #if defined(HAVE_WIN32_THREADS) |
416 | | static BOOL WINAPI |
417 | | xmlInitParserWinWrapper(INIT_ONCE *initOnce ATTRIBUTE_UNUSED, |
418 | | void *parameter ATTRIBUTE_UNUSED, |
419 | | void **context ATTRIBUTE_UNUSED) { |
420 | | xmlInitParserInternal(); |
421 | | return(TRUE); |
422 | | } |
423 | | #endif |
424 | | |
425 | | /** |
426 | | * xmlInitParser: |
427 | | * |
428 | | * Initialization function for the XML parser. |
429 | | * |
430 | | * For older versions, it's recommended to call this function once |
431 | | * from the main thread before using the library in multithreaded |
432 | | * programs. |
433 | | * |
434 | | * Since 2.14.0, there's no distinction between threads. It should |
435 | | * be unnecessary to call this function. |
436 | | */ |
437 | | void |
438 | 8.17M | xmlInitParser(void) { |
439 | 8.17M | #ifdef HAVE_POSIX_THREADS |
440 | 8.17M | pthread_once(&onceControl, xmlInitParserInternal); |
441 | | #elif defined(HAVE_WIN32_THREADS) |
442 | | InitOnceExecuteOnce(&onceControl, xmlInitParserWinWrapper, NULL, NULL); |
443 | | #else |
444 | | if (onceControl == 0) { |
445 | | xmlInitParserInternal(); |
446 | | onceControl = 1; |
447 | | } |
448 | | #endif |
449 | 8.17M | } |
450 | | |
451 | | /** |
452 | | * xmlCleanupParser: |
453 | | * |
454 | | * This function is named somewhat misleadingly. It does not clean up |
455 | | * parser state but global memory allocated by the library itself. |
456 | | * |
457 | | * Since 2.9.11, cleanup is performed automatically if a shared or |
458 | | * dynamic libxml2 library is unloaded. This function should only |
459 | | * be used to avoid false positives from memory leak checkers in |
460 | | * static builds. |
461 | | * |
462 | | * WARNING: xmlCleanupParser assumes that all other threads that called |
463 | | * libxml2 functions have terminated. No library calls must be made |
464 | | * after calling this function. In general, THIS FUNCTION SHOULD ONLY |
465 | | * BE CALLED RIGHT BEFORE THE WHOLE PROCESS EXITS. |
466 | | */ |
467 | | void |
468 | 9.89k | xmlCleanupParser(void) { |
469 | | /* |
470 | | * Unfortunately, some users call this function to fix memory |
471 | | * leaks on unload with versions before 2.9.11. This can result |
472 | | * in the library being reinitialized, so this use case must |
473 | | * be supported. |
474 | | */ |
475 | 9.89k | if (!xmlParserInitialized) |
476 | 0 | return; |
477 | | |
478 | 9.89k | xmlCleanupCharEncodingHandlers(); |
479 | 9.89k | #ifdef LIBXML_CATALOG_ENABLED |
480 | 9.89k | xmlCatalogCleanup(); |
481 | 9.89k | xmlCleanupCatalogInternal(); |
482 | 9.89k | #endif |
483 | 9.89k | #ifdef LIBXML_SCHEMAS_ENABLED |
484 | 9.89k | xmlSchemaCleanupTypes(); |
485 | 9.89k | xmlRelaxNGCleanupTypes(); |
486 | 9.89k | #endif |
487 | | |
488 | 9.89k | xmlCleanupDictInternal(); |
489 | 9.89k | xmlCleanupRandom(); |
490 | 9.89k | xmlCleanupGlobalsInternal(); |
491 | 9.89k | xmlCleanupThreadsInternal(); |
492 | | |
493 | | /* |
494 | | * Must come after all cleanup functions that call xmlFree which |
495 | | * uses xmlMemMutex in debug mode. |
496 | | */ |
497 | 9.89k | xmlCleanupMemoryInternal(); |
498 | | |
499 | 9.89k | xmlParserInitialized = 0; |
500 | | |
501 | | /* |
502 | | * This is a bit sketchy but should make reinitialization work. |
503 | | */ |
504 | 9.89k | #ifdef HAVE_POSIX_THREADS |
505 | 9.89k | { |
506 | 9.89k | pthread_once_t tmp = PTHREAD_ONCE_INIT; |
507 | 9.89k | memcpy(&onceControl, &tmp, sizeof(tmp)); |
508 | 9.89k | } |
509 | | #elif defined(HAVE_WIN32_THREADS) |
510 | | { |
511 | | INIT_ONCE tmp = INIT_ONCE_STATIC_INIT; |
512 | | memcpy(&onceControl, &tmp, sizeof(tmp)); |
513 | | } |
514 | | #else |
515 | | onceControl = 0; |
516 | | #endif |
517 | 9.89k | } |
518 | | |
519 | | #if defined(HAVE_FUNC_ATTRIBUTE_DESTRUCTOR) && \ |
520 | | !defined(LIBXML_THREAD_ALLOC_ENABLED) && \ |
521 | | !defined(LIBXML_STATIC) && \ |
522 | | !defined(_WIN32) |
523 | | static void |
524 | | ATTRIBUTE_DESTRUCTOR |
525 | 0 | xmlDestructor(void) { |
526 | | /* |
527 | | * Calling custom deallocation functions in a destructor can cause |
528 | | * problems, for example with Nokogiri. |
529 | | */ |
530 | 0 | if (xmlFree == free) |
531 | 0 | xmlCleanupParser(); |
532 | 0 | } |
533 | | #endif |