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_
An asynchronous CANopen master.
Definition: master.hpp:1296
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:671
A CANopen driver running its tasks and callbacks in fibers.
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 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.
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
result_type & get()
Returns the result of a ready future.
Definition: future.hpp:490
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
value_type & value()
Returns a reference to the value if *this contains a value, and throws an exception if *this contains...
Definition: result.hpp:274
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.