Changeset View
Changeset View
Standalone View
Standalone View
src/util/time.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/time.h> | |||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <compat.h> | #include <compat.h> | ||||
#include <util/time.h> | #include <util/check.h> | ||||
#include <boost/date_time/posix_time/posix_time.hpp> | #include <boost/date_time/posix_time/posix_time.hpp> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <atomic> | #include <atomic> | ||||
#include <ctime> | #include <ctime> | ||||
#include <thread> | #include <thread> | ||||
void UninterruptibleSleep(const std::chrono::microseconds &n) { | void UninterruptibleSleep(const std::chrono::microseconds &n) { | ||||
std::this_thread::sleep_for(n); | std::this_thread::sleep_for(n); | ||||
} | } | ||||
//! For unit testing | //! For testing | ||||
static std::atomic<int64_t> nMockTime(0); | static std::atomic<int64_t> nMockTime(0); | ||||
int64_t GetTime() { | int64_t GetTime() { | ||||
int64_t mocktime = nMockTime.load(std::memory_order_relaxed); | int64_t mocktime = nMockTime.load(std::memory_order_relaxed); | ||||
if (mocktime) { | if (mocktime) { | ||||
return mocktime; | return mocktime; | ||||
} | } | ||||
Show All 10 Lines | return std::chrono::duration_cast<T>( | ||||
mocktime.count() ? mocktime | mocktime.count() ? mocktime | ||||
: std::chrono::microseconds{GetTimeMicros()}); | : std::chrono::microseconds{GetTimeMicros()}); | ||||
} | } | ||||
template std::chrono::seconds GetTime(); | template std::chrono::seconds GetTime(); | ||||
template std::chrono::milliseconds GetTime(); | template std::chrono::milliseconds GetTime(); | ||||
template std::chrono::microseconds GetTime(); | template std::chrono::microseconds GetTime(); | ||||
void SetMockTime(int64_t nMockTimeIn) { | void SetMockTime(int64_t nMockTimeIn) { | ||||
assert(nMockTimeIn >= 0); | Assert(nMockTimeIn >= 0); | ||||
nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetMockTime() { | int64_t GetMockTime() { | ||||
return nMockTime.load(std::memory_order_relaxed); | return nMockTime.load(std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetTimeMillis() { | int64_t GetTimeMillis() { | ||||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |