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>
26 
27 namespace boost {
28 namespace interprocess {
29 namespace ipcdetail {
30 
31 template<class MutexType>
32 bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time)
33 {
34    //Same as lock()
35    if(abs_time == boost::posix_time::pos_infin){
36       m.lock();
37       return true;
38    }
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()){
44       return true;
45    }
46    else{
47       spin_wait swait;
48       while(microsec_clock::universal_time() < abs_time){
49          if(m.try_lock()){
50             return true;
51          }
52          swait.yield();
53       }
54       return false;
55    }
56 }
57 
58 template<class MutexType>
59 void try_based_lock(MutexType &m)
60 {
61    if(!m.try_lock()){
62       spin_wait swait;
63       do{
64          if(m.try_lock()){
65             break;
66          }
67          else{
68             swait.yield();
69          }
70       }
71       while(1);
72    }
73 }
74 
75 }  //namespace ipcdetail
76 }  //namespace interprocess
77 }  //namespace boost
78 
79 #include <boost/interprocess/detail/config_end.hpp>
80 
81 #endif   //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
82