From 01fdc6ee1ea93abababc3435775db818e507fd0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= Date: Sat, 18 May 2019 18:02:51 +0300 Subject: [PATCH] Fixed|libcore: Minor data races related to timers --- doomsday/libs/core/src/core/timer.cpp | 3 ++- doomsday/libs/core/src/data/time.cpp | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doomsday/libs/core/src/core/timer.cpp b/doomsday/libs/core/src/core/timer.cpp index 125b7e8f56..3cd13cb452 100644 --- a/doomsday/libs/core/src/core/timer.cpp +++ b/doomsday/libs/core/src/core/timer.cpp @@ -102,6 +102,7 @@ struct TimerScheduler : public Thread, public Lockable void addPending(Timer &timer) { + DE_GUARD(this); const TimeSpan repeatDuration = timer.interval(); pending.push( Pending{sc::system_clock::now() + sc::microseconds(dint64(repeatDuration * 1.0e6)), @@ -145,7 +146,7 @@ DE_PIMPL_NOREF(Timer) TimeSpan interval = 1.0; bool isSingleShot = false; bool isActive = false; - bool isPending = false; // posted but not executed yet + std::atomic_bool isPending{false}; // posted but not executed yet ~Impl() { diff --git a/doomsday/libs/core/src/data/time.cpp b/doomsday/libs/core/src/data/time.cpp index a61f227276..5a055836c7 100644 --- a/doomsday/libs/core/src/data/time.cpp +++ b/doomsday/libs/core/src/data/time.cpp @@ -26,6 +26,7 @@ #include "de/Thread" #include "de/Writer" +#include #include #include @@ -37,7 +38,7 @@ static HighPerformanceTimer &highPerfTimer() return hpt; } -static TimeSpan currentHighPerfDelta; +static std::atomic currentHighPerfDelta; duint64 Time::Span::asMicroSeconds() const { @@ -741,17 +742,17 @@ TimeSpan Time::highPerformanceTime() const Time Time::currentHighPerformanceTime() // static { - return fromHighPerformanceDelta(currentHighPerfDelta); + return fromHighPerformanceDelta(currentHighPerfDelta.load()); } void Time::updateCurrentHighPerformanceTime() // static { - currentHighPerfDelta = highPerfTimer().elapsed(); + currentHighPerfDelta.store(highPerfTimer().elapsed()); } Time::Span Time::currentHighPerformanceDelta() { - return currentHighPerfDelta; + return currentHighPerfDelta.load(); } std::ostream &operator<<(std::ostream &os, Time const &t)