git: 884ca28b97e7 - stable/14 - libusb: use eventfd
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 23 Jun 2025 07:40:57 UTC
The branch stable/14 has been updated by bapt:
URL: https://cgit.FreeBSD.org/src/commit/?id=884ca28b97e73f297b512aaf04deb6f951854c02
commit 884ca28b97e73f297b512aaf04deb6f951854c02
Author: Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2025-05-15 13:21:50 +0000
Commit: Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2025-06-23 07:40:33 +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
(cherry picked from commit ae28929b7c78267815e994e0ad12e3cc7db361bc)
---
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 6d9c11910ea0..5564bfd8b499 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);
@@ -233,19 +231,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) {
@@ -288,8 +283,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 544364386061..47d918959f63 100644
--- a/lib/libusb/libusb10.h
+++ b/lib/libusb/libusb10.h
@@ -93,7 +93,7 @@ TAILQ_HEAD(libusb_device_head, libusb_device);
struct libusb_context {
int debug;
int debug_fixed;
- int ctrl_pipe[2];
+ int event;
int tr_done_ref;
int tr_done_gen;
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)
- ;
}
}