Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/detail/atomic.hpp>
|
Chris@16
|
25 #include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@101
|
27 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
Chris@101
|
28 #include <boost/interprocess/sync/detail/locks.hpp>
|
Chris@16
|
29 #include <boost/cstdint.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace interprocess {
|
Chris@16
|
33 namespace ipcdetail {
|
Chris@16
|
34
|
Chris@16
|
35 class spin_semaphore
|
Chris@16
|
36 {
|
Chris@16
|
37 spin_semaphore(const spin_semaphore &);
|
Chris@16
|
38 spin_semaphore &operator=(const spin_semaphore &);
|
Chris@16
|
39
|
Chris@16
|
40 public:
|
Chris@16
|
41 spin_semaphore(unsigned int initialCount);
|
Chris@16
|
42 ~spin_semaphore();
|
Chris@16
|
43
|
Chris@16
|
44 void post();
|
Chris@16
|
45 void wait();
|
Chris@16
|
46 bool try_wait();
|
Chris@16
|
47 bool timed_wait(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
48
|
Chris@16
|
49 // int get_count() const;
|
Chris@16
|
50 private:
|
Chris@16
|
51 volatile boost::uint32_t m_count;
|
Chris@16
|
52 };
|
Chris@16
|
53
|
Chris@16
|
54
|
Chris@16
|
55 inline spin_semaphore::~spin_semaphore()
|
Chris@16
|
56 {}
|
Chris@16
|
57
|
Chris@16
|
58 inline spin_semaphore::spin_semaphore(unsigned int initialCount)
|
Chris@16
|
59 { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
|
Chris@16
|
60
|
Chris@16
|
61 inline void spin_semaphore::post()
|
Chris@16
|
62 {
|
Chris@16
|
63 ipcdetail::atomic_inc32(&m_count);
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 inline void spin_semaphore::wait()
|
Chris@16
|
67 {
|
Chris@101
|
68 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
|
Chris@101
|
69 return ipcdetail::try_based_lock(lw);
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 inline bool spin_semaphore::try_wait()
|
Chris@16
|
73 {
|
Chris@16
|
74 return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
78 {
|
Chris@101
|
79 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
|
Chris@101
|
80 return ipcdetail::try_based_timed_lock(lw, abs_time);
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 //inline int spin_semaphore::get_count() const
|
Chris@16
|
84 //{
|
Chris@16
|
85 //return (int)ipcdetail::atomic_read32(&m_count);
|
Chris@16
|
86 //}
|
Chris@16
|
87
|
Chris@16
|
88 } //namespace ipcdetail {
|
Chris@16
|
89 } //namespace interprocess {
|
Chris@16
|
90 } //namespace boost {
|
Chris@16
|
91
|
Chris@16
|
92 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
93
|
Chris@16
|
94 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|