From a63f6ea6caca9a2ab5bab2f44f7bd1caba816315 Mon Sep 17 00:00:00 2001 From: Bin Qian Date: Fri, 12 Oct 2018 08:34:36 -0400 Subject: [PATCH] Add idle time to worker thread Adding 50ms idle time in worker thread loop. This is to fix sm taking 100% cpu for a busy loop. Closes-Bug 1797438 Change-Id: Ia41acfab86c0188ceb5c80822010376977c6fc74 Signed-off-by: Bin Qian --- .../sm-1.0.0/src/sm_worker_thread.cpp | 20 ++++++++++++++++--- service-mgmt/sm-1.0.0/src/sm_worker_thread.h | 1 - 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/service-mgmt/sm-1.0.0/src/sm_worker_thread.cpp b/service-mgmt/sm-1.0.0/src/sm_worker_thread.cpp index 8d5daeb0..93821548 100644 --- a/service-mgmt/sm-1.0.0/src/sm_worker_thread.cpp +++ b/service-mgmt/sm-1.0.0/src/sm_worker_thread.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "sm_util_types.h" #include "sm_debug.h" @@ -84,7 +85,7 @@ SmErrorT SmWorkerThread::go() } this->_thread_created = true; - if( 0 != sem_init(&this->_sem, 0, MAX_QUEUED_ACTIONS) ) + if( 0 != sem_init(&this->_sem, 0, 0) ) { DPRINTFE("Cannot init semaphore"); return SM_FAILED; @@ -147,9 +148,22 @@ void SmWorkerThread::add_priority_action(SmAction& action) // **************************************************************************** void SmWorkerThread::thread_run() { + int wait_interval_ns = 50*1000000; //50 ms + int ns_to_sec = 1000000000; + struct timespec wait_time; + while(this->_goon) { - if(0 == sem_trywait(&this->_sem)) + clock_gettime(CLOCK_REALTIME, &wait_time); + + wait_time.tv_nsec += wait_interval_ns; + if(wait_time.tv_nsec > ns_to_sec -1) + { + wait_time.tv_nsec -= ns_to_sec; + wait_time.tv_sec ++; + } + + if(0 == sem_timedwait(&this->_sem, &wait_time)) { SmAction* action = NULL; if(!this->_priority_queue.empty()) @@ -165,7 +179,7 @@ void SmWorkerThread::thread_run() { action->action(); } - }else if(EAGAIN != errno) + }else if(ETIMEDOUT != errno) { DPRINTFE("Semaphore wait failed. Error %d", errno); } diff --git a/service-mgmt/sm-1.0.0/src/sm_worker_thread.h b/service-mgmt/sm-1.0.0/src/sm_worker_thread.h index 49ed2547..06028a69 100644 --- a/service-mgmt/sm-1.0.0/src/sm_worker_thread.h +++ b/service-mgmt/sm-1.0.0/src/sm_worker_thread.h @@ -11,7 +11,6 @@ #include #include -#define MAX_QUEUED_ACTIONS 24 // **************************************************************************** // SmAction interface, action to process in a worker thread // ****************************************************************************