From: Krzysztof Dynowski Date: Wed, 21 Mar 2018 13:00:18 +0000 (+0100) Subject: Use cond to wait for new command and not waste 100% CPU X-Git-Tag: submit/tizen/20180412.092951~21^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a981a164c1aedd7ab0f769b718cec7e245a4f26f;p=platform%2Fcore%2Fsecurity%2Ftef-simulator.git Use cond to wait for new command and not waste 100% CPU Change-Id: I0e263861c83a6feeef203c14576dc4793aede87b --- diff --git a/TEEStub/TaskStrategy/TaskQueuedStrategy.cpp b/TEEStub/TaskStrategy/TaskQueuedStrategy.cpp index 11d23bd..9f358de 100644 --- a/TEEStub/TaskStrategy/TaskQueuedStrategy.cpp +++ b/TEEStub/TaskStrategy/TaskQueuedStrategy.cpp @@ -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 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 lck(ready_mutex); + while (!cmdReady) ready_cond.wait(lck); if (sessionTaskMap.size() > 0) { for (std::map::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)); diff --git a/TEEStub/TaskStrategy/TaskQueuedStrategy.h b/TEEStub/TaskStrategy/TaskQueuedStrategy.h index ba0fb13..415564d 100644 --- a/TEEStub/TaskStrategy/TaskQueuedStrategy.h +++ b/TEEStub/TaskStrategy/TaskQueuedStrategy.h @@ -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 sessionTaskMap; std::vector cancelVector; boost::thread executorThread; - boost::mutex map_mutex; // The socket used to communicate with the client. stream_protocol::socket &clientSocket;