From 939a1ddd372f764ac0d6b8c8824dc3993e9c2bae Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Tue, 29 Jun 2021 20:17:54 +0200 Subject: [PATCH] Minor improvements to the udev polling loop. Change-Id: I068e2cbfe0243221f879961bb19ae8ea5e9b65f3 --- src/common.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/common.c b/src/common.c index 969b39a..ed56e67 100644 --- a/src/common.c +++ b/src/common.c @@ -7,6 +7,7 @@ #include #define UDEV_MONITOR_POLL_TIMEOUT_MILISECONDS 100 +#define UDEV_MONITOR_POLL_ITERATIONS 10 //! How many times to poll? Affects the total timeout. int peripheral_lock(const char *file_name) { @@ -95,13 +96,17 @@ int peripheral_wait_for_udev(struct udev_monitor *monitor, UdevCompareFunc func, pfd.fd = udev_monitor_get_fd(monitor); pfd.events = POLLIN; - for (int cnt = 0; cnt < 10; cnt++) { - _D("poll iteration"); - if (poll(&pfd, 1, UDEV_MONITOR_POLL_TIMEOUT_MILISECONDS) < 0) { - _E("Failed to watch udev monitor"); + for (int cnt = 0; cnt < UDEV_MONITOR_POLL_ITERATIONS; cnt++) { + _D("poll iteration %d", cnt); + int ready = poll(&pfd, 1, UDEV_MONITOR_POLL_TIMEOUT_MILISECONDS); + if (ready < 0) { + _E("Failed to watch udev monitor %m"); return -EIO; } + if (ready == 0) + continue; + struct udev_device *dev = udev_monitor_receive_device(monitor); if (dev) { if (func(dev, func_data)) { -- 2.34.1