Chris@102
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
2 //
|
Chris@102
|
3 // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost
|
Chris@102
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6 //
|
Chris@102
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@102
|
8 //
|
Chris@102
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
10
|
Chris@102
|
11 #ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
|
Chris@102
|
12 #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
|
Chris@102
|
13
|
Chris@102
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@102
|
15 # include <boost/config.hpp>
|
Chris@102
|
16 #endif
|
Chris@102
|
17 #
|
Chris@102
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@102
|
19 # pragma once
|
Chris@102
|
20 #endif
|
Chris@102
|
21
|
Chris@102
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@102
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@102
|
24
|
Chris@102
|
25 #include <boost/interprocess/sync/spin/wait.hpp>
|
Chris@102
|
26
|
Chris@102
|
27 namespace boost {
|
Chris@102
|
28 namespace interprocess {
|
Chris@102
|
29 namespace ipcdetail {
|
Chris@102
|
30
|
Chris@102
|
31 template<class MutexType>
|
Chris@102
|
32 bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time)
|
Chris@102
|
33 {
|
Chris@102
|
34 //Same as lock()
|
Chris@102
|
35 if(abs_time == boost::posix_time::pos_infin){
|
Chris@102
|
36 m.lock();
|
Chris@102
|
37 return true;
|
Chris@102
|
38 }
|
Chris@102
|
39 //Always try to lock to achieve POSIX guarantees:
|
Chris@102
|
40 // "Under no circumstance shall the function fail with a timeout if the mutex
|
Chris@102
|
41 // can be locked immediately. The validity of the abs_timeout parameter need not
|
Chris@102
|
42 // be checked if the mutex can be locked immediately."
|
Chris@102
|
43 else if(m.try_lock()){
|
Chris@102
|
44 return true;
|
Chris@102
|
45 }
|
Chris@102
|
46 else{
|
Chris@102
|
47 spin_wait swait;
|
Chris@102
|
48 while(microsec_clock::universal_time() < abs_time){
|
Chris@102
|
49 if(m.try_lock()){
|
Chris@102
|
50 return true;
|
Chris@102
|
51 }
|
Chris@102
|
52 swait.yield();
|
Chris@102
|
53 }
|
Chris@102
|
54 return false;
|
Chris@102
|
55 }
|
Chris@102
|
56 }
|
Chris@102
|
57
|
Chris@102
|
58 template<class MutexType>
|
Chris@102
|
59 void try_based_lock(MutexType &m)
|
Chris@102
|
60 {
|
Chris@102
|
61 if(!m.try_lock()){
|
Chris@102
|
62 spin_wait swait;
|
Chris@102
|
63 do{
|
Chris@102
|
64 if(m.try_lock()){
|
Chris@102
|
65 break;
|
Chris@102
|
66 }
|
Chris@102
|
67 else{
|
Chris@102
|
68 swait.yield();
|
Chris@102
|
69 }
|
Chris@102
|
70 }
|
Chris@102
|
71 while(1);
|
Chris@102
|
72 }
|
Chris@102
|
73 }
|
Chris@102
|
74
|
Chris@102
|
75 } //namespace ipcdetail
|
Chris@102
|
76 } //namespace interprocess
|
Chris@102
|
77 } //namespace boost
|
Chris@102
|
78
|
Chris@102
|
79 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@102
|
80
|
Chris@102
|
81 #endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
|