]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/spin/semaphore.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / spin / semaphore.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_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_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/atomic.hpp>
25 #include <boost/interprocess/detail/os_thread_functions.hpp>
26 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
27 #include <boost/interprocess/sync/detail/locks.hpp>
28 #include <boost/cstdint.hpp>
29
30 namespace boost {
31 namespace interprocess {
32 namespace ipcdetail {
33
34 class spin_semaphore
35 {
36 spin_semaphore(const spin_semaphore &);
37 spin_semaphore &operator=(const spin_semaphore &);
38
39 public:
40 spin_semaphore(unsigned int initialCount);
41 ~spin_semaphore();
42
43 void post();
44 void wait();
45 bool try_wait();
46 template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
47
48 // int get_count() const;
49 private:
50 volatile boost::uint32_t m_count;
51 };
52
53
54 inline spin_semaphore::~spin_semaphore()
55 {}
56
57 inline spin_semaphore::spin_semaphore(unsigned int initialCount)
58 { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
59
60 inline void spin_semaphore::post()
61 {
62 ipcdetail::atomic_inc32(&m_count);
63 }
64
65 inline void spin_semaphore::wait()
66 {
67 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
68 return ipcdetail::try_based_lock(lw);
69 }
70
71 inline bool spin_semaphore::try_wait()
72 {
73 return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
74 }
75
76 template<class TimePoint>
77 inline bool spin_semaphore::timed_wait(const TimePoint &abs_time)
78 {
79 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
80 return ipcdetail::try_based_timed_lock(lw, abs_time);
81 }
82
83 //inline int spin_semaphore::get_count() const
84 //{
85 //return (int)ipcdetail::atomic_read32(&m_count);
86 //}
87
88 } //namespace ipcdetail {
89 } //namespace interprocess {
90 } //namespace boost {
91
92 #include <boost/interprocess/detail/config_end.hpp>
93
94 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP