Apache Mesos
delay.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_DELAY_HPP__
14 #define __PROCESS_DELAY_HPP__
15 
16 #include <process/clock.hpp>
17 #include <process/dispatch.hpp>
18 #include <process/timer.hpp>
19 
20 #include <stout/duration.hpp>
21 #include <stout/preprocessor.hpp>
22 
23 namespace process {
24 
25 // The 'delay' mechanism enables you to delay a dispatch to a process
26 // for some specified number of seconds. Returns a Timer instance that
27 // can be cancelled (but it might have already executed or be
28 // executing concurrently).
29 
30 template <typename T>
31 Timer delay(const Duration& duration,
32  const PID<T>& pid,
33  void (T::*method)())
34 {
35  return Clock::timer(duration, [=]() {
36  dispatch(pid, method);
37  });
38 }
39 
40 
41 template <typename T>
42 Timer delay(const Duration& duration,
43  const Process<T>& process,
44  void (T::*method)())
45 {
46  return delay(duration, process.self(), method);
47 }
48 
49 
50 template <typename T>
51 Timer delay(const Duration& duration,
52  const Process<T>* process,
53  void (T::*method)())
54 {
55  return delay(duration, process->self(), method);
56 }
57 
58 
59 #define TEMPLATE(Z, N, DATA) \
60  template <typename T, \
61  ENUM_PARAMS(N, typename P), \
62  ENUM_PARAMS(N, typename A)> \
63  Timer delay(const Duration& duration, \
64  const PID<T>& pid, \
65  void (T::*method)(ENUM_PARAMS(N, P)), \
66  ENUM_BINARY_PARAMS(N, A, a)) \
67  { \
68  return Clock::timer(duration, [=]() { \
69  dispatch(pid, method, ENUM_PARAMS(N, a)); \
70  }); \
71  } \
72  \
73  template <typename T, \
74  ENUM_PARAMS(N, typename P), \
75  ENUM_PARAMS(N, typename A)> \
76  Timer delay(const Duration& duration, \
77  const Process<T>& process, \
78  void (T::*method)(ENUM_PARAMS(N, P)), \
79  ENUM_BINARY_PARAMS(N, A, a)) \
80  { \
81  return delay(duration, process.self(), method, ENUM_PARAMS(N, a)); \
82  } \
83  \
84  template <typename T, \
85  ENUM_PARAMS(N, typename P), \
86  ENUM_PARAMS(N, typename A)> \
87  Timer delay(const Duration& duration, \
88  const Process<T>* process, \
89  void (T::*method)(ENUM_PARAMS(N, P)), \
90  ENUM_BINARY_PARAMS(N, A, a)) \
91  { \
92  return delay(duration, process->self(), method, ENUM_PARAMS(N, a)); \
93  }
94 
95  REPEAT_FROM_TO(1, 13, TEMPLATE, _) // Args A0 -> A11.
96 #undef TEMPLATE
97 
98 } // namespace process {
99 
100 #endif // __PROCESS_DELAY_HPP__
REPEAT_FROM_TO(1, 13, TEMPLATE, _) class AsyncExecutorProcess
Definition: async.hpp:63
Definition: duration.hpp:32
void dispatch(const PID< T > &pid, void(T::*method)())
Definition: dispatch.hpp:174
Timer delay(const Duration &duration, const PID< T > &pid, void(T::*method)())
Definition: delay.hpp:31
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:289
#define TEMPLATE(Z, N, DATA)
Definition: delay.hpp:59
Definition: timer.hpp:30
Definition: executor.hpp:48
PID< T > self() const
Returns the PID of the process.
Definition: process.hpp:514
static Timer timer(const Duration &duration, const lambda::function< void()> &thunk)
Definition: process.hpp:505