threads.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. #endif
  804. }
  805. /**
  806. * xmlCleanupThreads:
  807. *
  808. * xmlCleanupThreads() is used to to cleanup all the thread related
  809. * data of the libxml2 library once processing has ended.
  810. *
  811. * WARNING: if your application is multithreaded or has plugin support
  812. * calling this may crash the application if another thread or
  813. * a plugin is still using libxml2. It's sometimes very hard to
  814. * guess if libxml2 is in use in the application, some libraries
  815. * or plugins may use it without notice. In case of doubt abstain
  816. * from calling this function or do it just before calling exit()
  817. * to avoid leak reports from valgrind !
  818. */
  819. void
  820. xmlCleanupThreads(void)
  821. {
  822. #ifdef DEBUG_THREADS
  823. xmlGenericError(xmlGenericErrorContext, "xmlCleanupThreads()\n");
  824. #endif
  825. #ifdef HAVE_PTHREAD_H
  826. if (libxml_is_threaded != 0)
  827. pthread_key_delete(globalkey);
  828. once_control = once_control_init;
  829. #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
  830. if (globalkey != TLS_OUT_OF_INDEXES) {
  831. xmlGlobalStateCleanupHelperParams *p;
  832. EnterCriticalSection(&cleanup_helpers_cs);
  833. p = cleanup_helpers_head;
  834. while (p != NULL) {
  835. xmlGlobalStateCleanupHelperParams *temp = p;
  836. p = p->next;
  837. xmlFreeGlobalState(temp->memory);
  838. free(temp);
  839. }
  840. cleanup_helpers_head = 0;
  841. LeaveCriticalSection(&cleanup_helpers_cs);
  842. TlsFree(globalkey);
  843. globalkey = TLS_OUT_OF_INDEXES;
  844. }
  845. DeleteCriticalSection(&cleanup_helpers_cs);
  846. #endif
  847. }
  848. #ifdef LIBXML_THREAD_ENABLED
  849. /**
  850. * xmlOnceInit
  851. *
  852. * xmlOnceInit() is used to initialize the value of mainthread for use
  853. * in other routines. This function should only be called using
  854. * pthread_once() in association with the once_control variable to ensure
  855. * that the function is only called once. See man pthread_once for more
  856. * details.
  857. */
  858. static void
  859. xmlOnceInit(void)
  860. {
  861. #ifdef HAVE_PTHREAD_H
  862. (void) pthread_key_create(&globalkey, xmlFreeGlobalState);
  863. mainthread = pthread_self();
  864. __xmlInitializeDict();
  865. #elif defined(HAVE_WIN32_THREADS)
  866. if (!run_once.done) {
  867. if (InterlockedIncrement(&run_once.control) == 1) {
  868. #if !defined(HAVE_COMPILER_TLS)
  869. #if !defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)
  870. InitializeCriticalSection(&cleanup_helpers_cs);
  871. #endif
  872. globalkey = TlsAlloc();
  873. #endif
  874. mainthread = GetCurrentThreadId();
  875. __xmlInitializeDict();
  876. run_once.done = 1;
  877. } else {
  878. /* Another thread is working; give up our slice and
  879. * wait until they're done. */
  880. while (!run_once.done)
  881. Sleep(0);
  882. }
  883. }
  884. #elif defined HAVE_BEOS_THREADS
  885. if (atomic_add(&run_once_init, 1) == 0) {
  886. globalkey = tls_allocate();
  887. tls_set(globalkey, NULL);
  888. mainthread = find_thread(NULL);
  889. __xmlInitializeDict();
  890. } else
  891. atomic_add(&run_once_init, -1);
  892. #endif
  893. }
  894. #endif
  895. /**
  896. * DllMain:
  897. * @hinstDLL: handle to DLL instance
  898. * @fdwReason: Reason code for entry
  899. * @lpvReserved: generic pointer (depends upon reason code)
  900. *
  901. * Entry point for Windows library. It is being used to free thread-specific
  902. * storage.
  903. *
  904. * Returns TRUE always
  905. */
  906. #ifdef HAVE_PTHREAD_H
  907. #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL))
  908. #if defined(LIBXML_STATIC_FOR_DLL)
  909. int XMLCALL
  910. xmlDllMain(ATTRIBUTE_UNUSED void *hinstDLL, unsigned long fdwReason,
  911. ATTRIBUTE_UNUSED void *lpvReserved)
  912. #else
  913. /* declare to avoid "no previous prototype for 'DllMain'" warning */
  914. /* Note that we do NOT want to include this function declaration in
  915. a public header because it's meant to be called by Windows itself,
  916. not a program that uses this library. This also has to be exported. */
  917. XMLPUBFUN BOOL WINAPI
  918. DllMain (HINSTANCE hinstDLL,
  919. DWORD fdwReason,
  920. LPVOID lpvReserved);
  921. BOOL WINAPI
  922. DllMain(ATTRIBUTE_UNUSED HINSTANCE hinstDLL, DWORD fdwReason,
  923. ATTRIBUTE_UNUSED LPVOID lpvReserved)
  924. #endif
  925. {
  926. switch (fdwReason) {
  927. case DLL_THREAD_DETACH:
  928. if (globalkey != TLS_OUT_OF_INDEXES) {
  929. xmlGlobalState *globalval = NULL;
  930. xmlGlobalStateCleanupHelperParams *p =
  931. (xmlGlobalStateCleanupHelperParams *)
  932. TlsGetValue(globalkey);
  933. globalval = (xmlGlobalState *) (p ? p->memory : NULL);
  934. if (globalval) {
  935. xmlFreeGlobalState(globalval);
  936. TlsSetValue(globalkey, NULL);
  937. }
  938. if (p) {
  939. EnterCriticalSection(&cleanup_helpers_cs);
  940. if (p == cleanup_helpers_head)
  941. cleanup_helpers_head = p->next;
  942. else
  943. p->prev->next = p->next;
  944. if (p->next != NULL)
  945. p->next->prev = p->prev;
  946. LeaveCriticalSection(&cleanup_helpers_cs);
  947. free(p);
  948. }
  949. }
  950. break;
  951. }
  952. return TRUE;
  953. }
  954. #endif
  955. #define bottom_threads
  956. #include "elfgcchack.h"