8 #include <capnp/schema.h> 14 #include <kj/string-tree.h> 18 #include <type_traits> 31 template <
typename... Types>
34 static constexpr
size_t size =
sizeof...(Types);
45 template <
template <
typename...>
class Class,
typename... Types,
typename... Args>
46 Class<Types..., std::remove_reference_t<Args>...>
Make(Args&&...
args)
48 return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(
args)...};
56 template <std::
size_t index,
typename List,
typename _First = TypeList<>,
bool done = index == 0>
60 template <
typename _Second,
typename _First>
61 struct Split<0, _Second, _First, true>
68 template <std::size_t index,
typename Type,
typename... _Second,
typename... _First>
72 using First =
typename _Next::First;
73 using Second =
typename _Next::Second;
77 template <
typename Callable>
78 using ResultOf = decltype(std::declval<Callable>()());
82 using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
89 template <
typename SfinaeExpr,
typename Result_>
96 template <
typename SfinaeExpr,
typename Result =
void>
107 template <
int priority>
119 template <
typename T>
128 const char* display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
129 const char* short_name = strchr(display_name,
':');
130 return short_name ? short_name + 1 : display_name;
134 template <
typename T>
138 template <
typename... Args>
148 #if defined(__clang__) && (!defined(SWIG)) 149 #define MP_TSA(x) __attribute__((x)) 151 #define MP_TSA(x) // no-op 154 #define MP_CAPABILITY(x) MP_TSA(capability(x)) 155 #define MP_SCOPED_CAPABILITY MP_TSA(scoped_lockable) 156 #define MP_REQUIRES(x) MP_TSA(requires_capability(x)) 157 #define MP_ACQUIRE(...) MP_TSA(acquire_capability(__VA_ARGS__)) 158 #define MP_RELEASE(...) MP_TSA(release_capability(__VA_ARGS__)) 159 #define MP_ASSERT_CAPABILITY(x) MP_TSA(assert_capability(x)) 160 #define MP_GUARDED_BY(x) MP_TSA(guarded_by(x)) 161 #define MP_NO_TSA MP_TSA(no_thread_safety_analysis) 166 void unlock()
MP_RELEASE() { m_mutex.unlock(); }
179 assert(m_lock.mutex() == &mutex.m_mutex);
198 template <
typename Lock>
206 template <
typename Lock,
typename Callback>
222 template <
typename Fn,
typename After>
226 using R = std::invoke_result_t<Fn>;
228 if constexpr (std::is_void_v<R>) {
229 std::forward<Fn>(fn)();
231 std::forward<After>(after)();
234 decltype(
auto)
result = std::forward<Fn>(fn)();
236 std::forward<After>(after)();
240 if (!success) std::forward<After>(after)();
250 std::string
LogEscape(
const kj::StringTree&
string,
size_t max_size);
253 using FdToArgsFn = std::function<std::vector<std::string>(
int fd)>;
273 inline char*
CharCast(
unsigned char* c) {
return (
char*)c; }
274 inline const char*
CharCast(
const char* c) {
return c; }
275 inline const char*
CharCast(
const unsigned char* c) {
return (
const char*)c; }
Convenient wrapper around std::variant<T*, T>
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
InterruptException(std::string message)
std::remove_cv_t< std::remove_reference_t< T > > RemoveCvRef
Substitutue for std::remove_cvref_t.
std::variant< T *, T > data
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
static constexpr size_t size
Exception thrown from code executing an IPC call that is interrupted.
Generic utility functions used by capnp code.
T &ref MP_GUARDED_BY(mutex)
const char * what() const noexcept override
Functions to serialize / deserialize common bitcoin types.
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
PtrOrValue(T *ptr, Args &&... args)
void promiseDestroyed(CancelProbe &probe)
CancelProbe(CancelMonitor &monitor)
Helper object to attach to a promise and update a CancelMonitor.
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...
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
void Unlock(Lock &lock, Callback &&callback)
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
#define MP_ASSERT_CAPABILITY(x)
SFINAE helper, see using Require below.
Type helper splitting a TypeList into two halves at position index.
const char * TypeName()
Return capnp type name with filename prefix removed.
GuardedRef(Mutex &, U &) -> GuardedRef< U >
std::function< void()> m_on_cancel
CancelMonitor * m_monitor
class MP_CAPABILITY("mutex") Mutex
std::unique_lock< std::mutex > m_lock
decltype(auto) TryFinally(Fn &&fn, After &&after)
Invoke a function and run a follow-up action before returning the original result.
decltype(std::declval< Callable >()()) ResultOf
Type helper giving return type of a callable type.
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::decay_t< T > Decay
Type helper abbreviating std::decay.
Analog to std::lock_guard that unlocks instead of locks.
#define MP_SCOPED_CAPABILITY
void assert_locked(Mutex &mutex) MP_ASSERT_CAPABILITY() MP_ASSERT_CAPABILITY(mutex)
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
void unlock() MP_RELEASE()
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...
typename _Next::First First
typename _Next::Second Second
Lock(Mutex &m) MP_ACQUIRE(m)
#define T(expected, seed, data)
Helper class that detects when a promise is canceled.