Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-07 08:09:54

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
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 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 #include <boost/assert.hpp>
0026 #include <boost/interprocess/detail/atomic.hpp>
0027 #include <boost/cstdint.hpp>
0028 #include <boost/interprocess/detail/os_thread_functions.hpp>
0029 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0030 #include <boost/interprocess/timed_utils.hpp>
0031 
0032 namespace boost {
0033 namespace interprocess {
0034 namespace ipcdetail {
0035 
0036 class spin_mutex
0037 {
0038    spin_mutex(const spin_mutex &);
0039    spin_mutex &operator=(const spin_mutex &);
0040    public:
0041 
0042    spin_mutex();
0043    ~spin_mutex();
0044 
0045    void lock();
0046    bool try_lock();
0047    template<class TimePoint>
0048    bool timed_lock(const TimePoint &abs_time);
0049 
0050    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0051    {  return this->timed_lock(abs_time);  }
0052 
0053    template<class Duration>  bool try_lock_for(const Duration &dur)
0054    {  return this->timed_lock(duration_to_ustime(dur)); }
0055 
0056    void unlock();
0057    void take_ownership(){}
0058    private:
0059    volatile boost::uint32_t m_s;
0060 
0061    struct common_lock_wrapper
0062    {
0063       common_lock_wrapper(spin_mutex &sp)
0064          : m_sp(sp)
0065       {}
0066 
0067       void lock()
0068       {
0069          ipcdetail::try_based_lock(m_sp);
0070       }
0071 
0072       template<class TimePoint>
0073       bool timed_lock(const TimePoint &abs_time)
0074       {  return m_sp.timed_lock(abs_time);   }
0075 
0076       spin_mutex &m_sp;
0077    };
0078 };
0079 
0080 inline spin_mutex::spin_mutex()
0081    : m_s(0)
0082 {
0083    //Note that this class is initialized to zero.
0084    //So zeroed memory can be interpreted as an
0085    //initialized mutex
0086 }
0087 
0088 inline spin_mutex::~spin_mutex()
0089 {
0090    //Trivial destructor
0091 }
0092 
0093 inline void spin_mutex::lock(void)
0094 {
0095    common_lock_wrapper clw(*this);
0096    ipcdetail::timeout_when_locking_aware_lock(clw);
0097 }
0098 
0099 inline bool spin_mutex::try_lock(void)
0100 {
0101    boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
0102    return m_s == 1 && prev_s == 0;
0103 }
0104 
0105 template<class TimePoint>
0106 inline bool spin_mutex::timed_lock(const TimePoint &abs_time)
0107 {  return ipcdetail::try_based_timed_lock(*this, abs_time); }
0108 
0109 inline void spin_mutex::unlock(void)
0110 {  ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1);   }
0111 
0112 }  //namespace ipcdetail {
0113 }  //namespace interprocess {
0114 }  //namespace boost {
0115 
0116 #include <boost/interprocess/detail/config_end.hpp>
0117 
0118 #endif   //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP