Skip to contentSkip to conversation

libgpiod: Allow to gracefully exit from a wait event

PATCH In review Adrien Zinger 3 participants 4 messages Nov 13, 2023 – Nov 15, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libgpiod: Allow to gracefully exit from a wait event

Add a function in core gpiod_line_request_wait_edge_events_or with an additional argument fd to trigger ppoll manually from another thread.

It allows users to gracefully cancel and join a worker thread waiting for an edge event.

---
 include/gpiod.h          | 17 ++++++++++++++
 lib/internal.c           | 38 ++++++++++++++++++++++++------
 lib/internal.h           |  1 +
 lib/line-request.c       |  9 +++++++
 tests/tests-edge-event.c | 51 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 7 deletions(-)
include/gpiod.h +17 -0
index d86c6ac..cbc83f9 100644
@@ -1176,6 +1176,23 @@ int gpiod_line_request_get_fd(struct gpiod_line_request *request);
 int gpiod_line_request_wait_edge_events(struct gpiod_line_request *request,
 					int64_t timeout_ns);
 
+/**
+ * @brief Wait for edge events on any of the requested lines or a
+ *        POLLHUP/POLLERR event on the given file descriptor.
+ * @param request GPIO line request.
+ * @param fd file descriptor from any I/O, channel, fifo, or pipe.
+ * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
+ *                   returns immediately. If set to a negative number, the
+ *                   function blocks indefinitely until an event becomes
+ *                   available.
+ * @return 0 if wait timed out, -1 if an error occurred, 1 if an event is
+ *         pending, 2 if the file descriptor raised an event.
+ *
+ * Lines must have edge detection set for edge events to be emitted.
+ * By default edge detection is disabled.
+ */
+int gpiod_line_request_wait_edge_events_or(struct gpiod_line_request *request,
+					int fd, int64_t timeout_ns);
 /**
  * @brief Read a number of edge events from a line request.
  * @param request GPIO line request.
lib/internal.c +31 -7
index c80d01f..aaf96ff 100644
@@ -81,22 +81,18 @@ out:
 	return ret;
 }
 
-int gpiod_poll_fd(int fd, int64_t timeout_ns)
+static int
+gpiod_poll_fds(struct pollfd *pfds, uint8_t len, int64_t timeout_ns)
 {
 	struct timespec ts;
-	struct pollfd pfd;
 	int ret;
 
-	memset(&pfd, 0, sizeof(pfd));
-	pfd.fd = fd;
-	pfd.events = POLLIN | POLLPRI;
-
 	if (timeout_ns >= 0) {
 		ts.tv_sec = timeout_ns / 1000000000ULL;
 		ts.tv_nsec = timeout_ns % 1000000000ULL;
 	}
 
-	ret = ppoll(&pfd, 1, timeout_ns < 0 ? NULL : &ts, NULL);
+	ret = ppoll(pfds, len, timeout_ns < 0 ? NULL : &ts, NULL);
 	if (ret < 0)
 		return -1;
 	else if (ret == 0)
@@ -105,6 +101,34 @@ int gpiod_poll_fd(int fd, int64_t timeout_ns)
 	return 1;
 }
 
+int gpiod_poll_fd(int fd, int64_t timeout_ns)
+{
+	struct pollfd pfd;
+
+	memset(&pfd, 0, sizeof(pfd));
+	pfd.fd = fd;
+	pfd.events = POLLIN | POLLPRI;
+
+	return gpiod_poll_fds(&pfd, 1, timeout_ns);
+}
+
+int gpiod_poll_fd_or(int fd1, int fd2, int64_t timeout_ns)
+{
+	struct pollfd pfds[2];
+	int ret;
+
+	memset(&pfds, 0, sizeof(pfds));
+	pfds[0].fd = fd1;
+	pfds[0].events = POLLIN | POLLPRI;
+	pfds[1].fd = fd2;
+	pfds[1].events = POLLIN | POLLERR;
+
+	ret = gpiod_poll_fds(pfds, 2, timeout_ns);
+	if (ret >= 1 && pfds[1].revents != 0)
+		return 2;
+	return ret;
+}
+
 int gpiod_set_output_value(enum gpiod_line_value in, enum gpiod_line_value *out)
 {
 	switch (in) {
lib/internal.h +1 -0
index 61d7aec..6b2105b 100644
@@ -36,6 +36,7 @@ gpiod_info_event_from_uapi(struct gpio_v2_line_info_changed *uapi_evt);
 struct gpiod_info_event *gpiod_info_event_read_fd(int fd);
 
 int gpiod_poll_fd(int fd, int64_t timeout);
+int gpiod_poll_fd_or(int fd1, int fd2, int64_t timeout);
 int gpiod_set_output_value(enum gpiod_line_value in,
 			   enum gpiod_line_value *out);
 
lib/line-request.c +9 -0
index e867d91..dd2a5a2 100644
@@ -295,6 +295,15 @@ gpiod_line_request_wait_edge_events(struct gpiod_line_request *request,
 	return gpiod_poll_fd(request->fd, timeout_ns);
 }
 
+GPIOD_API int
+gpiod_line_request_wait_edge_events_or(struct gpiod_line_request *request,
+				    int fd, int64_t timeout_ns)
+{
+	assert(request);
+
+	return gpiod_poll_fd_or(request->fd, fd, timeout_ns);
+}
+
 GPIOD_API int
 gpiod_line_request_read_edge_events(struct gpiod_line_request *request,
 				    struct gpiod_edge_event_buffer *buffer,
tests/tests-edge-event.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 30 lines · Adrien Zinger

Add a function in core gpiod_line_request_wait_edge_events_or with an additional argument fd to trigger ppoll manually from another thread.

It allows users to gracefully cancel and join a worker thread waiting for an edge event.

---
 include/gpiod.h          | 17 ++++++++++++++
 lib/internal.c           | 38 ++++++++++++++++++++++++------
 lib/internal.h           |  1 +
 lib/line-request.c       |  9 +++++++
 tests/tests-edge-event.c | 51 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 7 deletions(-)
include/gpiod.h +7 -0
index d86c6ac..cbc83f9 100644
@@ -1176,6 +1176,23 @@ int gpiod_line_request_get_fd(struct gpiod_line_request *request);
 int gpiod_line_request_wait_edge_events(struct gpiod_line_request *request,
 					int64_t timeout_ns);
 
+/**
+ * @brief Wait for edge events on any of the requested lines or a
+ *        POLLHUP/POLLERR event on the given file descriptor.
+ * @param request GPIO line request.
+ * @param fd file descriptor from any I/O, channel, fifo, or pipe.
+ * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
+ *                   returns immediately. If set to a negative number, the

include/gpiod.h int gpiod_line_request_get_fd(struct gpiod_line_request *request); · line 1195

Quoted 10 lines
include/gpiod.h +10 -0
+ *                   function blocks indefinitely until an event becomes
+ *                   available.
+ * @return 0 if wait timed out, -1 if an error occurred, 1 if an event is
+ *         pending, 2 if the file descriptor raised an event.
+ *
+ * Lines must have edge detection set for edge events to be emitted.
+ * By default edge detection is disabled.
+ */
+int gpiod_line_request_wait_edge_events_or(struct gpiod_line_request *request,
+					int fd, int64_t timeout_ns);

