2018-12-29 10:08:32 +01:00
|
|
|
// Copyright (c) 2016-2018 The Bitcoin Core developers
|
2016-12-27 23:12:15 +01: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 23:07:40 +01:00
|
|
|
#include <sync.h>
|
|
|
|
|
2016-12-27 23:12:15 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
/*
|
|
|
|
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:
|
2017-12-08 19:52:42 +01:00
|
|
|
CThreadInterrupt();
|
2016-12-27 23:12:15 +01:00
|
|
|
explicit operator bool() const;
|
|
|
|
void operator()();
|
|
|
|
void reset();
|
|
|
|
bool sleep_for(std::chrono::milliseconds rel_time);
|
|
|
|
bool sleep_for(std::chrono::seconds rel_time);
|
|
|
|
bool sleep_for(std::chrono::minutes rel_time);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::condition_variable cond;
|
2017-11-03 12:49:16 +01:00
|
|
|
Mutex mut;
|
2016-12-27 23:12:15 +01:00
|
|
|
std::atomic<bool> flag;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //BITCOIN_THREADINTERRUPT_H
|