0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-15 11:36:00 -05:00
bitcoin-bitcoin-core/src/util/signalinterrupt.cpp
TheCharlatan e2d680a32d
util: Add SignalInterrupt class and use in shutdown.cpp
This change helps generalize shutdown code so an interrupt can be
provided to libbitcoinkernel callers. This may also be useful to
eventually de-globalize all of the shutdown code.

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Co-authored-by: TheCharlatan <seb.kung@gmail.com>
2023-06-28 09:49:28 +02:00

74 lines
1.8 KiB
C++

// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <util/signalinterrupt.h>
#ifdef WIN32
#include <mutex>
#else
#include <util/tokenpipe.h>
#endif
#include <ios>
#include <optional>
namespace util {
SignalInterrupt::SignalInterrupt() : m_flag{false}
{
#ifndef WIN32
std::optional<TokenPipe> pipe = TokenPipe::Make();
if (!pipe) throw std::ios_base::failure("Could not create TokenPipe");
m_pipe_r = pipe->TakeReadEnd();
m_pipe_w = pipe->TakeWriteEnd();
#endif
}
SignalInterrupt::operator bool() const
{
return m_flag;
}
void SignalInterrupt::reset()
{
// Cancel existing interrupt by waiting for it, this will reset condition flags and remove
// the token from the pipe.
if (*this) wait();
m_flag = false;
}
void SignalInterrupt::operator()()
{
#ifdef WIN32
std::unique_lock<std::mutex> lk(m_mutex);
m_flag = true;
m_cv.notify_one();
#else
// This must be reentrant and safe for calling in a signal handler, so using a condition variable is not safe.
// Make sure that the token is only written once even if multiple threads call this concurrently or in
// case of a reentrant signal.
if (!m_flag.exchange(true)) {
// Write an arbitrary byte to the write end of the pipe.
int res = m_pipe_w.TokenWrite('x');
if (res != 0) {
throw std::ios_base::failure("Could not write interrupt token");
}
}
#endif
}
void SignalInterrupt::wait()
{
#ifdef WIN32
std::unique_lock<std::mutex> lk(m_mutex);
m_cv.wait(lk, [this] { return m_flag.load(); });
#else
int res = m_pipe_r.TokenRead();
if (res != 'x') {
throw std::ios_base::failure("Did not read expected interrupt token");
}
#endif
}
} // namespace util