123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- #ifndef BOOST_INTERPROCESS_SCOPED_LOCK_HPP
- #define BOOST_INTERPROCESS_SCOPED_LOCK_HPP
- #ifndef BOOST_CONFIG_HPP
- # include <boost/config.hpp>
- #endif
- #
- #if defined(BOOST_HAS_PRAGMA_ONCE)
- # pragma once
- #endif
- #include <boost/interprocess/detail/config_begin.hpp>
- #include <boost/interprocess/detail/workaround.hpp>
- #include <boost/interprocess/interprocess_fwd.hpp>
- #include <boost/interprocess/sync/lock_options.hpp>
- #include <boost/interprocess/exceptions.hpp>
- #include <boost/interprocess/detail/mpl.hpp>
- #include <boost/interprocess/detail/type_traits.hpp>
- #include <boost/move/utility_core.hpp>
- #include <boost/interprocess/detail/simple_swap.hpp>
- namespace boost {
- namespace interprocess {
- template <class Mutex>
- class scoped_lock
- {
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- private:
- typedef scoped_lock<Mutex> this_type;
- BOOST_MOVABLE_BUT_NOT_COPYABLE(scoped_lock)
- typedef bool this_type::*unspecified_bool_type;
- #endif
- public:
- typedef Mutex mutex_type;
-
-
- scoped_lock() BOOST_NOEXCEPT
- : mp_mutex(0), m_locked(false)
- {}
-
-
-
-
-
- explicit scoped_lock(mutex_type& m)
- : mp_mutex(&m), m_locked(false)
- { mp_mutex->lock(); m_locked = true; }
-
-
-
- scoped_lock(mutex_type& m, defer_lock_type)
- : mp_mutex(&m), m_locked(false)
- {}
-
-
-
- scoped_lock(mutex_type& m, accept_ownership_type)
- : mp_mutex(&m), m_locked(true)
- {}
-
-
-
-
-
-
-
-
- scoped_lock(mutex_type& m, try_to_lock_type)
- : mp_mutex(&m), m_locked(mp_mutex->try_lock())
- {}
-
-
-
-
-
-
-
-
- template<class TimePoint>
- scoped_lock(mutex_type& m, const TimePoint& abs_time)
- : mp_mutex(&m), m_locked(mp_mutex->timed_lock(abs_time))
- {}
-
-
-
-
-
-
-
-
-
-
- scoped_lock(BOOST_RV_REF(scoped_lock) scop) BOOST_NOEXCEPT
- : mp_mutex(0), m_locked(scop.owns())
- { mp_mutex = scop.release(); }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T>
- explicit scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr
- , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
- : mp_mutex(0), m_locked(false)
- {
- upgradable_lock<mutex_type> &u_lock = upgr;
- if(u_lock.owns()){
- u_lock.mutex()->unlock_upgradable_and_lock();
- m_locked = true;
- }
- mp_mutex = u_lock.release();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T>
- scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr, try_to_lock_type
- , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
- : mp_mutex(0), m_locked(false)
- {
- upgradable_lock<mutex_type> &u_lock = upgr;
- if(u_lock.owns()){
- if((m_locked = u_lock.mutex()->try_unlock_upgradable_and_lock()) == true){
- mp_mutex = u_lock.release();
- }
- }
- else{
- u_lock.release();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T, class TimePoint>
- scoped_lock(BOOST_RV_REF(upgradable_lock<T>) upgr, const TimePoint &abs_time
- , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
- : mp_mutex(0), m_locked(false)
- {
- upgradable_lock<mutex_type> &u_lock = upgr;
- if(u_lock.owns()){
- if((m_locked = u_lock.mutex()->timed_unlock_upgradable_and_lock(abs_time)) == true){
- mp_mutex = u_lock.release();
- }
- }
- else{
- u_lock.release();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T>
- scoped_lock(BOOST_RV_REF(sharable_lock<T>) shar, try_to_lock_type
- , typename ipcdetail::enable_if< ipcdetail::is_same<T, Mutex> >::type * = 0)
- : mp_mutex(0), m_locked(false)
- {
- sharable_lock<mutex_type> &s_lock = shar;
- if(s_lock.owns()){
- if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock()) == true){
- mp_mutex = s_lock.release();
- }
- }
- else{
- s_lock.release();
- }
- }
-
-
- ~scoped_lock()
- {
- BOOST_TRY{ if(m_locked && mp_mutex) mp_mutex->unlock(); }
- BOOST_CATCH(...){} BOOST_CATCH_END
- }
-
-
-
-
-
-
- scoped_lock &operator=(BOOST_RV_REF(scoped_lock) scop)
- {
- if(this->owns())
- this->unlock();
- m_locked = scop.owns();
- mp_mutex = scop.release();
- return *this;
- }
-
-
-
-
-
- void lock()
- {
- if(!mp_mutex || m_locked)
- throw lock_exception();
- mp_mutex->lock();
- m_locked = true;
- }
-
-
-
-
-
-
-
- bool try_lock()
- {
- if(!mp_mutex || m_locked)
- throw lock_exception();
- m_locked = mp_mutex->try_lock();
- return m_locked;
- }
-
-
-
-
-
-
-
- template<class TimePoint>
- bool timed_lock(const TimePoint& abs_time)
- {
- if(!mp_mutex || m_locked)
- throw lock_exception();
- m_locked = mp_mutex->timed_lock(abs_time);
- return m_locked;
- }
-
-
-
-
-
-
-
- template<class TimePoint>
- bool try_lock_until(const TimePoint& abs_time)
- {
- if(!mp_mutex || m_locked)
- throw lock_exception();
- m_locked = mp_mutex->try_lock_until(abs_time);
- return m_locked;
- }
-
-
-
-
-
-
-
- template<class Duration>
- bool try_lock_for(const Duration& dur)
- {
- if(!mp_mutex || m_locked)
- throw lock_exception();
- m_locked = mp_mutex->try_lock_for(dur);
- return m_locked;
- }
-
-
-
-
-
- void unlock()
- {
- if(!mp_mutex || !m_locked)
- throw lock_exception();
- mp_mutex->unlock();
- m_locked = false;
- }
-
-
- bool owns() const BOOST_NOEXCEPT
- { return m_locked && mp_mutex; }
-
-
- operator unspecified_bool_type() const BOOST_NOEXCEPT
- { return m_locked? &this_type::m_locked : 0; }
-
-
- mutex_type* mutex() const BOOST_NOEXCEPT
- { return mp_mutex; }
-
-
-
- mutex_type* release() BOOST_NOEXCEPT
- {
- mutex_type *mut = mp_mutex;
- mp_mutex = 0;
- m_locked = false;
- return mut;
- }
-
-
- void swap( scoped_lock<mutex_type> &other) BOOST_NOEXCEPT
- {
- (simple_swap)(mp_mutex, other.mp_mutex);
- (simple_swap)(m_locked, other.m_locked);
- }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- private:
- mutex_type *mp_mutex;
- bool m_locked;
- #endif
- };
- }
- }
- #include <boost/interprocess/detail/config_end.hpp>
- #endif
|