9#include <capnp/schema.h>
15#include <kj/async-io.h>
17#include <kj/string-tree.h>
27#if __has_include(<cxxabi.h>)
38template <
typename A,
typename B>
41 if constexpr (std::is_floating_point_v<A> || std::is_floating_point_v<B>)
return a <= b;
42 else return std::cmp_less_equal(
a, b);
44template <
typename A,
typename B>
47 if constexpr (std::is_floating_point_v<A> || std::is_floating_point_v<B>)
return a >= b;
48 else return std::cmp_greater_equal(
a, b);
55template <
typename... Types>
58 static constexpr size_t size =
sizeof...(Types);
69template <
template <
typename...>
class Class,
typename... Types,
typename... Args>
70Class<Types..., std::remove_reference_t<Args>...>
Make(Args&&...
args)
72 return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(
args)...};
80template <std::
size_t index,
typename List,
typename _First = TypeList<>,
bool done = index == 0>
84template <
typename _Second,
typename _First>
92template <std::size_t index,
typename Type,
typename...
_Second,
typename...
_First>
96 using First =
typename _Next::First;
97 using Second =
typename _Next::Second;
101template <
typename Callable>
102using ResultOf =
decltype(std::declval<Callable>()());
114template <
typename T,
typename U>
115concept FieldTypeIs = std::is_same_v<decltype(std::declval<T>().get()),
U>;
123template <
typename SfinaeExpr,
typename Result_>
130template <
typename SfinaeExpr,
typename Result =
void>
141template <
int priority>
162 const char*
display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
172 template <
typename... Args>
185#if defined(__clang__) && (!defined(SWIG))
186#define MP_TSA(x) __attribute__((x))
191#define MP_CAPABILITY(x) MP_TSA(capability(x))
192#define MP_SCOPED_CAPABILITY MP_TSA(scoped_lockable)
193#define MP_REQUIRES(x) MP_TSA(requires_capability(x))
194#define MP_ACQUIRE(...) MP_TSA(acquire_capability(__VA_ARGS__))
195#define MP_RELEASE(...) MP_TSA(release_capability(__VA_ARGS__))
196#define MP_ASSERT_CAPABILITY(x) MP_TSA(assert_capability(x))
197#define MP_GUARDED_BY(x) MP_TSA(guarded_by(x))
198#define MP_NO_TSA MP_TSA(no_thread_safety_analysis)
203 void unlock()
MP_RELEASE() { m_mutex.unlock(); }
216 assert(m_lock.mutex() == &mutex.m_mutex);
235template <
typename Lock>
243template <
typename Lock,
typename Callback>
259template <
typename Fn,
typename After>
263 using R = std::invoke_result_t<Fn>;
265 if constexpr (std::is_void_v<R>) {
266 std::forward<Fn>(fn)();
268 std::forward<After>(
after)();
271 decltype(
auto) result = std::forward<Fn>(fn)();
273 std::forward<After>(
after)();
277 if (!success) std::forward<After>(
after)();
290std::string
LogEscape(
const kj::StringTree&
string,
size_t max_size);
292using Stream = kj::Own<kj::AsyncIoStream>;
329inline const char*
CharCast(
const unsigned char*
c) {
return (
const char*)
c; }
331#if __has_include(<cxxabi.h>)
336 abi::__cxa_demangle(
m,
nullptr,
nullptr, &status), std::free};
337 return (status == 0 &&
p) ?
p.get() :
m;
349 return "<type information unavailable without rtti>";
void lock() EXCLUSIVE_LOCK_FUNCTION()
Helper class that detects when a promise is canceled.
void promiseDestroyed(CancelProbe &probe)
std::function< void()> m_on_cancel
Helper object to attach to a promise and update a CancelMonitor.
CancelMonitor * m_monitor
CancelProbe(CancelMonitor &monitor)
void assert_locked(Mutex &mutex) MP_ASSERT_CAPABILITY() MP_ASSERT_CAPABILITY(mutex)
void unlock() MP_RELEASE()
Lock(Mutex &m) MP_ACQUIRE(m)
~Lock() MP_RELEASE()=default
std::unique_lock< std::mutex > m_lock
Concept satisfied when T's .get() method returns exactly type U.
Concept satisfied when T's .get() method returns a capnp interface type (i.e., a type that exposes a ...
#define T(expected, seed, data)
#define MP_SCOPED_CAPABILITY
#define MP_ASSERT_CAPABILITY(x)
Functions to serialize / deserialize common bitcoin types.
constexpr bool safe_less_equal(A a, B b)
Generic utility functions used by capnp code.
void Unlock(Lock &lock, Callback &&callback)
std::string CxxTypeName(const T &)
const char * TypeName()
Return capnp type name with filename prefix removed.
std::string _demangle(const char *m)
decltype(auto) TryFinally(Fn &&fn, After &&after)
Invoke a function and run a follow-up action before returning the original result.
void SetOsThreadName(const char *name)
Set the OS-level name of the current thread.
ProcessId StartProcess(const std::vector< std::string > &args)
Start a process and return its process id.
kj::Own< kj::AsyncIoStream > Stream
SocketId StartSpawned(const SpawnConnectInfo &connect_info)
Initialize spawned child process using the SpawnConnectInfo string passed to it, returning a socket i...
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
typename _Require< SfinaeExpr, Result >::Result Require
SFINAE helper, basically the same as to C++17's void_t, but allowing types other than void to be retu...
int WaitProcess(ProcessId pid)
Wait for a process to exit and return its exit code.
Class< Types..., std::remove_reference_t< Args >... > Make(Args &&... args)
Construct a template class value by deducing template arguments from the types of constructor argumen...
constexpr bool safe_greater_equal(A a, B b)
std::tuple< ProcessId, SocketId > SpawnProcess(SpawnConnectInfoToArgsFn &&connect_info_to_args)
Spawn a new process that communicates with the current process over a socket pair.
std::decay_t< T > Decay
Type helper abbreviating std::decay.
std::array< SocketId, 2 > SocketPair()
Create a socket pair that can be used to communicate within a process or between parent and child pro...
std::remove_cv_t< std::remove_reference_t< T > > RemoveCvRef
Substitutue for std::remove_cvref_t.
decltype(std::declval< Callable >()()) ResultOf
Type helper giving return type of a callable type.
constexpr SocketId SocketError
std::string SpawnConnectInfo
Information about parent process passed to child process as a command-line argument.
std::string LogEscape(const kj::StringTree &string, size_t max_size)
Escape binary string for use in log so it doesn't trigger unicode decode errors in python unit tests.
std::function< std::vector< std::string >(const SpawnConnectInfo &)> SpawnConnectInfoToArgsFn
Callback type used by SpawnProcess below.
SFINAE helper, see using Require below.
T &ref MP_GUARDED_BY(mutex)
Exception thrown from code executing an IPC call that is interrupted.
const char * what() const noexcept override
InterruptException(std::string message)
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Convenient wrapper around std::variant<T*, T>
std::variant< T *, T > data
PtrOrValue(T *ptr, Args &&... args)
typename _Next::Second Second
typename _Next::First First
Type helper splitting a TypeList into two halves at position index.
Type holding a list of types.
static constexpr size_t size
Analog to std::lock_guard that unlocks instead of locks.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.