Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // (C) Copyright 2008-2009,2012 Vicente J. Botet Escriba Chris@16: Chris@16: #ifndef BOOST_THREAD_STRICT_LOCK_HPP Chris@16: #define BOOST_THREAD_STRICT_LOCK_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: Chris@16: //[strict_lock Chris@16: template Chris@16: class strict_lock Chris@16: { Chris@16: Chris@16: BOOST_CONCEPT_ASSERT(( BasicLockable )); Chris@16: public: Chris@16: typedef Lockable mutex_type; Chris@16: Chris@16: // construct/copy/destroy: Chris@16: Chris@16: BOOST_THREAD_NO_COPYABLE( strict_lock) Chris@16: Chris@16: /** Chris@16: * Constructor from a mutex reference. Chris@16: * Chris@16: * @param mtx the mutex to lock. Chris@16: * Chris@16: * __Effects: Stores a reference to the mutex to lock and locks it. Chris@16: * __Throws: Any exception BasicMutex::lock() can throw. Chris@16: */ Chris@16: explicit strict_lock(mutex_type& mtx) : Chris@16: mtx_(mtx) Chris@16: { Chris@16: mtx.lock(); Chris@16: } /*< locks on construction >*/ Chris@16: Chris@16: Chris@16: #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST Chris@16: strict_lock(std::initializer_list > l_) : Chris@16: mtx_(*(const_cast*>(l_.begin())->m)) Chris@16: { Chris@16: mtx_.lock(); Chris@16: } Chris@16: #endif Chris@16: Chris@16: /** Chris@16: * Destructor Chris@16: * Chris@16: * __Effects: unlocks the stored mutex. Chris@16: * Chris@16: * __Throws Chris@16: */ Chris@16: ~strict_lock() Chris@16: { Chris@16: mtx_.unlock(); Chris@16: } /*< unlocks on destruction >*/ Chris@16: Chris@16: Chris@16: // observers Chris@16: Chris@16: /** Chris@16: * @return the owned mutex. Chris@16: */ Chris@16: mutex_type* mutex() const BOOST_NOEXCEPT Chris@16: { Chris@16: return &mtx_; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @return whether this lock is locking a mutex. Chris@16: */ Chris@16: bool owns_lock() const BOOST_NOEXCEPT Chris@16: { Chris@16: return true; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @return whether this lock is locking that mutex. Chris@16: */ Chris@16: bool owns_lock(const mutex_type* l) const BOOST_NOEXCEPT Chris@16: { Chris@16: return l == mutex(); Chris@16: } /*< strict locks specific function >*/ Chris@16: Chris@16: //BOOST_ADRESS_OF_DELETE(strict_lock) /*< disable aliasing >*/ Chris@16: //BOOST_HEAP_ALLOCATION_DELETE(strict_lock) /*< disable heap allocation >*/ Chris@16: Chris@16: /*< no possibility to unlock >*/ Chris@16: Chris@16: private: Chris@16: mutex_type& mtx_; Chris@16: }; Chris@16: //] Chris@16: template Chris@16: struct is_strict_lock_sur_parole > : true_type Chris@16: { Chris@16: }; Chris@16: Chris@16: /** Chris@16: * A nested strict lock is a scoped lock guard ensuring the mutex is locked on its Chris@16: * scope, by taking ownership of an nesting lock, locking the mutex on construction if not already locked Chris@16: * and restoring the ownership to the nesting lock on destruction. Chris@16: */ Chris@16: //[nested_strict_lock Chris@16: template Chris@16: class nested_strict_lock Chris@16: { Chris@16: BOOST_CONCEPT_ASSERT(( BasicLock )); /*< The Lock must be a movable lock >*/ Chris@16: public: Chris@16: typedef typename Lock::mutex_type mutex_type; /*< Name the lockable type locked by Lock >*/ Chris@16: Chris@16: BOOST_THREAD_NO_COPYABLE( nested_strict_lock) Chris@16: Chris@16: /** Chris@16: * Constructor from a nesting @c Lock. Chris@16: * Chris@16: * @param lk the nesting lock Chris@16: * Chris@16: * __Requires: lk.mutex() != null_ptr Chris@16: * __Effects: Stores the reference to the lock parameter and takes ownership on it. Chris@16: * If the lock doesn't owns the mutex @c mtx lock it. Chris@16: * __Postconditions: @c owns_lock(lk.mutex()) Chris@16: * __StrongException Chris@16: * __Throws: Chris@16: * Chris@16: * - lock_error when BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED is defined and lk.mutex() == null_ptr Chris@16: * Chris@16: * - Any exception that @c lk.lock() can throw. Chris@16: * Chris@16: */ Chris@16: explicit nested_strict_lock(Lock& lk) : Chris@16: lk_(lk) /*< Store reference to lk >*/ Chris@16: { Chris@16: /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/ Chris@16: BOOST_THREAD_ASSERT_PRECONDITION( lk.mutex() != 0, Chris@16: lock_error() Chris@16: ); Chris@16: if (!lk.owns_lock()) lk.lock(); /*< ensures it is locked >*/ Chris@16: tmp_lk_ = move(lk); /*< Move ownership to temporary lk >*/ Chris@16: } Chris@16: Chris@16: #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST Chris@16: nested_strict_lock(std::initializer_list > l_) : Chris@16: lk_(*(const_cast*>(l_.begin())->m)) Chris@16: { Chris@16: /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/ Chris@16: BOOST_THREAD_ASSERT_PRECONDITION( lk_.mutex() != 0, Chris@16: lock_error() Chris@16: ); Chris@16: if (!lk_.owns_lock()) lk_.lock(); /*< ensures it is locked >*/ Chris@16: tmp_lk_ = move(lk_); /*< Move ownership to temporary lk >*/ Chris@16: } Chris@16: #endif Chris@16: Chris@16: /** Chris@16: * Destructor Chris@16: * Chris@16: * __Effects: Restores ownership to the nesting lock. Chris@16: */ Chris@16: ~nested_strict_lock()BOOST_NOEXCEPT Chris@16: { Chris@16: lk_ = move(tmp_lk_); /*< Move ownership to nesting lock >*/ Chris@16: } Chris@16: Chris@16: // observers Chris@16: /** Chris@16: * return @c the owned mutex. Chris@16: */ Chris@16: mutex_type* mutex() const BOOST_NOEXCEPT Chris@16: { Chris@16: return tmp_lk_.mutex(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * @return whether this lock is locking a mutex. Chris@16: */ Chris@16: bool owns_lock() const BOOST_NOEXCEPT Chris@16: { Chris@16: return true; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @return whether if this lock is locking that mutex. Chris@16: */ Chris@16: bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT Chris@16: { Chris@16: return l == mutex(); Chris@16: } Chris@16: Chris@16: //BOOST_ADRESS_OF_DELETE(nested_strict_lock) Chris@16: //BOOST_HEAP_ALLOCATEION_DELETE(nested_strict_lock) Chris@16: Chris@16: private: Chris@16: Lock& lk_; Chris@16: Lock tmp_lk_; Chris@16: }; Chris@16: //] Chris@16: Chris@16: template Chris@16: struct is_strict_lock_sur_parole > : true_type Chris@16: { Chris@16: }; Chris@16: Chris@16: #if ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK Chris@16: template Chris@16: strict_lock make_strict_lock(Lockable& mtx) Chris@16: { Chris@16: return { thread_detail::lockable_wrapper(mtx) }; Chris@16: } Chris@16: template Chris@16: nested_strict_lock make_nested_strict_lock(Lock& lk) Chris@16: { Chris@16: return { thread_detail::lockable_wrapper(lk) }; Chris@16: } Chris@16: #endif Chris@16: } Chris@16: #include Chris@16: Chris@16: #endif