File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/detail/atomic.hpp>
0025 #include <boost/interprocess/detail/os_thread_functions.hpp>
0026 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0027 #include <boost/interprocess/sync/detail/locks.hpp>
0028 #include <boost/cstdint.hpp>
0029
0030 namespace boost {
0031 namespace interprocess {
0032 namespace ipcdetail {
0033
0034 class spin_semaphore
0035 {
0036 spin_semaphore(const spin_semaphore &);
0037 spin_semaphore &operator=(const spin_semaphore &);
0038
0039 public:
0040 spin_semaphore(unsigned int initialCount);
0041 ~spin_semaphore();
0042
0043 void post();
0044 void wait();
0045 bool try_wait();
0046 template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
0047
0048
0049 private:
0050 volatile boost::uint32_t m_count;
0051 };
0052
0053
0054 inline spin_semaphore::~spin_semaphore()
0055 {}
0056
0057 inline spin_semaphore::spin_semaphore(unsigned int initialCount)
0058 { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
0059
0060 inline void spin_semaphore::post()
0061 {
0062 ipcdetail::atomic_inc32(&m_count);
0063 }
0064
0065 inline void spin_semaphore::wait()
0066 {
0067 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
0068 return ipcdetail::try_based_lock(lw);
0069 }
0070
0071 inline bool spin_semaphore::try_wait()
0072 {
0073 return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
0074 }
0075
0076 template<class TimePoint>
0077 inline bool spin_semaphore::timed_wait(const TimePoint &abs_time)
0078 {
0079 ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
0080 return ipcdetail::try_based_timed_lock(lw, abs_time);
0081 }
0082
0083
0084
0085
0086
0087
0088 }
0089 }
0090 }
0091
0092 #include <boost/interprocess/detail/config_end.hpp>
0093
0094 #endif