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_MUTEX_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_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/assert.hpp>
0025 #include <boost/interprocess/detail/atomic.hpp>
0026 #include <boost/cstdint.hpp>
0027 #include <boost/interprocess/detail/os_thread_functions.hpp>
0028 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0029
0030 namespace boost {
0031 namespace interprocess {
0032 namespace ipcdetail {
0033
0034 class spin_mutex
0035 {
0036 spin_mutex(const spin_mutex &);
0037 spin_mutex &operator=(const spin_mutex &);
0038 public:
0039
0040 spin_mutex();
0041 ~spin_mutex();
0042
0043 void lock();
0044 bool try_lock();
0045 template<class TimePoint>
0046 bool timed_lock(const TimePoint &abs_time);
0047
0048 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0049 { return this->timed_lock(abs_time); }
0050
0051 template<class Duration> bool try_lock_for(const Duration &dur)
0052 { return this->timed_lock(duration_to_ustime(dur)); }
0053
0054 void unlock();
0055 void take_ownership(){}
0056 private:
0057 volatile boost::uint32_t m_s;
0058
0059 struct common_lock_wrapper
0060 {
0061 common_lock_wrapper(spin_mutex &sp)
0062 : m_sp(sp)
0063 {}
0064
0065 void lock()
0066 {
0067 ipcdetail::try_based_lock(m_sp);
0068 }
0069
0070 template<class TimePoint>
0071 bool timed_lock(const TimePoint &abs_time)
0072 { return m_sp.timed_lock(abs_time); }
0073
0074 spin_mutex &m_sp;
0075 };
0076 };
0077
0078 inline spin_mutex::spin_mutex()
0079 : m_s(0)
0080 {
0081
0082
0083
0084 }
0085
0086 inline spin_mutex::~spin_mutex()
0087 {
0088
0089 }
0090
0091 inline void spin_mutex::lock(void)
0092 {
0093 common_lock_wrapper clw(*this);
0094 ipcdetail::timeout_when_locking_aware_lock(clw);
0095 }
0096
0097 inline bool spin_mutex::try_lock(void)
0098 {
0099 boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
0100 return m_s == 1 && prev_s == 0;
0101 }
0102
0103 template<class TimePoint>
0104 inline bool spin_mutex::timed_lock(const TimePoint &abs_time)
0105 { return ipcdetail::try_based_timed_lock(*this, abs_time); }
0106
0107 inline void spin_mutex::unlock(void)
0108 { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
0109
0110 }
0111 }
0112 }
0113
0114 #include <boost/interprocess/detail/config_end.hpp>
0115
0116 #endif