diff --git a/src/xercesc/framework/XMLValidator.cpp b/src/xercesc/framework/XMLValidator.cpp index d0b533a09593ff02e56d356e33035a478d2cd15f..0c5153e3c72d54a015247e82d7cb7727b4c6d7bc 100644 --- a/src/xercesc/framework/XMLValidator.cpp +++ b/src/xercesc/framework/XMLValidator.cpp @@ -56,6 +56,9 @@ /** * $Log$ + * Revision 1.7 2004/01/09 04:39:56 knoaman + * Use a global static mutex for locking when creating local static mutexes instead of compareAndSwap. + * * Revision 1.6 2003/12/24 15:24:13 cargilld * More updates to memory management so that the static memory manager. * @@ -158,14 +161,11 @@ static XMLMutex& gValidatorMutex() { if (!sMsgMutex) { - XMLMutex* tmpMutex = new XMLMutex; - if (XMLPlatformUtils::compareAndSwap((void**)&sMsgMutex, tmpMutex, 0)) - { - // Someone beat us to it, so let's clean up ours - delete tmpMutex; - } - else + XMLMutexLock lockInit(XMLPlatformUtils::fgAtomicMutex); + + if (!sMsgMutex) { + sMsgMutex = new XMLMutex; validatorMutexCleanup.registerCleanup(XMLValidator::reinitMsgMutex); } } @@ -175,22 +175,23 @@ static XMLMutex& gValidatorMutex() static XMLMsgLoader& getMsgLoader() { - - // Lock the mutex - XMLMutexLock lockInit(&gValidatorMutex()); - if (!sMsgLoader) - { - sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgValidityDomain); - if (!sMsgLoader) - XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); + { + // Lock the mutex + XMLMutexLock lockInit(&gValidatorMutex()); - // - // Register this XMLMsgLoader for cleanup at Termination. - // - msgLoaderCleanup.registerCleanup(XMLValidator::reinitMsgLoader); - - } + if (!sMsgLoader) + { + sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgValidityDomain); + if (!sMsgLoader) + XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); + + // + // Register this XMLMsgLoader for cleanup at Termination. + // + msgLoaderCleanup.registerCleanup(XMLValidator::reinitMsgLoader); + } + } return *sMsgLoader; } diff --git a/src/xercesc/internal/XMLScanner.cpp b/src/xercesc/internal/XMLScanner.cpp index f7ea19a22d32ddf1763e6981ef61614fff0a7c5a..7f962e514da6f525b7c2e30b5fb330a15ad6f7cb 100644 --- a/src/xercesc/internal/XMLScanner.cpp +++ b/src/xercesc/internal/XMLScanner.cpp @@ -122,23 +122,14 @@ void XMLScanner::reinitScannerMutex() // static XMLMutex& gScannerMutex() { - - if (!sScannerMutex) + if (!sRegistered) { - XMLMutex* tmpMutex = new XMLMutex; - if (XMLPlatformUtils::compareAndSwap((void**)&sScannerMutex, tmpMutex, 0)) - { - // Someone beat us to it, so let's clean up ours - delete tmpMutex; - } + XMLMutexLock lockInit(XMLPlatformUtils::fgAtomicMutex); - // Now lock it and try to register it - XMLMutexLock lock(sScannerMutex); - - // If we got here first, then register it and set the registered flag if (!sRegistered) { - scannerMutexCleanup.registerCleanup(XMLScanner::reinitScannerMutex); + sScannerMutex = new XMLMutex; + scannerMutexCleanup.registerCleanup(XMLScanner::reinitScannerMutex); sRegistered = true; } } @@ -147,17 +138,20 @@ static XMLMutex& gScannerMutex() static XMLMsgLoader& gScannerMsgLoader() { - XMLMutexLock lockInit(&gScannerMutex()); - - // If we haven't loaded our message yet, then do that if (!gMsgLoader) { - gMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgXMLErrDomain); + XMLMutexLock lockInit(&gScannerMutex()); + + // If we haven't loaded our message yet, then do that if (!gMsgLoader) - XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); + { + gMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgXMLErrDomain); + if (!gMsgLoader) + XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); - // Register this object to be cleaned up at termination - cleanupMsgLoader.registerCleanup(XMLScanner::reinitMsgLoader); + // Register this object to be cleaned up at termination + cleanupMsgLoader.registerCleanup(XMLScanner::reinitMsgLoader); + } } return *gMsgLoader; diff --git a/src/xercesc/util/PlatformUtils.cpp b/src/xercesc/util/PlatformUtils.cpp index f82f61b28a9e9cbf56cde61f8fb8094a18e2eb00..90d52604471167fa4618b8ffd3deda50818a47eb 100644 --- a/src/xercesc/util/PlatformUtils.cpp +++ b/src/xercesc/util/PlatformUtils.cpp @@ -56,6 +56,9 @@ /* * $Log$ + * Revision 1.17 2004/01/09 04:39:56 knoaman + * Use a global static mutex for locking when creating local static mutexes instead of compareAndSwap. + * * Revision 1.16 2003/12/17 00:18:35 cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * @@ -230,6 +233,7 @@ MemoryManager* XMLPlatformUtils::fgMemoryManager = 0; MemoryManagerArrayImpl gArrayMemoryManager; MemoryManager* XMLPlatformUtils::fgArrayMemoryManager = &gArrayMemoryManager; bool XMLPlatformUtils::fgMemMgrAdopted = true; +XMLMutex* XMLPlatformUtils::fgAtomicMutex = 0; // --------------------------------------------------------------------------- // XMLPlatformUtils: Init/term methods @@ -302,6 +306,7 @@ void XMLPlatformUtils::Initialize(const char* const locale // Create the mutex for the static data cleanup list gXMLCleanupListMutex = new XMLMutex; + fgAtomicMutex = new XMLMutex; // // Ask the per-platform code to make the desired transcoding service for @@ -386,6 +391,10 @@ void XMLPlatformUtils::Terminate() delete gSyncMutex; gSyncMutex = 0; + // Clean up mutex + delete fgAtomicMutex; + fgAtomicMutex = 0; + // Clean up statically allocated, lazily cleaned data in each class // that has registered for it. // Note that calling doCleanup() also unregisters the cleanup diff --git a/src/xercesc/util/PlatformUtils.hpp b/src/xercesc/util/PlatformUtils.hpp index 4d5f771fa7d1aa0afb6ca1552495b67d63b00ae0..98b7b9cc4c1e42018f7a62e67854ff9bacad756d 100644 --- a/src/xercesc/util/PlatformUtils.hpp +++ b/src/xercesc/util/PlatformUtils.hpp @@ -71,6 +71,7 @@ class XMLMsgLoader; class XMLNetAccessor; class XMLTransService; class MemoryManager; +class XMLMutex; // // For internal use only @@ -161,6 +162,8 @@ public : * there is no reason, nor facility, to override it. */ static MemoryManager* fgArrayMemoryManager; + + static XMLMutex* fgAtomicMutex; //@} diff --git a/src/xercesc/util/XMLException.cpp b/src/xercesc/util/XMLException.cpp index 8cbb0f00d3a396bf63437fd7a5eb0251c39d21af..b97d1193910faf7d28fa2f5548f1a8a0627ceadc 100644 --- a/src/xercesc/util/XMLException.cpp +++ b/src/xercesc/util/XMLException.cpp @@ -95,7 +95,7 @@ static const XMLCh gDefErrMsg[] = // --------------------------------------------------------------------------- static XMLMsgLoader* sMsgLoader = 0; static XMLRegisterCleanup msgLoaderCleanup; - +static bool sScannerMutexRegistered = false; static XMLMutex* sMsgMutex = 0; static XMLRegisterCleanup msgMutexCleanup; @@ -109,21 +109,16 @@ static XMLRegisterCleanup msgMutexCleanup; // static XMLMutex& gMsgMutex() { - - if (!sMsgMutex) + if (!sScannerMutexRegistered) { - XMLMutex* tmpMutex = new XMLMutex; - if (XMLPlatformUtils::compareAndSwap((void**)&sMsgMutex, tmpMutex, 0)) - { - // Some other thread beat us to it, so let's clean up ours. - delete tmpMutex; - } - else + XMLMutexLock lockInit(XMLPlatformUtils::fgAtomicMutex); + + if (!sScannerMutexRegistered) { - // This is the real mutex. Register it for cleanup at Termination. - msgMutexCleanup.registerCleanup(XMLException::reinitMsgMutex); + sMsgMutex = new XMLMutex; + msgMutexCleanup.registerCleanup(XMLException::reinitMsgMutex); + sScannerMutexRegistered = true; } - } return *sMsgMutex; @@ -135,21 +130,23 @@ static XMLMutex& gMsgMutex() // static XMLMsgLoader& gGetMsgLoader() { - - // Lock the message loader mutex and load the text - XMLMutexLock lockInit(&gMsgMutex()); - - // Fault it in on first request if (!sMsgLoader) { - sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgExceptDomain); - if (!sMsgLoader) - XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); + // Lock the message loader mutex and load the text + XMLMutexLock lockInit(&gMsgMutex()); - // - // Register this XMLMsgLoader for cleanup at Termination. - // - msgLoaderCleanup.registerCleanup(XMLException::reinitMsgLoader); + // Fault it in on first request + if (!sMsgLoader) + { + sMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgExceptDomain); + if (!sMsgLoader) + XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain); + + // + // Register this XMLMsgLoader for cleanup at Termination. + // + msgLoaderCleanup.registerCleanup(XMLException::reinitMsgLoader); + } } // We got it, so return it @@ -353,8 +350,9 @@ XMLException::loadExceptText(const XMLExcepts::Codes toLoad // ----------------------------------------------------------------------- void XMLException::reinitMsgMutex() { - delete sMsgMutex; - sMsgMutex = 0; + delete sMsgMutex; + sMsgMutex = 0; + sScannerMutexRegistered = false; } // ----------------------------------------------------------------------- @@ -362,8 +360,8 @@ void XMLException::reinitMsgMutex() // ----------------------------------------------------------------------- void XMLException::reinitMsgLoader() { - delete sMsgLoader; - sMsgLoader = 0; + delete sMsgLoader; + sMsgLoader = 0; } XERCES_CPP_NAMESPACE_END