mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
fix tsan: utiltime race on nMockTime
This commit is contained in:
parent
321bbc2079
commit
1d31093d4d
@ -9,14 +9,17 @@
|
||||
|
||||
#include "utiltime.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
static int64_t nMockTime = 0; //!< For unit testing
|
||||
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
|
||||
|
||||
int64_t GetTime()
|
||||
{
|
||||
if (nMockTime) return nMockTime;
|
||||
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
|
||||
if (mocktime) return mocktime;
|
||||
|
||||
time_t now = time(NULL);
|
||||
assert(now > 0);
|
||||
@ -25,7 +28,7 @@ int64_t GetTime()
|
||||
|
||||
void SetMockTime(int64_t nMockTimeIn)
|
||||
{
|
||||
nMockTime = nMockTimeIn;
|
||||
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
int64_t GetTimeMillis()
|
||||
@ -52,7 +55,8 @@ int64_t GetSystemTimeInSeconds()
|
||||
/** Return a time useful for the debug log */
|
||||
int64_t GetLogTimeMicros()
|
||||
{
|
||||
if (nMockTime) return nMockTime*1000000;
|
||||
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
|
||||
if (mocktime) return mocktime*1000000;
|
||||
|
||||
return GetTimeMicros();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user