From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kent Gibson <warthog618@gmail.com>,
Linus Walleij <linus.walleij@linaro.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski <brgl@bgdev.pl>
Subject: [libgpiod v2][PATCH] treewide: allow polling functions to block indefinitely
Date: Fri, 1 Jul 2022 13:00:25 +0200 [thread overview]
Message-ID: <20220701110025.58399-1-brgl@bgdev.pl> (raw)
All polling system calls have some way of being instructed to block
indefinitely until some event is registered on the file descriptor.
Make both the gpiod_chip_wait_info_event() and
gpiod_line_request_wait_edge_event() accept negative timeout values in
which case the underlying ppoll() will block indefinitely.
Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
include/gpiod.h | 14 ++++++++++----
lib/chip.c | 2 +-
lib/internal.c | 10 ++++++----
lib/internal.h | 2 +-
lib/line-request.c | 2 +-
5 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/include/gpiod.h b/include/gpiod.h
index 5595ff2..d8b5f39 100644
--- a/include/gpiod.h
+++ b/include/gpiod.h
@@ -135,11 +135,14 @@ int gpiod_chip_get_fd(struct gpiod_chip *chip);
* @brief Wait for line status change events on any of the watched lines
* on the chip.
* @param chip GPIO chip object.
- * @param timeout_ns Wait time limit in nanoseconds.
+ * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
+ * returns immediatelly. 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.
*/
-int gpiod_chip_wait_info_event(struct gpiod_chip *chip, uint64_t timeout_ns);
+int gpiod_chip_wait_info_event(struct gpiod_chip *chip, int64_t timeout_ns);
/**
* @brief Read a single line status change event from the chip.
@@ -1320,7 +1323,10 @@ int gpiod_line_request_get_fd(struct gpiod_line_request *request);
/**
* @brief Wait for edge events on any of the requested lines.
* @param request GPIO line request.
- * @param timeout_ns Wait time limit in nanoseconds.
+ * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
+ * returns immediatelly. 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.
*q
@@ -1328,7 +1334,7 @@ int gpiod_line_request_get_fd(struct gpiod_line_request *request);
* By default edge detection is disabled.
*/
int gpiod_line_request_wait_edge_event(struct gpiod_line_request *request,
- uint64_t timeout_ns);
+ int64_t timeout_ns);
/**
* @brief Read a number of edge events from a line request.
diff --git a/lib/chip.c b/lib/chip.c
index fc3dda2..038d3dd 100644
--- a/lib/chip.c
+++ b/lib/chip.c
@@ -145,7 +145,7 @@ GPIOD_API int gpiod_chip_get_fd(struct gpiod_chip *chip)
}
GPIOD_API int gpiod_chip_wait_info_event(struct gpiod_chip *chip,
- uint64_t timeout_ns)
+ int64_t timeout_ns)
{
return gpiod_poll_fd(chip->fd, timeout_ns);
}
diff --git a/lib/internal.c b/lib/internal.c
index b7da67e..d948814 100644
--- a/lib/internal.c
+++ b/lib/internal.c
@@ -7,7 +7,7 @@
#include "internal.h"
-int gpiod_poll_fd(int fd, uint64_t timeout_ns)
+int gpiod_poll_fd(int fd, int64_t timeout_ns)
{
struct timespec ts;
struct pollfd pfd;
@@ -17,10 +17,12 @@ int gpiod_poll_fd(int fd, uint64_t timeout_ns)
pfd.fd = fd;
pfd.events = POLLIN | POLLPRI;
- ts.tv_sec = timeout_ns / 1000000000ULL;
- ts.tv_nsec = timeout_ns % 1000000000ULL;
+ if (timeout_ns >= 0) {
+ ts.tv_sec = timeout_ns / 1000000000ULL;
+ ts.tv_nsec = timeout_ns % 1000000000ULL;
+ }
- ret = ppoll(&pfd, 1, &ts, NULL);
+ ret = ppoll(&pfd, 1, timeout_ns < 0 ? NULL : &ts, NULL);
if (ret < 0)
return -1;
else if (ret == 0)
diff --git a/lib/internal.h b/lib/internal.h
index c87df91..fab12c3 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -36,7 +36,7 @@ struct gpiod_info_event *
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, uint64_t timeout);
+int gpiod_poll_fd(int fd, int64_t timeout);
void gpiod_line_mask_zero(uint64_t *mask);
void gpiod_line_mask_fill(uint64_t *mask);
diff --git a/lib/line-request.c b/lib/line-request.c
index 33f7f67..04bd78d 100644
--- a/lib/line-request.c
+++ b/lib/line-request.c
@@ -200,7 +200,7 @@ GPIOD_API int gpiod_line_request_get_fd(struct gpiod_line_request *request)
GPIOD_API int
gpiod_line_request_wait_edge_event(struct gpiod_line_request *request,
- uint64_t timeout_ns)
+ int64_t timeout_ns)
{
return gpiod_poll_fd(request->fd, timeout_ns);
}
--
2.34.1
next reply other threads:[~2022-07-01 11:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-01 11:00 Bartosz Golaszewski [this message]
2022-07-01 11:10 ` [libgpiod v2][PATCH] treewide: allow polling functions to block indefinitely Andy Shevchenko
2022-07-01 11:52 ` Kent Gibson
2022-07-01 11:52 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220701110025.58399-1-brgl@bgdev.pl \
--to=brgl@bgdev.pl \
--cc=andriy.shevchenko@linux.intel.com \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=warthog618@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).