Apache Mesos
clock.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_CLOCK_HPP__
14 #define __PROCESS_CLOCK_HPP__
15 
16 #include <list>
17 
18 #include <process/time.hpp>
19 
20 // TODO(benh): We should really be including <process/timer.hpp> but
21 // there are currently too many circular dependencies in header files
22 // that would need to get moved to translation units first.
23 
24 #include <stout/duration.hpp>
25 #include <stout/lambda.hpp>
26 
27 namespace process {
28 
29 // Forward declarations (to avoid circular dependencies).
30 class ProcessBase;
31 class Time;
32 class Timer;
33 
37 class Clock
38 {
39 public:
44  // TODO(benh): Introduce a "channel" or listener pattern for getting
45  // the expired Timers rather than passing in a callback. This might
46  // mean we don't need 'initialize' or 'shutdown'.
47  static void initialize(
48  lambda::function<void(const std::list<Timer>&)>&& callback);
49 
59  static void finalize();
60 
68  static Time now();
69 
70  static Time now(ProcessBase* process);
71 
72  static Timer timer(
73  const Duration& duration,
74  const lambda::function<void()>& thunk);
75 
76  static bool cancel(const Timer& timer);
77 
81  static void pause();
82 
86  static bool paused();
87 
88  static void resume();
89 
90  static void advance(const Duration& duration);
91  static void advance(ProcessBase* process, const Duration& duration);
92 
93  static void update(const Time& time);
94 
95  // When updating the time of a particular process you can specify
96  // whether or not you want to override the existing value even if
97  // you're going backwards in time! SAFE means don't update the
98  // previous Clock for a process if going backwards in time, where as
99  // FORCE forces this change.
100  enum Update
101  {
104  };
105 
106  static void update(
107  ProcessBase* process,
108  const Time& time,
109  Update update = SAFE);
110 
111  static void order(ProcessBase* from, ProcessBase* to);
112 
113  // When the clock is paused this returns only after
114  // (1) all expired timers are executed,
115  // (2) no processes are running, and
116  // (3) no processes are ready to run.
117  //
118  // In other words, this function blocks synchronously until no other
119  // execution on any processes will occur unless the clock is
120  // advanced.
121  //
122  // TODO(benh): Move this function elsewhere, for instance, to a
123  // top-level function in the 'process' namespace since it deals with
124  // both processes and the clock.
125  static void settle();
126 
127  // When the clock is paused this returns true if all timers that
128  // expire before the paused time have executed, otherwise false.
129  // Note that if the clock continually gets advanced concurrently
130  // this function may never return true because the "paused" time
131  // will continue to get pushed out farther in the future making more
132  // timers candidates for execution.
133  static bool settled();
134 };
135 
136 } // namespace process {
137 
138 #endif // __PROCESS_CLOCK_HPP__
Provides timers.
Definition: clock.hpp:37
static bool cancel(const Timer &timer)
static bool settled()
static void order(ProcessBase *from, ProcessBase *to)
Definition: process.hpp:72
static void update(const Time &time)
Definition: duration.hpp:32
Definition: clock.hpp:102
Definition: clock.hpp:103
static void advance(const Duration &duration)
Definition: time.hpp:23
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Update
Definition: clock.hpp:100
static void settle()
Definition: timer.hpp:30
static Time now()
The current clock time for either the current process that makes this call or the global clock time i...
static Timer timer(const Duration &duration, const lambda::function< void()> &thunk)
static void pause()
Pauses the clock e.g.
static void initialize(lambda::function< void(const std::list< Timer > &)> &&callback)
Initialize the clock with the specified callback that will be invoked whenever a batch of timers has ...
static void resume()
static bool paused()
Check whether clock is currently running.
static void finalize()
Clears all timers without executing them.
Try< std::vector< Value > > time(const std::string &hierarchy, const std::string &cgroup)