diff --git a/include/private/threads.h b/include/private/threads.h index 643e18ce2..4baed3e46 100644 --- a/include/private/threads.h +++ b/include/private/threads.h @@ -58,4 +58,12 @@ xmlInitRMutex(xmlRMutex *mutex); XML_HIDDEN void xmlCleanupRMutex(xmlRMutex *mutex); +#ifdef LIBXML_SCHEMAS_ENABLED +XML_HIDDEN void +xmlInitSchemasTypesInternal(void); +XML_HIDDEN void +xmlCleanupSchemasTypesInternal(void); +#endif + + #endif /* XML_THREADS_H_PRIVATE__ */ diff --git a/threads.c b/threads.c index 947398b1c..832bf2d0c 100644 --- a/threads.c +++ b/threads.c @@ -370,6 +370,9 @@ xmlInitParserInternal(void) { #ifdef LIBXML_CATALOG_ENABLED xmlInitCatalogInternal(); #endif +#ifdef LIBXML_SCHEMAS_ENABLED + xmlInitSchemasTypesInternal(); +#endif xmlParserInitialized = 1; } @@ -456,6 +459,11 @@ xmlCleanupParser(void) { xmlRelaxNGCleanupTypes(); #endif +#ifdef LIBXML_SCHEMAS_ENABLED + /* Must be after xmlRelaxNGCleanupTypes */ + xmlCleanupSchemasTypesInternal(); +#endif + xmlCleanupDictInternal(); xmlCleanupRandom(); xmlCleanupGlobalsInternal(); diff --git a/xmlschemastypes.c b/xmlschemastypes.c index f4c7025e4..bbdd80b64 100644 --- a/xmlschemastypes.c +++ b/xmlschemastypes.c @@ -35,6 +35,7 @@ #include #include "private/error.h" +#include "private/threads.h" #ifndef isnan #define isnan(x) (!((x) == (x))) @@ -514,6 +515,20 @@ xmlSchemaCleanupTypesInternal(void) { /* Note that the xmlSchemaType*Def pointers aren't set to NULL. */ } +static xmlMutex xmlSchemasTypesMutex; + +void +xmlInitSchemasTypesInternal(void) +{ + xmlInitMutex(&xmlSchemasTypesMutex); +} + +void +xmlCleanupSchemasTypesInternal(void) +{ + xmlCleanupMutex(&xmlSchemasTypesMutex); +} + /** * Initialize the default XML Schemas type library * @@ -522,8 +537,13 @@ xmlSchemaCleanupTypesInternal(void) { int xmlSchemaInitTypes(void) { - if (xmlSchemaTypesInitialized != 0) + xmlInitParser(); + + xmlMutexLock(&xmlSchemasTypesMutex); + if (xmlSchemaTypesInitialized != 0) { + xmlMutexUnlock(&xmlSchemasTypesMutex); return (0); + } #if defined(NAN) && defined(INFINITY) xmlSchemaNAN = NAN; @@ -853,10 +873,12 @@ xmlSchemaInitTypes(void) xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef; xmlSchemaTypesInitialized = 1; + xmlMutexUnlock(&xmlSchemasTypesMutex); return (0); error: xmlSchemaCleanupTypesInternal(); + xmlMutexUnlock(&xmlSchemasTypesMutex); return (-1); } @@ -871,10 +893,12 @@ error: */ void xmlSchemaCleanupTypes(void) { + xmlMutexLock(&xmlSchemasTypesMutex); if (xmlSchemaTypesInitialized != 0) { xmlSchemaCleanupTypesInternal(); xmlSchemaTypesInitialized = 0; } + xmlMutexUnlock(&xmlSchemasTypesMutex); } /** @@ -965,8 +989,7 @@ xmlSchemaIsBuiltInTypeFacet(xmlSchemaType *type, int facetType) xmlSchemaType * xmlSchemaGetBuiltInType(xmlSchemaValType type) { - if ((xmlSchemaTypesInitialized == 0) && - (xmlSchemaInitTypes() < 0)) + if (xmlSchemaInitTypes() < 0) return (NULL); switch (type) { @@ -1299,8 +1322,7 @@ xmlSchemaFreeValue(xmlSchemaVal *value) { */ xmlSchemaType * xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) { - if ((xmlSchemaTypesInitialized == 0) && - (xmlSchemaInitTypes() < 0)) + if (xmlSchemaInitTypes() < 0) return (NULL); if (name == NULL) return(NULL); @@ -2418,8 +2440,7 @@ xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value, xmlChar *norm = NULL; int ret = 0; - if ((xmlSchemaTypesInitialized == 0) && - (xmlSchemaInitTypes() < 0)) + if (xmlSchemaInitTypes() < 0) return (-1); if (type == NULL) return (-1);