Priority queue implementation for the event queue after change in public code for... 06/30306/2
authorVibhor Gaur <vibhor.gaur@samsung.com>
Fri, 14 Nov 2014 09:41:56 +0000 (15:11 +0530)
committerVibhor Gaur <vibhor.gaur@samsung.com>
Fri, 14 Nov 2014 09:52:00 +0000 (15:22 +0530)
-Adding priority queue implementation after public code is changed for sync with private code.
-Shifting addition into priority list to server side as compared to the previous implementation where it was done on the shared side.Optimizes the number of instructions executed and enables insertion during event registration.
-priority list has been declared extern in csensor_event_queue.h  since we cannot include the file command_worker.cpp(where it is initially declared) in csensor_event_queue.h as it creates a cyclic dependency and fails in build.

Change-Id: I4ca49167c78beac802ba868543817d326e6d8a8e

src/server/command_worker.cpp
src/shared/csensor_event_queue.cpp
src/shared/csensor_event_queue.h

index 37a65b2..4c3adab 100755 (executable)
 #include <command_worker.h>
 #include <sensor_plugin_loader.h>
 #include <sensor_info.h>
+#include <sensor_accel.h>
+#include <sensor_gyro.h>
+#include <sensor_geomag.h>
+#include <sensor_orientation.h>
+#include <sensor_linear_accel.h>
+#include <sensor_gravity.h>
 #include <thread>
 #include <string>
 #include <utility>
 #include <dlfcn.h>
+#include <set>
 
+using namespace std;
 using std::string;
+using std::set;
 using std::make_pair;
 
 #define SECURITY_LIB "/usr/lib/libsecurity-server-client.so.1"
 
+set<unsigned int> priority_list;
+
 void *command_worker::m_security_handle  = NULL;
 command_worker::security_server_check_privilege_by_sockfd_t command_worker::security_server_check_privilege_by_sockfd = NULL;
 command_worker::cmd_handler_t command_worker::m_cmd_handlers[];
@@ -552,6 +563,11 @@ bool command_worker::cmd_register_event(void *payload)
                ret_value = OP_ERROR;
                goto out;
        }
+               if (cmd->event_type == GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME || cmd->event_type ==  LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME || cmd->event_type == ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME) {
+                       priority_list.insert(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME);
+                       priority_list.insert(GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME);
+                       priority_list.insert(GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME);
+       }
 
        m_module->add_client(cmd->event_type);
 
index 40059ea..d3aa0dc 100755 (executable)
@@ -77,7 +77,7 @@ void* csensor_event_queue::pop(void)
        while (m_queue.empty())
                m_cond_var.wait(u);
 
-       void* event = m_queue.front();
+       void* event = m_queue.top();
        m_queue.pop();
        return event;
 }
index cd8dee6..4c554d9 100755 (executable)
 #include <queue>
 #include <mutex>
 #include <condition_variable>
+#include <set>
 
+using namespace std;
 using std::queue;
 using std::mutex;
 using std::lock_guard;
 using std::unique_lock;
 using std::condition_variable;
+using std::set;
+
+extern set<unsigned int> priority_list;
 
 class csensor_event_queue
 {
 private:
        static const unsigned int QUEUE_FULL_SIZE = 1000;
 
-       queue<void* > m_queue;
+       class compare {
+       public:
+               bool operator() (void* &v1,void *&v2) {
+                       sensor_event_t *e1 = (sensor_event_t *)v1;
+                       sensor_event_t *e2 = (sensor_event_t *)v2;
+                       bool prioritize_e1 = true;
+                       bool prioritize_e2 = true;
+
+                       if (priority_list.empty())
+                               return (e2->data.timestamp < e1->data.timestamp);
+
+                       set<unsigned int>::iterator iter_e1 = priority_list.find(e1->event_type);
+                       set<unsigned int>::iterator iter_e2 = priority_list.find(e2->event_type);
+
+                       if (iter_e1 == priority_list.end())
+                               prioritize_e1 = false;
+
+                       if (iter_e2 == priority_list.end())
+                               prioritize_e2 = false;
+
+                       if (prioritize_e2) {
+                               if (!prioritize_e1)
+                                       return true;
+                               else {
+                                       if (e2->data.timestamp <= e1->data.timestamp)
+                                               return true;
+                                       return false;
+                               }
+                       }
+                       else {
+                               if (prioritize_e1)
+                                       return false;
+                               else if (e2->data.timestamp <= e1->data.timestamp)
+                                       return true;
+                               else
+                                       return false;
+                       }
+               }
+       };
+
+       std::priority_queue<void*, std::vector<void*>, compare> m_queue;
+
        mutex m_mutex;
        condition_variable m_cond_var;