Lely core libraries  2.2.5
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>
27 #include <lely/ev/fiber_exec.hpp>
28 #include <lely/ev/strand.hpp>
29 
30 #include <utility>
31 
32 namespace lely {
33 
34 namespace canopen {
35 
36 namespace detail {
37 
40  protected:
42 
43  ev::FiberThread thrd;
44  ev::FiberExecutor exec;
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  return f.get().value();
109  }
110 
123  template <class T>
124  typename ::std::enable_if<!::std::is_void<T>::value, T>::type
125  Wait(SdoFuture<T> f, ::std::error_code& ec) {
126  fiber_await(f);
127  try {
128  return f.get().value();
129  } catch (const ::std::system_error& e) {
130  ec = e.code();
131  } catch (const ev::future_not_ready& e) {
132  ec = ::std::make_error_code(::std::errc::operation_canceled);
133  }
134  }
135 
145  void Wait(SdoFuture<void> f, ::std::error_code& ec);
146 
151  void USleep(uint_least64_t usec);
152 
157  void USleep(uint_least64_t usec, ::std::error_code& ec);
158 };
159 
160 } // namespace canopen
161 
162 } // namespace lely
163 
164 #endif // LELY_COAPP_FIBER_DRIVER_HPP_
lely::ev::Executor::post
void post(ev_task &task) noexcept
Definition: exec.hpp:75
lely::ev::FiberThread
Convenience class providing a RAII-style mechanism to ensure the calling thread can be used by fiber ...
Definition: fiber_exec.hpp:44
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
lely::canopen::BasicDriver::master
BasicMaster & master
A reference to the master with which this driver is registered.
Definition: driver.hpp:671
lely::canopen::BasicDriver
The base class for drivers for remote CANopen nodes.
Definition: driver.hpp:278
lely::canopen::AsyncMaster
An asynchronous CANopen master.
Definition: master.hpp:1296
lely::ev::Strand
A strand executor.
Definition: strand.hpp:37
lely::canopen::FiberDriver
A CANopen driver running its tasks and callbacks in fibers.
Definition: fiber_driver.hpp:54
lely::canopen::FiberDriver::USleep
void USleep(uint_least64_t usec)
Suspends the calling fiber for usec microseconds.
Definition: fiber_driver.cpp:67
lely::canopen::detail::FiberDriverBase
A base class for lely::canopen::FiberDriver, containing a fiber executor.
Definition: fiber_driver.hpp:39
lely::canopen::FiberDriver::FiberDriver
FiberDriver(AsyncMaster &master, uint8_t id)
Creates a new CANopen driver and its associated fiber executor.
Definition: fiber_driver.hpp:70
lely::canopen::FiberDriver::Wait
T Wait(SdoFuture< T > f)
Waits for the specified future to become ready by suspending the calling fiber.
Definition: fiber_driver.hpp:106
lely::ev::Executor
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
lely::ev::Future
A future.
Definition: future.hpp:50
lely::ev::Future::get
result_type & get()
Returns the result of a ready future.
Definition: future.hpp:490
lely::canopen::FiberDriver::Defer
void Defer(F &&f, Args &&... args)
Schedules the specified Callable object for execution by strand for this driver.
Definition: fiber_driver.hpp:87
driver.hpp
strand.hpp
fiber_exec.hpp
lely::ev::future_not_ready
The exception thrown when retrieving the result of a future which is not ready or does not contain a ...
Definition: future.hpp:45
lely::canopen::BasicDriver::id
uint8_t id() const noexcept final
Returns the node-ID.
Definition: driver.hpp:321
lely::canopen::FiberDriver::Wait
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.
Definition: fiber_driver.hpp:125
lely::ev::FiberExecutor
A fiber executor.
Definition: fiber_exec.hpp:115
lely::canopen::FiberDriver::GetStrand
ev::Executor GetStrand() const noexcept
Returns the strand executor associated with the driver.
Definition: fiber_driver.hpp:75
lely::canopen::make_error_code
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
lely::canopen::FiberDriver::FiberDriver
FiberDriver(ev_exec_t *exec, AsyncMaster &master, uint8_t id)
Creates a new CANopen driver and its associated fiber executor.
Definition: fiber_driver.cpp:49