]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | |
6 | ||
7 | #ifndef BOOST_THREAD_LOCK_GUARD_HPP | |
8 | #define BOOST_THREAD_LOCK_GUARD_HPP | |
9 | ||
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> | |
18 | #endif | |
19 | ||
20 | #include <boost/config/abi_prefix.hpp> | |
21 | ||
22 | namespace boost | |
23 | { | |
24 | ||
25 | template <typename Mutex> | |
26 | class lock_guard | |
27 | { | |
28 | private: | |
29 | Mutex& m; | |
30 | ||
31 | public: | |
32 | typedef Mutex mutex_type; | |
33 | BOOST_THREAD_NO_COPYABLE( lock_guard ) | |
34 | ||
35 | explicit lock_guard(Mutex& m_) : | |
36 | m(m_) | |
37 | { | |
38 | m.lock(); | |
39 | } | |
40 | ||
41 | lock_guard(Mutex& m_, adopt_lock_t) : | |
42 | m(m_) | |
43 | { | |
44 | #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS | |
45 | BOOST_ASSERT(is_locked_by_this_thread(m)); | |
46 | #endif | |
47 | } | |
48 | ||
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)) | |
52 | { | |
53 | m.lock(); | |
54 | } | |
55 | ||
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)) | |
58 | { | |
59 | #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS | |
60 | BOOST_ASSERT(is_locked_by_this_thread(m)); | |
61 | #endif | |
62 | } | |
63 | ||
64 | #endif | |
65 | ~lock_guard() | |
66 | { | |
67 | m.unlock(); | |
68 | } | |
69 | }; | |
70 | ||
71 | ||
72 | #if ! defined BOOST_THREAD_NO_MAKE_LOCK_GUARD | |
73 | template <typename Lockable> | |
74 | lock_guard<Lockable> make_lock_guard(Lockable& mtx) | |
75 | { | |
76 | return { thread_detail::lockable_wrapper<Lockable>(mtx) }; | |
77 | } | |
78 | template <typename Lockable> | |
79 | lock_guard<Lockable> make_lock_guard(Lockable& mtx, adopt_lock_t) | |
80 | { | |
81 | return { thread_detail::lockable_adopt_wrapper<Lockable>(mtx) }; | |
82 | } | |
83 | #endif | |
84 | } | |
85 | ||
86 | #include <boost/config/abi_suffix.hpp> | |
87 | ||
88 | #endif |