diff --git a/include/loki/Threads.h b/include/loki/Threads.h index 9c4f4a3..196b983 100644 --- a/include/loki/Threads.h +++ b/include/loki/Threads.h @@ -523,6 +523,19 @@ namespace Loki { struct Initializer { + + /// This function provides a Scott-Meyers type of Singleton as the initializer + /// for the shared mutex. + static Initializer & GetIt( void ) + { + static Initializer initializer_; + return initializer_; + } + + inline bool IsInit( void ) { return init_; } + inline MutexPolicy & GetMutex( void ) { return mtx_; } + + private: bool init_; MutexPolicy mtx_; @@ -535,9 +548,10 @@ namespace Loki { assert(init_); } - }; - static Initializer initializer_; + Initializer( const Initializer & ); + Initializer & operator = ( const Initializer & ); + }; public: @@ -553,29 +567,33 @@ namespace Loki /// Lock class Lock() { - assert(initializer_.init_); - initializer_.mtx_.Lock(); + Initializer & initializer = Initializer::GetIt(); + assert( initializer.IsInit() ); + initializer.GetMutex().Lock(); } /// Lock class explicit Lock(const ClassLevelLockable&) { - assert(initializer_.init_); - initializer_.mtx_.Lock(); + Initializer & initializer = Initializer::GetIt(); + assert( initializer.IsInit() ); + initializer.GetMutex().Lock(); } /// Lock class explicit Lock(const ClassLevelLockable*) { - assert(initializer_.init_); - initializer_.mtx_.Lock(); + Initializer & initializer = Initializer::GetIt(); + assert( initializer.IsInit() ); + initializer.GetMutex().Lock(); } /// Unlock class ~Lock() { - assert(initializer_.init_); - initializer_.mtx_.Unlock(); + Initializer & initializer = Initializer::GetIt(); + assert( initializer.IsInit() ); + initializer.GetMutex().Unlock(); } private: @@ -596,10 +614,6 @@ namespace Loki pthread_mutex_t ClassLevelLockable::atomic_mutex_ = PTHREAD_MUTEX_INITIALIZER; #endif - template < class Host, class MutexPolicy > - typename ClassLevelLockable< Host, MutexPolicy >::Initializer - ClassLevelLockable< Host, MutexPolicy >::initializer_; - #endif // #if defined(LOKI_WINDOWS_H) || defined(LOKI_PTHREAD_H) } // namespace Loki