]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2012-2013. 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 | #ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP | |
12 | #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP | |
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 | ||
22 | #include <boost/interprocess/detail/config_begin.hpp> | |
23 | #include <boost/interprocess/detail/workaround.hpp> | |
24 | ||
25 | #include <boost/interprocess/sync/spin/wait.hpp> | |
1e59de90 | 26 | #include <boost/interprocess/detail/timed_utils.hpp> |
7c673cae FG |
27 | |
28 | namespace boost { | |
29 | namespace interprocess { | |
30 | namespace ipcdetail { | |
31 | ||
1e59de90 TL |
32 | template<class MutexType, class TimePoint> |
33 | bool try_based_timed_lock(MutexType &m, const TimePoint &abs_time) | |
7c673cae FG |
34 | { |
35 | //Same as lock() | |
1e59de90 | 36 | if(is_pos_infinity(abs_time)){ |
7c673cae FG |
37 | m.lock(); |
38 | return true; | |
39 | } | |
40 | //Always try to lock to achieve POSIX guarantees: | |
41 | // "Under no circumstance shall the function fail with a timeout if the mutex | |
42 | // can be locked immediately. The validity of the abs_timeout parameter need not | |
43 | // be checked if the mutex can be locked immediately." | |
44 | else if(m.try_lock()){ | |
45 | return true; | |
46 | } | |
47 | else{ | |
48 | spin_wait swait; | |
1e59de90 | 49 | while(microsec_clock<TimePoint>::universal_time() < abs_time){ |
7c673cae FG |
50 | if(m.try_lock()){ |
51 | return true; | |
52 | } | |
53 | swait.yield(); | |
54 | } | |
55 | return false; | |
56 | } | |
57 | } | |
58 | ||
59 | template<class MutexType> | |
60 | void try_based_lock(MutexType &m) | |
61 | { | |
62 | if(!m.try_lock()){ | |
63 | spin_wait swait; | |
64 | do{ | |
65 | if(m.try_lock()){ | |
66 | break; | |
67 | } | |
68 | else{ | |
69 | swait.yield(); | |
70 | } | |
71 | } | |
72 | while(1); | |
73 | } | |
74 | } | |
75 | ||
11fdf7f2 | 76 | template<class MutexType> |
20effc67 | 77 | void timeout_when_locking_aware_lock(MutexType &m) |
11fdf7f2 | 78 | { |
20effc67 | 79 | #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING |
1e59de90 TL |
80 | if (!m.timed_lock(microsec_clock<ustime>::universal_time() |
81 | + usduration_milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS))) | |
20effc67 TL |
82 | { |
83 | throw interprocess_exception(timeout_when_locking_error | |
84 | , "Interprocess mutex timeout when locking. Possible deadlock: " | |
85 | "owner died without unlocking?"); | |
11fdf7f2 | 86 | } |
20effc67 TL |
87 | #else |
88 | m.lock(); | |
89 | #endif | |
11fdf7f2 TL |
90 | } |
91 | ||
7c673cae FG |
92 | } //namespace ipcdetail |
93 | } //namespace interprocess | |
94 | } //namespace boost | |
95 | ||
96 | #include <boost/interprocess/detail/config_end.hpp> | |
97 | ||
98 | #endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP |