git: ae28929b7c78 - main - libusb: use eventfd
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 15 May 2025 14:24:53 UTC
The branch main has been updated by bapt:
URL: https://cgit.FreeBSD.org/src/commit/?id=ae28929b7c78267815e994e0ad12e3cc7db361bc
commit ae28929b7c78267815e994e0ad12e3cc7db361bc
Author: Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2025-05-15 13:21:50 +0000
Commit: Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2025-05-15 14:24:46 +0000
libusb: use eventfd
Simplify libusb code by replacing pipe(2) inter thread event mecanism
with eventfd(2).
MFC After: 3 weeks
Reviewed by: kevans
Differential Revision: https://reviews.freebsd.org/D50360
---
lib/libusb/libusb10.c | 20 +++++++-------------
lib/libusb/libusb10.h | 2 +-
lib/libusb/libusb10_io.c | 5 ++---
3 files changed, 10 insertions(+), 17 deletions(-)
diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c
index afa25eabf459..679f4e64b5f6 100644
--- a/lib/libusb/libusb10.c
+++ b/lib/libusb/libusb10.c
@@ -39,6 +39,7 @@
#include <string.h>
#include <unistd.h>
#include <time.h>
+#include <sys/eventfd.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
@@ -118,15 +119,13 @@ libusb_set_nonblocking(int f)
void
libusb_interrupt_event_handler(libusb_context *ctx)
{
- uint8_t dummy;
int err;
if (ctx == NULL)
return;
- dummy = 0;
- err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
- if (err < (int)sizeof(dummy)) {
+ err = eventfd_write(ctx->event, 1);
+ if (err < 0) {
/* ignore error, if any */
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
}
@@ -145,7 +144,6 @@ libusb_init_context(libusb_context **context,
struct libusb_context *ctx;
pthread_condattr_t attr;
char *debug, *ep;
- int ret;
if (num_options < 0)
return (LIBUSB_ERROR_INVALID_PARAM);
@@ -234,19 +232,16 @@ libusb_init_context(libusb_context **context,
ctx->ctx_handler = NO_THREAD;
ctx->hotplug_handler = NO_THREAD;
- ret = pipe(ctx->ctrl_pipe);
- if (ret < 0) {
+ ctx->event = eventfd(0, EFD_NONBLOCK);
+ if (ctx->event < 0) {
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_mutex_destroy(&ctx->hotplug_lock);
pthread_cond_destroy(&ctx->ctx_cond);
free(ctx);
return (LIBUSB_ERROR_OTHER);
}
- /* set non-blocking mode on the control pipe to avoid deadlock */
- libusb_set_nonblocking(ctx->ctrl_pipe[0]);
- libusb_set_nonblocking(ctx->ctrl_pipe[1]);
- libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
+ libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->event, POLLIN);
pthread_mutex_lock(&default_context_lock);
if (usbi_default_context == NULL) {
@@ -296,8 +291,7 @@ libusb_exit(libusb_context *ctx)
/* XXX cleanup devices */
libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
- close(ctx->ctrl_pipe[0]);
- close(ctx->ctrl_pipe[1]);
+ close(ctx->event);
pthread_mutex_destroy(&ctx->ctx_lock);
pthread_mutex_destroy(&ctx->hotplug_lock);
pthread_cond_destroy(&ctx->ctx_cond);
diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h
index 3402e0377c92..f34ea25740d6 100644
--- a/lib/libusb/libusb10.h
+++ b/lib/libusb/libusb10.h
@@ -106,7 +106,7 @@ typedef enum {
struct libusb_context {
int debug;
int debug_fixed;
- int ctrl_pipe[2];
+ int event;
int tr_done_ref;
int tr_done_gen;
usb_event_mode_t usb_event_mode;
diff --git a/lib/libusb/libusb10_io.c b/lib/libusb/libusb10_io.c
index 2790142486ba..f1e31c2a7416 100644
--- a/lib/libusb/libusb10_io.c
+++ b/lib/libusb/libusb10_io.c
@@ -36,6 +36,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
+#include <sys/eventfd.h>
#include <sys/queue.h>
#include <sys/endian.h>
#endif
@@ -187,10 +188,8 @@ libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
CTX_LOCK(ctx);
} else {
- uint8_t dummy;
+ eventfd_read(fds[i].fd, &(eventfd_t){0});
- while (read(fds[i].fd, &dummy, 1) == 1)
- ;
}
}