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