testThreads.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "libxml.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  5. #include <libxml/globals.h>
  6. #include <libxml/threads.h>
  7. #include <libxml/parser.h>
  8. #include <libxml/catalog.h>
  9. #ifdef HAVE_PTHREAD_H
  10. #include <pthread.h>
  11. #elif defined HAVE_WIN32_THREADS
  12. #include <windows.h>
  13. #elif defined HAVE_BEOS_THREADS
  14. #include <OS.h>
  15. #endif
  16. #include <string.h>
  17. #if !defined(_MSC_VER)
  18. #include <unistd.h>
  19. #endif
  20. #include <assert.h>
  21. #define MAX_ARGC 20
  22. #define TEST_REPEAT_COUNT 500
  23. #ifdef HAVE_PTHREAD_H
  24. static pthread_t tid[MAX_ARGC];
  25. #elif defined HAVE_WIN32_THREADS
  26. static HANDLE tid[MAX_ARGC];
  27. #elif defined HAVE_BEOS_THREADS
  28. static thread_id tid[MAX_ARGC];
  29. #endif
  30. typedef struct {
  31. const char *filename;
  32. int okay;
  33. } xmlThreadParams;
  34. static const char *catalog = "test/threads/complex.xml";
  35. static xmlThreadParams threadParams[] = {
  36. { "test/threads/abc.xml", 0 },
  37. { "test/threads/acb.xml", 0 },
  38. { "test/threads/bac.xml", 0 },
  39. { "test/threads/bca.xml", 0 },
  40. { "test/threads/cab.xml", 0 },
  41. { "test/threads/cba.xml", 0 },
  42. { "test/threads/invalid.xml", 0 }
  43. };
  44. static const unsigned int num_threads = sizeof(threadParams) /
  45. sizeof(threadParams[0]);
  46. #ifndef xmlDoValidityCheckingDefaultValue
  47. #error xmlDoValidityCheckingDefaultValue is not a macro
  48. #endif
  49. #ifndef xmlGenericErrorContext
  50. #error xmlGenericErrorContext is not a macro
  51. #endif
  52. static void *
  53. thread_specific_data(void *private_data)
  54. {
  55. xmlDocPtr myDoc;
  56. xmlThreadParams *params = (xmlThreadParams *) private_data;
  57. const char *filename = params->filename;
  58. int okay = 1;
  59. if (!strcmp(filename, "test/threads/invalid.xml")) {
  60. xmlDoValidityCheckingDefaultValue = 0;
  61. xmlGenericErrorContext = stdout;
  62. } else {
  63. xmlDoValidityCheckingDefaultValue = 1;
  64. xmlGenericErrorContext = stderr;
  65. }
  66. #ifdef LIBXML_SAX1_ENABLED
  67. myDoc = xmlParseFile(filename);
  68. #else
  69. myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
  70. #endif
  71. if (myDoc) {
  72. xmlFreeDoc(myDoc);
  73. } else {
  74. printf("parse failed\n");
  75. okay = 0;
  76. }
  77. if (!strcmp(filename, "test/threads/invalid.xml")) {
  78. if (xmlDoValidityCheckingDefaultValue != 0) {
  79. printf("ValidityCheckingDefaultValue override failed\n");
  80. okay = 0;
  81. }
  82. if (xmlGenericErrorContext != stdout) {
  83. printf("xmlGenericErrorContext override failed\n");
  84. okay = 0;
  85. }
  86. } else {
  87. if (xmlDoValidityCheckingDefaultValue != 1) {
  88. printf("ValidityCheckingDefaultValue override failed\n");
  89. okay = 0;
  90. }
  91. if (xmlGenericErrorContext != stderr) {
  92. printf("xmlGenericErrorContext override failed\n");
  93. okay = 0;
  94. }
  95. }
  96. params->okay = okay;
  97. return(NULL);
  98. }
  99. #ifdef HAVE_PTHREAD_H
  100. int
  101. main(void)
  102. {
  103. unsigned int i, repeat;
  104. int ret;
  105. xmlInitParser();
  106. for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
  107. xmlLoadCatalog(catalog);
  108. memset(tid, 0xff, sizeof(*tid)*num_threads);
  109. for (i = 0; i < num_threads; i++) {
  110. ret = pthread_create(&tid[i], NULL, thread_specific_data,
  111. (void *) &threadParams[i]);
  112. if (ret != 0) {
  113. perror("pthread_create");
  114. exit(1);
  115. }
  116. }
  117. for (i = 0; i < num_threads; i++) {
  118. void *result;
  119. ret = pthread_join(tid[i], &result);
  120. if (ret != 0) {
  121. perror("pthread_join");
  122. exit(1);
  123. }
  124. }
  125. xmlCatalogCleanup();
  126. for (i = 0; i < num_threads; i++)
  127. if (threadParams[i].okay == 0)
  128. printf("Thread %d handling %s failed\n", i,
  129. threadParams[i].filename);
  130. }
  131. xmlCleanupParser();
  132. xmlMemoryDump();
  133. return (0);
  134. }
  135. #elif defined HAVE_WIN32_THREADS
  136. static DWORD WINAPI
  137. win32_thread_specific_data(void *private_data)
  138. {
  139. thread_specific_data(private_data);
  140. return(0);
  141. }
  142. int
  143. main(void)
  144. {
  145. unsigned int i, repeat;
  146. BOOL ret;
  147. xmlInitParser();
  148. for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++)
  149. {
  150. xmlLoadCatalog(catalog);
  151. for (i = 0; i < num_threads; i++)
  152. {
  153. tid[i] = (HANDLE) -1;
  154. }
  155. for (i = 0; i < num_threads; i++)
  156. {
  157. DWORD useless;
  158. tid[i] = CreateThread(NULL, 0,
  159. win32_thread_specific_data, &threadParams[i], 0, &useless);
  160. if (tid[i] == NULL)
  161. {
  162. perror("CreateThread");
  163. exit(1);
  164. }
  165. }
  166. if (WaitForMultipleObjects (num_threads, tid, TRUE, INFINITE) == WAIT_FAILED)
  167. perror ("WaitForMultipleObjects failed");
  168. for (i = 0; i < num_threads; i++)
  169. {
  170. DWORD exitCode;
  171. ret = GetExitCodeThread (tid[i], &exitCode);
  172. if (ret == 0)
  173. {
  174. perror("GetExitCodeThread");
  175. exit(1);
  176. }
  177. CloseHandle (tid[i]);
  178. }
  179. xmlCatalogCleanup();
  180. for (i = 0; i < num_threads; i++) {
  181. if (threadParams[i].okay == 0)
  182. printf("Thread %d handling %s failed\n", i,
  183. threadParams[i].filename);
  184. }
  185. }
  186. xmlCleanupParser();
  187. xmlMemoryDump();
  188. return (0);
  189. }
  190. #elif defined HAVE_BEOS_THREADS
  191. int
  192. main(void)
  193. {
  194. unsigned int i, repeat;
  195. status_t ret;
  196. xmlInitParser();
  197. printf("Parser initialized\n");
  198. for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
  199. printf("repeat: %d\n",repeat);
  200. xmlLoadCatalog(catalog);
  201. printf("loaded catalog: %s\n", catalog);
  202. for (i = 0; i < num_threads; i++) {
  203. tid[i] = (thread_id) -1;
  204. }
  205. printf("cleaned threads\n");
  206. for (i = 0; i < num_threads; i++) {
  207. tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) &threadParams[i]);
  208. if (tid[i] < B_OK) {
  209. perror("beos_thread_create");
  210. exit(1);
  211. }
  212. printf("beos_thread_create %d -> %d\n", i, tid[i]);
  213. }
  214. for (i = 0; i < num_threads; i++) {
  215. void *result;
  216. ret = wait_for_thread(tid[i], &result);
  217. printf("beos_thread_wait %d -> %d\n", i, ret);
  218. if (ret != B_OK) {
  219. perror("beos_thread_wait");
  220. exit(1);
  221. }
  222. }
  223. xmlCatalogCleanup();
  224. ret = B_OK;
  225. for (i = 0; i < num_threads; i++)
  226. if (threadParams[i].okay == 0) {
  227. printf("Thread %d handling %s failed\n", i,
  228. threadParams[i].filename);
  229. ret = B_ERROR;
  230. }
  231. }
  232. xmlCleanupParser();
  233. xmlMemoryDump();
  234. if (ret == B_OK)
  235. printf("testThread : BeOS : SUCCESS!\n");
  236. else
  237. printf("testThread : BeOS : FAILED!\n");
  238. return (0);
  239. }
  240. #endif /* pthreads or BeOS threads */
  241. #else /* !LIBXML_THREADS_ENABLED */
  242. int
  243. main(void)
  244. {
  245. fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
  246. return (0);
  247. }
  248. #endif