Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
251//! Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".252std::string ThreadName(const char* exe_name);254//! Escape binary string for use in log so it doesn't trigger unicode decode255//! errors in python unit tests.256std::string LogEscape(const kj::StringTree& string, size_t max_size);258//! Callback type used by SpawnProcess below.259using FdToArgsFn = std::function<std::vector<std::string>(int fd)>;261//! Spawn a new process that communicates with the current process over a socket262//! pair. Returns pid through an output argument, and file descriptor for the263//! local side of the socket.264//! The fd_to_args callback is invoked in the parent process before fork().265//! It must not rely on child pid/state, and must return the command line266//! arguments that should be used to execute the process. Embed the remote file267//! descriptor number in whatever format the child process expects.268int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args);270//! Call execvp with vector args.271//! Not safe to call in a post-fork child of a multi-threaded process.272//! Currently only used by mpgen at build time.273void ExecProcess(const std::vector<std::string>& args);275//! Wait for a process to exit and return its exit code.276int WaitProcess(int pid);278inline char* CharCast(char* c) { return c; }279inline char* CharCast(unsigned char* c) { return (char*)c; }280inline const char* CharCast(const char* c) { return c; }281inline const char* CharCast(const unsigned char* c) { return (const char*)c; }283#if __has_include(<cxxabi.h>) // GCC & Clang ─ use <cxxabi.h> to demangle284inline std::string _demangle(const char* m)285{286int status = 0;287std::unique_ptr<char, void(*)(void*)> p{288abi::__cxa_demangle(m, /*output_buffer=*/nullptr, /*length=*/nullptr, &status), std::free};289return (status == 0 && p) ? p.get() : m; // fall back on mangled if needed290}291#else // MSVC or other ─ no demangling available292inline std::string _demangle(const char* m) { return m; }293#endif295template<class T>296std::string CxxTypeName(const T& /*unused*/)297{298#ifdef __cpp_rtti299return _demangle(typeid(std::decay_t<T>).name());300#else301return "<type information unavailable without rtti>";302#endif303}305//! Exception thrown from code executing an IPC call that is interrupted.306struct InterruptException final : std::exception {307explicit InterruptException(std::string message) : m_message(std::move(message)) {}308const char* what() const noexcept override { return m_message.c_str(); }309std::string m_message;310};312class CancelProbe;314//! Helper class that detects when a promise is canceled. Used to detect315//! canceled requests and prevent potential crashes on unclean disconnects.316//!317//! In the future, this could also be used to support a way for wrapped C++318//! methods to detect cancellation (like approach #4 in319//! https://github.com/bitcoin/bitcoin/issues/33575).320class CancelMonitor321{322public:323inline ~CancelMonitor();324inline void promiseDestroyed(CancelProbe& probe);326bool m_canceled{false};327std::function<void()> m_on_cancel;328CancelProbe* m_probe{nullptr};329};331//! Helper object to attach to a promise and update a CancelMonitor.332class CancelProbe333{334public:335CancelProbe(CancelMonitor& monitor) : m_monitor(&monitor)336{337assert(!monitor.m_probe);338monitor.m_probe = this;339}340~CancelProbe()341{342if (m_monitor) m_monitor->promiseDestroyed(*this);343}344CancelMonitor* m_monitor;345};347CancelMonitor::~CancelMonitor()348{349if (m_probe) {350assert(m_probe->m_monitor == this);351m_probe->m_monitor = nullptr;352m_probe = nullptr;353}354}356void CancelMonitor::promiseDestroyed(CancelProbe& probe)357{358// If promise is being destroyed, assume the promise has been canceled. In359// theory this method could be called when a promise was fulfilled or360// rejected rather than canceled, but it's safe to assume that's not the361// case because the CancelMonitor class is meant to be used inside code362// fulfilling or rejecting the promise and destroyed before doing so.363assert(m_probe == &probe);364m_canceled = true;365if (m_on_cancel) m_on_cancel();366m_probe = nullptr;367}368} // namespace mp370#endif // MP_UTIL_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.