Lely core libraries  2.2.5
exec.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_EV_EXEC_HPP_
25 #define LELY_EV_EXEC_HPP_
26 
27 #include <lely/ev/exec.h>
28 #include <lely/ev/task.hpp>
29 
30 #include <cstddef>
31 #include <type_traits>
32 #include <utility>
33 
34 namespace lely {
35 namespace ev {
36 
38 class Executor {
39  public:
40  Executor(ev_exec_t* exec) noexcept : exec_(exec) {}
41 
42  operator ev_exec_t*() const noexcept { return exec_; }
43 
45  void
46  on_task_init() noexcept {
47  ev_exec_on_task_init(*this);
48  }
49 
51  void
52  on_task_fini() noexcept {
53  ev_exec_on_task_fini(*this);
54  }
55 
57  bool
58  dispatch(ev_task& task) noexcept {
59  return ev_exec_dispatch(*this, &task) != 0;
60  }
61 
63  template <class F, class... Args>
64  typename ::std::enable_if<
65  !::std::is_base_of<ev_task, typename ::std::decay<F>::type>::value,
66  bool>::type
67  dispatch(F&& f, Args&&... args) {
68  auto task = make_task_wrapper(*this, ::std::forward<F>(f),
69  ::std::forward<Args>(args)...);
70  dispatch(*task);
71  }
72 
74  void
75  post(ev_task& task) noexcept {
76  ev_exec_post(*this, &task);
77  }
78 
80  template <class F, class... Args>
81  typename ::std::enable_if<
82  !::std::is_base_of<ev_task, typename ::std::decay<F>::type>::value>::type
83  post(F&& f, Args&&... args) {
84  auto task = make_task_wrapper(*this, ::std::forward<F>(f),
85  ::std::forward<Args>(args)...);
86  post(*task);
87  }
88 
90  void
91  defer(ev_task& task) noexcept {
92  ev_exec_defer(*this, &task);
93  }
94 
96  template <class F, class... Args>
97  typename ::std::enable_if<
98  !::std::is_base_of<ev_task, typename ::std::decay<F>::type>::value>::type
99  defer(F&& f, Args&&... args) {
100  auto task = make_task_wrapper(*this, ::std::forward<F>(f),
101  ::std::forward<Args>(args)...);
102  defer(*task);
103  }
104 
106  bool
107  abort(ev_task& task) noexcept {
108  return ev_exec_abort(*this, &task) != 0;
109  }
110 
112  ::std::size_t
113  abort_all() noexcept {
114  return ev_exec_abort(*this, nullptr);
115  }
116 
118  void
119  run(ev_task& task) noexcept {
120  ev_exec_run(*this, &task);
121  }
122 
123  protected:
124  ev_exec_t* exec_{nullptr};
125 };
126 
127 inline Executor
128 Task::get_executor() const noexcept {
129  return Executor(exec);
130 }
131 
132 } // namespace ev
133 } // namespace lely
134 
135 #endif // !LELY_EV_EXEC_HPP_
lely::ev::Executor::post
void post(ev_task &task) noexcept
Definition: exec.hpp:75
lely::ev::Executor::on_task_init
void on_task_init() noexcept
Definition: exec.hpp:46
task.hpp
lely::ev::Executor::post
typename ::std::enable_if< !::std::is_base_of< ev_task, typename ::std::decay< F >::type >::value >::type post(F &&f, Args &&... args)
Definition: exec.hpp:83
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
lely::ev::Executor::dispatch
typename ::std::enable_if< !::std::is_base_of< ev_task, typename ::std::decay< F >::type >::value, bool >::type dispatch(F &&f, Args &&... args)
Definition: exec.hpp:67
lely::ev::make_task_wrapper
TaskWrapper< F, Args... > * make_task_wrapper(ev_exec_t *exec, F &&f, Args &&... args)
Creates a temporary task from a callable object with an associated executor (can be nullptr).
Definition: task.hpp:77
lely::ev::Executor::dispatch
bool dispatch(ev_task &task) noexcept
Definition: exec.hpp:58
exec.h
lely::ev::Executor::defer
typename ::std::enable_if< !::std::is_base_of< ev_task, typename ::std::decay< F >::type >::value >::type defer(F &&f, Args &&... args)
Definition: exec.hpp:99
lely::ev::Task::get_executor
Executor get_executor() const noexcept
Returns the executor to which the task is (to be) submitted.
Definition: exec.hpp:128
ev_exec_dispatch
int ev_exec_dispatch(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:120
ev_exec_run
void ev_exec_run(ev_exec_t *exec, struct ev_task *task)
Invokes the task function in *task as if the task is being executed by *exec.
Definition: exec.h:144
lely::ev::Executor::on_task_fini
void on_task_fini() noexcept
Definition: exec.hpp:52
lely::ev::Executor
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
ev_exec_on_task_fini
void ev_exec_on_task_fini(ev_exec_t *exec)
Undoes the effect of a previous call to ev_exec_on_task_init().
Definition: exec.h:114
lely::ev::Executor::abort_all
::std::size_t abort_all() noexcept
Definition: exec.hpp:113
ev_exec_abort
size_t ev_exec_abort(ev_exec_t *exec, struct ev_task *task)
Aborts the specified task submitted to *exec, if it has not yet begun executing, or all pending tasks...
Definition: exec.h:138
lely::ev::Executor::defer
void defer(ev_task &task) noexcept
Definition: exec.hpp:91
ev_exec_defer
void ev_exec_defer(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:132
ev_task
An executable task.
Definition: task.h:41
ev_exec_on_task_init
void ev_exec_on_task_init(ev_exec_t *exec)
Indicates to the specified executor that a task will be submitted for execution in the future.
Definition: exec.h:108
ev_exec_post
void ev_exec_post(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:126
lely::ev::Executor::abort
bool abort(ev_task &task) noexcept
Definition: exec.hpp:107
lely::ev::Executor::run
void run(ev_task &task) noexcept
Definition: exec.hpp:119
ev_task::exec
ev_exec_t * exec
A pointer to the executor to which the task is (to be) submitted.
Definition: task.h:43