]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | // Parts of the pthread code come from Boost Threads code: | |
12 | // | |
13 | ////////////////////////////////////////////////////////////////////////////// | |
14 | // | |
15 | // Copyright (C) 2001-2003 | |
16 | // William E. Kempf | |
17 | // | |
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 | ////////////////////////////////////////////////////////////////////////////// | |
26 | ||
27 | #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP | |
28 | #define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP | |
29 | ||
30 | #ifndef BOOST_CONFIG_HPP | |
31 | # include <boost/config.hpp> | |
32 | #endif | |
33 | # | |
34 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
35 | # pragma once | |
36 | #endif | |
37 | ||
38 | #include <boost/interprocess/detail/config_begin.hpp> | |
39 | #include <boost/interprocess/detail/workaround.hpp> | |
40 | ||
7c673cae FG |
41 | #include <boost/interprocess/detail/os_thread_functions.hpp> |
42 | #include <boost/interprocess/exceptions.hpp> | |
43 | #include <boost/interprocess/detail/atomic.hpp> | |
44 | #include <boost/cstdint.hpp> | |
45 | #include <boost/interprocess/detail/os_thread_functions.hpp> | |
46 | #include <boost/interprocess/sync/spin/mutex.hpp> | |
47 | #include <boost/assert.hpp> | |
48 | ||
49 | namespace boost { | |
50 | namespace interprocess { | |
51 | namespace ipcdetail { | |
52 | ||
53 | class spin_recursive_mutex | |
54 | { | |
55 | spin_recursive_mutex(const spin_recursive_mutex &); | |
56 | spin_recursive_mutex &operator=(const spin_recursive_mutex &); | |
57 | public: | |
58 | ||
59 | spin_recursive_mutex(); | |
60 | ~spin_recursive_mutex(); | |
61 | ||
62 | void lock(); | |
63 | bool try_lock(); | |
1e59de90 TL |
64 | template<class TimePoint> |
65 | bool timed_lock(const TimePoint &abs_time); | |
66 | ||
67 | template<class TimePoint> bool try_lock_until(const TimePoint &abs_time) | |
68 | { return this->timed_lock(abs_time); } | |
69 | ||
70 | template<class Duration> bool try_lock_for(const Duration &dur) | |
71 | { return this->timed_lock(duration_to_ustime(dur)); } | |
72 | ||
7c673cae FG |
73 | void unlock(); |
74 | void take_ownership(); | |
75 | private: | |
76 | spin_mutex m_mutex; | |
77 | unsigned int m_nLockCount; | |
78 | volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner; | |
79 | volatile boost::uint32_t m_s; | |
80 | }; | |
81 | ||
82 | inline spin_recursive_mutex::spin_recursive_mutex() | |
83 | : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){} | |
84 | ||
85 | inline spin_recursive_mutex::~spin_recursive_mutex(){} | |
86 | ||
87 | inline void spin_recursive_mutex::lock() | |
88 | { | |
89 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
90 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
91 | handle_t old_id; | |
92 | ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id); | |
93 | if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){ | |
94 | if((unsigned int)(m_nLockCount+1) == 0){ | |
95 | //Overflow, throw an exception | |
96 | throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow"); | |
97 | } | |
98 | ++m_nLockCount; | |
99 | } | |
100 | else{ | |
101 | m_mutex.lock(); | |
102 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
103 | m_nLockCount = 1; | |
104 | } | |
105 | } | |
106 | ||
107 | inline bool spin_recursive_mutex::try_lock() | |
108 | { | |
109 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
110 | handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
111 | handle_t old_id; | |
112 | ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id); | |
113 | if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it | |
114 | if((unsigned int)(m_nLockCount+1) == 0){ | |
115 | //Overflow, throw an exception | |
116 | throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow"); | |
117 | } | |
118 | ++m_nLockCount; | |
119 | return true; | |
120 | } | |
121 | if(m_mutex.try_lock()){ | |
122 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
123 | m_nLockCount = 1; | |
124 | return true; | |
125 | } | |
126 | return false; | |
127 | } | |
128 | ||
1e59de90 TL |
129 | template<class TimePoint> |
130 | inline bool spin_recursive_mutex::timed_lock(const TimePoint &abs_time) | |
7c673cae FG |
131 | { |
132 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
133 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
134 | handle_t old_id; | |
135 | ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id); | |
136 | if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it | |
137 | if((unsigned int)(m_nLockCount+1) == 0){ | |
138 | //Overflow, throw an exception | |
139 | throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow"); | |
140 | } | |
141 | ++m_nLockCount; | |
142 | return true; | |
143 | } | |
144 | //m_mutex supports abs_time so no need to check it | |
145 | if(m_mutex.timed_lock(abs_time)){ | |
146 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
147 | m_nLockCount = 1; | |
148 | return true; | |
149 | } | |
150 | return false; | |
151 | } | |
152 | ||
153 | inline void spin_recursive_mutex::unlock() | |
154 | { | |
155 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
156 | handle_t old_id; | |
157 | ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id); | |
158 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
159 | (void)old_id; | |
160 | (void)thr_id; | |
161 | BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id)); | |
162 | --m_nLockCount; | |
163 | if(!m_nLockCount){ | |
164 | const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id()); | |
165 | ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner); | |
166 | m_mutex.unlock(); | |
167 | } | |
168 | } | |
169 | ||
170 | inline void spin_recursive_mutex::take_ownership() | |
171 | { | |
172 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
173 | this->m_nLockCount = 1; | |
174 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
175 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
176 | } | |
177 | ||
178 | } //namespace ipcdetail { | |
179 | } //namespace interprocess { | |
180 | } //namespace boost { | |
181 | ||
182 | #include <boost/interprocess/detail/config_end.hpp> | |
183 | ||
184 | #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP |