Skip to content

Commit

Permalink
fix #6102
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed Dec 16, 2018
1 parent 9ccb433 commit 19e2041
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
5 changes: 3 additions & 2 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Expand Up @@ -2892,14 +2892,15 @@ int LuaUnsyncedCtrl::SetLogSectionFilterLevel(lua_State* L) {

int LuaUnsyncedCtrl::ClearWatchDogTimer(lua_State* L) {
if (lua_gettop(L) == 0) {
// clear for current thread
Watchdog::ClearTimer();
return 0;
}

if (lua_isstring(L, 1)) {
Watchdog::ClearTimer(lua_tostring(L, 1));
Watchdog::ClearTimer(lua_tostring(L, 1), luaL_optboolean(L, 2, false));
} else {
Watchdog::ClearTimer("main");
Watchdog::ClearTimer("main", luaL_optboolean(L, 2, false));
}

return 0;
Expand Down
33 changes: 16 additions & 17 deletions rts/System/Platform/Watchdog.cpp
Expand Up @@ -17,7 +17,9 @@
#include "System/Platform/CrashHandler.h"
#include "System/Platform/Misc.h"
#include "System/Platform/Threading.h"
#include "System/StringHash.h"
#include "System/Threading/SpringThreading.h"
#include "System/UnorderedMap.hpp"

CONFIG(int, HangTimeout).defaultValue(10).minimumValue(-1).maximumValue(600)
.description("Number of seconds that, if spent in the same code segment, indicate a hang; -1 to disable.");
Expand Down Expand Up @@ -68,14 +70,9 @@ namespace Watchdog
};

struct WatchDogThreadSlot {
WatchDogThreadSlot()
: primary(false)
, active(false)
, regorder(0)
{}
std::atomic<bool> primary;
std::atomic<bool> active;
std::atomic<unsigned int> regorder;
std::atomic<bool> primary = {false};
std::atomic<bool> active = {false};
std::atomic<unsigned int> regorder = {0};
};

// NOTE:
Expand All @@ -85,7 +82,8 @@ namespace Watchdog
static WatchDogThreadInfo* registeredThreads[WDT_COUNT + 1] = {nullptr};
static WatchDogThreadSlot threadSlots[WDT_COUNT + 1];

static std::map<std::string, unsigned int> threadNameToNum;
// maps hash(name) to WTD_*
static spring::unsynced_map<unsigned int, unsigned int> threadNumTable;

static spring::thread hangDetectorThread;
static std::atomic<bool> hangDetectorThreadInterrupted = {false};
Expand Down Expand Up @@ -279,7 +277,7 @@ namespace Watchdog
}


void ClearTimer(bool disable, Threading::NativeThreadId* _threadId)
void ClearTimer(Threading::NativeThreadId* _threadId, bool disable)
{
// bail if Watchdog isn't running
if (!hangDetectorThread.joinable())
Expand All @@ -292,7 +290,7 @@ namespace Watchdog

Threading::NativeThreadId threadId;

if (_threadId) {
if (_threadId != nullptr) {
threadId = *_threadId;
} else {
threadId = Threading::GetCurrentThreadId();
Expand All @@ -312,6 +310,7 @@ namespace Watchdog
return;
}

// notime always satisfies !spring_istime
threadInfo->timer = disable ? spring_notime : spring_gettime();
}

Expand All @@ -333,19 +332,19 @@ namespace Watchdog
threadInfo->timer = disable ? spring_notime : spring_gettime();
}

void ClearTimer(const std::string& name, bool disable)
void ClearTimer(const char* name, bool disable)
{
if (!hangDetectorThread.joinable())
return;
if (Threading::IsWatchDogThread())
return;

const auto i = threadNameToNum.find(name);
const auto i = threadNumTable.find(hashString(name));
unsigned int num;
WatchDogThreadInfo* threadInfo;

if (i == threadNameToNum.end() || (num = i->second) >= WDT_COUNT || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s(name)] Invalid thread name \"%s\"", __func__, name.c_str());
if (i == threadNumTable.end() || (num = i->second) >= WDT_COUNT || (threadInfo = registeredThreads[num])->numreg == 0) {
LOG_L(L_ERROR, "[Watchdog::%s(name)] Invalid thread name \"%s\"", __func__, name);
return;
}

Expand All @@ -372,7 +371,7 @@ namespace Watchdog
memset(registeredThreadsData, 0, sizeof(registeredThreadsData));
for (unsigned int i = 0; i < WDT_COUNT; ++i) {
registeredThreads[i] = &registeredThreadsData[WDT_COUNT];
threadNameToNum[std::string(threadNames[i])] = i;
threadNumTable[hashString(threadNames[i])] = i;
}
memset(threadSlots, 0, sizeof(threadSlots));

Expand Down Expand Up @@ -423,6 +422,6 @@ namespace Watchdog
for (unsigned int i = 0; i < WDT_COUNT; ++i)
registeredThreads[i] = &registeredThreadsData[WDT_COUNT];
memset(threadSlots, 0, sizeof(threadSlots));
threadNameToNum.clear();
threadNumTable.clear();
}
}
7 changes: 2 additions & 5 deletions rts/System/Platform/Watchdog.h
Expand Up @@ -22,12 +22,9 @@ namespace Watchdog
void Uninstall();

//! Call this to reset the watchdog timer of the current thread
void ClearTimer(bool disable = false, Threading::NativeThreadId* _threadId = nullptr);
void ClearTimer(Threading::NativeThreadId* _threadId = nullptr, bool disable = false);
void ClearTimer(const WatchdogThreadnum num, bool disable = false);
void ClearTimer(const std::string& name, bool disable = false);
inline void ClearTimer(const char* name, bool disable = false) {
ClearTimer(std::string(name), disable);
}
void ClearTimer(const char* name, bool disable = false);
void ClearPrimaryTimers(bool disable = false);

//! Call these in the threads you want to monitor
Expand Down

0 comments on commit 19e2041

Please sign in to comment.