| 1 | ////////////////////////////////////////////////////////////////////////////// |
|---|---|
| 2 | // |
| 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) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/interprocess for documentation. |
| 8 | // |
| 9 | ////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP |
| 12 | #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP |
| 13 | |
| 14 | #ifndef BOOST_CONFIG_HPP |
| 15 | # include <boost/config.hpp> |
| 16 | #endif |
| 17 | # |
| 18 | #if defined(BOOST_HAS_PRAGMA_ONCE) |
| 19 | # pragma once |
| 20 | #endif |
| 21 | |
| 22 | #include <boost/interprocess/detail/config_begin.hpp> |
| 23 | #include <boost/interprocess/detail/workaround.hpp> |
| 24 | #include <boost/assert.hpp> |
| 25 | #include <boost/interprocess/detail/atomic.hpp> |
| 26 | #include <boost/cstdint.hpp> |
| 27 | #include <boost/interprocess/detail/os_thread_functions.hpp> |
| 28 | #include <boost/interprocess/sync/detail/common_algorithms.hpp> |
| 29 | #include <boost/interprocess/timed_utils.hpp> |
| 30 | |
| 31 | namespace boost { |
| 32 | namespace interprocess { |
| 33 | namespace ipcdetail { |
| 34 | |
| 35 | class spin_mutex |
| 36 | { |
| 37 | spin_mutex(const spin_mutex &); |
| 38 | spin_mutex &operator=(const spin_mutex &); |
| 39 | public: |
| 40 | |
| 41 | spin_mutex(); |
| 42 | ~spin_mutex(); |
| 43 | |
| 44 | void lock(); |
| 45 | bool try_lock(); |
| 46 | template<class TimePoint> |
| 47 | bool timed_lock(const TimePoint &abs_time); |
| 48 | |
| 49 | template<class TimePoint> bool try_lock_until(const TimePoint &abs_time) |
| 50 | { return this->timed_lock(abs_time); } |
| 51 | |
| 52 | template<class Duration> bool try_lock_for(const Duration &dur) |
| 53 | { return this->timed_lock(duration_to_ustime(dur)); } |
| 54 | |
| 55 | void unlock(); |
| 56 | void take_ownership(){} |
| 57 | private: |
| 58 | volatile boost::uint32_t m_s; |
| 59 | |
| 60 | struct common_lock_wrapper |
| 61 | { |
| 62 | common_lock_wrapper(spin_mutex &sp) |
| 63 | : m_sp(sp) |
| 64 | {} |
| 65 | |
| 66 | void lock() |
| 67 | { |
| 68 | ipcdetail::try_based_lock(m&: m_sp); |
| 69 | } |
| 70 | |
| 71 | template<class TimePoint> |
| 72 | bool timed_lock(const TimePoint &abs_time) |
| 73 | { return m_sp.timed_lock(abs_time); } |
| 74 | |
| 75 | spin_mutex &m_sp; |
| 76 | }; |
| 77 | }; |
| 78 | |
| 79 | inline spin_mutex::spin_mutex() |
| 80 | : m_s(0) |
| 81 | { |
| 82 | //Note that this class is initialized to zero. |
| 83 | //So zeroed memory can be interpreted as an |
| 84 | //initialized mutex |
| 85 | } |
| 86 | |
| 87 | inline spin_mutex::~spin_mutex() |
| 88 | { |
| 89 | //Trivial destructor |
| 90 | } |
| 91 | |
| 92 | inline void spin_mutex::lock(void) |
| 93 | { |
| 94 | common_lock_wrapper clw(*this); |
| 95 | ipcdetail::timeout_when_locking_aware_lock(m&: clw); |
| 96 | } |
| 97 | |
| 98 | inline bool spin_mutex::try_lock(void) |
| 99 | { |
| 100 | boost::uint32_t prev_s = ipcdetail::atomic_cas32(mem: const_cast<boost::uint32_t*>(&m_s), with: 1, cmp: 0); |
| 101 | return m_s == 1 && prev_s == 0; |
| 102 | } |
| 103 | |
| 104 | template<class TimePoint> |
| 105 | inline bool spin_mutex::timed_lock(const TimePoint &abs_time) |
| 106 | { return ipcdetail::try_based_timed_lock(*this, abs_time); } |
| 107 | |
| 108 | inline void spin_mutex::unlock(void) |
| 109 | { ipcdetail::atomic_cas32(mem: const_cast<boost::uint32_t*>(&m_s), with: 0, cmp: 1); } |
| 110 | |
| 111 | } //namespace ipcdetail { |
| 112 | } //namespace interprocess { |
| 113 | } //namespace boost { |
| 114 | |
| 115 | #include <boost/interprocess/detail/config_end.hpp> |
| 116 | |
| 117 | #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP |
| 118 |
