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