1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/**
* testThreads.c: testing of heavilly multithreaded concurrent accesses
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
/*
* TODO: extend it to allow giving the stylesheets/input as filenames on the
* command line to test specifics, also add exslt
*/
#include "config.h"
#include "libexslt/exslt.h"
#include <stdlib.h>
#include <stdio.h>
#define _REENTRANT
#include <libxml/xmlversion.h>
#if defined(LIBXML_THREAD_ENABLED) && defined(HAVE_PTHREAD_H)
#include <libxml/globals.h>
#include <libxml/threads.h>
#include <libxml/parser.h>
#include <libxml/catalog.h>
#include <libxml/xpathInternals.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libxslt/extensions.h>
#include <libexslt/exsltconfig.h>
#include <pthread.h>
#include <string.h>
#if !defined(_MSC_VER)
#include <unistd.h>
#endif
#include <assert.h>
#define MAX_ARGC 20
static pthread_t tid[MAX_ARGC];
#define EXT_NS BAD_CAST "http://foo.org"
#define EXT_DATA "bar"
const char *stylesheet = "<xsl:stylesheet version='1.0' \
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' \
xmlns:foo='http://foo.org' \
extension-element-prefixes='foo'>\
<xsl:template match='text()'>\
Success <xsl:value-of select='foo:foo()'/>\
</xsl:template>\
</xsl:stylesheet>\
";
int init = 0;
const char *doc = "<doc>Failed</doc>";
const char *expect = "<?xml version=\"1.0\"?>\nSuccess foo\n";
static void fooFunction(xmlXPathParserContextPtr ctxt,
int nargs ATTRIBUTE_UNUSED) {
xmlXPathReturnString(ctxt, xmlStrdup(BAD_CAST "foo"));
}
static
void * registerFooExtensions(ATTRIBUTE_UNUSED xsltTransformContextPtr ctxt,
ATTRIBUTE_UNUSED const xmlChar *URI) {
xsltRegisterExtModuleFunction(BAD_CAST "foo", EXT_NS, fooFunction);
return((void *)EXT_DATA);
}
static
void shutdownFooExtensions(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
const xmlChar *URI, void *data) {
const char *str = (const char *) data;
if (!xmlStrEqual(URI, EXT_NS)) {
fprintf(stderr, "Mismatch in extensions shutdown URI");
}
if (!xmlStrEqual(BAD_CAST str, BAD_CAST EXT_DATA)) {
fprintf(stderr, "Mismatch in extensions shutdown DATA");
}
}
static void registerFooModule(void) {
xsltRegisterExtModule(EXT_NS, registerFooExtensions, shutdownFooExtensions);
}
static void *
threadRoutine1(void *data)
{
xmlDocPtr input;
xmlDocPtr style;
xmlDocPtr res;
xmlChar *result;
int len;
xsltStylesheetPtr cur;
int id = (int)(unsigned long) data;
input = xmlReadMemory(doc, strlen(doc), "doc.xml", NULL, 0);
if (input == NULL) {
fprintf(stderr, "Thread id %d failed to parse input\n", id);
exit(1);
}
style = xmlReadMemory(stylesheet, strlen(stylesheet), "doc.xsl", NULL, 0);
if (style == NULL) {
fprintf(stderr, "Thread id %d failed to parse stylesheet\n", id);
exit(1);
}
cur = xsltParseStylesheetDoc(style);
if (cur == NULL) {
fprintf(stderr, "Thread id %d failed to compile stylesheet\n", id);
exit(1);
}
res = xsltApplyStylesheet(cur, input, NULL);
if (res == NULL) {
fprintf(stderr, "Thread id %d failed to apply stylesheet\n", id);
exit(1);
}
if (xsltSaveResultToString(&result, &len, res, cur) < 0) {
fprintf(stderr, "Thread id %d failed to output result\n", id);
exit(1);
}
if (!xmlStrEqual(BAD_CAST expect, result)) {
fprintf(stderr, "Thread id %d output not conform\n", id);
exit(1);
}
xsltFreeStylesheet(cur);
xmlFreeDoc(input);
xmlFreeDoc(res);
xmlFree(result);
return(0);
}
static void *
threadRoutine2(void *data)
{
xmlDocPtr input;
xmlDocPtr res;
xmlChar *result;
int len;
xsltStylesheetPtr cur = (xsltStylesheetPtr) data;
if (cur == NULL) {
fprintf(stderr, "Thread failed to get the stylesheet\n");
exit(1);
}
input = xmlReadMemory(doc, strlen(doc), "doc.xml", NULL, 0);
if (input == NULL) {
fprintf(stderr, "Thread failed to parse input\n");
exit(1);
}
res = xsltApplyStylesheet(cur, input, NULL);
if (res == NULL) {
fprintf(stderr, "Thread failed to apply stylesheet\n");
exit(1);
}
if (xsltSaveResultToString(&result, &len, res, cur) < 0) {
fprintf(stderr, "Thread failed to output result\n");
exit(1);
}
if (!xmlStrEqual(BAD_CAST expect, result)) {
fprintf(stderr, "Thread output not conform\n");
exit(1);
}
xmlFreeDoc(input);
xmlFreeDoc(res);
xmlFree(result);
return(0);
}
int
main(void)
{
unsigned int i, repeat;
unsigned int num_threads = 8;
void *results[MAX_ARGC];
int ret;
xmlInitParser();
/*
* Register the EXSLT extensions and the test module
*/
exsltRegisterAll();
xsltRegisterTestModule();
/*
* Register our own extension module
*/
registerFooModule();
/*
* First pass each thread has its own version of the stylesheet
* each of them will initialize and shutdown the extension
*/
printf("Pass 1\n");
for (repeat = 0;repeat < 500;repeat++) {
memset(results, 0, sizeof(*results)*num_threads);
memset(tid, 0xff, sizeof(*tid)*num_threads);
for (i = 0; i < num_threads; i++) {
ret = pthread_create(&tid[i], NULL, threadRoutine1,
(void *) (unsigned long) i);
if (ret != 0) {
perror("pthread_create");
exit(1);
}
}
for (i = 0; i < num_threads; i++) {
ret = pthread_join(tid[i], &results[i]);
if (ret != 0) {
perror("pthread_join");
exit(1);
}
}
}
/*
* Second pass all threads share the same stylesheet instance
* look for transformation clashes
*/
printf("Pass 2\n");
for (repeat = 0;repeat < 500;repeat++) {
xmlDocPtr style;
xsltStylesheetPtr cur;
style = xmlReadMemory(stylesheet, strlen(stylesheet), "doc.xsl",
NULL, 0);
if (style == NULL) {
fprintf(stderr, "Main failed to parse stylesheet\n");
exit(1);
}
cur = xsltParseStylesheetDoc(style);
if (cur == NULL) {
fprintf(stderr, "Main failed to compile stylesheet\n");
exit(1);
}
memset(results, 0, sizeof(*results)*num_threads);
memset(tid, 0xff, sizeof(*tid)*num_threads);
for (i = 0; i < num_threads; i++) {
ret = pthread_create(&tid[i], NULL, threadRoutine2, (void *) cur);
if (ret != 0) {
perror("pthread_create");
exit(1);
}
}
for (i = 0; i < num_threads; i++) {
ret = pthread_join(tid[i], &results[i]);
if (ret != 0) {
perror("pthread_join");
exit(1);
}
}
xsltFreeStylesheet(cur);
}
xsltCleanupGlobals();
xmlCleanupParser();
xmlMemoryDump();
printf("Ok\n");
return (0);
}
#else /* !LIBXML_THREADS_ENABLED | !HAVE_PTHREAD_H */
int
main(void)
{
fprintf(stderr, "libxml was not compiled with thread\n");
return (0);
}
#endif
|