Lely core libraries 2.3.4
fiber_driver.hpp
Go to the documentation of this file.
1
23#ifndef LELY_COAPP_FIBER_DRIVER_HPP_
24#define LELY_COAPP_FIBER_DRIVER_HPP_
25
26#include <lely/coapp/driver.hpp>
28#include <lely/ev/strand.hpp>
29
30#include <utility>
31
32namespace lely {
33
34namespace canopen {
35
36namespace detail {
37
40 protected:
42
43 ev::FiberThread thrd;
45 ev::Strand strand;
46};
47
48} // namespace detail
49
55 public:
67 explicit FiberDriver(ev_exec_t* exec, AsyncMaster& master, uint8_t id);
68
70 explicit FiberDriver(AsyncMaster& master, uint8_t id)
71 : FiberDriver(nullptr, master, id) {}
72
75 GetStrand() const noexcept {
76 return strand;
77 }
78
85 template <class F, class... Args>
86 void
87 Defer(F&& f, Args&&... args) {
88 GetStrand().post(::std::forward<F>(f), ::std::forward<Args>(args)...);
89 }
90
104 template <class T>
105 T
107 fiber_await(f);
108 try {
109 return f.get().value();
110 } catch (const ev::future_not_ready& e) {
111 util::throw_error_code("Wait", ::std::errc::operation_canceled);
112 }
113 }
114
127 template <class T>
128 typename ::std::enable_if<!::std::is_void<T>::value, T>::type
129 Wait(SdoFuture<T> f, ::std::error_code& ec) {
130 fiber_await(f);
131 try {
132 return f.get().value();
133 } catch (const ::std::system_error& e) {
134 ec = e.code();
135 } catch (const ev::future_not_ready& e) {
136 ec = ::std::make_error_code(::std::errc::operation_canceled);
137 }
138 }
139
149 void Wait(SdoFuture<void> f, ::std::error_code& ec);
150
155 void USleep(uint_least64_t usec);
156
161 void USleep(uint_least64_t usec, ::std::error_code& ec);
162};
163
164} // namespace canopen
165
166} // namespace lely
167
168#endif // LELY_COAPP_FIBER_DRIVER_HPP_
An asynchronous CANopen master.
Definition: master.hpp:1957
The base class for drivers for remote CANopen nodes.
Definition: driver.hpp:279
uint8_t id() const noexcept final
Returns the node-ID.
Definition: driver.hpp:321
BasicMaster & master
A reference to the master with which this driver is registered.
Definition: driver.hpp:1097
A CANopen driver running its tasks and callbacks in fibers.
void Defer(F &&f, Args &&... args)
Schedules the specified Callable object for execution by strand for this driver.
FiberDriver(AsyncMaster &master, uint8_t id)
Creates a new CANopen driver and its associated fiber executor.
T Wait(SdoFuture< T > f)
Waits for the specified future to become ready by suspending the calling fiber.
FiberDriver(ev_exec_t *exec, AsyncMaster &master, uint8_t id)
Creates a new CANopen driver and its associated fiber executor.
typename::std::enable_if<!::std::is_void< T >::value, T >::type Wait(SdoFuture< T > f, ::std::error_code &ec)
Waits for the specified future to become ready by suspending the calling fiber.
void USleep(uint_least64_t usec)
Suspends the calling fiber for usec microseconds.
ev::Executor GetStrand() const noexcept
Returns the strand executor associated with the driver.
A base class for lely::canopen::FiberDriver, containing a fiber executor.
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
void post(ev_task &task) noexcept
Definition: exec.hpp:75
A fiber executor.
Definition: fiber_exec.hpp:115
Convenience class providing a RAII-style mechanism to ensure the calling thread can be used by fiber ...
Definition: fiber_exec.hpp:44
A future.
Definition: future.hpp:384
A strand executor.
Definition: strand.hpp:37
The exception thrown when retrieving the result of a future which is not ready or does not contain a ...
Definition: future.hpp:45
This header file is part of the C++ CANopen application library; it contains the remote node driver i...
This header file is part of the event library; it contains the C++ interface for the fiber executor,...
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
This header file is part of the event library; it contains the C++ interface for the strand executor.