testThreads.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "libxml.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_SAX1_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_BEOS_THREADS
  12. #include <OS.h>
  13. #endif
  14. #include <string.h>
  15. #if !defined(_MSC_VER)
  16. #include <unistd.h>
  17. #endif
  18. #include <assert.h>
  19. #define MAX_ARGC 20
  20. #ifdef HAVE_PTHREAD_H
  21. static pthread_t tid[MAX_ARGC];
  22. #elif defined HAVE_BEOS_THREADS
  23. static thread_id tid[MAX_ARGC];
  24. #endif
  25. static const char *catalog = "test/threads/complex.xml";
  26. static const char *testfiles[] = {
  27. "test/threads/abc.xml",
  28. "test/threads/acb.xml",
  29. "test/threads/bac.xml",
  30. "test/threads/bca.xml",
  31. "test/threads/cab.xml",
  32. "test/threads/cba.xml",
  33. "test/threads/invalid.xml",
  34. };
  35. static const char *Okay = "OK";
  36. static const char *Failed = "Failed";
  37. #ifndef xmlDoValidityCheckingDefaultValue
  38. #error xmlDoValidityCheckingDefaultValue is not a macro
  39. #endif
  40. #ifndef xmlGenericErrorContext
  41. #error xmlGenericErrorContext is not a macro
  42. #endif
  43. static void *
  44. thread_specific_data(void *private_data)
  45. {
  46. xmlDocPtr myDoc;
  47. const char *filename = (const char *) private_data;
  48. int okay = 1;
  49. if (!strcmp(filename, "test/threads/invalid.xml")) {
  50. xmlDoValidityCheckingDefaultValue = 0;
  51. xmlGenericErrorContext = stdout;
  52. } else {
  53. xmlDoValidityCheckingDefaultValue = 1;
  54. xmlGenericErrorContext = stderr;
  55. }
  56. myDoc = xmlParseFile(filename);
  57. if (myDoc) {
  58. xmlFreeDoc(myDoc);
  59. } else {
  60. printf("parse failed\n");
  61. okay = 0;
  62. }
  63. if (!strcmp(filename, "test/threads/invalid.xml")) {
  64. if (xmlDoValidityCheckingDefaultValue != 0) {
  65. printf("ValidityCheckingDefaultValue override failed\n");
  66. okay = 0;
  67. }
  68. if (xmlGenericErrorContext != stdout) {
  69. printf("xmlGenericErrorContext override failed\n");
  70. okay = 0;
  71. }
  72. } else {
  73. if (xmlDoValidityCheckingDefaultValue != 1) {
  74. printf("ValidityCheckingDefaultValue override failed\n");
  75. okay = 0;
  76. }
  77. if (xmlGenericErrorContext != stderr) {
  78. printf("xmlGenericErrorContext override failed\n");
  79. okay = 0;
  80. }
  81. }
  82. if (okay == 0)
  83. return((void *) Failed);
  84. return ((void *) Okay);
  85. }
  86. #ifdef HAVE_PTHREAD_H
  87. int
  88. main(void)
  89. {
  90. unsigned int i, repeat;
  91. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  92. void *results[MAX_ARGC];
  93. int ret;
  94. xmlInitParser();
  95. for (repeat = 0;repeat < 500;repeat++) {
  96. xmlLoadCatalog(catalog);
  97. for (i = 0; i < num_threads; i++) {
  98. results[i] = NULL;
  99. tid[i] = (pthread_t) -1;
  100. }
  101. for (i = 0; i < num_threads; i++) {
  102. ret = pthread_create(&tid[i], NULL, thread_specific_data,
  103. (void *) testfiles[i]);
  104. if (ret != 0) {
  105. perror("pthread_create");
  106. exit(1);
  107. }
  108. }
  109. for (i = 0; i < num_threads; i++) {
  110. ret = pthread_join(tid[i], &results[i]);
  111. if (ret != 0) {
  112. perror("pthread_join");
  113. exit(1);
  114. }
  115. }
  116. xmlCatalogCleanup();
  117. for (i = 0; i < num_threads; i++)
  118. if (results[i] != (void *) Okay)
  119. printf("Thread %d handling %s failed\n", i, testfiles[i]);
  120. }
  121. xmlCleanupParser();
  122. xmlMemoryDump();
  123. return (0);
  124. }
  125. #elif defined HAVE_BEOS_THREADS
  126. int
  127. main(void)
  128. {
  129. unsigned int i, repeat;
  130. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  131. void *results[MAX_ARGC];
  132. status_t ret;
  133. xmlInitParser();
  134. printf("Parser initialized\n");
  135. for (repeat = 0;repeat < 500;repeat++) {
  136. printf("repeat: %d\n",repeat);
  137. xmlLoadCatalog(catalog);
  138. printf("loaded catalog: %s\n", catalog);
  139. for (i = 0; i < num_threads; i++) {
  140. results[i] = NULL;
  141. tid[i] = (thread_id) -1;
  142. }
  143. printf("cleaned threads\n");
  144. for (i = 0; i < num_threads; i++) {
  145. tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
  146. if (tid[i] < B_OK) {
  147. perror("beos_thread_create");
  148. exit(1);
  149. }
  150. printf("beos_thread_create %d -> %d\n", i, tid[i]);
  151. }
  152. for (i = 0; i < num_threads; i++) {
  153. ret = wait_for_thread(tid[i], &results[i]);
  154. printf("beos_thread_wait %d -> %d\n", i, ret);
  155. if (ret != B_OK) {
  156. perror("beos_thread_wait");
  157. exit(1);
  158. }
  159. }
  160. xmlCatalogCleanup();
  161. ret = B_OK;
  162. for (i = 0; i < num_threads; i++)
  163. if (results[i] != (void *) Okay) {
  164. printf("Thread %d handling %s failed\n", i, testfiles[i]);
  165. ret = B_ERROR;
  166. }
  167. }
  168. xmlCleanupParser();
  169. xmlMemoryDump();
  170. if (ret == B_OK)
  171. printf("testThread : BeOS : SUCCESS!\n");
  172. else
  173. printf("testThread : BeOS : FAILED!\n");
  174. return (0);
  175. }
  176. #endif /* pthreads or BeOS threads */
  177. #else /* !LIBXML_THREADS_ENABLED */
  178. int
  179. main(void)
  180. {
  181. fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
  182. return (0);
  183. }
  184. #endif