1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
12 #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/sync/spin/wait.hpp>
28 namespace interprocess {
31 template<class MutexType>
32 bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time)
35 if(abs_time == boost::posix_time::pos_infin){
39 //Always try to lock to achieve POSIX guarantees:
40 // "Under no circumstance shall the function fail with a timeout if the mutex
41 // can be locked immediately. The validity of the abs_timeout parameter need not
42 // be checked if the mutex can be locked immediately."
43 else if(m.try_lock()){
48 while(microsec_clock::universal_time() < abs_time){
58 template<class MutexType>
59 void try_based_lock(MutexType &m)
75 template<class MutexType>
76 void timed_based_lock(MutexType &m, unsigned const uCheckPeriodSec)
78 const boost::posix_time::time_duration dur(0, 0, uCheckPeriodSec);
79 boost::posix_time::ptime deadline(microsec_clock::universal_time()+dur);
80 if(!m.timed_lock(deadline)){
83 deadline = microsec_clock::universal_time()+dur;
84 if(m.timed_lock(deadline)){
95 template<class MutexType>
96 void timed_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time, unsigned const uCheckPeriodSec)
98 const boost::posix_time::time_duration dur(0, 0, uCheckPeriodSec);
99 boost::posix_time::ptime deadline(microsec_clock::universal_time()+dur);
100 if(abs_time <= deadline){
101 m.timed_lock(abs_time);
103 else if(!m.timed_lock(deadline)){
106 deadline = microsec_clock::universal_time()+dur;
107 if(m.timed_lock(deadline)){
118 } //namespace ipcdetail
119 } //namespace interprocess
122 #include <boost/interprocess/detail/config_end.hpp>
124 #endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP