• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/interprocess/detail/posix_time_types_wrk.hpp>
25 #include <boost/assert.hpp>
26 #include <boost/interprocess/detail/atomic.hpp>
27 #include <boost/cstdint.hpp>
28 #include <boost/interprocess/detail/os_thread_functions.hpp>
29 #include <boost/interprocess/sync/detail/common_algorithms.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    bool timed_lock(const boost::posix_time::ptime &abs_time);
47    void unlock();
take_ownership()48    void take_ownership(){}
49    private:
50    volatile boost::uint32_t m_s;
51 
52    struct common_lock_wrapper
53    {
common_lock_wrapperboost::interprocess::ipcdetail::spin_mutex::common_lock_wrapper54       common_lock_wrapper(spin_mutex &sp)
55          : m_sp(sp)
56       {}
57 
lockboost::interprocess::ipcdetail::spin_mutex::common_lock_wrapper58       void lock()
59       {
60          ipcdetail::try_based_lock(m_sp);
61       }
62 
timed_lockboost::interprocess::ipcdetail::spin_mutex::common_lock_wrapper63       bool timed_lock(const boost::posix_time::ptime &abs_time)
64       {  return m_sp.timed_lock(abs_time);   }
65 
66       spin_mutex &m_sp;
67    };
68 };
69 
spin_mutex()70 inline spin_mutex::spin_mutex()
71    : m_s(0)
72 {
73    //Note that this class is initialized to zero.
74    //So zeroed memory can be interpreted as an
75    //initialized mutex
76 }
77 
~spin_mutex()78 inline spin_mutex::~spin_mutex()
79 {
80    //Trivial destructor
81 }
82 
lock(void)83 inline void spin_mutex::lock(void)
84 {
85    common_lock_wrapper clw(*this);
86    ipcdetail::timeout_when_locking_aware_lock(clw);
87 }
88 
try_lock(void)89 inline bool spin_mutex::try_lock(void)
90 {
91    boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
92    return m_s == 1 && prev_s == 0;
93 }
94 
timed_lock(const boost::posix_time::ptime & abs_time)95 inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
96 {  return ipcdetail::try_based_timed_lock(*this, abs_time); }
97 
unlock(void)98 inline void spin_mutex::unlock(void)
99 {  ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1);   }
100 
101 }  //namespace ipcdetail {
102 }  //namespace interprocess {
103 }  //namespace boost {
104 
105 #include <boost/interprocess/detail/config_end.hpp>
106 
107 #endif   //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
108