From a4d0c2a85e73a0fdba0b46a61aa5926c16bcca94 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 18 May 2016 20:56:26 +0900 Subject: [PATCH] sensord: add stop() in event_dispatcher thread - because sensord can be shutdown, event_dispatcher can be stopped. Change-Id: Idd59e0f34dad468c3f4a32d3a47e4329cdec0936 Signed-off-by: kibak.yoon --- src/server/sensor_event_dispatcher.cpp | 26 ++++++++++++++++++-------- src/server/sensor_event_dispatcher.h | 2 ++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/server/sensor_event_dispatcher.cpp b/src/server/sensor_event_dispatcher.cpp index 200cc95..c1fc3cb 100644 --- a/src/server/sensor_event_dispatcher.cpp +++ b/src/server/sensor_event_dispatcher.cpp @@ -30,6 +30,7 @@ using std::pair; #define MAX_PENDING_CONNECTION 32 sensor_event_dispatcher::sensor_event_dispatcher() +: m_running(false) { } @@ -45,12 +46,21 @@ sensor_event_dispatcher& sensor_event_dispatcher::get_instance() bool sensor_event_dispatcher::run(void) { + m_running = true; + thread dispatcher(&sensor_event_dispatcher::dispatch_event, this); dispatcher.detach(); return true; } +bool sensor_event_dispatcher::stop(void) +{ + m_running = false; + + return true; +} + void sensor_event_dispatcher::accept_event_channel(csocket client_socket) { int client_id; @@ -99,7 +109,7 @@ void sensor_event_dispatcher::dispatch_event(void) _I("Event Dispatcher started"); - while (true) { + while (m_running) { void *seed_event = get_event_queue().pop(); vector sensor_events; @@ -273,6 +283,9 @@ bool sensor_event_dispatcher::add_active_virtual_sensor(virtual_sensor *sensor) { AUTOLOCK(m_active_virtual_sensors_mutex); + if (!m_running) + return true; + if (has_active_virtual_sensor(sensor)) { _E("[%s] sensor is already added on active virtual sensors", sensor->get_name()); return false; @@ -287,14 +300,11 @@ bool sensor_event_dispatcher::delete_active_virtual_sensor(virtual_sensor *senso { AUTOLOCK(m_active_virtual_sensors_mutex); - auto it_v_sensor = find(m_active_virtual_sensors.begin(), m_active_virtual_sensors.end(), sensor); - - if (it_v_sensor == m_active_virtual_sensors.end()) { - _E("Fail to delete non-existent [%s] sensor on active virtual sensors", sensor->get_name()); - return false; - } + if (!m_running) + return true; - m_active_virtual_sensors.erase(it_v_sensor); + if (sensor) + m_active_virtual_sensors.remove(sensor); return true; } diff --git a/src/server/sensor_event_dispatcher.h b/src/server/sensor_event_dispatcher.h index 243fb70..ee7e630 100644 --- a/src/server/sensor_event_dispatcher.h +++ b/src/server/sensor_event_dispatcher.h @@ -37,6 +37,7 @@ public: static sensor_event_dispatcher& get_instance(); bool run(void); + bool stop(void); void accept_event_connections(csocket client_socket); void request_last_event(int client_id, sensor_id_t sensor_id); @@ -51,6 +52,7 @@ private: event_type_last_event_map m_last_events; virtual_sensors m_active_virtual_sensors; cmutex m_active_virtual_sensors_mutex; + bool m_running; sensor_event_dispatcher(); ~sensor_event_dispatcher(); -- 2.7.4