Apache Mesos
timeout.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_TIMEOUT_HPP__
14 #define __PROCESS_TIMEOUT_HPP__
15 
16 #include <process/clock.hpp>
17 #include <process/time.hpp>
18 
19 #include <stout/duration.hpp>
20 
21 
22 namespace process {
23 
24 class Timeout
25 {
26 public:
27  Timeout() : timeout(Clock::now()) {}
28 
29  explicit Timeout(const Time& time) : timeout(time) {}
30 
31  Timeout(const Timeout& that) : timeout(that.timeout) {}
32 
33  // Constructs a Timeout instance from a Time that is the 'duration'
34  // from now.
35  static Timeout in(const Duration& duration)
36  {
37  Time now = Clock::now();
38  if (duration < Duration::zero()) {
39  // If duration is negative, we need now() + duration > Duration::min() to avoid overflow.
40  // Therefore, we check for now() > duration::min() - duration.
41  if (now.duration() > Duration::min() - duration) {
42  return Timeout(now + duration);
43  }
44  return Timeout(Time::epoch());
45  }
46 
47  // We need now() + duration < Duration::max() to avoid overflow.
48  // Therefore, we check for now() < duration::max() - duration.
49  if (now.duration() < Duration::max() - duration) {
50  return Timeout(now + duration);
51  }
52 
53  return Timeout(Time::max());
54  }
55 
56  Timeout& operator=(const Timeout& that)
57  {
58  if (this != &that) {
59  timeout = that.timeout;
60  }
61 
62  return *this;
63  }
64 
65  Timeout& operator=(const Duration& duration)
66  {
67  timeout = Clock::now() + duration;
68  return *this;
69  }
70 
71  bool operator==(const Timeout& that) const
72  {
73  return timeout == that.timeout;
74  }
75 
76  bool operator<(const Timeout& that) const
77  {
78  return timeout < that.timeout;
79  }
80 
81  bool operator<=(const Timeout& that) const
82  {
83  return timeout <= that.timeout;
84  }
85 
86  // Returns the value of the timeout as a Time object.
87  Time time() const
88  {
89  return timeout;
90  }
91 
92  // Returns the amount of time remaining.
94  {
95  Duration remaining = timeout - Clock::now();
96  return remaining > Duration::zero() ? remaining : Duration::zero();
97  }
98 
99  // Returns true if the timeout expired.
100  bool expired() const
101  {
102  return timeout <= Clock::now();
103  }
104 
105 private:
106  Time timeout;
107 };
108 
109 } // namespace process {
110 
111 #endif // __PROCESS_TIMEOUT_HPP__
static Timeout in(const Duration &duration)
Definition: timeout.hpp:35
Provides timers.
Definition: clock.hpp:37
Timeout & operator=(const Duration &duration)
Definition: timeout.hpp:65
bool operator==(const Timeout &that) const
Definition: timeout.hpp:71
Definition: duration.hpp:32
bool operator<(const Timeout &that) const
Definition: timeout.hpp:76
Time time() const
Definition: timeout.hpp:87
bool operator<=(const Timeout &that) const
Definition: timeout.hpp:81
Timeout(const Timeout &that)
Definition: timeout.hpp:31
Timeout & operator=(const Timeout &that)
Definition: timeout.hpp:56
Definition: time.hpp:23
Timeout()
Definition: timeout.hpp:27
static constexpr Duration zero()
Definition: duration.hpp:136
Definition: timeout.hpp:24
Definition: executor.hpp:48
static Time max()
Definition: time.hpp:88
static Time now()
The current clock time for either the current process that makes this call or the global clock time i...
Duration duration() const
Definition: time.hpp:36
Timeout(const Time &time)
Definition: timeout.hpp:29
static constexpr Duration min()
Definition: duration.hpp:435
static constexpr Duration max()
Definition: duration.hpp:429
bool expired() const
Definition: timeout.hpp:100
Duration remaining() const
Definition: timeout.hpp:93
static Time epoch()
Definition: time.hpp:87