Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:31

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
0012 #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 
0025 #include <boost/interprocess/sync/spin/wait.hpp>
0026 #include <boost/interprocess/detail/timed_utils.hpp>
0027 
0028 namespace boost {
0029 namespace interprocess {
0030 namespace ipcdetail {
0031 
0032 template<class MutexType, class TimePoint>
0033 bool try_based_timed_lock(MutexType &m, const TimePoint &abs_time)
0034 {
0035    //Same as lock()
0036    if(is_pos_infinity(abs_time)){
0037       m.lock();
0038       return true;
0039    }
0040    //Always try to lock to achieve POSIX guarantees:
0041    // "Under no circumstance shall the function fail with a timeout if the mutex
0042    //  can be locked immediately. The validity of the abs_timeout parameter need not
0043    //  be checked if the mutex can be locked immediately."
0044    else if(m.try_lock()){
0045       return true;
0046    }
0047    else{
0048       spin_wait swait;
0049       while(microsec_clock<TimePoint>::universal_time() < abs_time){
0050          if(m.try_lock()){
0051             return true;
0052          }
0053          swait.yield();
0054       }
0055       return false;
0056    }
0057 }
0058 
0059 template<class MutexType>
0060 void try_based_lock(MutexType &m)
0061 {
0062    if(!m.try_lock()){
0063       spin_wait swait;
0064       do{
0065          if(m.try_lock()){
0066             break;
0067          }
0068          else{
0069             swait.yield();
0070          }
0071       }
0072       while(1);
0073    }
0074 }
0075 
0076 template<class MutexType>
0077 void timeout_when_locking_aware_lock(MutexType &m)
0078 {
0079    #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
0080       if (!m.timed_lock(microsec_clock<ustime>::universal_time()
0081            + usduration_milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS)))
0082       {
0083          throw interprocess_exception(timeout_when_locking_error
0084                                      , "Interprocess mutex timeout when locking. Possible deadlock: "
0085                                        "owner died without unlocking?");
0086       }
0087    #else
0088       m.lock();
0089    #endif
0090 }
0091 
0092 }  //namespace ipcdetail
0093 }  //namespace interprocess
0094 }  //namespace boost
0095 
0096 #include <boost/interprocess/detail/config_end.hpp>
0097 
0098 #endif   //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP