Commit 8cb235bf authored by Christian Speckner's avatar Christian Speckner
Browse files

Hack around race condition in timers.

parent 9b22c6af
......@@ -67,6 +67,7 @@
"unordered_map": "cpp",
"istream": "cpp",
"thread": "cpp",
"utility": "cpp"
"utility": "cpp",
"streambuf": "cpp"
}
}
......@@ -43,7 +43,8 @@ StaggeredLogger::StaggeredLogger(const string& message, Logger logger)
myMaxIntervalFactor(9),
myCurrentIntervalFactor(1),
myCooldownTime(1000),
myTimer(new TimerManager())
myTimer(new TimerManager()),
myTimerCallbackId(0)
{
if (logger) myLogger = logger;
}
......@@ -148,14 +149,16 @@ void StaggeredLogger::startInterval()
myLastIntervalStartTimestamp = now;
myTimer->clear(myTimerId);
myTimerId = myTimer->setTimeout(std::bind(&StaggeredLogger::onTimerExpired, this), myCurrentIntervalSize);
myTimerId = myTimer->setTimeout(std::bind(&StaggeredLogger::onTimerExpired, this, ++myTimerCallbackId), myCurrentIntervalSize);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void StaggeredLogger::onTimerExpired()
void StaggeredLogger::onTimerExpired(uInt32 timerCallbackId)
{
std::lock_guard<std::mutex> lock(myMutex);
if (timerCallbackId != myTimerCallbackId) return;
logLine();
myIsCurrentlyCollecting = false;
......
......@@ -52,7 +52,7 @@ class StaggeredLogger {
void _setLogger(Logger logger);
void onTimerExpired();
void onTimerExpired(uInt32 timerId);
void startInterval();
......@@ -83,6 +83,11 @@ class StaggeredLogger {
// our destructor.
TimerManager *myTimer;
TimerManager::TimerId myTimerId;
// It is possible that the timer callback is running even after TimerManager::clear
// returns. This id is unique per timer and is used to return from the callback
// early in case the time is stale.
uInt32 myTimerCallbackId;
};
#endif // STAGGERED_LOGGER
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment