tests: tweak the event worker
authorBartosz Golaszewski <bartekgola@gmail.com>
Tue, 16 May 2017 11:30:05 +0000 (13:30 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Tue, 16 May 2017 11:30:05 +0000 (13:30 +0200)
The event worker thread should only inject another line event if
pthread_cond_timedwait() returned -ETIMEDOUT. If it returned 0, then
the main thread called pthread_cond_broadcast() and either requested
the worker thread to terminate or to change the event configuration.

In both cases we should not inject an event.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
tests/gpiod-test.c

index bccf02f83eaee444a85ad831a7ea90cfc8884719..b28c0797506052399846949675e64dbd6329842a 100644 (file)
@@ -284,32 +284,33 @@ static void * event_worker(void *data TEST_UNUSED)
                ts.tv_nsec = tv_res.tv_usec * 1000;
 
                status = pthread_cond_timedwait(&ev->cond, &ev->lock, &ts);
-               if (status != 0 && status != ETIMEDOUT)
+               if (status == ETIMEDOUT) {
+                       path = xappend(NULL,
+                                      "/sys/kernel/debug/gpio-mockup-event/gpio-mockup-%c/%u",
+                                      'A' + ev->chip_index, ev->line_offset);
+
+                       fd = open(path, O_RDWR);
+                       free(path);
+                       if (fd < 0)
+                               die_perr("error opening gpio event file");
+
+                       if (ev->event_type == TEST_EVENT_RISING)
+                               buf = '1';
+                       else if (ev->event_type == TEST_EVENT_FALLING)
+                               buf = '0';
+                       else
+                               buf = i % 2 == 0 ? '1' : '0';
+
+                       rd = write(fd, &buf, 1);
+                       close(fd);
+                       if (rd < 0)
+                               die_perr("error writing to gpio event file");
+                       else if (rd != 1)
+                               die("invalid write size to gpio event file");
+               } else if (status != 0) {
                        die("error waiting for conditional variable: %s",
                            strerror(status));
-
-               path = xappend(NULL,
-                              "/sys/kernel/debug/gpio-mockup-event/gpio-mockup-%c/%u",
-                              'A' + ev->chip_index, ev->line_offset);
-
-               fd = open(path, O_RDWR);
-               free(path);
-               if (fd < 0)
-                       die_perr("error opening gpio event file");
-
-               if (ev->event_type == TEST_EVENT_RISING)
-                       buf = '1';
-               else if (ev->event_type == TEST_EVENT_FALLING)
-                       buf = '0';
-               else
-                       buf = i % 2 == 0 ? '1' : '0';
-
-               rd = write(fd, &buf, 1);
-               close(fd);
-               if (rd < 0)
-                       die_perr("error writing to gpio event file");
-               else if (rd != 1)
-                       die("invalid write size to gpio event file");
+               }
 
                event_unlock();
        }