]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/interprocess/sync/detail/common_algorithms.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / interprocess / sync / detail / common_algorithms.hpp
CommitLineData
7c673cae
FG
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
27namespace boost {
28namespace interprocess {
29namespace ipcdetail {
30
31template<class MutexType>
32bool 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
58template<class MutexType>
59void 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
11fdf7f2
TL
75template<class MutexType>
76void timed_based_lock(MutexType &m, unsigned const uCheckPeriodSec)
77{
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)){
81 spin_wait swait;
82 do{
83 deadline = microsec_clock::universal_time()+dur;
84 if(m.timed_lock(deadline)){
85 break;
86 }
87 else{
88 swait.yield();
89 }
90 }
91 while(1);
92 }
93}
94
95template<class MutexType>
96void timed_based_timed_lock(MutexType &m, const boost::posix_time::ptime &abs_time, unsigned const uCheckPeriodSec)
97{
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);
102 }
103 else if(!m.timed_lock(deadline)){
104 spin_wait swait;
105 do{
106 deadline = microsec_clock::universal_time()+dur;
107 if(m.timed_lock(deadline)){
108 break;
109 }
110 else{
111 swait.yield();
112 }
113 }
114 while(1);
115 }
116}
117
7c673cae
FG
118} //namespace ipcdetail
119} //namespace interprocess
120} //namespace boost
121
122#include <boost/interprocess/detail/config_end.hpp>
123
124#endif //BOOST_INTERPROCESS_SYNC_DETAIL_COMMON_ALGORITHMS_HPP