Lely core libraries 2.3.5
fiber_driver.cpp
Go to the documentation of this file.
1
24
25#include "coapp.hpp"
26
27#if !LELY_NO_COAPP_MASTER
28
30
31#include <cassert>
32
33namespace lely {
34
35namespace canopen {
36
37namespace detail {
38
39FiberDriverBase::FiberDriverBase(ev_exec_t* exec_)
40#if !_WIN32 && _POSIX_MAPPED_FILES
41 : thrd(ev::FiberFlag::SAVE_ERROR | ev::FiberFlag::GUARD_STACK),
42#else
43 : thrd(ev::FiberFlag::SAVE_ERROR),
44#endif
45 exec(exec_),
46 strand(exec) {
47}
48
49} // namespace detail
50
52 : FiberDriverBase(exec ? exec
53 : static_cast<ev_exec_t*>(master.GetExecutor())),
54 BasicDriver(FiberDriverBase::exec, master, id) {}
55
56void
57FiberDriver::Wait(SdoFuture<void> f, ::std::error_code& ec) {
58 fiber_await(f);
59 try {
60 f.get().value();
61 } catch (const ::std::system_error& e) {
62 ec = e.code();
63 } catch (const ev::future_not_ready& e) {
64 ec = ::std::make_error_code(::std::errc::operation_canceled);
65 }
66}
67
68void
69FiberDriver::USleep(uint_least64_t usec) {
70 ::std::error_code ec;
71 USleep(usec, ec);
72 if (ec) throw ::std::system_error(ec, "USleep");
73}
74
75void
76FiberDriver::USleep(uint_least64_t usec, ::std::error_code& ec) {
77 io_tqueue_wait* wait = nullptr;
78 auto f = master.AsyncWait(::std::chrono::microseconds(usec), &wait);
79 assert(wait);
80 Wait(f, ec);
81 if (!f.is_ready()) master.CancelWait(*wait);
82}
83
84} // namespace canopen
85
86} // namespace lely
87
88#endif // !LELY_NO_COAPP_MASTER
An asynchronous CANopen master.
Definition master.hpp:1957
ev::Executor GetExecutor() const noexcept final
Returns the executor used to execute event handlers for this driver, including SDO confirmation funct...
Definition driver.hpp:311
uint8_t id() const noexcept final
Returns the node-ID.
Definition driver.hpp:321
BasicDriver(ev_exec_t *exec, BasicMaster &master, uint8_t id)
Creates a new driver for a remote CANopen node and registers it with the master.
Definition driver.cpp:40
BasicMaster & master
A reference to the master with which this driver is registered.
Definition driver.hpp:1097
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.
result_type & get()
Returns the result of a ready future.
Definition future.hpp:490
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 is the internal header file of the C++ CANopen application library.
This header file is part of the C++ CANopen application library; it contains the declarations for the...
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition ev.h:29
The namespace for implementation details of the C++ CANopen application library.
The namespace for the C++ CANopen application library.
Definition device.hpp:39
ev::Future< T, ::std::exception_ptr > SdoFuture
A helper alias template for the type of future used to retrieve the result of an asynchronous SDO req...
Definition sdo.hpp:64
A wait operation suitable for use with a timer queue.
Definition tqueue.h:36