1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 // Parts of the pthread code come from Boost Threads code:
13 //////////////////////////////////////////////////////////////////////////////
15 // Copyright (C) 2001-2003
18 // Permission to use, copy, modify, distribute and sell this software
19 // and its documentation for any purpose is hereby granted without fee,
20 // provided that the above copyright notice appear in all copies and
21 // that both that copyright notice and this permission notice appear
22 // in supporting documentation. William E. Kempf makes no representations
23 // about the suitability of this software for any purpose.
24 // It is provided "as is" without express or implied warranty.
25 //////////////////////////////////////////////////////////////////////////////
27 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
28 #define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
30 #ifndef BOOST_CONFIG_HPP
31 # include <boost/config.hpp>
34 #if defined(BOOST_HAS_PRAGMA_ONCE)
38 #include <boost/interprocess/detail/config_begin.hpp>
39 #include <boost/interprocess/detail/workaround.hpp>
41 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
42 #include <boost/interprocess/detail/os_thread_functions.hpp>
43 #include <boost/interprocess/exceptions.hpp>
44 #include <boost/interprocess/detail/atomic.hpp>
45 #include <boost/cstdint.hpp>
46 #include <boost/interprocess/detail/os_thread_functions.hpp>
47 #include <boost/interprocess/sync/spin/mutex.hpp>
48 #include <boost/assert.hpp>
51 namespace interprocess {
54 class spin_recursive_mutex
56 spin_recursive_mutex(const spin_recursive_mutex &);
57 spin_recursive_mutex &operator=(const spin_recursive_mutex &);
60 spin_recursive_mutex();
61 ~spin_recursive_mutex();
65 bool timed_lock(const boost::posix_time::ptime &abs_time);
67 void take_ownership();
70 unsigned int m_nLockCount;
71 volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
72 volatile boost::uint32_t m_s;
75 inline spin_recursive_mutex::spin_recursive_mutex()
76 : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
78 inline spin_recursive_mutex::~spin_recursive_mutex(){}
80 inline void spin_recursive_mutex::lock()
82 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
83 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
85 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
86 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
87 if((unsigned int)(m_nLockCount+1) == 0){
88 //Overflow, throw an exception
89 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
95 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
100 inline bool spin_recursive_mutex::try_lock()
102 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
103 handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
105 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
106 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
107 if((unsigned int)(m_nLockCount+1) == 0){
108 //Overflow, throw an exception
109 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
114 if(m_mutex.try_lock()){
115 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
122 inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
124 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
125 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
127 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
128 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
129 if((unsigned int)(m_nLockCount+1) == 0){
130 //Overflow, throw an exception
131 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
136 //m_mutex supports abs_time so no need to check it
137 if(m_mutex.timed_lock(abs_time)){
138 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
145 inline void spin_recursive_mutex::unlock()
147 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
149 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
150 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
153 BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
156 const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
157 ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
162 inline void spin_recursive_mutex::take_ownership()
164 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
165 this->m_nLockCount = 1;
166 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
167 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
170 } //namespace ipcdetail {
171 } //namespace interprocess {
172 } //namespace boost {
174 #include <boost/interprocess/detail/config_end.hpp>
176 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP