Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10 //
|
Chris@16
|
11 // Parts of the pthread code come from Boost Threads code:
|
Chris@16
|
12 //
|
Chris@16
|
13 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
14 //
|
Chris@16
|
15 // Copyright (C) 2001-2003
|
Chris@16
|
16 // William E. Kempf
|
Chris@16
|
17 //
|
Chris@16
|
18 // Permission to use, copy, modify, distribute and sell this software
|
Chris@16
|
19 // and its documentation for any purpose is hereby granted without fee,
|
Chris@16
|
20 // provided that the above copyright notice appear in all copies and
|
Chris@16
|
21 // that both that copyright notice and this permission notice appear
|
Chris@16
|
22 // in supporting documentation. William E. Kempf makes no representations
|
Chris@16
|
23 // about the suitability of this software for any purpose.
|
Chris@16
|
24 // It is provided "as is" without express or implied warranty.
|
Chris@16
|
25 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
26
|
Chris@16
|
27 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|
Chris@16
|
28 #define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|
Chris@16
|
29
|
Chris@101
|
30 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
31 # include <boost/config.hpp>
|
Chris@101
|
32 #endif
|
Chris@101
|
33 #
|
Chris@101
|
34 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
35 # pragma once
|
Chris@16
|
36 #endif
|
Chris@16
|
37
|
Chris@16
|
38 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
39 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
42 #include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@16
|
43 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
44 #include <boost/interprocess/detail/atomic.hpp>
|
Chris@16
|
45 #include <boost/cstdint.hpp>
|
Chris@16
|
46 #include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@16
|
47 #include <boost/interprocess/sync/spin/mutex.hpp>
|
Chris@16
|
48 #include <boost/assert.hpp>
|
Chris@16
|
49
|
Chris@16
|
50 namespace boost {
|
Chris@16
|
51 namespace interprocess {
|
Chris@16
|
52 namespace ipcdetail {
|
Chris@16
|
53
|
Chris@16
|
54 class spin_recursive_mutex
|
Chris@16
|
55 {
|
Chris@16
|
56 spin_recursive_mutex(const spin_recursive_mutex &);
|
Chris@16
|
57 spin_recursive_mutex &operator=(const spin_recursive_mutex &);
|
Chris@16
|
58 public:
|
Chris@16
|
59
|
Chris@16
|
60 spin_recursive_mutex();
|
Chris@16
|
61 ~spin_recursive_mutex();
|
Chris@16
|
62
|
Chris@16
|
63 void lock();
|
Chris@16
|
64 bool try_lock();
|
Chris@16
|
65 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
66 void unlock();
|
Chris@16
|
67 void take_ownership();
|
Chris@16
|
68 private:
|
Chris@16
|
69 spin_mutex m_mutex;
|
Chris@16
|
70 unsigned int m_nLockCount;
|
Chris@16
|
71 volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
|
Chris@16
|
72 volatile boost::uint32_t m_s;
|
Chris@16
|
73 };
|
Chris@16
|
74
|
Chris@16
|
75 inline spin_recursive_mutex::spin_recursive_mutex()
|
Chris@16
|
76 : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
|
Chris@16
|
77
|
Chris@16
|
78 inline spin_recursive_mutex::~spin_recursive_mutex(){}
|
Chris@16
|
79
|
Chris@16
|
80 inline void spin_recursive_mutex::lock()
|
Chris@16
|
81 {
|
Chris@16
|
82 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
Chris@16
|
83 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
Chris@16
|
84 handle_t old_id;
|
Chris@16
|
85 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
Chris@16
|
86 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
|
Chris@16
|
87 if((unsigned int)(m_nLockCount+1) == 0){
|
Chris@16
|
88 //Overflow, throw an exception
|
Chris@16
|
89 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
Chris@16
|
90 }
|
Chris@16
|
91 ++m_nLockCount;
|
Chris@16
|
92 }
|
Chris@16
|
93 else{
|
Chris@16
|
94 m_mutex.lock();
|
Chris@16
|
95 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
Chris@16
|
96 m_nLockCount = 1;
|
Chris@16
|
97 }
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 inline bool spin_recursive_mutex::try_lock()
|
Chris@16
|
101 {
|
Chris@16
|
102 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
Chris@16
|
103 handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
Chris@16
|
104 handle_t old_id;
|
Chris@16
|
105 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
Chris@16
|
106 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
|
Chris@16
|
107 if((unsigned int)(m_nLockCount+1) == 0){
|
Chris@16
|
108 //Overflow, throw an exception
|
Chris@16
|
109 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
Chris@16
|
110 }
|
Chris@16
|
111 ++m_nLockCount;
|
Chris@16
|
112 return true;
|
Chris@16
|
113 }
|
Chris@16
|
114 if(m_mutex.try_lock()){
|
Chris@16
|
115 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
Chris@16
|
116 m_nLockCount = 1;
|
Chris@16
|
117 return true;
|
Chris@16
|
118 }
|
Chris@16
|
119 return false;
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
123 {
|
Chris@16
|
124 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
Chris@16
|
125 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
Chris@16
|
126 handle_t old_id;
|
Chris@16
|
127 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
Chris@16
|
128 if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
|
Chris@16
|
129 if((unsigned int)(m_nLockCount+1) == 0){
|
Chris@16
|
130 //Overflow, throw an exception
|
Chris@16
|
131 throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
Chris@16
|
132 }
|
Chris@16
|
133 ++m_nLockCount;
|
Chris@16
|
134 return true;
|
Chris@16
|
135 }
|
Chris@101
|
136 //m_mutex supports abs_time so no need to check it
|
Chris@16
|
137 if(m_mutex.timed_lock(abs_time)){
|
Chris@16
|
138 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
Chris@16
|
139 m_nLockCount = 1;
|
Chris@16
|
140 return true;
|
Chris@16
|
141 }
|
Chris@16
|
142 return false;
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 inline void spin_recursive_mutex::unlock()
|
Chris@16
|
146 {
|
Chris@16
|
147 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
Chris@16
|
148 handle_t old_id;
|
Chris@16
|
149 ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
Chris@16
|
150 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
Chris@16
|
151 (void)old_id;
|
Chris@16
|
152 (void)thr_id;
|
Chris@16
|
153 BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
|
Chris@16
|
154 --m_nLockCount;
|
Chris@16
|
155 if(!m_nLockCount){
|
Chris@16
|
156 const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
|
Chris@16
|
157 ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
|
Chris@16
|
158 m_mutex.unlock();
|
Chris@16
|
159 }
|
Chris@16
|
160 }
|
Chris@16
|
161
|
Chris@16
|
162 inline void spin_recursive_mutex::take_ownership()
|
Chris@16
|
163 {
|
Chris@16
|
164 typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
Chris@16
|
165 this->m_nLockCount = 1;
|
Chris@16
|
166 const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
Chris@16
|
167 ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 } //namespace ipcdetail {
|
Chris@16
|
171 } //namespace interprocess {
|
Chris@16
|
172 } //namespace boost {
|
Chris@16
|
173
|
Chris@16
|
174 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
175
|
Chris@16
|
176 #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|