123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #ifndef BOOST_THREAD_STRICT_LOCK_HPP
- #define BOOST_THREAD_STRICT_LOCK_HPP
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/detail/delete.hpp>
- #include <boost/thread/detail/lockable_wrapper.hpp>
- #include <boost/thread/lock_options.hpp>
- #include <boost/thread/lock_traits.hpp>
- #include <boost/thread/lockable_traits.hpp>
- #include <boost/thread/lockable_concepts.hpp>
- #include <boost/thread/lock_concepts.hpp>
- #include <boost/thread/exceptions.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/config/abi_prefix.hpp>
- namespace boost
- {
-
- template <typename Lockable>
- class strict_lock
- {
- BOOST_CONCEPT_ASSERT(( BasicLockable<Lockable> ));
- public:
- typedef Lockable mutex_type;
-
- BOOST_THREAD_NO_COPYABLE( strict_lock)
-
- explicit strict_lock(mutex_type& mtx) :
- mtx_(mtx)
- {
- mtx.lock();
- }
- #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
- strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lockable> > l_) :
- mtx_(*(const_cast<thread_detail::lockable_wrapper<Lockable>*>(l_.begin())->m))
- {
- mtx_.lock();
- }
- #endif
-
- ~strict_lock()
- {
- mtx_.unlock();
- }
-
-
- mutex_type* mutex() const BOOST_NOEXCEPT
- {
- return &mtx_;
- }
-
- bool owns_lock() const BOOST_NOEXCEPT
- {
- return true;
- }
-
- bool owns_lock(const mutex_type* l) const BOOST_NOEXCEPT
- {
- return l == mutex();
- }
-
-
-
- private:
- mutex_type& mtx_;
- };
-
- template <typename Lockable>
- struct is_strict_lock_sur_parole<strict_lock<Lockable> > : true_type
- {
- };
-
-
- template <typename Lock>
- class nested_strict_lock
- {
- BOOST_CONCEPT_ASSERT(( BasicLock<Lock> ));
- public:
- typedef typename Lock::mutex_type mutex_type;
- BOOST_THREAD_NO_COPYABLE( nested_strict_lock)
-
- explicit nested_strict_lock(Lock& lk) :
- lk_(lk)
- {
-
- BOOST_THREAD_ASSERT_PRECONDITION( lk.mutex() != 0,
- lock_error()
- );
- if (!lk.owns_lock()) lk.lock();
- tmp_lk_ = move(lk);
- }
- #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
- nested_strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lock> > l_) :
- lk_(*(const_cast<thread_detail::lockable_wrapper<Lock>*>(l_.begin())->m))
- {
-
- BOOST_THREAD_ASSERT_PRECONDITION( lk_.mutex() != 0,
- lock_error()
- );
- if (!lk_.owns_lock()) lk_.lock();
- tmp_lk_ = move(lk_);
- }
- #endif
-
- ~nested_strict_lock()BOOST_NOEXCEPT
- {
- lk_ = move(tmp_lk_);
- }
-
-
- mutex_type* mutex() const BOOST_NOEXCEPT
- {
- return tmp_lk_.mutex();
- }
-
- bool owns_lock() const BOOST_NOEXCEPT
- {
- return true;
- }
-
- bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT
- {
- return l == mutex();
- }
-
-
- private:
- Lock& lk_;
- Lock tmp_lk_;
- };
-
- template <typename Lock>
- struct is_strict_lock_sur_parole<nested_strict_lock<Lock> > : true_type
- {
- };
- #if ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK
- template <typename Lockable>
- strict_lock<Lockable> make_strict_lock(Lockable& mtx)
- {
- return { thread_detail::lockable_wrapper<Lockable>(mtx) };
- }
- template <typename Lock>
- nested_strict_lock<Lock> make_nested_strict_lock(Lock& lk)
- {
- return { thread_detail::lockable_wrapper<Lock>(lk) };
- }
- #endif
- }
- #include <boost/config/abi_suffix.hpp>
- #endif
|