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