1 // Distributed under the Boost Software License, Version 1.0. (See
2 // accompanying file LICENSE_1_0.txt or copy at
3 // http://www.boost.org/LICENSE_1_0.txt)
4 // (C) Copyright 2007 Anthony Williams
5 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
7 #ifndef BOOST_THREAD_LOCK_GUARD_HPP
8 #define BOOST_THREAD_LOCK_GUARD_HPP
10 #include <boost/thread/detail/config.hpp>
11 #include <boost/thread/detail/delete.hpp>
12 #include <boost/thread/detail/move.hpp>
13 #include <boost/thread/detail/lockable_wrapper.hpp>
14 #include <boost/thread/lock_options.hpp>
15 #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
16 #include <boost/thread/is_locked_by_this_thread.hpp>
17 #include <boost/assert.hpp>
20 #include <boost/config/abi_prefix.hpp>
25 template <typename Mutex>
32 typedef Mutex mutex_type;
33 BOOST_THREAD_NO_COPYABLE( lock_guard )
35 explicit lock_guard(Mutex& m_) :
41 lock_guard(Mutex& m_, adopt_lock_t) :
44 #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
45 BOOST_ASSERT(is_locked_by_this_thread(m));
49 #if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
50 lock_guard(std::initializer_list<thread_detail::lockable_wrapper<Mutex> > l_) :
51 m(*(const_cast<thread_detail::lockable_wrapper<Mutex>*>(l_.begin())->m))
56 lock_guard(std::initializer_list<thread_detail::lockable_adopt_wrapper<Mutex> > l_) :
57 m(*(const_cast<thread_detail::lockable_adopt_wrapper<Mutex>*>(l_.begin())->m))
59 #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
60 BOOST_ASSERT(is_locked_by_this_thread(m));
72 #if ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD
73 template <typename Lockable>
74 lock_guard<Lockable> make_lock_guard(Lockable& mtx)
76 return { thread_detail::lockable_wrapper<Lockable>(mtx) };
78 template <typename Lockable>
79 lock_guard<Lockable> make_lock_guard(Lockable& mtx, adopt_lock_t)
81 return { thread_detail::lockable_adopt_wrapper<Lockable>(mtx) };
86 #include <boost/config/abi_suffix.hpp>