Minor improvements to the udev polling loop. 08/260608/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 29 Jun 2021 18:17:54 +0000 (20:17 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 30 Jun 2021 10:18:18 +0000 (10:18 +0000)
Change-Id: I068e2cbfe0243221f879961bb19ae8ea5e9b65f3

src/common.c

index 969b39a..ed56e67 100644 (file)
@@ -7,6 +7,7 @@
 #include <system_info.h>
 
 #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)) {