8#include <capnp/schema.h>
14#include <kj/string-tree.h>
24#if __has_include(<cxxabi.h>)
37template <
typename... Types>
40 static constexpr size_t size =
sizeof...(Types);
51template <
template <
typename...>
class Class,
typename... Types,
typename... Args>
52Class<Types..., std::remove_reference_t<Args>...>
Make(Args&&...
args)
54 return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(
args)...};
62template <std::
size_t index,
typename List,
typename _First = TypeList<>,
bool done = index == 0>
66template <
typename _Second,
typename _First>
67struct Split<0, _Second, _First, true>
74template <std::size_t index,
typename Type,
typename... _Second,
typename... _First>
78 using First =
typename _Next::First;
79 using Second =
typename _Next::Second;
83template <
typename Callable>
84using ResultOf =
decltype(std::declval<Callable>()());
88using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
95template <
typename SfinaeExpr,
typename Result_>
102template <
typename SfinaeExpr,
typename Result =
void>
113template <
int priority>
134 const char* display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
135 const char* short_name = strchr(display_name,
':');
136 return short_name ? short_name + 1 : display_name;
144 template <
typename... Args>
154#if defined(__clang__) && (!defined(SWIG))
155#define MP_TSA(x) __attribute__((x))
160#define MP_CAPABILITY(x) MP_TSA(capability(x))
161#define MP_SCOPED_CAPABILITY MP_TSA(scoped_lockable)
162#define MP_REQUIRES(x) MP_TSA(requires_capability(x))
163#define MP_ACQUIRE(...) MP_TSA(acquire_capability(__VA_ARGS__))
164#define MP_RELEASE(...) MP_TSA(release_capability(__VA_ARGS__))
165#define MP_ASSERT_CAPABILITY(x) MP_TSA(assert_capability(x))
166#define MP_GUARDED_BY(x) MP_TSA(guarded_by(x))
167#define MP_NO_TSA MP_TSA(no_thread_safety_analysis)
172 void unlock()
MP_RELEASE() { m_mutex.unlock(); }
185 assert(m_lock.mutex() == &mutex.m_mutex);
204template <
typename Lock>
212template <
typename Lock,
typename Callback>
228template <
typename Fn,
typename After>
232 using R = std::invoke_result_t<Fn>;
234 if constexpr (std::is_void_v<R>) {
235 std::forward<Fn>(fn)();
237 std::forward<After>(after)();
240 decltype(
auto) result = std::forward<Fn>(fn)();
242 std::forward<After>(after)();
246 if (!success) std::forward<After>(after)();
256std::string
LogEscape(
const kj::StringTree&
string,
size_t max_size);
259using FdToArgsFn = std::function<std::vector<std::string>(
int fd)>;
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>)
284inline std::string
_demangle(
const char* m)
287 std::unique_ptr<char, void(*)(
void*)> p{
288 abi::__cxa_demangle(m,
nullptr,
nullptr, &status), std::free};
289 return (status == 0 && p) ? p.get() :
m;
301 return "<type information unavailable without rtti>";
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
#define T(expected, seed, data)
#define MP_SCOPED_CAPABILITY
#define MP_ASSERT_CAPABILITY(x)
Functions to serialize / deserialize common bitcoin types.
void Unlock(Lock &lock, Callback &&callback)
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
std::string CxxTypeName(const T &)
const char * TypeName()
Return capnp type name with filename prefix removed.
class MP_CAPABILITY("mutex") Mutex
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.
GuardedRef(Mutex &, U &) -> GuardedRef< U >
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 SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
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...
std::decay_t< T > Decay
Type helper abbreviating std::decay.
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.
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
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.
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.
Generic utility functions used by capnp code.
static constexpr size_t size
Analog to std::lock_guard that unlocks instead of locks.