diff --git a/clipper/core/clipper_instance.cpp b/clipper/core/clipper_instance.cpp index 86c26e33d893a15db18d7c57d175349a48c2b3ed..039472ff0992e2c498795347c1dc575e1870691b 100644 --- a/clipper/core/clipper_instance.cpp +++ b/clipper/core/clipper_instance.cpp @@ -42,6 +42,18 @@ #include "clipper_instance.h" +template<class Lockable> +class _MutexLocker { +public: + explicit _MutexLocker() + { + Lockable::mutex.lock(); + } + ~_MutexLocker() + { + Lockable::mutex.unlock(); + } +}; namespace clipper { @@ -90,7 +102,8 @@ namespace clipper { template<class T> typename ObjectCache<T>::Reference ObjectCache<T>::cache( const typename T::Key& key ) { - T::mutex.lock(); + _MutexLocker<T> lk; + std::pair<int,T>* ptr = NULL; // find existing data for ( int i = 0; i < cache_.size(); i++ ) @@ -104,9 +117,9 @@ namespace clipper { if ( mode_ == NORMAL ) // NORMAL: replace unreferenced for ( int i = 0; i < cache_.size(); i++ ) if ( cache_[i]->first == 0 ) { - ptr = cache_[i]; - ptr->second = T(key); - break; + ptr = cache_[i]; + ptr->second = T(key); + break; } // otherwise add new if ( ptr == NULL ) { @@ -115,7 +128,6 @@ namespace clipper { } } Reference result( ptr ); - T::mutex.unlock(); return result; // we have a ref to the new obj, so it is thread safe }