mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-13 11:25:02 -05:00
![Ryan Ofsky](/assets/img/avatar_default.png)
Replace exceptions thrown by signal and wait methods with [[nodiscard]] return values. This is mostly a refactoring, but there is a slight change of behavior if AbortShutdown function fails. The original behavior which was unintentionally changed in #27861 is restored, so it now triggers an assert failure again instead of throwing an exception. (The AbortShutdown function is only ever called in the the GUI version of Bitcoin Core when corruption is detected on loading and the user tries to reindex.) Problems with using exceptions were pointed out by MarcoFalke in https://github.com/bitcoin/bitcoin/pull/27861#discussion_r1255496707.
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
// Copyright (c) 2023 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_UTIL_SIGNALINTERRUPT_H
|
|
#define BITCOIN_UTIL_SIGNALINTERRUPT_H
|
|
|
|
#ifdef WIN32
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#else
|
|
#include <util/tokenpipe.h>
|
|
#endif
|
|
|
|
#include <atomic>
|
|
#include <cstdlib>
|
|
|
|
namespace util {
|
|
/**
|
|
* Helper class that manages an interrupt flag, and allows a thread or
|
|
* signal to interrupt another thread.
|
|
*
|
|
* This class is safe to be used in a signal handler. If sending an interrupt
|
|
* from a signal handler is not necessary, the more lightweight \ref
|
|
* CThreadInterrupt class can be used instead.
|
|
*/
|
|
|
|
class SignalInterrupt
|
|
{
|
|
public:
|
|
SignalInterrupt();
|
|
explicit operator bool() const;
|
|
[[nodiscard]] bool operator()();
|
|
[[nodiscard]] bool reset();
|
|
[[nodiscard]] bool wait();
|
|
|
|
private:
|
|
std::atomic<bool> m_flag;
|
|
|
|
#ifndef WIN32
|
|
// On UNIX-like operating systems use the self-pipe trick.
|
|
TokenPipeEnd m_pipe_r;
|
|
TokenPipeEnd m_pipe_w;
|
|
#else
|
|
// On windows use a condition variable, since we don't have any signals there
|
|
std::mutex m_mutex;
|
|
std::condition_variable m_cv;
|
|
#endif
|
|
};
|
|
} // namespace util
|
|
|
|
#endif // BITCOIN_UTIL_SIGNALINTERRUPT_H
|