VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/threads.c@ 24345

Last change on this file since 24345 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette