Chris@102: ////////////////////////////////////////////////////////////////////////////// Chris@102: // Chris@102: // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost Chris@102: // Software License, Version 1.0. (See accompanying file Chris@102: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // See http://www.boost.org/libs/interprocess for documentation. Chris@102: // Chris@102: ////////////////////////////////////////////////////////////////////////////// Chris@102: Chris@102: #ifndef BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP Chris@102: #define BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP Chris@102: Chris@102: #ifndef BOOST_CONFIG_HPP Chris@102: # include Chris@102: #endif Chris@102: # Chris@102: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: Chris@102: #include Chris@102: Chris@102: namespace boost { Chris@102: namespace interprocess { Chris@102: namespace ipcdetail { Chris@102: Chris@102: template Chris@102: bool try_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time) Chris@102: { Chris@102: //Same as lock() Chris@102: if(abs_time == boost::posix_time::pos_infin){ Chris@102: m.lock(); Chris@102: return true; Chris@102: } Chris@102: //Always try to lock to achieve POSIX guarantees: Chris@102: // "Under no circumstance shall the function fail with a timeout if the mutex Chris@102: // can be locked immediately. The validity of the abs_timeout parameter need not Chris@102: // be checked if the mutex can be locked immediately." Chris@102: else if(m.try_lock()){ Chris@102: return true; Chris@102: } Chris@102: else{ Chris@102: spin_wait swait; Chris@102: while(microsec_clock::universal_time() < abs_time){ Chris@102: if(m.try_lock()){ Chris@102: return true; Chris@102: } Chris@102: swait.yield(); Chris@102: } Chris@102: return false; Chris@102: } Chris@102: } Chris@102: Chris@102: template Chris@102: void try_based_lock(MutexType &m) Chris@102: { Chris@102: if(!m.try_lock()){ Chris@102: spin_wait swait; Chris@102: do{ Chris@102: if(m.try_lock()){ Chris@102: break; Chris@102: } Chris@102: else{ Chris@102: swait.yield(); Chris@102: } Chris@102: } Chris@102: while(1); Chris@102: } Chris@102: } Chris@102: Chris@102: } //namespace ipcdetail Chris@102: } //namespace interprocess Chris@102: } //namespace boost Chris@102: Chris@102: #include Chris@102: Chris@102: #endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP