gpio: make read function threadsafe 45/190645/1
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Thu, 4 Oct 2018 10:06:55 +0000 (12:06 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Thu, 4 Oct 2018 10:08:24 +0000 (12:08 +0200)
A new thread is created when setting interrupted callback on gpio pin,
which is polling and reading in loop. Reading at the same time from main
thread makes race condition as it is doing lseek() and read() on static
variable gpio->fd_value. This is fixed by adding mutex to
peripheral_gpio_read().

Change-Id: I55508fc36b91993226b373d460407c40d551276f
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
src/interface/peripheral_interface_gpio.c

index ce9dbf4..e7b618f 100644 (file)
@@ -158,9 +158,13 @@ int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
        int ret;
        int length = 1;
        char gpio_buf[GPIO_BUFFER_MAX] = {0, };
+       static GMutex mutex;
 
+       g_mutex_lock(&mutex);
        lseek(gpio->fd_value, 0, SEEK_SET);
        ret = read(gpio->fd_value, &gpio_buf, length);
+       g_mutex_unlock(&mutex);
+
        CHECK_ERROR(ret != length);
 
        if (gpio_buf[0] == '0') {