Chris@16: // (C) Copyright 2012 Vicente Botet Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_THREAD_LOCKABLE_CONCEPTS_HPP Chris@16: #define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: /** Chris@16: * BasicLockable object supports the basic features Chris@16: * required to delimit a critical region Chris@16: * Supports the basic lock and unlock functions. Chris@16: */ Chris@16: Chris@16: //[BasicLockable Chris@16: template Chris@16: struct BasicLockable Chris@16: { Chris@16: Chris@16: BOOST_CONCEPT_USAGE(BasicLockable) Chris@16: { Chris@16: l.lock(); Chris@16: l.unlock(); Chris@16: } Chris@16: BasicLockable() : l(*static_cast(0)) {} Chris@16: private: Chris@16: BasicLockable operator=(BasicLockable const&); Chris@16: Chris@16: Mutex& l; Chris@16: } Chris@16: ; Chris@16: //] Chris@16: /** Chris@16: * Lockable extends BasicLockable Chris@16: * with try_lock functions. Chris@16: */ Chris@16: Chris@16: //[Lockable Chris@16: template Chris@16: struct Lockable Chris@16: { Chris@16: BOOST_CONCEPT_ASSERT(( BasicLockable )); Chris@16: Chris@16: BOOST_CONCEPT_USAGE(Lockable) Chris@16: { Chris@16: if (l.try_lock()) return; Chris@16: } Chris@16: Lockable() : l(*static_cast(0)) {} Chris@16: private: Chris@16: Lockable operator=(Lockable const&); Chris@16: Mutex& l; Chris@16: }; Chris@16: //] Chris@16: Chris@16: /** Chris@16: * TimedLockable object extends Lockable Chris@16: * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for Chris@16: */ Chris@16: Chris@16: //[TimedLockable Chris@16: template Chris@16: struct TimedLockable Chris@16: { Chris@16: BOOST_CONCEPT_ASSERT(( Lockable )); Chris@16: Chris@16: BOOST_CONCEPT_USAGE(TimedLockable) Chris@16: { Chris@16: if (l.try_lock_until(t)) return; Chris@16: if (l.try_lock_for(d)) return; Chris@16: } Chris@16: TimedLockable() : l(*static_cast(0)) {} Chris@16: private: Chris@16: TimedLockable operator=(TimedLockable const&); Chris@16: Mutex& l; Chris@16: chrono::system_clock::time_point t; Chris@16: chrono::system_clock::duration d; Chris@16: }; Chris@16: //] Chris@16: Chris@16: /** Chris@16: * SharedLockable object extends TimedLockable Chris@16: * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared Chris@16: * and unlock_shared functions Chris@16: */ Chris@16: //[SharedLockable Chris@16: template Chris@16: struct SharedLockable Chris@16: { Chris@16: BOOST_CONCEPT_ASSERT(( TimedLockable )); Chris@16: Chris@16: BOOST_CONCEPT_USAGE(SharedLockable) Chris@16: { Chris@16: l.lock_shared(); Chris@16: l.unlock_shared(); Chris@16: if (l.try_lock_shared()) return; Chris@16: if (l.try_lock_shared_until(t)) return; Chris@16: if (l.try_lock_shared_for(d)) return; Chris@16: } Chris@16: SharedLockable() : l(*static_cast(0)) {} Chris@16: private: Chris@16: SharedLockable operator=(SharedLockable const&); Chris@16: Mutex& l; Chris@16: chrono::system_clock::time_point t; Chris@16: chrono::system_clock::duration d; Chris@16: }; Chris@16: //] Chris@16: Chris@16: /** Chris@16: * UpgradeLockable object extends SharedLockable Chris@16: * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock, Chris@16: * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions Chris@16: */ Chris@16: Chris@16: //[UpgradeLockable Chris@16: template Chris@16: struct UpgradeLockable Chris@16: { Chris@16: BOOST_CONCEPT_ASSERT(( SharedLockable )); Chris@16: Chris@16: BOOST_CONCEPT_USAGE(UpgradeLockable) Chris@16: { Chris@16: l.lock_upgrade(); Chris@16: l.unlock_upgrade(); Chris@16: if (l.try_lock_upgrade()) return; Chris@16: if (l.try_lock_upgrade_until(t)) return; Chris@16: if (l.try_lock_upgrade_for(d)) return; Chris@16: if (l.try_unlock_shared_and_lock()) return; Chris@16: if (l.try_unlock_shared_and_lock_until(t)) return; Chris@16: if (l.try_unlock_shared_and_lock_for(d)) return; Chris@16: l.unlock_and_lock_shared(); Chris@16: if (l.try_unlock_shared_and_lock_upgrade()) return; Chris@16: if (l.try_unlock_shared_and_lock_upgrade_until(t)) return; Chris@16: if (l.try_unlock_shared_and_lock_upgrade_for(d)) return; Chris@16: l.unlock_and_lock_upgrade(); Chris@16: l.unlock_upgrade_and_lock(); Chris@16: if (l.try_unlock_upgrade_and_lock()) return; Chris@16: if (l.try_unlock_upgrade_and_lock_until(t)) return; Chris@16: if (l.try_unlock_upgrade_and_lock_for(d)) return; Chris@16: l.unlock_upgrade_and_lock_shared(); Chris@16: } Chris@16: UpgradeLockable() : l(*static_cast(0)) {} Chris@16: private: Chris@16: UpgradeLockable operator=(UpgradeLockable const&); Chris@16: Mutex& l; Chris@16: chrono::system_clock::time_point t; Chris@16: chrono::system_clock::duration d; Chris@16: }; Chris@16: //] Chris@16: Chris@16: } Chris@16: #endif