]>
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 | ||
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> | |
49 | ||
50 | namespace boost { | |
51 | namespace interprocess { | |
52 | namespace ipcdetail { | |
53 | ||
54 | class spin_recursive_mutex | |
55 | { | |
56 | spin_recursive_mutex(const spin_recursive_mutex &); | |
57 | spin_recursive_mutex &operator=(const spin_recursive_mutex &); | |
58 | public: | |
59 | ||
60 | spin_recursive_mutex(); | |
61 | ~spin_recursive_mutex(); | |
62 | ||
63 | void lock(); | |
64 | bool try_lock(); | |
65 | bool timed_lock(const boost::posix_time::ptime &abs_time); | |
66 | void unlock(); | |
67 | void take_ownership(); | |
68 | private: | |
69 | spin_mutex m_mutex; | |
70 | unsigned int m_nLockCount; | |
71 | volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner; | |
72 | volatile boost::uint32_t m_s; | |
73 | }; | |
74 | ||
75 | inline spin_recursive_mutex::spin_recursive_mutex() | |
76 | : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){} | |
77 | ||
78 | inline spin_recursive_mutex::~spin_recursive_mutex(){} | |
79 | ||
80 | inline void spin_recursive_mutex::lock() | |
81 | { | |
82 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
83 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
84 | handle_t old_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"); | |
90 | } | |
91 | ++m_nLockCount; | |
92 | } | |
93 | else{ | |
94 | m_mutex.lock(); | |
95 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
96 | m_nLockCount = 1; | |
97 | } | |
98 | } | |
99 | ||
100 | inline bool spin_recursive_mutex::try_lock() | |
101 | { | |
102 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
103 | handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
104 | handle_t old_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"); | |
110 | } | |
111 | ++m_nLockCount; | |
112 | return true; | |
113 | } | |
114 | if(m_mutex.try_lock()){ | |
115 | ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner); | |
116 | m_nLockCount = 1; | |
117 | return true; | |
118 | } | |
119 | return false; | |
120 | } | |
121 | ||
122 | inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
123 | { | |
124 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
125 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
126 | handle_t old_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"); | |
132 | } | |
133 | ++m_nLockCount; | |
134 | return true; | |
135 | } | |
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); | |
139 | m_nLockCount = 1; | |
140 | return true; | |
141 | } | |
142 | return false; | |
143 | } | |
144 | ||
145 | inline void spin_recursive_mutex::unlock() | |
146 | { | |
147 | typedef ipcdetail::OS_systemwide_thread_id_t handle_t; | |
148 | handle_t old_id; | |
149 | ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id); | |
150 | const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id()); | |
151 | (void)old_id; | |
152 | (void)thr_id; | |
153 | BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id)); | |
154 | --m_nLockCount; | |
155 | if(!m_nLockCount){ | |
156 | const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id()); | |
157 | ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner); | |
158 | m_mutex.unlock(); | |
159 | } | |
160 | } | |
161 | ||
162 | inline void spin_recursive_mutex::take_ownership() | |
163 | { | |
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); | |
168 | } | |
169 | ||
170 | } //namespace ipcdetail { | |
171 | } //namespace interprocess { | |
172 | } //namespace boost { | |
173 | ||
174 | #include <boost/interprocess/detail/config_end.hpp> | |
175 | ||
176 | #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP |