changeset 278:59c5f8aba0b5

Bug 3764: Agent thread might not be stopped when it works Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/151
author Yasumasa Suenaga <yasuenag@gmail.com>
date Mon, 11 Nov 2019 17:54:33 +0900
parents ebbca28d3efd
children 898008f06beb
files ChangeLog agent/src/heapstats-engines/agentThread.cpp agent/src/heapstats-engines/gcWatcher.cpp agent/src/heapstats-engines/snapShotProcessor.cpp agent/src/heapstats-engines/timer.cpp
diffstat 5 files changed, 22 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Nov 10 00:25:20 2019 +0900
+++ b/ChangeLog	Mon Nov 11 17:54:33 2019 +0900
@@ -1,3 +1,7 @@
+2019-11-11 Yasumasa Suenaga <yasuenag@gmail.com>
+
+	* Bug 3764: Agent thread might not be stopped when it works
+
 2019-11-10 Yasumasa Suenaga <yasuenag@gmail.com>
 
 	* Bug 3765: pthread mutex lock/unlock enhancement
--- a/agent/src/heapstats-engines/agentThread.cpp	Sun Nov 10 00:25:20 2019 +0900
+++ b/agent/src/heapstats-engines/agentThread.cpp	Mon Nov 11 17:54:33 2019 +0900
@@ -19,6 +19,8 @@
  *
  */
 
+#include <sched.h>
+
 #include "globals.hpp"
 #include "util.hpp"
 #include "agentThread.hpp"
@@ -134,7 +136,7 @@
 
   /* SpinLock for AgentThread termination. */
   while (this->_isRunning) {
-    ; /* none. */
+    sched_yield();
   }
 
   /* Clean termination flag. */
--- a/agent/src/heapstats-engines/gcWatcher.cpp	Sun Nov 10 00:25:20 2019 +0900
+++ b/agent/src/heapstats-engines/gcWatcher.cpp	Mon Nov 11 17:54:33 2019 +0900
@@ -64,13 +64,17 @@
   controller->_isRunning = true;
 
   /* Loop for agent run. */
-  while (!controller->_terminateRequest) {
+  while (true) {
     /* Variable for notification flag. */
     bool needProcess = false;
 
     {
       TMutexLocker locker(&controller->mutex);
 
+      if (controller->_terminateRequest) {
+        break;
+      }
+
       /* If no exists request. */
       if (likely(controller->_numRequests == 0)) {
         /* Wait for notification or termination. */
--- a/agent/src/heapstats-engines/snapShotProcessor.cpp	Sun Nov 10 00:25:20 2019 +0900
+++ b/agent/src/heapstats-engines/snapShotProcessor.cpp	Mon Nov 11 17:54:33 2019 +0900
@@ -71,7 +71,7 @@
 
   bool existRemainder = false;
   /* Loop for agent run or remaining work exist. */
-  while (!controller->_terminateRequest || existRemainder) {
+  while (true) {
     TSnapShotContainer *snapshot = NULL;
     /* Is notify flag. */
     bool needProcess = false;
@@ -79,6 +79,10 @@
     {
       TMutexLocker locker(&controller->mutex);
 
+      if (unlikely(controller->_terminateRequest && !existRemainder)) {
+        break;
+      }
+
       if (likely(controller->_numRequests == 0)) {
         /* Wait for notification or termination. */
         pthread_cond_wait(&controller->mutexCond, &controller->mutex);
--- a/agent/src/heapstats-engines/timer.cpp	Sun Nov 10 00:25:20 2019 +0900
+++ b/agent/src/heapstats-engines/timer.cpp	Mon Nov 11 17:54:33 2019 +0900
@@ -72,13 +72,17 @@
   controller->_isRunning = true;
 
   /* Loop for agent run. */
-  while (!controller->_terminateRequest) {
+  while (true) {
     /* Reset timer interrupt flag. */
     controller->_isInterrupted = false;
 
     {
       TMutexLocker locker(&controller->mutex);
 
+      if (unlikely(controller->_terminateRequest)) {
+        break;
+      }
+
       /* Create limit datetime. */
       struct timespec limitTs = {0};
       struct timeval nowTv = {0};