]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/spin/mutex.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / interprocess / sync / spin / mutex.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
25 #include <boost/assert.hpp>
26 #include <boost/interprocess/detail/atomic.hpp>
27 #include <boost/cstdint.hpp>
28 #include <boost/interprocess/detail/os_thread_functions.hpp>
29 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
30
31 namespace boost {
32 namespace interprocess {
33 namespace ipcdetail {
34
35 class spin_mutex
36 {
37 spin_mutex(const spin_mutex &);
38 spin_mutex &operator=(const spin_mutex &);
39 public:
40
41 spin_mutex();
42 ~spin_mutex();
43
44 void lock();
45 bool try_lock();
46 bool timed_lock(const boost::posix_time::ptime &abs_time);
47 void unlock();
48 void take_ownership(){}
49 private:
50 volatile boost::uint32_t m_s;
51 };
52
53 inline spin_mutex::spin_mutex()
54 : m_s(0)
55 {
56 //Note that this class is initialized to zero.
57 //So zeroed memory can be interpreted as an
58 //initialized mutex
59 }
60
61 inline spin_mutex::~spin_mutex()
62 {
63 //Trivial destructor
64 }
65
66 inline void spin_mutex::lock(void)
67 {
68 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
69 boost::posix_time::ptime wait_time
70 = microsec_clock::universal_time()
71 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
72 if (!timed_lock(wait_time))
73 {
74 throw interprocess_exception(timeout_when_locking_error
75 , "Interprocess mutex timeout when locking. Possible deadlock: "
76 "owner died without unlocking?");
77 }
78 #else
79 return ipcdetail::try_based_lock(*this);
80 #endif
81 }
82
83 inline bool spin_mutex::try_lock(void)
84 {
85 boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
86 return m_s == 1 && prev_s == 0;
87 }
88
89 inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
90 { return ipcdetail::try_based_timed_lock(*this, abs_time); }
91
92 inline void spin_mutex::unlock(void)
93 { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
94
95 } //namespace ipcdetail {
96 } //namespace interprocess {
97 } //namespace boost {
98
99 #include <boost/interprocess/detail/config_end.hpp>
100
101 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP