Fixed the blocking issue when reset eventfd 20/259620/1
authorYunmi Ha <yunmi.ha@samsung.com>
Thu, 10 Jun 2021 08:20:31 +0000 (17:20 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Thu, 10 Jun 2021 08:23:12 +0000 (17:23 +0900)
Change-Id: Ia9e43943e6446646c7fe0c40449a980bc2679aee
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/accelerometer/accel_device.cpp
src/gyroscope/gyro_device.cpp
src/magnetometer/magnet_device.cpp
src/sensor_update_event.cpp
src/sensor_update_event.h

index 4ca33d9..9bf7def 100644 (file)
@@ -73,12 +73,14 @@ static sensor_info_t sensor_info = {
        wakeup_supported: false
 };
 
-static void update_event_cb(void *data)
+static bool update_event_cb(void *data)
 {
        accel_device *device = (accel_device *)data;
 
        if (device)
-               device->update_value();
+               return device->update_value();
+
+       return false;
 }
 
 
@@ -213,8 +215,6 @@ bool accel_device::update_value_i2c(void)
        m_y = temp[1];
        m_z = temp[2];
 
-       m_update_event.invoke();
-
        //time stamp
        return true;
 }
@@ -223,7 +223,7 @@ int accel_device::read_fd(uint32_t **ids)
 {
        retvm_if(ids == NULL || ids == nullptr, -EINVAL, "%s:NULL interface", SENSOR_NAME);
 
-       m_update_event.reset();
+       m_update_event.clear();
 
        event_ids.clear();
        event_ids.push_back(sensor_info.id);
index f884433..66c7028 100644 (file)
@@ -72,12 +72,14 @@ static sensor_info_t sensor_info = {
        wakeup_supported: false
 };
 
-static void update_event_cb(void *data)
+static bool update_event_cb(void *data)
 {
        gyro_device *device = (gyro_device *)data;
 
        if (device)
-               device->update_value();
+               return device->update_value();
+
+       return false;
 }
 
 gyro_device::gyro_device()
@@ -207,8 +209,6 @@ bool gyro_device::update_value_i2c(void)
        m_y = temp[1];
        m_z = temp[2];
 
-       m_update_event.invoke();
-
        return true;
 }
 
@@ -216,7 +216,7 @@ int gyro_device::read_fd(uint32_t **ids)
 {
        retvm_if(ids == NULL || ids == nullptr, -EINVAL, "%s:NULL interface", SENSOR_NAME);
 
-       m_update_event.reset();
+       m_update_event.clear();
 
        event_ids.clear();
        event_ids.push_back(sensor_info.id);
index 8a63775..e139846 100644 (file)
@@ -81,12 +81,14 @@ static sensor_info_t sensor_info = {
        wakeup_supported: false
 };
 
-static void update_event_cb(void *data)
+static bool update_event_cb(void *data)
 {
        magnet_device *device = (magnet_device *)data;
 
        if (device)
-               device->update_value();
+               return device->update_value();
+
+       return false;
 }
 
 magnet_device::magnet_device()
@@ -247,8 +249,6 @@ bool magnet_device::update_value_i2c(void)
        m_y = temp[1];
        m_z = temp[2];
 
-       m_update_event.invoke();
-
        return true;
 }
 
@@ -256,7 +256,7 @@ int magnet_device::read_fd(uint32_t **ids)
 {
        retvm_if(ids == NULL || ids == nullptr, -EINVAL, "%s:NULL interface", SENSOR_NAME);
 
-       m_update_event.reset();
+       m_update_event.clear();
 
        event_ids.clear();
        event_ids.push_back(sensor_info.id);
index 642f495..31242e4 100644 (file)
@@ -19,6 +19,7 @@
 #include <unistd.h>
 #include <sensor_log.h>
 #include <glib.h>
+#include <poll.h>
 #include <sys/eventfd.h>
 #include "sensor_update_event.h"
 
@@ -103,7 +104,8 @@ void sensor_update_event::stop()
 
 void sensor_update_event::update(void)
 {
-       m_update_func(m_user_data);
+       if(m_update_func(m_user_data))
+               invoke();
 }
 
 void sensor_update_event::invoke(void)
@@ -116,12 +118,25 @@ void sensor_update_event::invoke(void)
                _E("%s: Failed to notify.", m_sensor_name.c_str());
 }
 
-void sensor_update_event::reset(void)
+void sensor_update_event::clear(void)
 {
        uint64_t data;
        ssize_t size;
 
-       size = read(m_event_fd, &data, sizeof(uint64_t));
-       if (size != sizeof(uint64_t))
-               _E("%s: Failed to flush.", m_sensor_name.c_str());
+       struct pollfd pfd;
+       pfd.fd = m_event_fd;
+       pfd.events = POLLIN | POLLERR;
+       pfd.revents = 0;
+
+       int ret = poll(&pfd, 1, 0);
+       if (ret <= 0) {
+               _E("%s: There is no event data.", m_sensor_name.c_str());
+               return ;
+       }
+
+       if (pfd.revents & POLLIN) {
+               size = read(m_event_fd, &data, sizeof(uint64_t));
+               if (size != sizeof(uint64_t))
+                       _E("%s: Failed to flush.", m_sensor_name.c_str());
+       }
 }
index dae521e..175e952 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <string>
 
-typedef void (*fp_update_func)(void *data);
+typedef bool (*fp_update_func)(void *data);
 
 class sensor_update_event {
 public:
@@ -33,7 +33,7 @@ public:
        int get_event_fd();
 
        void invoke();
-       void reset();
+       void clear();
 
        bool start();
        void stop();