Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <util/signalinterrupt.h>7#ifdef WIN328#include <mutex>9#else10#include <util/tokenpipe.h>11#endif13#include <ios>14#include <optional>16namespace util {18SignalInterrupt::SignalInterrupt() : m_flag{false}19{20#ifndef WIN3221std::optional<TokenPipe> pipe = TokenPipe::Make();22if (!pipe) throw std::ios_base::failure("Could not create TokenPipe");23m_pipe_r = pipe->TakeReadEnd();24m_pipe_w = pipe->TakeWriteEnd();25#endif26}28SignalInterrupt::operator bool() const29{30return m_flag;31}33bool SignalInterrupt::reset()34{35// Cancel existing interrupt by waiting for it, this will reset condition flags and remove36// the token from the pipe.37if (*this && !wait()) return false;38m_flag = false;39return true;40}42bool SignalInterrupt::operator()()43{44#ifdef WIN3245std::unique_lock<std::mutex> lk(m_mutex);46m_flag = true;47m_cv.notify_one();48#else49// This must be reentrant and safe for calling in a signal handler, so using a condition variable is not safe.50// Make sure that the token is only written once even if multiple threads call this concurrently or in51// case of a reentrant signal.52if (!m_flag.exchange(true)) {53// Write an arbitrary byte to the write end of the pipe.54int res = m_pipe_w.TokenWrite('x');55if (res != 0) {56return false;57}58}59#endif60return true;61}63bool SignalInterrupt::wait()64{65#ifdef WIN3266std::unique_lock<std::mutex> lk(m_mutex);67m_cv.wait(lk, [this] { return m_flag.load(); });68#else69int res = m_pipe_r.TokenRead();70if (res != 'x') {71return false;72}73#endif74return true;75}77} // namespace util