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  // We need now() + duration < Duration::max() to avoid overflow.
38  // Therefore, we check for now() < duration::max() - duration.
39  if (Clock::now().duration() < Duration::max() - duration) {
40  return Timeout(Clock::now() + duration);
41  }
42 
43  return Timeout(Time::max());
44  }
45 
46  Timeout& operator=(const Timeout& that)
47  {
48  if (this != &that) {
49  timeout = that.timeout;
50  }
51 
52  return *this;
53  }
54 
55  Timeout& operator=(const Duration& duration)
56  {
57  timeout = Clock::now() + duration;
58  return *this;
59  }
60 
61  bool operator==(const Timeout& that) const
62  {
63  return timeout == that.timeout;
64  }
65 
66  bool operator<(const Timeout& that) const
67  {
68  return timeout < that.timeout;
69  }
70 
71  bool operator<=(const Timeout& that) const
72  {
73  return timeout <= that.timeout;
74  }
75 
76  // Returns the value of the timeout as a Time object.
77  Time time() const
78  {
79  return timeout;
80  }
81 
82  // Returns the amount of time remaining.
84  {
85  Duration remaining = timeout - Clock::now();
86  return remaining > Duration::zero() ? remaining : Duration::zero();
87  }
88 
89  // Returns true if the timeout expired.
90  bool expired() const
91  {
92  return timeout <= Clock::now();
93  }
94 
95 private:
96  Time timeout;
97 };
98 
99 } // namespace process {
100 
101 #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:55
bool operator==(const Timeout &that) const
Definition: timeout.hpp:61
Definition: duration.hpp:32
bool operator<(const Timeout &that) const
Definition: timeout.hpp:66
Time time() const
Definition: timeout.hpp:77
bool operator<=(const Timeout &that) const
Definition: timeout.hpp:71
Timeout(const Timeout &that)
Definition: timeout.hpp:31
Timeout & operator=(const Timeout &that)
Definition: timeout.hpp:46
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...
Timeout(const Time &time)
Definition: timeout.hpp:29
static constexpr Duration max()
Definition: duration.hpp:429
bool expired() const
Definition: timeout.hpp:90
Duration remaining() const
Definition: timeout.hpp:83