]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/log/detail/multiple_lock.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / log / detail / multiple_lock.hpp
1 /*
2  *          Copyright Andrey Semashev 2007 - 2010.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   multiple_lock.hpp
9  * \author Andrey Semashev
10  * \date   18.04.2008
11  *
12  * \brief  This header is the Boost.Log library implementation, see the library documentation
13  *         at http://www.boost.org/libs/log/doc/log.html.
14  */
15
16 #if (defined(_MSC_VER) && _MSC_VER > 1000)
17 #pragma once
18 #endif // _MSC_VER > 1000
19
20 #ifndef BOOST_LOG_DETAIL_MULTIPLE_LOCK_HPP_INCLUDED_
21 #define BOOST_LOG_DETAIL_MULTIPLE_LOCK_HPP_INCLUDED_
22
23 #include <functional>
24 #include <boost/utility/addressof.hpp>
25 #include <boost/log/detail/prologue.hpp>
26
27 namespace boost {
28
29 namespace BOOST_LOG_NAMESPACE {
30
31 namespace aux {
32
33 //! A deadlock-safe lock type that locks two mutexes
34 template< typename T1, typename T2 >
35 class multiple_unique_lock2
36 {
37     T1* m_p1;
38     T2* m_p2;
39
40 public:
41     multiple_unique_lock2(T1& m1, T2& m2) :
42         m_p1(boost::addressof(m1)),
43         m_p2(boost::addressof(m2))
44     {
45         std::less< void* > order;
46         if (order(m_p1, m_p2))
47         {
48             m_p1->lock();
49             m_p2->lock();
50         }
51         else
52         {
53             m_p2->lock();
54             m_p1->lock();
55         }
56     }
57     ~multiple_unique_lock2()
58     {
59         m_p2->unlock();
60         m_p1->unlock();
61     }
62 };
63
64 } // namespace aux
65
66 } // namespace log
67
68 } // namespace boost
69
70 #endif // BOOST_LOG_DETAIL_MULTIPLE_LOCK_HPP_INCLUDED_