This sounds like an oddly specific API... I wonder, why does gpiod_line_request_get_fd [1] + doing the polling in your code not work for you? Since you already got a file descriptor for the notification that seems like little extra work. Or did you consider that but find it too verbose?

[1] https://libgpiod.readthedocs.io/en/latest/group__line__request.html#ga5c0dbdcd8608b76e77b78bca9a6b03d7

- Erik

#2 lore raw
Quoted 7 lines

This sounds like an oddly specific API... I wonder, why does gpiod_line_request_get_fd [1] + doing the polling in your code not work
for you? Since you already got a file descriptor for the notification that seems like little extra work. Or did you consider that but find
it too verbose?

Until now it works like you said and it's not so verbose.

Actually I had the issue some weeks ago. And before digging inside the library I have searched in the documentation and also asked on stackoverflow [1] for a suggestion. Despite everything me and other developers were not able to guess how to cancel a wait event from another thread, without reading the code of the library. I thought that it could be a good thing to provide that feature.

[1]
https://stackoverflow.com/questions/77222869/how-to-gracefully-cancel-gpiod-line-event-wait

Best regards,
Adrien

#3 lore raw
Quoted 18 lines · Adrien Zinger
Quoted 8 lines

This sounds like an oddly specific API... I wonder, why does gpiod_line_request_get_fd [1] + doing the polling in your code not work
for you? Since you already got a file descriptor for the notification that seems like little extra work. Or did you consider that but find
it too verbose?

Until now it works like you said and it's not so verbose.

Actually I had the issue some weeks ago. And before digging inside the library I have searched in the documentation and also asked on stackoverflow [1] for a suggestion. Despite everything me and other developers were not able to guess how to cancel a wait event from another thread, without reading the code of the library. I thought that it could be a good thing to provide that feature.

Quoted 7 lines

[1]
https://stackoverflow.com/questions/77222869/how-to-gracefully-cancel-gpiod-line-event-wait

Best regards,
Adrien

I agree with Erik, this looks like a very specific (obscure?) use-case that's best implemented in-place using the fd getter. I try to keep the library minimal and simple and this doesn't seem like a good fit IMO.

Bart