// ********************************************************************** // // Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #ifndef ICE_UTIL_MUTEX_PTR_LOCK_H #define ICE_UTIL_MUTEX_PTR_LOCK_H #include #include namespace IceUtilInternal { template class MutexPtrLock { public: MutexPtrLock(const T* mutex) : _mutex(mutex), _acquired(false) { if(_mutex) { _mutex->lock(); _acquired = true; } } ~MutexPtrLock() { if(_mutex && _acquired) { _mutex->unlock(); } } void acquire() const { if(_mutex) { _mutex->lock(); _acquired = true; } } void release() const { if(_mutex) { if(!_acquired) { throw IceUtil::ThreadLockedException(__FILE__, __LINE__); } _mutex->unlock(); _acquired = false; } } bool acquired() const { return _acquired; } private: // Not implemented; prevents accidental use. // MutexPtrLock(const MutexPtrLock&); MutexPtrLock& operator=(const MutexPtrLock&); const T* _mutex; mutable bool _acquired; }; } // End namespace IceUtilInternal #endif