Use cond to wait for new command and not waste 100% CPU 49/173749/1
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Wed, 21 Mar 2018 13:00:18 +0000 (14:00 +0100)
committerKrzysztof Dynowski <k.dynowski@samsung.com>
Fri, 23 Mar 2018 13:47:51 +0000 (14:47 +0100)
Change-Id: I0e263861c83a6feeef203c14576dc4793aede87b

TEEStub/TaskStrategy/TaskQueuedStrategy.cpp
TEEStub/TaskStrategy/TaskQueuedStrategy.h

index 11d23bd..9f358de 100644 (file)
@@ -41,6 +41,7 @@
 TaskQueuedStrategy::TaskQueuedStrategy(stream_protocol::socket &msocket) :
                clientSocket(msocket) {
        runThread = false;
+       cmdReady = false;
 }
 
 /**
@@ -55,7 +56,7 @@ void TaskQueuedStrategy::handleCommand(CommandBasePtr command) {
                LOGD(TEE_STUB, "A Cancel command has been received!");
                executeCancellation(command);
        } else {
-               map_mutex.lock();
+               boost::unique_lock<boost::mutex> lck(ready_mutex);
                //If new session ID, just add to map
                if (sessionTaskMap.find(command->sessionID) == sessionTaskMap.end()) {
                        SessionState ss(command->sessionID);
@@ -68,7 +69,8 @@ void TaskQueuedStrategy::handleCommand(CommandBasePtr command) {
                        sessionTaskMap[command->sessionID].addTask(command);
                }
                LOGD(TEE_STUB, "MapSize: %d", sessionTaskMap.size());
-               map_mutex.unlock();
+               cmdReady = true;
+               ready_cond.notify_all();
        }
 }
 
@@ -112,7 +114,8 @@ void TaskQueuedStrategy::executeCommands() {
         */
        unsigned char writeData[1024];
        while (runThread) {
-               map_mutex.lock();
+               boost::unique_lock<boost::mutex> lck(ready_mutex);
+               while (!cmdReady) ready_cond.wait(lck);
 
                if (sessionTaskMap.size() > 0) {
                        for (std::map<uint32_t, SessionState>::iterator itr =
@@ -158,7 +161,7 @@ void TaskQueuedStrategy::executeCommands() {
                                } //if queue
                        } //for
                } //if map
-               map_mutex.unlock();
+               cmdReady = false; // all commands done
 
                // TODO: check how to reduce cycles from while(1)
                // boost::this_thread::sleep(boost::posix_time::milliseconds(1));
index ba0fb13..415564d 100644 (file)
@@ -61,10 +61,13 @@ using boost::asio::local::stream_protocol;
 class TaskQueuedStrategy:
     public TaskStrategy {
 private:
+       boost::mutex ready_mutex;
+       boost::condition_variable ready_cond;
+       bool cmdReady;
+
        std::map<uint32_t, SessionState> sessionTaskMap;
        std::vector<CommandRequestCancel> cancelVector;
        boost::thread executorThread;
-       boost::mutex map_mutex;
 
        // The socket used to communicate with the client.
        stream_protocol::socket &clientSocket;