Browse Source

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>
Bin Qian 6 months ago
parent
commit
a63f6ea6ca

+ 17
- 3
service-mgmt/sm-1.0.0/src/sm_worker_thread.cpp View File

@@ -8,6 +8,7 @@
8 8
 #include <stdio.h>
9 9
 #include <errno.h>
10 10
 #include <unistd.h>
11
+#include <time.h>
11 12
 #include "sm_util_types.h"
12 13
 #include "sm_debug.h"
13 14
 
@@ -84,7 +85,7 @@ SmErrorT SmWorkerThread::go()
84 85
     }
85 86
 
86 87
     this->_thread_created = true;
87
-    if( 0 != sem_init(&this->_sem, 0, MAX_QUEUED_ACTIONS) )
88
+    if( 0 != sem_init(&this->_sem, 0, 0) )
88 89
     {
89 90
         DPRINTFE("Cannot init semaphore");
90 91
         return SM_FAILED;
@@ -147,9 +148,22 @@ void SmWorkerThread::add_priority_action(SmAction& action)
147 148
 // ****************************************************************************
148 149
 void SmWorkerThread::thread_run()
149 150
 {
151
+    int wait_interval_ns = 50*1000000; //50 ms
152
+    int ns_to_sec = 1000000000;
153
+    struct timespec wait_time;
154
+
150 155
     while(this->_goon)
151 156
     {
152
-        if(0 == sem_trywait(&this->_sem))
157
+        clock_gettime(CLOCK_REALTIME, &wait_time);
158
+
159
+        wait_time.tv_nsec += wait_interval_ns;
160
+        if(wait_time.tv_nsec > ns_to_sec -1)
161
+        {
162
+            wait_time.tv_nsec -= ns_to_sec;
163
+            wait_time.tv_sec ++;
164
+        }
165
+
166
+        if(0 ==  sem_timedwait(&this->_sem, &wait_time))
153 167
         {
154 168
             SmAction* action = NULL;
155 169
             if(!this->_priority_queue.empty())
@@ -165,7 +179,7 @@ void SmWorkerThread::thread_run()
165 179
             {
166 180
                 action->action();
167 181
             }
168
-        }else if(EAGAIN != errno)
182
+        }else if(ETIMEDOUT != errno)
169 183
         {
170 184
             DPRINTFE("Semaphore wait failed. Error %d", errno);
171 185
         }

+ 0
- 1
service-mgmt/sm-1.0.0/src/sm_worker_thread.h View File

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

Loading…
Cancel
Save