[xml] Testing pthreads safety in xmllint

Date view Thread view Subject view Author view

From: Jorma Laaksonen (jorma.laaksonen@hut.fi)
Date: Wed Jan 31 2001 - 03:47:42 EST


Hello!

I've had thread safety problems in a program that uses libxml (v. 2.2.10
now). In order to just test the library itself, I've added a piece
of code in xmllint.c so that it can be invoked to run in concurrent
pthreads. This kind of an extension might be of interest to others too.

Here is the patch:

--- xmllint.c.orig Fri Jan 5 08:39:29 2001
+++ xmllint.c Wed Jan 31 10:23:26 2001
@@ -16,6 +16,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <pthread.h>
 
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -78,6 +79,7 @@
 static int html = 0;
 static int htmlout = 0;
 static int push = 0;
+static int pthreads = 0;
 #ifdef HAVE_SYS_MMAN_H
 static int memory = 0;
 #endif
@@ -382,6 +384,31 @@
  * Test processing *
  * *
  ************************************************************************/
+static void parseAndPrintFile(char*);
+
+void *parseAndPrintFile_inner(void *filename) {
+ parseAndPrintFile((char*)filename);
+ return NULL;
+}
+
+void parseAndPrintFile_outer(char *filename) {
+ if (!pthreads)
+ parseAndPrintFile(filename);
+ else {
+ const int n = 10;
+ pthread_t thread[n];
+ int i;
+
+ xmlInitParser();
+
+ for (i=0; i<n; i++)
+ pthread_create(thread+i, NULL, parseAndPrintFile_inner, (void*)filename);
+
+ for (i=0; i<n; i++)
+ pthread_join(thread[i], NULL);
+ }
+}
+
 void parseAndPrintFile(char *filename) {
     xmlDocPtr doc = NULL, tmp;
 
@@ -704,6 +731,9 @@
         else if ((!strcmp(argv[i], "-push")) ||
                  (!strcmp(argv[i], "--push")))
             push++;
+ else if ((!strcmp(argv[i], "-pthreads")) ||
+ (!strcmp(argv[i], "--pthreads")))
+ pthreads++;
 #ifdef HAVE_SYS_MMAN_H
         else if ((!strcmp(argv[i], "-memory")) ||
                  (!strcmp(argv[i], "--memory")))
@@ -783,9 +813,9 @@
         if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
             if (repeat) {
                 for (count = 0;count < 100 * repeat;count++)
- parseAndPrintFile(argv[i]);
+ parseAndPrintFile_outer(argv[i]);
             } else
- parseAndPrintFile(argv[i]);
+ parseAndPrintFile_outer(argv[i]);
             files ++;
         }
     }
@@ -817,6 +847,7 @@
         printf("\t--html : use the HTML parser\n");
 #endif
         printf("\t--push : use the push mode of the parser\n");
+ printf("\t--pthreads : run the test in ten concurrent pthreads\n");
 #ifdef HAVE_SYS_MMAN_H
         printf("\t--memory : parse from memory\n");
 #endif

The proper insertion of this kind of a patch of course needs some
#ifdef HAVE_PTHREADS or something. I'm not familiar with these, so
I'll leave it for you.

Also, I had to hack to resulting Makefile to include the pthread lib:

--- Makefile.orig Wed Jan 31 10:28:32 2001
+++ Makefile Wed Jan 31 10:32:42 2001
@@ -140,7 +140,7 @@
 xmllint_SOURCES = xmllint.c
 xmllint_LDFLAGS =
 xmllint_DEPENDENCIES = $(DEPS)
-xmllint_LDADD = $(LDADDS)
+xmllint_LDADD = $(LDADDS) -lpthread
 
 testSAX_SOURCES = testSAX.c
 testSAX_LDFLAGS =

That should, of course, be done in somewhere else.

Now I'll start to examine what is wrong in my code with pthreads.
At least my first experiments with this pthread-extended xmllint
have not revealed any bug in the library...

Yours,

Jorma Laaksonen

-- 
Jorma Laaksonen                                 email: jorma.laaksonen@hut.fi
Dr. of Science in Technology, Docent            http://www.cis.hut.fi/jorma/
Laboratory of Computer and Information Science  tel. +358-9-4513269
Helsinki University of Technology               fax. +358-9-4513277
P.O.Box 5400, Fin-02015 HUT, Finland            mob. +358-50-3058719
----
Message from the list xml@rpmfind.net
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rpmfind.net


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Jan 31 2001 - 04:44:51 EST