Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp @ 101:c530137014c0
Update Boost headers (1.58.0)
author | Chris Cannam |
---|---|
date | Mon, 07 Sep 2015 11:12:49 +0100 |
parents | 2665513ce2d3 |
children |
comparison
equal
deleted
inserted
replaced
100:793467b5e61c | 101:c530137014c0 |
---|---|
9 ////////////////////////////////////////////////////////////////////////////// | 9 ////////////////////////////////////////////////////////////////////////////// |
10 | 10 |
11 #ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP | 11 #ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP |
12 #define BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP | 12 #define BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP |
13 | 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 | |
14 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | 22 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> |
15 #include <boost/interprocess/exceptions.hpp> | 23 #include <boost/interprocess/exceptions.hpp> |
16 #include <boost/interprocess/creation_tags.hpp> | 24 #include <boost/interprocess/creation_tags.hpp> |
17 #include <boost/interprocess/detail/os_file_functions.hpp> | 25 #include <boost/interprocess/detail/os_file_functions.hpp> |
18 #include <boost/interprocess/detail/tmp_dir_helpers.hpp> | 26 #include <boost/interprocess/detail/shared_dir_helpers.hpp> |
19 #include <boost/interprocess/permissions.hpp> | 27 #include <boost/interprocess/permissions.hpp> |
20 | 28 |
21 #include <fcntl.h> //O_CREAT, O_*... | 29 #include <fcntl.h> //O_CREAT, O_*... |
22 #include <unistd.h> //close | 30 #include <unistd.h> //close |
23 #include <string> //std::string | 31 #include <string> //std::string |
33 | 41 |
34 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | 42 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS |
35 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp> | 43 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp> |
36 #else | 44 #else |
37 #include <boost/interprocess/detail/os_thread_functions.hpp> | 45 #include <boost/interprocess/detail/os_thread_functions.hpp> |
38 #include <boost/interprocess/sync/spin/wait.hpp> | 46 #include <boost/interprocess/sync/detail/locks.hpp> |
47 #include <boost/interprocess/sync/detail/common_algorithms.hpp> | |
39 #endif | 48 #endif |
40 | 49 |
41 namespace boost { | 50 namespace boost { |
42 namespace interprocess { | 51 namespace interprocess { |
43 namespace ipcdetail { | 52 namespace ipcdetail { |
53 | |
54 #ifdef BOOST_INTERPROCESS_POSIX_NAMED_SEMAPHORES | |
44 | 55 |
45 inline bool semaphore_open | 56 inline bool semaphore_open |
46 (sem_t *&handle, create_enum_t type, const char *origname, | 57 (sem_t *&handle, create_enum_t type, const char *origname, |
47 unsigned int count = 0, const permissions &perm = permissions()) | 58 unsigned int count = 0, const permissions &perm = permissions()) |
48 { | 59 { |
49 std::string name; | 60 std::string name; |
50 #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES | 61 #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES |
51 add_leading_slash(origname, name); | 62 add_leading_slash(origname, name); |
52 #else | 63 #else |
53 create_tmp_and_clean_old_and_get_filename(origname, name); | 64 create_shared_dir_cleaning_old_and_get_filepath(origname, name); |
54 #endif | 65 #endif |
55 | 66 |
56 //Create new mapping | 67 //Create new mapping |
57 int oflag = 0; | 68 int oflag = 0; |
58 switch(type){ | 69 switch(type){ |
114 try{ | 125 try{ |
115 std::string sem_str; | 126 std::string sem_str; |
116 #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES | 127 #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES |
117 add_leading_slash(semname, sem_str); | 128 add_leading_slash(semname, sem_str); |
118 #else | 129 #else |
119 tmp_filename(semname, sem_str); | 130 shared_filepath(semname, sem_str); |
120 #endif | 131 #endif |
121 return 0 == sem_unlink(sem_str.c_str()); | 132 return 0 == sem_unlink(sem_str.c_str()); |
122 } | 133 } |
123 catch(...){ | 134 catch(...){ |
124 return false; | 135 return false; |
125 } | 136 } |
126 } | 137 } |
138 | |
139 #endif //BOOST_INTERPROCESS_POSIX_NAMED_SEMAPHORES | |
140 | |
141 #ifdef BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES | |
127 | 142 |
128 inline void semaphore_init(sem_t *handle, unsigned int initialCount) | 143 inline void semaphore_init(sem_t *handle, unsigned int initialCount) |
129 { | 144 { |
130 int ret = sem_init(handle, 1, initialCount); | 145 int ret = sem_init(handle, 1, initialCount); |
131 //According to SUSV3 version 2003 edition, the return value of a successful | 146 //According to SUSV3 version 2003 edition, the return value of a successful |
132 //sem_init call is not defined, but -1 is returned on failure. | 147 //sem_init call is not defined, but -1 is returned on failure. |
133 //In the future, a successful call might be required to return 0. | 148 //In the future, a successful call might be required to return 0. |
134 if(ret == -1){ | 149 if(ret == -1){ |
135 throw interprocess_exception(system_error_code()); | 150 error_info err = system_error_code(); |
151 throw interprocess_exception(err); | |
136 } | 152 } |
137 } | 153 } |
138 | 154 |
139 inline void semaphore_destroy(sem_t *handle) | 155 inline void semaphore_destroy(sem_t *handle) |
140 { | 156 { |
142 if(ret != 0){ | 158 if(ret != 0){ |
143 BOOST_ASSERT(0); | 159 BOOST_ASSERT(0); |
144 } | 160 } |
145 } | 161 } |
146 | 162 |
163 #endif //BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES | |
164 | |
147 inline void semaphore_post(sem_t *handle) | 165 inline void semaphore_post(sem_t *handle) |
148 { | 166 { |
149 int ret = sem_post(handle); | 167 int ret = sem_post(handle); |
150 if(ret != 0){ | 168 if(ret != 0){ |
151 throw interprocess_exception(system_error_code()); | 169 error_info err = system_error_code(); |
170 throw interprocess_exception(err); | |
152 } | 171 } |
153 } | 172 } |
154 | 173 |
155 inline void semaphore_wait(sem_t *handle) | 174 inline void semaphore_wait(sem_t *handle) |
156 { | 175 { |
157 int ret = sem_wait(handle); | 176 int ret = sem_wait(handle); |
158 if(ret != 0){ | 177 if(ret != 0){ |
159 throw interprocess_exception(system_error_code()); | 178 error_info err = system_error_code(); |
179 throw interprocess_exception(err); | |
160 } | 180 } |
161 } | 181 } |
162 | 182 |
163 inline bool semaphore_try_wait(sem_t *handle) | 183 inline bool semaphore_try_wait(sem_t *handle) |
164 { | 184 { |
166 if(res == 0) | 186 if(res == 0) |
167 return true; | 187 return true; |
168 if(system_error_code() == EAGAIN){ | 188 if(system_error_code() == EAGAIN){ |
169 return false; | 189 return false; |
170 } | 190 } |
171 throw interprocess_exception(system_error_code()); | 191 error_info err = system_error_code(); |
172 return false; | 192 throw interprocess_exception(err); |
173 } | 193 } |
194 | |
195 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
196 | |
197 struct semaphore_wrapper_try_wrapper | |
198 { | |
199 explicit semaphore_wrapper_try_wrapper(sem_t *handle) | |
200 : m_handle(handle) | |
201 {} | |
202 | |
203 void wait() | |
204 { semaphore_wait(m_handle); } | |
205 | |
206 bool try_wait() | |
207 { return semaphore_try_wait(m_handle); } | |
208 | |
209 private: | |
210 sem_t *m_handle; | |
211 }; | |
212 | |
213 #endif | |
174 | 214 |
175 inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time) | 215 inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time) |
176 { | 216 { |
217 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
218 //Posix does not support infinity absolute time so handle it here | |
177 if(abs_time == boost::posix_time::pos_infin){ | 219 if(abs_time == boost::posix_time::pos_infin){ |
178 semaphore_wait(handle); | 220 semaphore_wait(handle); |
179 return true; | 221 return true; |
180 } | 222 } |
181 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | 223 |
182 timespec tspec = ptime_to_timespec(abs_time); | 224 timespec tspec = ptime_to_timespec(abs_time); |
183 for (;;){ | 225 for (;;){ |
184 int res = sem_timedwait(handle, &tspec); | 226 int res = sem_timedwait(handle, &tspec); |
185 if(res == 0) | 227 if(res == 0) |
186 return true; | 228 return true; |
189 errno = res; | 231 errno = res; |
190 } | 232 } |
191 if(system_error_code() == ETIMEDOUT){ | 233 if(system_error_code() == ETIMEDOUT){ |
192 return false; | 234 return false; |
193 } | 235 } |
194 throw interprocess_exception(system_error_code()); | 236 error_info err = system_error_code(); |
237 throw interprocess_exception(err); | |
195 } | 238 } |
196 return false; | 239 return false; |
197 #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | 240 #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS |
198 boost::posix_time::ptime now; | 241 |
199 spin_wait swait; | 242 semaphore_wrapper_try_wrapper swtw(handle); |
200 do{ | 243 ipcdetail::lock_to_wait<semaphore_wrapper_try_wrapper> lw(swtw); |
201 if(semaphore_try_wait(handle)) | 244 return ipcdetail::try_based_timed_lock(lw, abs_time); |
202 return true; | 245 |
203 swait.yield(); | |
204 }while((now = microsec_clock::universal_time()) < abs_time); | |
205 return false; | |
206 #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | 246 #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS |
207 } | 247 } |
208 | 248 |
209 } //namespace ipcdetail { | 249 } //namespace ipcdetail { |
210 } //namespace interprocess { | 250 } //namespace interprocess { |