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 <bin.qian@windriver.com>
This commit is contained in:
Bin Qian 2018-10-12 08:34:36 -04:00
parent 481b44f8a7
commit 3eb68cc231
2 changed files with 17 additions and 4 deletions

View File

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include "sm_util_types.h" #include "sm_util_types.h"
#include "sm_debug.h" #include "sm_debug.h"
@ -84,7 +85,7 @@ SmErrorT SmWorkerThread::go()
} }
this->_thread_created = true; 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"); DPRINTFE("Cannot init semaphore");
return SM_FAILED; return SM_FAILED;
@ -147,9 +148,22 @@ void SmWorkerThread::add_priority_action(SmAction& action)
// **************************************************************************** // ****************************************************************************
void SmWorkerThread::thread_run() void SmWorkerThread::thread_run()
{ {
int wait_interval_ns = 50*1000000; //50 ms
int ns_to_sec = 1000000000;
struct timespec wait_time;
while(this->_goon) 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; SmAction* action = NULL;
if(!this->_priority_queue.empty()) if(!this->_priority_queue.empty())
@ -165,7 +179,7 @@ void SmWorkerThread::thread_run()
{ {
action->action(); action->action();
} }
}else if(EAGAIN != errno) }else if(ETIMEDOUT != errno)
{ {
DPRINTFE("Semaphore wait failed. Error %d", errno); DPRINTFE("Semaphore wait failed. Error %d", errno);
} }

View File

@ -11,7 +11,6 @@
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
#define MAX_QUEUED_ACTIONS 24
// **************************************************************************** // ****************************************************************************
// SmAction interface, action to process in a worker thread // SmAction interface, action to process in a worker thread
// **************************************************************************** // ****************************************************************************