blob: bff53912ecbf93689532256dc8ba28ecd7b991a5 [file] [log] [blame]
William M. Brackc1099be2007-01-31 18:38:56 +00001#include "libxml.h"
2
Daniel Veillardab7488e2001-10-17 11:30:37 +00003#include <stdlib.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00004#include <stdio.h>
Daniel Veillardab7488e2001-10-17 11:30:37 +00005
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00006#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_SAX1_ENABLED)
Daniel Veillardab7488e2001-10-17 11:30:37 +00007#include <libxml/globals.h>
8#include <libxml/threads.h>
9#include <libxml/parser.h>
10#include <libxml/catalog.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000011#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000012#include <pthread.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000013#elif defined HAVE_BEOS_THREADS
14#include <OS.h>
15#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000016#include <string.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000017#if !defined(_MSC_VER)
Daniel Veillardab7488e2001-10-17 11:30:37 +000018#include <unistd.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000019#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000020#include <assert.h>
21
22#define MAX_ARGC 20
Daniel Veillard82cb3192003-10-29 13:39:15 +000023#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000024static pthread_t tid[MAX_ARGC];
Daniel Veillard82cb3192003-10-29 13:39:15 +000025#elif defined HAVE_BEOS_THREADS
26static thread_id tid[MAX_ARGC];
27#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000028
29static const char *catalog = "test/threads/complex.xml";
30static 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 Veillard24505b02005-07-28 23:49:35 +000040static const char *Okay = "OK";
41static const char *Failed = "Failed";
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000042
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 Veillardab7488e2001-10-17 11:30:37 +000050static void *
51thread_specific_data(void *private_data)
52{
53 xmlDocPtr myDoc;
54 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000055 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000056
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000057 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000058 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 Veillardab7488e2001-10-17 11:30:37 +000067 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000068 printf("parse failed\n");
69 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000070 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000071 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 Veillardab7488e2001-10-17 11:30:37 +000093}
94
Daniel Veillard82cb3192003-10-29 13:39:15 +000095#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000096int
William M. Bracka71a8ef2003-08-06 04:43:55 +000097main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +000098{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000099 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000100 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000101 void *results[MAX_ARGC];
102 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000103
104 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +0000105 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000106 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000107
Andrew W. Nosenkod794a842010-11-15 13:00:29 +0100108 memset(results, 0, sizeof(*results)*num_threads);
109 memset(tid, 0xff, sizeof(*tid)*num_threads);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000110
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000111 for (i = 0; i < num_threads; i++) {
Daniel Veillard24505b02005-07-28 23:49:35 +0000112 ret = pthread_create(&tid[i], NULL, thread_specific_data,
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000113 (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 Veillardab7488e2001-10-17 11:30:37 +0000132 xmlCleanupParser();
133 xmlMemoryDump();
134 return (0);
135}
Daniel Veillard82cb3192003-10-29 13:39:15 +0000136#elif defined HAVE_BEOS_THREADS
137int
138main(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 Veillardab7488e2001-10-17 11:30:37 +0000192
193#else /* !LIBXML_THREADS_ENABLED */
194int
Daniel Veillard118aed72002-09-24 14:13:13 +0000195main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000196{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000197 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000198 return (0);
199}
200#endif