/ libxml2 / testThreads.c
testThreads.c
  1  #include "libxml.h"
  2  
  3  #include <stdlib.h>
  4  #include <stdio.h>
  5  
  6  #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  7  #include <libxml/globals.h>
  8  #include <libxml/threads.h>
  9  #include <libxml/parser.h>
 10  #include <libxml/catalog.h>
 11  #ifdef HAVE_PTHREAD_H
 12  #include <pthread.h>
 13  #elif defined HAVE_BEOS_THREADS
 14  #include <OS.h>
 15  #endif
 16  #include <string.h>
 17  #if !defined(_MSC_VER)
 18  #include <unistd.h>
 19  #endif
 20  #include <assert.h>
 21  
 22  #define	MAX_ARGC	20
 23  #ifdef HAVE_PTHREAD_H
 24  static pthread_t tid[MAX_ARGC];
 25  #elif defined HAVE_BEOS_THREADS
 26  static thread_id tid[MAX_ARGC];
 27  #endif
 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  
 40  static const char *Okay = "OK";
 41  static const char *Failed = "Failed";
 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  
 50  static void *
 51  thread_specific_data(void *private_data)
 52  {
 53      xmlDocPtr myDoc;
 54      const char *filename = (const char *) private_data;
 55      int okay = 1;
 56  
 57      if (!strcmp(filename, "test/threads/invalid.xml")) {
 58          xmlDoValidityCheckingDefaultValue = 0;
 59          xmlGenericErrorContext = stdout;
 60      } else {
 61          xmlDoValidityCheckingDefaultValue = 1;
 62          xmlGenericErrorContext = stderr;
 63      }
 64  #ifdef LIBXML_SAX1_ENABLED
 65      myDoc = xmlParseFile(filename);
 66  #else
 67      myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
 68  #endif
 69      if (myDoc) {
 70          xmlFreeDoc(myDoc);
 71      } else {
 72          printf("parse failed\n");
 73  	okay = 0;
 74      }
 75      if (!strcmp(filename, "test/threads/invalid.xml")) {
 76          if (xmlDoValidityCheckingDefaultValue != 0) {
 77  	    printf("ValidityCheckingDefaultValue override failed\n");
 78  	    okay = 0;
 79  	}
 80          if (xmlGenericErrorContext != stdout) {
 81  	    printf("xmlGenericErrorContext override failed\n");
 82  	    okay = 0;
 83  	}
 84      } else {
 85          if (xmlDoValidityCheckingDefaultValue != 1) {
 86  	    printf("ValidityCheckingDefaultValue override failed\n");
 87  	    okay = 0;
 88  	}
 89          if (xmlGenericErrorContext != stderr) {
 90  	    printf("xmlGenericErrorContext override failed\n");
 91  	    okay = 0;
 92  	}
 93      }
 94      if (okay == 0)
 95  	return((void *) Failed);
 96      return ((void *) Okay);
 97  }
 98  
 99  #ifdef HAVE_PTHREAD_H
100  int
101  main(void)
102  {
103      unsigned int i, repeat;
104      unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
105      void *results[MAX_ARGC];
106      int ret;
107  
108      xmlInitParser();
109      for (repeat = 0;repeat < 500;repeat++) {
110  	xmlLoadCatalog(catalog);
111  
112          memset(results, 0, sizeof(*results)*num_threads);
113          memset(tid, 0xff, sizeof(*tid)*num_threads);
114  
115  	for (i = 0; i < num_threads; i++) {
116  	    ret = pthread_create(&tid[i], NULL, thread_specific_data,
117  				 (void *) testfiles[i]);
118  	    if (ret != 0) {
119  		perror("pthread_create");
120  		exit(1);
121  	    }
122  	}
123  	for (i = 0; i < num_threads; i++) {
124  	    ret = pthread_join(tid[i], &results[i]);
125  	    if (ret != 0) {
126  		perror("pthread_join");
127  		exit(1);
128  	    }
129  	}
130  
131  	xmlCatalogCleanup();
132  	for (i = 0; i < num_threads; i++)
133  	    if (results[i] != (void *) Okay)
134  		printf("Thread %d handling %s failed\n", i, testfiles[i]);
135      }
136      xmlCleanupParser();
137      xmlMemoryDump();
138      return (0);
139  }
140  #elif defined HAVE_BEOS_THREADS
141  int
142  main(void)
143  {
144      unsigned int i, repeat;
145      unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
146      void *results[MAX_ARGC];
147      status_t ret;
148  
149      xmlInitParser();
150      printf("Parser initialized\n");
151      for (repeat = 0;repeat < 500;repeat++) {
152      printf("repeat: %d\n",repeat);
153  	xmlLoadCatalog(catalog);
154  	printf("loaded catalog: %s\n", catalog);
155  	for (i = 0; i < num_threads; i++) {
156  	    results[i] = NULL;
157  	    tid[i] = (thread_id) -1;
158  	}
159  	printf("cleaned threads\n");
160  	for (i = 0; i < num_threads; i++) {
161  		tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
162  		if (tid[i] < B_OK) {
163  			perror("beos_thread_create");
164  			exit(1);
165  		}
166  		printf("beos_thread_create %d -> %d\n", i, tid[i]);
167  	}
168  	for (i = 0; i < num_threads; i++) {
169  	    ret = wait_for_thread(tid[i], &results[i]);
170  	    printf("beos_thread_wait %d -> %d\n", i, ret);
171  	    if (ret != B_OK) {
172  			perror("beos_thread_wait");
173  			exit(1);
174  	    }
175  	}
176  
177  	xmlCatalogCleanup();
178  	ret = B_OK;
179  	for (i = 0; i < num_threads; i++)
180  	    if (results[i] != (void *) Okay) {
181  			printf("Thread %d handling %s failed\n", i, testfiles[i]);
182  			ret = B_ERROR;
183  		}
184      }
185      xmlCleanupParser();
186      xmlMemoryDump();
187  
188  	if (ret == B_OK)
189  		printf("testThread : BeOS : SUCCESS!\n");
190  	else
191  		printf("testThread : BeOS : FAILED!\n");
192  
193      return (0);
194  }
195  #endif /* pthreads or BeOS threads */
196  
197  #else /* !LIBXML_THREADS_ENABLED */
198  int
199  main(void)
200  {
201      fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
202      return (0);
203  }
204  #endif