blob: bef6537744b289f44da3df1adbd9be1d587c3396 [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
Nick Wellnhofer01a4b812017-06-16 21:27:47 +02006#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_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>
Haibo Huangcfd91dc2020-07-30 23:01:33 -070013#elif defined HAVE_WIN32_THREADS
14#include <windows.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000015#elif defined HAVE_BEOS_THREADS
16#include <OS.h>
17#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000018#include <string.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000019#if !defined(_MSC_VER)
Daniel Veillardab7488e2001-10-17 11:30:37 +000020#include <unistd.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000021#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000022#include <assert.h>
23
24#define MAX_ARGC 20
Haibo Huangcfd91dc2020-07-30 23:01:33 -070025#define TEST_REPEAT_COUNT 500
Daniel Veillard82cb3192003-10-29 13:39:15 +000026#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000027static pthread_t tid[MAX_ARGC];
Haibo Huangcfd91dc2020-07-30 23:01:33 -070028#elif defined HAVE_WIN32_THREADS
29static HANDLE tid[MAX_ARGC];
Daniel Veillard82cb3192003-10-29 13:39:15 +000030#elif defined HAVE_BEOS_THREADS
31static thread_id tid[MAX_ARGC];
32#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000033
Haibo Huangcfd91dc2020-07-30 23:01:33 -070034typedef struct {
35 const char *filename;
36 int okay;
37} xmlThreadParams;
Daniel Veillardab7488e2001-10-17 11:30:37 +000038
Haibo Huangcfd91dc2020-07-30 23:01:33 -070039static const char *catalog = "test/threads/complex.xml";
40static xmlThreadParams threadParams[] = {
41 { "test/threads/abc.xml", 0 },
42 { "test/threads/acb.xml", 0 },
43 { "test/threads/bac.xml", 0 },
44 { "test/threads/bca.xml", 0 },
45 { "test/threads/cab.xml", 0 },
46 { "test/threads/cba.xml", 0 },
47 { "test/threads/invalid.xml", 0 }
48};
49static const unsigned int num_threads = sizeof(threadParams) /
50 sizeof(threadParams[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000051
52#ifndef xmlDoValidityCheckingDefaultValue
53#error xmlDoValidityCheckingDefaultValue is not a macro
54#endif
55#ifndef xmlGenericErrorContext
56#error xmlGenericErrorContext is not a macro
57#endif
58
Daniel Veillardab7488e2001-10-17 11:30:37 +000059static void *
60thread_specific_data(void *private_data)
61{
62 xmlDocPtr myDoc;
Haibo Huangcfd91dc2020-07-30 23:01:33 -070063 xmlThreadParams *params = (xmlThreadParams *) private_data;
64 const char *filename = params->filename;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000065 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000066
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000067 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000068 xmlDoValidityCheckingDefaultValue = 0;
69 xmlGenericErrorContext = stdout;
70 } else {
71 xmlDoValidityCheckingDefaultValue = 1;
72 xmlGenericErrorContext = stderr;
73 }
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020074#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardab7488e2001-10-17 11:30:37 +000075 myDoc = xmlParseFile(filename);
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020076#else
77 myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
78#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000079 if (myDoc) {
80 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000081 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000082 printf("parse failed\n");
83 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000084 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000085 if (!strcmp(filename, "test/threads/invalid.xml")) {
86 if (xmlDoValidityCheckingDefaultValue != 0) {
87 printf("ValidityCheckingDefaultValue override failed\n");
88 okay = 0;
89 }
90 if (xmlGenericErrorContext != stdout) {
91 printf("xmlGenericErrorContext override failed\n");
92 okay = 0;
93 }
94 } else {
95 if (xmlDoValidityCheckingDefaultValue != 1) {
96 printf("ValidityCheckingDefaultValue override failed\n");
97 okay = 0;
98 }
99 if (xmlGenericErrorContext != stderr) {
100 printf("xmlGenericErrorContext override failed\n");
101 okay = 0;
102 }
103 }
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700104 params->okay = okay;
105 return(NULL);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000106}
107
Daniel Veillard82cb3192003-10-29 13:39:15 +0000108#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +0000109int
William M. Bracka71a8ef2003-08-06 04:43:55 +0000110main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000111{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000112 unsigned int i, repeat;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000113 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000114
115 xmlInitParser();
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700116 for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000117 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000118
Andrew W. Nosenkod794a842010-11-15 13:00:29 +0100119 memset(tid, 0xff, sizeof(*tid)*num_threads);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000120
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000121 for (i = 0; i < num_threads; i++) {
Daniel Veillard24505b02005-07-28 23:49:35 +0000122 ret = pthread_create(&tid[i], NULL, thread_specific_data,
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700123 (void *) &threadParams[i]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000124 if (ret != 0) {
125 perror("pthread_create");
126 exit(1);
127 }
128 }
129 for (i = 0; i < num_threads; i++) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700130 void *result;
131 ret = pthread_join(tid[i], &result);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000132 if (ret != 0) {
133 perror("pthread_join");
134 exit(1);
135 }
136 }
137
138 xmlCatalogCleanup();
139 for (i = 0; i < num_threads; i++)
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700140 if (threadParams[i].okay == 0)
141 printf("Thread %d handling %s failed\n", i,
142 threadParams[i].filename);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000143 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000144 xmlCleanupParser();
145 xmlMemoryDump();
146 return (0);
147}
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700148#elif defined HAVE_WIN32_THREADS
149static DWORD WINAPI
150win32_thread_specific_data(void *private_data)
151{
152 thread_specific_data(private_data);
153 return(0);
154}
155
156int
157main(void)
158{
159 unsigned int i, repeat;
160 BOOL ret;
161
162 xmlInitParser();
163 for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++)
164 {
165 xmlLoadCatalog(catalog);
166
167 for (i = 0; i < num_threads; i++)
168 {
169 tid[i] = (HANDLE) -1;
170 }
171
172 for (i = 0; i < num_threads; i++)
173 {
174 DWORD useless;
175 tid[i] = CreateThread(NULL, 0,
176 win32_thread_specific_data, &threadParams[i], 0, &useless);
177 if (tid[i] == NULL)
178 {
179 perror("CreateThread");
180 exit(1);
181 }
182 }
183
184 if (WaitForMultipleObjects (num_threads, tid, TRUE, INFINITE) == WAIT_FAILED)
185 perror ("WaitForMultipleObjects failed");
186
187 for (i = 0; i < num_threads; i++)
188 {
189 DWORD exitCode;
190 ret = GetExitCodeThread (tid[i], &exitCode);
191 if (ret == 0)
192 {
193 perror("GetExitCodeThread");
194 exit(1);
195 }
196 CloseHandle (tid[i]);
197 }
198
199 xmlCatalogCleanup();
200 for (i = 0; i < num_threads; i++) {
201 if (threadParams[i].okay == 0)
202 printf("Thread %d handling %s failed\n", i,
203 threadParams[i].filename);
204 }
205 }
206
207 xmlCleanupParser();
208 xmlMemoryDump();
209
210 return (0);
211}
Daniel Veillard82cb3192003-10-29 13:39:15 +0000212#elif defined HAVE_BEOS_THREADS
213int
214main(void)
215{
216 unsigned int i, repeat;
Daniel Veillard82cb3192003-10-29 13:39:15 +0000217 status_t ret;
218
219 xmlInitParser();
220 printf("Parser initialized\n");
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700221 for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000222 printf("repeat: %d\n",repeat);
223 xmlLoadCatalog(catalog);
224 printf("loaded catalog: %s\n", catalog);
225 for (i = 0; i < num_threads; i++) {
Daniel Veillard82cb3192003-10-29 13:39:15 +0000226 tid[i] = (thread_id) -1;
227 }
228 printf("cleaned threads\n");
229 for (i = 0; i < num_threads; i++) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700230 tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) &threadParams[i]);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000231 if (tid[i] < B_OK) {
232 perror("beos_thread_create");
233 exit(1);
234 }
235 printf("beos_thread_create %d -> %d\n", i, tid[i]);
236 }
237 for (i = 0; i < num_threads; i++) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700238 void *result;
239 ret = wait_for_thread(tid[i], &result);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000240 printf("beos_thread_wait %d -> %d\n", i, ret);
241 if (ret != B_OK) {
242 perror("beos_thread_wait");
243 exit(1);
244 }
245 }
246
247 xmlCatalogCleanup();
248 ret = B_OK;
249 for (i = 0; i < num_threads; i++)
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700250 if (threadParams[i].okay == 0) {
251 printf("Thread %d handling %s failed\n", i,
252 threadParams[i].filename);
Daniel Veillard82cb3192003-10-29 13:39:15 +0000253 ret = B_ERROR;
254 }
255 }
256 xmlCleanupParser();
257 xmlMemoryDump();
258
259 if (ret == B_OK)
260 printf("testThread : BeOS : SUCCESS!\n");
261 else
262 printf("testThread : BeOS : FAILED!\n");
263
264 return (0);
265}
266#endif /* pthreads or BeOS threads */
Daniel Veillardab7488e2001-10-17 11:30:37 +0000267
268#else /* !LIBXML_THREADS_ENABLED */
269int
Daniel Veillard118aed72002-09-24 14:13:13 +0000270main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000271{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000272 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000273 return (0);
274}
275#endif