From: Konrad Kuchciak Date: Thu, 4 Oct 2018 10:06:55 +0000 (+0200) Subject: gpio: make read function threadsafe X-Git-Tag: submit/tizen/20190213.063316~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0976a095ee0e1ae9f9a3626f1381355995605708;p=platform%2Fcore%2Fapi%2Fperipheral-io.git gpio: make read function threadsafe 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 --- diff --git a/src/interface/peripheral_interface_gpio.c b/src/interface/peripheral_interface_gpio.c index ce9dbf4..e7b618f 100644 --- a/src/interface/peripheral_interface_gpio.c +++ b/src/interface/peripheral_interface_gpio.c @@ -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') {