Apache Mesos
timer.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_TIMER_HPP__
14 #define __PROCESS_TIMER_HPP__
15 
16 #include <stdint.h>
17 #include <stdlib.h> // For abort.
18 
19 #include <process/pid.hpp>
20 #include <process/timeout.hpp>
21 
22 #include <stout/duration.hpp>
23 #include <stout/lambda.hpp>
24 
25 namespace process {
26 
27 // Timer represents a delayed thunk, that can get created (scheduled)
28 // and canceled using the Clock.
29 
30 class Timer
31 {
32 public:
33  Timer() : id(0), pid(process::UPID()), thunk(&abort) {}
34 
35  bool operator==(const Timer& that) const
36  {
37  return id == that.id;
38  }
39 
40  // Invokes this timer's thunk.
41  void operator()() const
42  {
43  thunk();
44  }
45 
46  // Returns the timeout associated with this timer.
47  Timeout timeout() const
48  {
49  return t;
50  }
51 
52  // Returns the PID of the running process when this timer was
53  // created (via timers::create) or an empty PID if no process was
54  // running when this timer was created.
56  {
57  return pid;
58  }
59 
60 private:
61  friend class Clock;
62 
63  Timer(uint64_t _id,
64  const Timeout& _t,
65  const process::UPID& _pid,
66  const lambda::function<void()>& _thunk)
67  : id(_id), t(_t), pid(_pid), thunk(_thunk)
68  {}
69 
70  uint64_t id; // Used for equality.
71 
72  Timeout t;
73 
74  // We store the PID of the "issuing" (i.e., "running") process (if
75  // there is one). We don't store a pointer to the process because we
76  // can't dereference it since it might no longer be valid. (Instead,
77  // the PID can be used internally to check if the process is still
78  // valid and get a reference to it.)
79  process::UPID pid;
80 
81  lambda::function<void()> thunk;
82 };
83 
84 } // namespace process {
85 
86 #endif // __PROCESS_TIMER_HPP__
bool operator==(const Timer &that) const
Definition: timer.hpp:35
Provides timers.
Definition: clock.hpp:37
Definition: uuid.hpp:33
Timeout timeout() const
Definition: timer.hpp:47
void operator()() const
Definition: timer.hpp:41
An "untyped" PID, used to encapsulate the process ID for lower-layer abstractions (eg...
Definition: pid.hpp:39
Definition: timeout.hpp:24
Definition: timer.hpp:30
Definition: executor.hpp:48
process::UPID creator() const
Definition: timer.hpp:55
Timer()
Definition: timer.hpp:33