forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multiprocess: Add basic spawn and IPC support
This commit adds basic process spawning and IPC method call support to `bitcoin-node`, `bitcoin-gui`, and `bitcoin-wallet` executables built with `--enable-multiprocess`. These changes are used in bitcoin#10102 to let node, gui, and wallet functionality run in different processes, and in PRs after that to allow gui and wallet processes to be started and stopped independently and connect to the node over a socket. These changes can also be used to implement new functionality outside the bitcoin-node process like external indexes or pluggable transports (bitcoin#18988). The LocalInit::spawnProcess and IpcProcess::serve methods added here are entry points for spawning a child process and serving a parent process, and being able to make bidirectional, multithreaded method calls between the processes. A simple example of this is implemented in the next commit "Add echoipc RPC method and test."
- Loading branch information
Showing
36 changed files
with
629 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# capnp generated files | ||
*.capnp.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) 2019 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_INTERFACES_CAPNP_INIT_TYPES_H | ||
#define BITCOIN_INTERFACES_CAPNP_INIT_TYPES_H | ||
#endif // BITCOIN_INTERFACES_CAPNP_INIT_TYPES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (c) 2019 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
@0xf2c5cfa319406aa6; | ||
|
||
using Cxx = import "/capnp/c++.capnp"; | ||
$Cxx.namespace("interfaces::capnp::messages"); | ||
|
||
using Proxy = import "/mp/proxy.capnp"; | ||
|
||
interface Init $Proxy.wrap("interfaces::Init") { | ||
construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap :Proxy.ThreadMap); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Copyright (c) 2019 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2019 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_INTERFACES_CAPNP_INIT_H | ||
#define BITCOIN_INTERFACES_CAPNP_INIT_H | ||
|
||
#include <interfaces/init.h> | ||
#include <mp/proxy.h> | ||
|
||
#endif // BITCOIN_INTERFACES_CAPNP_INIT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2019 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 <interfaces/capnp/ipc.h> | ||
|
||
#include <interfaces/base.h> | ||
#include <interfaces/capnp/init.capnp.h> | ||
#include <interfaces/capnp/init.capnp.proxy.h> | ||
#include <interfaces/init.h> | ||
#include <interfaces/ipc.h> | ||
#include <sync.h> | ||
#include <util/memory.h> | ||
#include <util/threadnames.h> | ||
|
||
#include <assert.h> | ||
#include <boost/optional.hpp> | ||
#include <future> | ||
#include <list> | ||
#include <memory> | ||
#include <mp/proxy-io.h> | ||
#include <mp/proxy-types.h> | ||
#include <mp/util.h> | ||
#include <string> | ||
#include <thread> | ||
#include <utility> | ||
|
||
namespace interfaces { | ||
namespace capnp { | ||
namespace { | ||
void IpcLogFn(bool raise, std::string message) | ||
{ | ||
LogPrint(BCLog::IPC, "%s\n", message); | ||
if (raise) throw IpcException(message); | ||
} | ||
|
||
class IpcProtocolImpl : public IpcProtocol | ||
{ | ||
public: | ||
IpcProtocolImpl(const char* exe_name, LocalInit& init) : m_exe_name(exe_name), m_init(init) {} | ||
~IpcProtocolImpl() noexcept(true) | ||
{ | ||
if (m_loop) { | ||
std::unique_lock<std::mutex> lock(m_loop->m_mutex); | ||
m_loop->removeClient(lock); | ||
} | ||
if (m_loop_thread.joinable()) m_loop_thread.join(); | ||
assert(!m_loop); | ||
}; | ||
|
||
std::unique_ptr<interfaces::Init> connect(int fd) override | ||
{ | ||
startLoop(); | ||
return mp::ConnectStream<messages::Init>(*m_loop, fd); | ||
} | ||
void serve(int fd) override | ||
{ | ||
assert(!m_loop); | ||
mp::g_thread_context.thread_name = mp::ThreadName(m_exe_name); | ||
m_loop.emplace(m_exe_name, &IpcLogFn, &m_init); | ||
mp::ServeStream<messages::Init>(*m_loop, fd, m_init); | ||
m_loop->loop(); | ||
m_loop.reset(); | ||
} | ||
|
||
private: | ||
void startLoop() | ||
{ | ||
if (m_loop) return; | ||
std::promise<void> promise; | ||
m_loop_thread = std::thread([&] { | ||
util::ThreadRename("capnp-loop"); | ||
m_loop.emplace(m_exe_name, &IpcLogFn, &m_init); | ||
{ | ||
std::unique_lock<std::mutex> lock(m_loop->m_mutex); | ||
m_loop->addClient(lock); | ||
} | ||
promise.set_value(); | ||
m_loop->loop(); | ||
m_loop.reset(); | ||
}); | ||
promise.get_future().wait(); | ||
} | ||
const char* m_exe_name; | ||
LocalInit& m_init; | ||
std::thread m_loop_thread; | ||
boost::optional<mp::EventLoop> m_loop; | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<IpcProtocol> MakeCapnpProtocol(const char* exe_name, LocalInit& init) | ||
{ | ||
return MakeUnique<IpcProtocolImpl>(exe_name, init); | ||
} | ||
} // namespace capnp | ||
} // namespace interfaces |
Oops, something went wrong.