# HG changeset patch # User Yasumasa Suenaga # Date 1573462525 -32400 # Node ID 7649620bcf099e1b8720850988575fa788f3a7ab # Parent b7dba538ad10b2dca65080268f16a26448a70cd1 Bug 3764: Agent thread might not be stopped when it works Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/151 diff -r b7dba538ad10 -r 7649620bcf09 ChangeLog --- a/ChangeLog Sun Nov 10 00:26:07 2019 +0900 +++ b/ChangeLog Mon Nov 11 17:55:25 2019 +0900 @@ -1,4 +1,8 @@ -2018-11-10 Yasumasa Suenaga +2019-11-11 Yasumasa Suenaga + + * Bug 3764: Agent thread might not be stopped when it works + +2019-11-10 Yasumasa Suenaga * Bug 3765: pthread mutex lock/unlock enhancement diff -r b7dba538ad10 -r 7649620bcf09 agent/src/heapstats-engines/agentThread.cpp --- a/agent/src/heapstats-engines/agentThread.cpp Sun Nov 10 00:26:07 2019 +0900 +++ b/agent/src/heapstats-engines/agentThread.cpp Mon Nov 11 17:55:25 2019 +0900 @@ -19,6 +19,8 @@ * */ +#include + #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. */ diff -r b7dba538ad10 -r 7649620bcf09 agent/src/heapstats-engines/gcWatcher.cpp --- a/agent/src/heapstats-engines/gcWatcher.cpp Sun Nov 10 00:26:07 2019 +0900 +++ b/agent/src/heapstats-engines/gcWatcher.cpp Mon Nov 11 17:55:25 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. */ diff -r b7dba538ad10 -r 7649620bcf09 agent/src/heapstats-engines/snapShotProcessor.cpp --- a/agent/src/heapstats-engines/snapShotProcessor.cpp Sun Nov 10 00:26:07 2019 +0900 +++ b/agent/src/heapstats-engines/snapShotProcessor.cpp Mon Nov 11 17:55:25 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); diff -r b7dba538ad10 -r 7649620bcf09 agent/src/heapstats-engines/timer.cpp --- a/agent/src/heapstats-engines/timer.cpp Sun Nov 10 00:26:07 2019 +0900 +++ b/agent/src/heapstats-engines/timer.cpp Mon Nov 11 17:55:25 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};