1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
31 namespace interprocess {
36 spin_semaphore(const spin_semaphore &);
37 spin_semaphore &operator=(const spin_semaphore &);
40 spin_semaphore(unsigned int initialCount);
46 template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
48 // int get_count() const;
50 volatile boost::uint32_t m_count;
54 inline spin_semaphore::~spin_semaphore()
57 inline spin_semaphore::spin_semaphore(unsigned int initialCount)
58 { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
60 inline void spin_semaphore::post()
62 ipcdetail::atomic_inc32(&m_count);
65 inline void spin_semaphore::wait()
67 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
68 return ipcdetail::try_based_lock(lw);
71 inline bool spin_semaphore::try_wait()
73 return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
76 template<class TimePoint>
77 inline bool spin_semaphore::timed_wait(const TimePoint &abs_time)
79 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
80 return ipcdetail::try_based_timed_lock(lw, abs_time);
83 //inline int spin_semaphore::get_count() const
85 //return (int)ipcdetail::atomic_read32(&m_count);
88 } //namespace ipcdetail {
89 } //namespace interprocess {
92 #include <boost/interprocess/detail/config_end.hpp>
94 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP