--- include/libxml/parserInternals.h	2010-10-15 09:13:56.000000000 -0700
+++ include/libxml/parserInternals.h	2011-03-02 10:40:44.000000000 -0800
@@ -279,6 +279,12 @@
 XMLPUBVAR const xmlChar xmlStringText[];
 XMLPUBVAR const xmlChar xmlStringTextNoenc[];
 XMLPUBVAR const xmlChar xmlStringComment[];
+
+/*
+ * Initialize and Cleanup functions
+ */
+XMLPUBFUN void XMLCALL xmlInitParserInternals   (void);
+XMLPUBFUN void XMLCALL xmlCleanupParserInternals(void);
 
 /*
  * Function to finish the work of the macros where needed.
--- parser.c	2010-10-15 09:13:56.000000000 -0700
+++ parser.c	2011-03-01 15:48:37.000000000 -0800
@@ -14010,6 +14010,7 @@
 	    (xmlGenericError == NULL))
 	    initGenericErrorDefaultFunc(NULL);
 	xmlInitMemory();
+        xmlInitParserInternals();
 	xmlInitCharEncodingHandlers();
 	xmlDefaultSAXHandlerInit();
 	xmlRegisterDefaultInputCallbacks();
@@ -14070,6 +14071,7 @@
     xmlSchemaCleanupTypes();
     xmlRelaxNGCleanupTypes();
 #endif
+    xmlCleanupParserInternals();
     xmlCleanupGlobals();
     xmlResetLastError();
     xmlCleanupThreads(); /* must be last if called not from the main thread */
--- parserInternals.c	2010-10-15 09:13:56.000000000 -0700
+++ parserInternals.c	2011-03-01 16:08:26.000000000 -0800
@@ -54,6 +54,36 @@
 #endif
 #include "third_party/libxml/v2_7_7/include/libxml/globals.h"
 #include "third_party/libxml/v2_7_7/include/libxml/chvalid.h"
+
+/*
+ * Protects xmlInputId.
+ */
+static xmlMutexPtr xmlInputIdMutex = NULL;
+
+/*
+ * counter used for unique id in xmlNewInputStream.  Protected by
+ * xmlInputIdMutex.
+ */
+static int xmlInputId = 0;
+
+/**
+ * xmlInitParserInternals:
+ *
+ * Initialize globals used in this file.  e.g., xmlInputIdMutex.
+ */
+void xmlInitParserInternals(void) {
+  xmlInputIdMutex = xmlNewMutex();
+}
+
+/**
+ * xmlCleanupParserInternals:
+ *
+ * Cleanup globals used in this file.  e.g., xmlInputIdMutex.
+ */
+void xmlCleanupParserInternals(void) {
+  xmlFreeMutex(xmlInputIdMutex);
+  xmlInputIdMutex = NULL;
+}
 
 /*
  * Various global defaults for parsing
@@ -1378,7 +1408,11 @@
 xmlParserInputPtr
 xmlNewInputStream(xmlParserCtxtPtr ctxt) {
     xmlParserInputPtr input;
-    static int id = 0;
+
+    if (xmlInputIdMutex == NULL) {
+      xmlErrMemory(ctxt, "xmlInitParserInternals not called\n");
+      return(NULL);
+    }
 
     input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
     if (input == NULL) {
@@ -1389,11 +1423,9 @@
     input->line = 1;
     input->col = 1;
     input->standalone = -1;
-    /*
-     * we don't care about thread reentrancy unicity for a single
-     * parser context (and hence thread) is sufficient.
-     */
-    input->id = id++;
+    xmlMutexLock(xmlInputIdMutex);
+    input->id = xmlInputId++;
+    xmlMutexUnlock(xmlInputIdMutex);
     return(input);
 }
 
