Bitcoin ABC 0.30.7
P2P Digital Currency
threadinterrupt.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <threadinterrupt.h>
7
9
10CThreadInterrupt::operator bool() const {
11 return flag.load(std::memory_order_acquire);
12}
13
15 flag.store(false, std::memory_order_release);
16}
17
19 {
20 LOCK(mut);
21 flag.store(true, std::memory_order_release);
22 }
23 cond.notify_all();
24}
25
26bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time) {
27 WAIT_LOCK(mut, lock);
28 return !cond.wait_for(lock, rel_time, [this]() {
29 return flag.load(std::memory_order_acquire);
30 });
31}
32
33bool CThreadInterrupt::sleep_for(std::chrono::seconds rel_time) {
34 return sleep_for(
35 std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
36}
37
38bool CThreadInterrupt::sleep_for(std::chrono::minutes rel_time) {
39 return sleep_for(
40 std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
41}
void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut)
std::atomic< bool > flag
bool sleep_for(std::chrono::milliseconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut)
std::condition_variable cond
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define LOCK(cs)
Definition: sync.h:306