Lely core libraries 2.3.4
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
34namespace lely {
35namespace ev {
36
38class 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 {
48 }
49
51 void
52 on_task_fini() noexcept {
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
127inline Executor
128Task::get_executor() const noexcept {
129 return Executor(exec);
130}
131
132} // namespace ev
133} // namespace lely
134
135#endif // !LELY_EV_EXEC_HPP_
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
void on_task_fini() noexcept
Definition: exec.hpp:52
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
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
void on_task_init() noexcept
Definition: exec.hpp:46
void defer(ev_task &task) noexcept
Definition: exec.hpp:91
bool abort(ev_task &task) noexcept
Definition: exec.hpp:107
void run(ev_task &task) noexcept
Definition: exec.hpp:119
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
::std::size_t abort_all() noexcept
Definition: exec.hpp:113
void post(ev_task &task) noexcept
Definition: exec.hpp:75
bool dispatch(ev_task &task) noexcept
Definition: exec.hpp:58
Executor get_executor() const noexcept
Returns the executor to which the task is (to be) submitted.
Definition: exec.hpp:128
This header file is part of the event library; it contains the abstract task executor interface.
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:136
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:142
void ev_exec_post(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:124
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:112
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:106
void ev_exec_defer(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:130
int ev_exec_dispatch(ev_exec_t *exec, struct ev_task *task)
Submits *task to *exec for execution.
Definition: exec.h:118
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
An executable task.
Definition: task.h:41
ev_exec_t * exec
A pointer to the executor to which the task is (to be) submitted.
Definition: task.h:43
This header file is part of the event library; it contains the basic C++ task interface.
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