threads.c 27 KB

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