testThreads.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <libxml/parser.h>
  5. #include <libxml/threads.h>
  6. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  7. #include <libxml/catalog.h>
  8. #ifdef HAVE_PTHREAD_H
  9. #include <pthread.h>
  10. #elif defined(_WIN32)
  11. #include <windows.h>
  12. #endif
  13. #include <string.h>
  14. #include <assert.h>
  15. #define MAX_ARGC 20
  16. #define TEST_REPEAT_COUNT 500
  17. #ifdef HAVE_PTHREAD_H
  18. static pthread_t tid[MAX_ARGC];
  19. #elif defined(_WIN32)
  20. static HANDLE tid[MAX_ARGC];
  21. #endif
  22. typedef struct {
  23. const char *filename;
  24. int okay;
  25. } xmlThreadParams;
  26. static const char *catalog = "test/threads/complex.xml";
  27. static xmlThreadParams threadParams[] = {
  28. { "test/threads/abc.xml", 0 },
  29. { "test/threads/acb.xml", 0 },
  30. { "test/threads/bac.xml", 0 },
  31. { "test/threads/bca.xml", 0 },
  32. { "test/threads/cab.xml", 0 },
  33. { "test/threads/cba.xml", 0 },
  34. { "test/threads/invalid.xml", 0 }
  35. };
  36. static const unsigned int num_threads = sizeof(threadParams) /
  37. sizeof(threadParams[0]);
  38. static void *
  39. thread_specific_data(void *private_data)
  40. {
  41. xmlDocPtr myDoc;
  42. xmlThreadParams *params = (xmlThreadParams *) private_data;
  43. const char *filename = params->filename;
  44. int okay = 1;
  45. int options = 0;
  46. if (xmlCheckThreadLocalStorage() != 0) {
  47. printf("xmlCheckThreadLocalStorage failed\n");
  48. params->okay = 0;
  49. return(NULL);
  50. }
  51. if (strcmp(filename, "test/threads/invalid.xml") != 0) {
  52. options |= XML_PARSE_DTDVALID;
  53. }
  54. myDoc = xmlReadFile(filename, NULL, options);
  55. if (myDoc) {
  56. xmlFreeDoc(myDoc);
  57. } else {
  58. printf("parse failed\n");
  59. okay = 0;
  60. }
  61. params->okay = okay;
  62. return(NULL);
  63. }
  64. #ifdef _WIN32
  65. static DWORD WINAPI
  66. win32_thread_specific_data(void *private_data)
  67. {
  68. thread_specific_data(private_data);
  69. return(0);
  70. }
  71. #endif
  72. #endif /* LIBXML_THREADS_ENABLED */
  73. int
  74. main(void)
  75. {
  76. unsigned int repeat;
  77. int status = 0;
  78. (void) repeat;
  79. xmlInitParser();
  80. if (xmlCheckThreadLocalStorage() != 0) {
  81. printf("xmlCheckThreadLocalStorage failed for main thread\n");
  82. return(1);
  83. }
  84. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  85. for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
  86. unsigned int i;
  87. int ret;
  88. xmlLoadCatalog(catalog);
  89. #ifdef HAVE_PTHREAD_H
  90. memset(tid, 0xff, sizeof(*tid)*num_threads);
  91. for (i = 0; i < num_threads; i++) {
  92. ret = pthread_create(&tid[i], NULL, thread_specific_data,
  93. (void *) &threadParams[i]);
  94. if (ret != 0) {
  95. perror("pthread_create");
  96. exit(1);
  97. }
  98. }
  99. for (i = 0; i < num_threads; i++) {
  100. void *result;
  101. ret = pthread_join(tid[i], &result);
  102. if (ret != 0) {
  103. perror("pthread_join");
  104. exit(1);
  105. }
  106. }
  107. #elif defined(_WIN32)
  108. for (i = 0; i < num_threads; i++)
  109. {
  110. tid[i] = (HANDLE) -1;
  111. }
  112. for (i = 0; i < num_threads; i++)
  113. {
  114. DWORD useless;
  115. tid[i] = CreateThread(NULL, 0,
  116. win32_thread_specific_data, &threadParams[i], 0, &useless);
  117. if (tid[i] == NULL)
  118. {
  119. perror("CreateThread");
  120. exit(1);
  121. }
  122. }
  123. if (WaitForMultipleObjects (num_threads, tid, TRUE, INFINITE) == WAIT_FAILED)
  124. perror ("WaitForMultipleObjects failed");
  125. for (i = 0; i < num_threads; i++)
  126. {
  127. DWORD exitCode;
  128. ret = GetExitCodeThread (tid[i], &exitCode);
  129. if (ret == 0)
  130. {
  131. perror("GetExitCodeThread");
  132. exit(1);
  133. }
  134. CloseHandle (tid[i]);
  135. }
  136. #endif /* pthreads */
  137. xmlCatalogCleanup();
  138. for (i = 0; i < num_threads; i++) {
  139. if (threadParams[i].okay == 0) {
  140. printf("Thread %d handling %s failed\n", i,
  141. threadParams[i].filename);
  142. status = 1;
  143. }
  144. }
  145. }
  146. #endif /* LIBXML_THREADS_ENABLED */
  147. xmlCleanupParser();
  148. return (status);
  149. }