Apache Mesos
run.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_RUN_HPP__
14 #define __PROCESS_RUN_HPP__
15 
16 #include <memory> // TODO(benh): Replace shared_ptr with unique_ptr.
17 
18 #include <process/id.hpp>
19 #include <process/process.hpp>
20 
21 #include <stout/lambda.hpp>
22 #include <stout/preprocessor.hpp>
23 
24 namespace process {
25 
26 namespace internal {
27 
28 template <typename R>
29 class ThunkProcess : public Process<ThunkProcess<R>>
30 {
31 public:
32  ThunkProcess(std::shared_ptr<lambda::function<R()>> _thunk,
33  std::shared_ptr<Promise<R>> _promise)
34  : ProcessBase(ID::generate("__thunk__")),
35  thunk(_thunk),
36  promise(_promise) {}
37 
38  ~ThunkProcess() override {}
39 
40 protected:
41  void serve(Event&& event) override
42  {
43  promise->set((*thunk)());
44  }
45 
46 private:
47  std::shared_ptr<lambda::function<R()>> thunk;
48  std::shared_ptr<Promise<R>> promise;
49 };
50 
51 } // namespace internal {
52 
53 
54 template <typename R>
55 Future<R> run(R (*method)())
56 {
57  std::shared_ptr<lambda::function<R()>> thunk(
58  new lambda::function<R()>(
59  lambda::bind(method)));
60 
61  std::shared_ptr<Promise<R>> promise(new Promise<R>());
62  Future<R> future = promise->future();
63 
64  terminate(spawn(new internal::ThunkProcess<R>(thunk, promise), true));
65 
66  return future;
67 }
68 
69 
70 #define TEMPLATE(Z, N, DATA) \
71  template <typename R, \
72  ENUM_PARAMS(N, typename P), \
73  ENUM_PARAMS(N, typename A)> \
74  Future<R> run( \
75  R (*method)(ENUM_PARAMS(N, P)), \
76  ENUM_BINARY_PARAMS(N, A, a)) \
77  { \
78  std::shared_ptr<lambda::function<R()>> thunk( \
79  new lambda::function<R()>( \
80  lambda::bind(method, ENUM_PARAMS(N, a)))); \
81  \
82  std::shared_ptr<Promise<R>> promise(new Promise<R>()); \
83  Future<R> future = promise->future(); \
84  \
85  terminate(spawn(new internal::ThunkProcess<R>(thunk, promise), true)); \
86  \
87  return future; \
88  }
89 
90  REPEAT_FROM_TO(1, 13, TEMPLATE, _) // Args A0 -> A11.
91 #undef TEMPLATE
92 
93 } // namespace process {
94 
95 #endif // __PROCESS_RUN_HPP__
std::string generate(const std::string &prefix="")
Returns &#39;prefix(N)&#39; where N represents the number of instances where the same prefix (wrt...
REPEAT_FROM_TO(1, 13, TEMPLATE, _) class AsyncExecutorProcess
Definition: async.hpp:63
Definition: process.hpp:72
#define TEMPLATE(Z, N, DATA)
Definition: run.hpp:70
void serve(Event &&event) override
Invoked when an event is serviced.
Definition: run.hpp:41
UPID spawn(ProcessBase *process, bool manage=false)
Spawn a new process.
void terminate(const UPID &pid, bool inject=true)
Sends a TerminateEvent to the given process.
Definition: run.hpp:29
void run(std::vector< C > &&callbacks, Arguments &&...arguments)
Definition: future.hpp:621
Definition: attributes.hpp:24
Definition: executor.hpp:48
~ThunkProcess() override
Definition: run.hpp:38
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
ThunkProcess(std::shared_ptr< lambda::function< R()>> _thunk, std::shared_ptr< Promise< R >> _promise)
Definition: run.hpp:32
Definition: process.hpp:505
Definition: event.hpp:60