31 auto start (libusb_context *usb_ctx, std::stop_token
stop) ->
bool
33 auto handle_fn = [usb_ctx,
stop]
35 const timeval ctv = {.tv_sec = 0, .tv_usec = 10'000};
36 while (!
stop.stop_requested())
39 auto r = libusb_handle_events_timeout(usb_ctx, &tv);
40 if (r != LIBUSB_SUCCESS)
46 m_thread = std::thread{std::move(handle_fn)};
77 if (m_thread.joinable())
93static_assert(detail::EventHandler<trivial_event_handler>,
"Not a proper usb event handler");
94static_assert(detail::EventHandler<trivial_event_handler>,
"Not a proper usb event handler");
113 auto start (libusb_context *usb_ctx, std::stop_token
stop) ->
bool
117 auto handle_fn = [
this, usb_ctx,
stop]
119 const timeval ctv = {.tv_sec = 0, .tv_usec = 10'000};
123 auto r = libusb_handle_events_timeout(usb_ctx, &tv);
124 if (r != LIBUSB_SUCCESS) [[unlikely]]
126 throw std::system_error{
130 if (
stop.stop_requested() && m_counter.load(std::memory_order_acquire) == 0)
137 m_thread = std::thread{std::move(handle_fn)};
151 m_counter.fetch_add(1, std::memory_order_acquire);
159 m_counter.fetch_sub(1, std::memory_order_release);
169 if (m_thread.joinable())
182 std::atomic_size_t m_counter{0};
183 std::thread m_thread;
186static_assert(detail::EventHandler<refcounted_event_handler>,
"Not a proper usb event handler");
187static_assert(detail::EventHandler<refcounted_event_handler>,
"Not a proper usb event handler");
Specification and implementation of EventHandler concept.
Definition any_event_handler.hpp:15
usb_error
USB error enumeration.
Definition usb_error.hpp:20
std::error_code make_usb_error_code(usb_error e) noexcept
Definition usb_error.hpp:85
libusb event handler with simple internal reference counting.
Definition handlers.hpp:106
~refcounted_event_handler()
Stop the event thread on destruction.
Definition handlers.hpp:176
auto unref() noexcept
Decrement the internal reference counter.
Definition handlers.hpp:157
auto ref() noexcept
Increment the internal reference counter.
Definition handlers.hpp:149
auto stop()
Stop the event loop and wait for the thread to finish.
Definition handlers.hpp:167
auto start(libusb_context *usb_ctx, std::stop_token stop) -> bool
Start the libusb event processing loop.
Definition handlers.hpp:113
Trivial single-threaded libusb event handler.
Definition handlers.hpp:24
auto unref() noexcept
Decrement internal reference count.
Definition handlers.hpp:65
auto start(libusb_context *usb_ctx, std::stop_token stop) -> bool
Start the libusb event processing loop.
Definition handlers.hpp:31
~trivial_event_handler()
Stop the event thread on destruction.
Definition handlers.hpp:84
auto stop()
Stop the event loop and wait for the thread to finish.
Definition handlers.hpp:75
auto ref() noexcept
Increment internal reference count.
Definition handlers.hpp:55