blob: 709ae76905d4be17abb2771c5b2cb54a332aec8e [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>
Nick Wellnhofer1e60c762022-09-04 01:49:41 +020013#elif defined(_WIN32)
Nick Wellnhofer57d43292018-01-23 17:33:42 +010014#include <windows.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000015#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000016#include <string.h>
Daniel Veillardab7488e2001-10-17 11:30:37 +000017#include <assert.h>
18
19#define MAX_ARGC 20
Nick Wellnhofer57d43292018-01-23 17:33:42 +010020#define TEST_REPEAT_COUNT 500
Daniel Veillard82cb3192003-10-29 13:39:15 +000021#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000022static pthread_t tid[MAX_ARGC];
Nick Wellnhofer1e60c762022-09-04 01:49:41 +020023#elif defined(_WIN32)
Nick Wellnhofer57d43292018-01-23 17:33:42 +010024static HANDLE tid[MAX_ARGC];
Daniel Veillard82cb3192003-10-29 13:39:15 +000025#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000026
Nick Wellnhofer57d43292018-01-23 17:33:42 +010027typedef struct {
28 const char *filename;
29 int okay;
30} xmlThreadParams;
Daniel Veillardab7488e2001-10-17 11:30:37 +000031
Nick Wellnhofer57d43292018-01-23 17:33:42 +010032static const char *catalog = "test/threads/complex.xml";
33static xmlThreadParams threadParams[] = {
34 { "test/threads/abc.xml", 0 },
35 { "test/threads/acb.xml", 0 },
36 { "test/threads/bac.xml", 0 },
37 { "test/threads/bca.xml", 0 },
38 { "test/threads/cab.xml", 0 },
39 { "test/threads/cba.xml", 0 },
40 { "test/threads/invalid.xml", 0 }
41};
42static const unsigned int num_threads = sizeof(threadParams) /
43 sizeof(threadParams[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000044
45#ifndef xmlDoValidityCheckingDefaultValue
46#error xmlDoValidityCheckingDefaultValue is not a macro
47#endif
48#ifndef xmlGenericErrorContext
49#error xmlGenericErrorContext is not a macro
50#endif
51
Daniel Veillardab7488e2001-10-17 11:30:37 +000052static void *
53thread_specific_data(void *private_data)
54{
55 xmlDocPtr myDoc;
Nick Wellnhofer57d43292018-01-23 17:33:42 +010056 xmlThreadParams *params = (xmlThreadParams *) private_data;
57 const char *filename = params->filename;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000058 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000059
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000060 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000061 xmlDoValidityCheckingDefaultValue = 0;
62 xmlGenericErrorContext = stdout;
63 } else {
64 xmlDoValidityCheckingDefaultValue = 1;
65 xmlGenericErrorContext = stderr;
66 }
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020067#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardab7488e2001-10-17 11:30:37 +000068 myDoc = xmlParseFile(filename);
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020069#else
70 myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
71#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000072 if (myDoc) {
73 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000074 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000075 printf("parse failed\n");
76 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000077 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000078 if (!strcmp(filename, "test/threads/invalid.xml")) {
79 if (xmlDoValidityCheckingDefaultValue != 0) {
80 printf("ValidityCheckingDefaultValue override failed\n");
81 okay = 0;
82 }
83 if (xmlGenericErrorContext != stdout) {
84 printf("xmlGenericErrorContext override failed\n");
85 okay = 0;
86 }
87 } else {
88 if (xmlDoValidityCheckingDefaultValue != 1) {
89 printf("ValidityCheckingDefaultValue override failed\n");
90 okay = 0;
91 }
92 if (xmlGenericErrorContext != stderr) {
93 printf("xmlGenericErrorContext override failed\n");
94 okay = 0;
95 }
96 }
Nick Wellnhofer57d43292018-01-23 17:33:42 +010097 params->okay = okay;
98 return(NULL);
Daniel Veillardab7488e2001-10-17 11:30:37 +000099}
100
Daniel Veillard82cb3192003-10-29 13:39:15 +0000101#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +0000102int
William M. Bracka71a8ef2003-08-06 04:43:55 +0000103main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000104{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000105 unsigned int i, repeat;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000106 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000107
108 xmlInitParser();
Nick Wellnhofer57d43292018-01-23 17:33:42 +0100109 for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000110 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000111
Andrew W. Nosenkod794a842010-11-15 13:00:29 +0100112 memset(tid, 0xff, sizeof(*tid)*num_threads);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000113
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000114 for (i = 0; i < num_threads; i++) {
Daniel Veillard24505b02005-07-28 23:49:35 +0000115 ret = pthread_create(&tid[i], NULL, thread_specific_data,
Nick Wellnhofer57d43292018-01-23 17:33:42 +0100116 (void *) &threadParams[i]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000117 if (ret != 0) {
118 perror("pthread_create");
119 exit(1);
120 }
121 }
122 for (i = 0; i < num_threads; i++) {
Nick Wellnhofer57d43292018-01-23 17:33:42 +0100123 void *result;
124 ret = pthread_join(tid[i], &result);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000125 if (ret != 0) {
126 perror("pthread_join");
127 exit(1);
128 }
129 }
130
131 xmlCatalogCleanup();
132 for (i = 0; i < num_threads; i++)
Nick Wellnhofer57d43292018-01-23 17:33:42 +0100133 if (threadParams[i].okay == 0)
134 printf("Thread %d handling %s failed\n", i,
135 threadParams[i].filename);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000136 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000137 xmlCleanupParser();
138 xmlMemoryDump();
139 return (0);
140}
Nick Wellnhofer1e60c762022-09-04 01:49:41 +0200141#elif defined(_WIN32)
Nick Wellnhofer57d43292018-01-23 17:33:42 +0100142static DWORD WINAPI
143win32_thread_specific_data(void *private_data)
144{
145 thread_specific_data(private_data);
146 return(0);
147}
148
149int
150main(void)
151{
152 unsigned int i, repeat;
153 BOOL ret;
154
155 xmlInitParser();
156 for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++)
157 {
158 xmlLoadCatalog(catalog);
159
160 for (i = 0; i < num_threads; i++)
161 {
162 tid[i] = (HANDLE) -1;
163 }
164
165 for (i = 0; i < num_threads; i++)
166 {
167 DWORD useless;
168 tid[i] = CreateThread(NULL, 0,
169 win32_thread_specific_data, &threadParams[i], 0, &useless);
170 if (tid[i] == NULL)
171 {
172 perror("CreateThread");
173 exit(1);
174 }
175 }
176
177 if (WaitForMultipleObjects (num_threads, tid, TRUE, INFINITE) == WAIT_FAILED)
178 perror ("WaitForMultipleObjects failed");
179
180 for (i = 0; i < num_threads; i++)
181 {
182 DWORD exitCode;
183 ret = GetExitCodeThread (tid[i], &exitCode);
184 if (ret == 0)
185 {
186 perror("GetExitCodeThread");
187 exit(1);
188 }
189 CloseHandle (tid[i]);
190 }
191
192 xmlCatalogCleanup();
193 for (i = 0; i < num_threads; i++) {
194 if (threadParams[i].okay == 0)
195 printf("Thread %d handling %s failed\n", i,
196 threadParams[i].filename);
197 }
198 }
199
200 xmlCleanupParser();
201 xmlMemoryDump();
202
203 return (0);
204}
Nick Wellnhofer13a66372022-09-04 01:05:51 +0200205#endif /* pthreads */
Daniel Veillardab7488e2001-10-17 11:30:37 +0000206
207#else /* !LIBXML_THREADS_ENABLED */
208int
Daniel Veillard118aed72002-09-24 14:13:13 +0000209main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000210{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000211 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000212 return (0);
213}
214#endif