2001-10-17 11:30:37 +00:00
# include <stdlib.h>
2001-10-17 15:58:35 +00:00
# include <stdio.h>
2001-12-06 14:34:08 +00:00
# include "libxml.h"
2001-10-17 11:30:37 +00:00
2001-11-04 20:19:12 +00:00
# if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
2001-10-17 11:30:37 +00:00
# include <libxml/globals.h>
# include <libxml/threads.h>
# include <libxml/parser.h>
# include <libxml/catalog.h>
# include <pthread.h>
# include <string.h>
2002-10-31 15:59:09 +00:00
# if !defined(_MSC_VER)
2001-10-17 11:30:37 +00:00
# include <unistd.h>
2002-10-31 15:59:09 +00:00
# endif
2001-10-17 11:30:37 +00:00
# include <assert.h>
# define MAX_ARGC 20
static pthread_t tid [ MAX_ARGC ] ;
static const char * catalog = " test/threads/complex.xml " ;
static const char * testfiles [ ] = {
" test/threads/abc.xml " ,
" test/threads/acb.xml " ,
" test/threads/bac.xml " ,
" test/threads/bca.xml " ,
" test/threads/cab.xml " ,
" test/threads/cba.xml " ,
" test/threads/invalid.xml " ,
} ;
2001-10-17 15:58:35 +00:00
const char * Okay = " OK " ;
const char * Failed = " Failed " ;
# ifndef xmlDoValidityCheckingDefaultValue
# error xmlDoValidityCheckingDefaultValue is not a macro
# endif
# ifndef xmlGenericErrorContext
# error xmlGenericErrorContext is not a macro
# endif
2001-10-17 11:30:37 +00:00
static void *
thread_specific_data ( void * private_data )
{
xmlDocPtr myDoc ;
const char * filename = ( const char * ) private_data ;
2001-10-17 15:58:35 +00:00
int okay = 1 ;
2001-10-17 11:30:37 +00:00
2001-10-17 15:58:35 +00:00
if ( ! strcmp ( filename , " test/threads/invalid.xml " ) ) {
2001-10-17 11:30:37 +00:00
xmlDoValidityCheckingDefaultValue = 0 ;
xmlGenericErrorContext = stdout ;
} else {
xmlDoValidityCheckingDefaultValue = 1 ;
xmlGenericErrorContext = stderr ;
}
myDoc = xmlParseFile ( filename ) ;
if ( myDoc ) {
xmlFreeDoc ( myDoc ) ;
2001-10-17 15:58:35 +00:00
} else {
2001-10-17 11:30:37 +00:00
printf ( " parse failed \n " ) ;
2001-10-17 15:58:35 +00:00
okay = 0 ;
}
if ( ! strcmp ( filename , " test/threads/invalid.xml " ) ) {
if ( xmlDoValidityCheckingDefaultValue ! = 0 ) {
2001-10-17 11:30:37 +00:00
printf ( " ValidityCheckingDefaultValue override failed \n " ) ;
2001-10-17 15:58:35 +00:00
okay = 0 ;
}
if ( xmlGenericErrorContext ! = stdout ) {
printf ( " xmlGenericErrorContext override failed \n " ) ;
okay = 0 ;
}
2001-10-17 11:30:37 +00:00
} else {
2001-10-17 15:58:35 +00:00
if ( xmlDoValidityCheckingDefaultValue ! = 1 ) {
2001-10-17 11:30:37 +00:00
printf ( " ValidityCheckingDefaultValue override failed \n " ) ;
2001-10-17 15:58:35 +00:00
okay = 0 ;
}
if ( xmlGenericErrorContext ! = stderr ) {
printf ( " xmlGenericErrorContext override failed \n " ) ;
okay = 0 ;
}
2001-10-17 11:30:37 +00:00
}
2001-10-17 15:58:35 +00:00
if ( okay = = 0 )
return ( ( void * ) Failed ) ;
return ( ( void * ) Okay ) ;
2001-10-17 11:30:37 +00:00
}
int
main ( )
{
2001-10-17 15:58:35 +00:00
unsigned int i , repeat ;
2001-10-17 11:30:37 +00:00
unsigned int num_threads = sizeof ( testfiles ) / sizeof ( testfiles [ 0 ] ) ;
2001-10-17 15:58:35 +00:00
void * results [ MAX_ARGC ] ;
int ret ;
2001-10-17 11:30:37 +00:00
xmlInitParser ( ) ;
2001-10-22 09:46:13 +00:00
for ( repeat = 0 ; repeat < 500 ; repeat + + ) {
2001-10-17 15:58:35 +00:00
xmlLoadCatalog ( catalog ) ;
2001-10-17 11:30:37 +00:00
2001-10-17 15:58:35 +00:00
for ( i = 0 ; i < num_threads ; i + + ) {
results [ i ] = NULL ;
2001-12-05 12:03:33 +00:00
tid [ i ] = ( pthread_t ) - 1 ;
2001-10-17 15:58:35 +00:00
}
2001-10-17 11:30:37 +00:00
2001-10-17 15:58:35 +00:00
for ( i = 0 ; i < num_threads ; i + + ) {
ret = pthread_create ( & tid [ i ] , 0 , thread_specific_data ,
( void * ) testfiles [ 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 ) ;
}
}
xmlCatalogCleanup ( ) ;
for ( i = 0 ; i < num_threads ; i + + )
if ( results [ i ] ! = ( void * ) Okay )
printf ( " Thread %d handling %s failed \n " , i , testfiles [ i ] ) ;
}
2001-10-17 11:30:37 +00:00
xmlCleanupParser ( ) ;
xmlMemoryDump ( ) ;
return ( 0 ) ;
}
# else /* !LIBXML_THREADS_ENABLED */
int
2002-09-24 14:13:13 +00:00
main ( void )
2001-10-17 11:30:37 +00:00
{
2001-11-04 20:19:12 +00:00
fprintf ( stderr , " libxml was not compiled with thread or catalog support \n " ) ;
2001-10-17 11:30:37 +00:00
return ( 0 ) ;
}
# endif