2019-12-31 18:35:41 +01:00
|
|
|
// Copyright (c) 2016-2019 The Bitcoin Core developers
|
2017-08-09 18:06:31 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_THREADINTERRUPT_H
|
|
|
|
#define BITCOIN_THREADINTERRUPT_H
|
|
|
|
|
2017-11-08 21:28:35 +01:00
|
|
|
#include <sync.h>
|
|
|
|
|
2017-08-09 18:06:31 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
|
|
/*
|
|
|
|
A helper class for interruptible sleeps. Calling operator() will interrupt
|
|
|
|
any current sleep, and after that point operator bool() will return true
|
|
|
|
until reset.
|
|
|
|
*/
|
|
|
|
class CThreadInterrupt
|
|
|
|
{
|
|
|
|
public:
|
2022-07-26 16:02:13 +02:00
|
|
|
using Clock = std::chrono::steady_clock;
|
2021-05-25 12:48:04 +02:00
|
|
|
CThreadInterrupt();
|
2017-08-09 18:06:31 +02:00
|
|
|
explicit operator bool() const;
|
|
|
|
void operator()();
|
|
|
|
void reset();
|
2022-07-26 16:02:13 +02:00
|
|
|
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
|
2017-08-09 18:06:31 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::condition_variable cond;
|
2021-06-04 21:26:33 +02:00
|
|
|
Mutex mut;
|
2017-08-09 18:06:31 +02:00
|
|
|
std::atomic<bool> flag;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //BITCOIN_THREADINTERRUPT_H
|