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