Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef MP_UTIL_H
6 : : #define MP_UTIL_H
7 : :
8 : : #include <array>
9 : : #include <capnp/schema.h>
10 : : #include <cassert>
11 : : #include <cstdlib>
12 : : #include <cstring>
13 : : #include <exception>
14 : : #include <functional>
15 : : #include <kj/async-io.h>
16 : : #include <kj/memory.h>
17 : : #include <kj/string-tree.h>
18 : : #include <mutex>
19 : : #include <string>
20 : : #include <tuple>
21 : : #include <typeinfo>
22 : : #include <type_traits>
23 : : #include <utility>
24 : : #include <variant>
25 : : #include <vector>
26 : :
27 : : #if __has_include(<cxxabi.h>)
28 : : #include <cxxabi.h>
29 : : #include <memory>
30 : : #endif
31 : :
32 : : namespace mp {
33 : :
34 : : //! Generic utility functions used by capnp code.
35 : :
36 : : // std::cmp_less_equal/cmp_greater_equal only accept integer types (C++20 §[utility.intcmp]).
37 : : // These wrappers fall back to regular comparison for floating-point types.
38 : : template <typename A, typename B>
39 : : constexpr bool safe_less_equal(A a, B b)
40 : : {
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);
43 : : }
44 : : template <typename A, typename B>
45 : : constexpr bool safe_greater_equal(A a, B b)
46 : : {
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);
49 : : }
50 : :
51 : : //! Type holding a list of types.
52 : : //!
53 : : //! Example:
54 : : //! TypeList<int, bool, void>
55 : : template <typename... Types>
56 : : struct TypeList
57 : : {
58 : : static constexpr size_t size = sizeof...(Types);
59 : : };
60 : :
61 : : //! Construct a template class value by deducing template arguments from the
62 : : //! types of constructor arguments, so they don't need to be specified manually.
63 : : //!
64 : : //! Uses of this can go away with class template deduction in C++17
65 : : //! (https://en.cppreference.com/w/cpp/language/class_template_argument_deduction)
66 : : //!
67 : : //! Example:
68 : : //! Make<std::pair>(5, true) // Constructs std::pair<int, bool>(5, true);
69 : : template <template <typename...> class Class, typename... Types, typename... Args>
70 : 362 : Class<Types..., std::remove_reference_t<Args>...> Make(Args&&... args)
71 : : {
72 : 362 : return Class<Types..., std::remove_reference_t<Args>...>{std::forward<Args>(args)...};
73 : : }
74 : :
75 : : //! Type helper splitting a TypeList into two halves at position index.
76 : : //!
77 : : //! Example:
78 : : //! is_same<TypeList<int, double>, Split<2, TypeList<int, double, float, bool>>::First>
79 : : //! is_same<TypeList<float, bool>, Split<2, TypeList<int, double, float, bool>>::Second>
80 : : template <std::size_t index, typename List, typename _First = TypeList<>, bool done = index == 0>
81 : : struct Split;
82 : :
83 : : //! Specialization of above (base case)
84 : : template <typename _Second, typename _First>
85 : : struct Split<0, _Second, _First, true>
86 : : {
87 : : using First = _First;
88 : : using Second = _Second;
89 : : };
90 : :
91 : : //! Specialization of above (recursive case)
92 : : template <std::size_t index, typename Type, typename... _Second, typename... _First>
93 : : struct Split<index, TypeList<Type, _Second...>, TypeList<_First...>, false>
94 : : {
95 : : using _Next = Split<index - 1, TypeList<_Second...>, TypeList<_First..., Type>>;
96 : : using First = typename _Next::First;
97 : : using Second = typename _Next::Second;
98 : : };
99 : :
100 : : //! Type helper giving return type of a callable type.
101 : : template <typename Callable>
102 : : using ResultOf = decltype(std::declval<Callable>()());
103 : :
104 : : //! Substitutue for std::remove_cvref_t
105 : : template <typename T>
106 : : using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
107 : :
108 : : //! Type helper abbreviating std::decay.
109 : : template <typename T>
110 : : using Decay = std::decay_t<T>;
111 : :
112 : : //! Concept satisfied when T's .get() method returns exactly type U.
113 : : //! Used to constrain overloads that handle a specific capnp field type.
114 : : template <typename T, typename U>
115 : : concept FieldTypeIs = std::is_same_v<decltype(std::declval<T>().get()), U>;
116 : :
117 : : //! Concept satisfied when T's .get() method returns a capnp interface type
118 : : //! (i.e., a type that exposes a nested ::Calls type in generated code).
119 : : template <typename T>
120 : : concept InterfaceField = requires { typename Decay<decltype(std::declval<T>().get())>::Calls; };
121 : :
122 : : //! SFINAE helper, see using Require below.
123 : : template <typename SfinaeExpr, typename Result_>
124 : : struct _Require
125 : : {
126 : : using Result = Result_;
127 : : };
128 : :
129 : : //! SFINAE helper, basically the same as to C++17's void_t, but allowing types other than void to be returned.
130 : : template <typename SfinaeExpr, typename Result = void>
131 : : using Require = typename _Require<SfinaeExpr, Result>::Result;
132 : :
133 : : //! Function parameter type for prioritizing overloaded function calls that
134 : : //! would otherwise be ambiguous.
135 : : //!
136 : : //! Example:
137 : : //! auto foo(Priority<1>) -> std::enable_if<>;
138 : : //! auto foo(Priority<0>) -> void;
139 : : //!
140 : : //! foo(Priority<1>()); // Calls higher priority overload if enabled.
141 : : template <int priority>
142 : : struct Priority : Priority<priority - 1>
143 : : {
144 : : };
145 : :
146 : : //! Specialization of above (base case)
147 : : template <>
148 : : struct Priority<0>
149 : : {
150 : : };
151 : :
152 : : //! Return capnp type name with filename prefix removed.
153 : : template <typename T>
154 : 180 : const char* TypeName()
155 : : {
156 : : // DisplayName string looks like
157 : : // "interfaces/capnp/common.capnp:ChainNotifications.resendWalletTransactions$Results"
158 : : // This discards the part of the string before the first ':' character.
159 : : // Another alternative would be to use the displayNamePrefixLength field,
160 : : // but this discards everything before the last '.' character, throwing away
161 : : // the object name, which is useful.
162 [ + - ]: 180 : const char* display_name = ::capnp::Schema::from<T>().getProto().getDisplayName().cStr();
163 : 180 : const char* short_name = strchr(display_name, ':');
164 [ + - ]: 180 : return short_name ? short_name + 1 : display_name;
165 : : }
166 : :
167 : : //! Convenient wrapper around std::variant<T*, T>
168 : : template <typename T>
169 : 898 : struct PtrOrValue {
170 : : std::variant<T*, T> data;
171 : :
172 : : template <typename... Args>
173 [ + + ]: 898 : PtrOrValue(T* ptr, Args&&... args) : data(std::in_place_type<T*>, ptr)
174 : : {
175 [ + + + - ]: 898 : if (!ptr) data.template emplace<T>(std::forward<Args>(args)...);
176 : 898 : }
177 : :
178 [ + + - + ]: 913 : T& operator*() { return data.index() ? std::get<T>(data) : *std::get<T*>(data); }
179 [ + - + - : 913 : T* operator->() { return &**this; }
+ - + - ]
180 : : T& operator*() const { return data.index() ? std::get<T>(data) : *std::get<T*>(data); }
181 : : T* operator->() const { return &**this; }
182 : : };
183 : :
184 : : // Annotated mutex and lock class (https://clang.llvm.org/docs/ThreadSafetyAnalysis.html)
185 : : #if defined(__clang__) && (!defined(SWIG))
186 : : #define MP_TSA(x) __attribute__((x))
187 : : #else
188 : : #define MP_TSA(x) // no-op
189 : : #endif
190 : :
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)
199 : :
200 : 31 : class MP_CAPABILITY("mutex") Mutex {
201 : : public:
202 : : void lock() MP_ACQUIRE() { m_mutex.lock(); }
203 : : void unlock() MP_RELEASE() { m_mutex.unlock(); }
204 : :
205 : : std::mutex m_mutex;
206 : : };
207 : :
208 : : class MP_SCOPED_CAPABILITY Lock {
209 : : public:
210 [ + - + - : 1557 : explicit Lock(Mutex& m) MP_ACQUIRE(m) : m_lock(m.m_mutex) {}
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
- - - - -
- - - - -
+ - + - +
- ][ + - +
- + - + -
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - - -
- - - - -
- - - - -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- - - + -
- - + - +
- + - + +
+ - + + +
- + - + -
- + + - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- - - + -
- - + - +
- + - - +
+ - + + +
- + - + -
- + + - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
211 [ + - - - : 2921 : ~Lock() MP_RELEASE() = default;
- - + - -
- + - - -
- - + - -
- + - - -
- - + - -
- + - - -
- - + - -
- + - - -
- - + - -
- + - - -
- - + - -
- + - - -
- - + - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - -
- + - - -
+ - + - +
+ + - + +
+ - - + -
- - - - -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
+ - + - -
+ - - ][ +
- - - - -
+ - - - +
- - - - -
+ - - - ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - + -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
- - + - +
- - + - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - +
- - - + -
- - - - +
- - - ][ +
+ + - - -
- - - - +
- + - - -
- - + - +
+ - - + -
+ - - - +
- - - + -
+ - + - +
- + - - -
- - - - +
- - - + -
+ - - - +
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + + + -
- - - - -
- + - + -
- - - - +
- + + - -
+ - + - -
- + - - -
+ - + - +
- + - + -
- - - - -
- + - - -
+ - + - -
- + - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
212 [ + - ]: 45 : void unlock() MP_RELEASE() { m_lock.unlock(); }
213 [ + - ]: 34 : void lock() MP_ACQUIRE() { m_lock.lock(); }
214 : 898 : void assert_locked(Mutex& mutex) MP_ASSERT_CAPABILITY() MP_ASSERT_CAPABILITY(mutex)
215 : : {
216 [ - + ]: 898 : assert(m_lock.mutex() == &mutex.m_mutex);
217 [ - + ]: 898 : assert(m_lock);
218 : 898 : }
219 : :
220 : : std::unique_lock<std::mutex> m_lock;
221 : : };
222 : :
223 : : template<typename T>
224 : : struct GuardedRef
225 : : {
226 : : Mutex& mutex;
227 : : T& ref MP_GUARDED_BY(mutex);
228 : : };
229 : :
230 : : // CTAD for Clang 16: GuardedRef{mutex, x} -> GuardedRef<decltype(x)>
231 : : template <class U>
232 : : GuardedRef(Mutex&, U&) -> GuardedRef<U>;
233 : :
234 : : //! Analog to std::lock_guard that unlocks instead of locks.
235 : : template <typename Lock>
236 : : struct UnlockGuard
237 : : {
238 : 1166 : UnlockGuard(Lock& lock) : m_lock(lock) { m_lock.unlock(); }
239 : 298 : ~UnlockGuard() { m_lock.lock(); }
240 : : Lock& m_lock;
241 : : };
242 : :
243 : : template <typename Lock, typename Callback>
244 : 583 : void Unlock(Lock& lock, Callback&& callback)
245 : : {
246 [ + - ]: 583 : const UnlockGuard<Lock> unlock(lock);
247 [ + - ]: 583 : callback();
248 : 583 : }
249 : :
250 : : //! Invoke a function and run a follow-up action before returning the original
251 : : //! result.
252 : : //!
253 : : //! This can be used similarly to KJ_DEFER to run cleanup code, but works better
254 : : //! if the cleanup function can throw because it avoids clang bug
255 : : //! https://github.com/llvm/llvm-project/issues/12658 which skips calling
256 : : //! destructors in that case and can lead to memory leaks. Also, if both
257 : : //! functions throw, this lets one exception take precedence instead of
258 : : //! terminating due to having two active exceptions.
259 : : template <typename Fn, typename After>
260 : 43 : decltype(auto) TryFinally(Fn&& fn, After&& after)
261 : : {
262 [ + - ]: 43 : bool success{false};
263 : : using R = std::invoke_result_t<Fn>;
264 : : try {
265 : : if constexpr (std::is_void_v<R>) {
266 [ # # ]: 12 : std::forward<Fn>(fn)();
267 : 12 : success = true;
268 [ + - ]: 12 : std::forward<After>(after)();
269 : 12 : return;
270 : : } else {
271 [ + - ]: 31 : decltype(auto) result = std::forward<Fn>(fn)();
272 : 31 : success = true;
273 [ + - ]: 31 : std::forward<After>(after)();
274 : 31 : return result;
275 : 0 : }
276 : 0 : } catch (...) {
277 [ - - - - ]: 0 : if (!success) std::forward<After>(after)();
278 : 0 : throw;
279 : : }
280 : : }
281 : :
282 : : //! Set the OS-level name of the current thread
283 : : void SetOsThreadName(const char* name);
284 : :
285 : : //! Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
286 : : std::string ThreadName(const char* exe_name);
287 : :
288 : : //! Escape binary string for use in log so it doesn't trigger unicode decode
289 : : //! errors in python unit tests.
290 : : std::string LogEscape(const kj::StringTree& string, size_t max_size);
291 : :
292 : : using Stream = kj::Own<kj::AsyncIoStream>;
293 : :
294 : : using ProcessId = int;
295 : : using SocketId = int;
296 : : inline constexpr SocketId SocketError{-1};
297 : :
298 : : //! Information about parent process passed to child process as a command-line
299 : : //! argument. On unix this is the child socket fd number formatted as a string.
300 : : using SpawnConnectInfo = std::string;
301 : :
302 : : //! Callback type used by SpawnProcess below.
303 : : using SpawnConnectInfoToArgsFn = std::function<std::vector<std::string>(const SpawnConnectInfo&)>;
304 : :
305 : : //! Spawn a new process that communicates with the current process over a socket
306 : : //! pair. Calls connect_info_to_args callback with a connection string that
307 : : //! needs to be passed to the child process, and executes the argv command line
308 : : //! it returns. Returns child process id and socket id.
309 : : std::tuple<ProcessId, SocketId> SpawnProcess(SpawnConnectInfoToArgsFn&& connect_info_to_args);
310 : :
311 : : //! Initialize spawned child process using the SpawnConnectInfo string passed to it,
312 : : //! returning a socket id for communicating with the parent process.
313 : : SocketId StartSpawned(const SpawnConnectInfo& connect_info);
314 : :
315 : : //! Create a socket pair that can be used to communicate within a process or
316 : : //! between parent and child processes.
317 : : std::array<SocketId, 2> SocketPair();
318 : :
319 : : //! Start a process and return its process id. Caller should call WaitProcess
320 : : //! on the returned id.
321 : : ProcessId StartProcess(const std::vector<std::string>& args);
322 : :
323 : : //! Wait for a process to exit and return its exit code.
324 : : int WaitProcess(ProcessId pid);
325 : :
326 : : inline char* CharCast(char* c) { return c; }
327 : : inline char* CharCast(unsigned char* c) { return (char*)c; }
328 : : inline const char* CharCast(const char* c) { return c; }
329 : : inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
330 : :
331 : : #if __has_include(<cxxabi.h>) // GCC & Clang ─ use <cxxabi.h> to demangle
332 : 328 : inline std::string _demangle(const char* m)
333 : : {
334 : 328 : int status = 0;
335 : 328 : std::unique_ptr<char, void(*)(void*)> p{
336 [ + - ]: 328 : abi::__cxa_demangle(m, /*output_buffer=*/nullptr, /*length=*/nullptr, &status), std::free};
337 [ + - - + : 328 : return (status == 0 && p) ? p.get() : m; // fall back on mangled if needed
+ - ]
338 : 328 : }
339 : : #else // MSVC or other ─ no demangling available
340 : : inline std::string _demangle(const char* m) { return m; }
341 : : #endif
342 : :
343 : : template<class T>
344 [ # # ]: 328 : std::string CxxTypeName(const T& /*unused*/)
345 : : {
346 : : #ifdef __cpp_rtti
347 : 328 : return _demangle(typeid(std::decay_t<T>).name());
348 : : #else
349 : : return "<type information unavailable without rtti>";
350 : : #endif
351 : : }
352 : :
353 : : //! Exception thrown from code executing an IPC call that is interrupted.
354 : : struct InterruptException final : std::exception {
355 : 0 : explicit InterruptException(std::string message) : m_message(std::move(message)) {}
356 [ # # # # : 0 : const char* what() const noexcept override { return m_message.c_str(); }
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # # ]
[ # # # #
# # ][ # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
357 : : std::string m_message;
358 : : };
359 : :
360 : : class CancelProbe;
361 : :
362 : : //! Helper class that detects when a promise is canceled. Used to detect
363 : : //! canceled requests and prevent potential crashes on unclean disconnects.
364 : : //!
365 : : //! In the future, this could also be used to support a way for wrapped C++
366 : : //! methods to detect cancellation (like approach #4 in
367 : : //! https://github.com/bitcoin/bitcoin/issues/33575).
368 : 32 : class CancelMonitor
369 : : {
370 : : public:
371 : : inline ~CancelMonitor();
372 : : inline void promiseDestroyed(CancelProbe& probe);
373 : :
374 : : bool m_canceled{false};
375 : : std::function<void()> m_on_cancel;
376 : : CancelProbe* m_probe{nullptr};
377 : : };
378 : :
379 : : //! Helper object to attach to a promise and update a CancelMonitor.
380 : : class CancelProbe
381 : : {
382 : : public:
383 : 32 : CancelProbe(CancelMonitor& monitor) : m_monitor(&monitor)
384 : : {
385 [ - + ]: 32 : assert(!monitor.m_probe);
386 : 32 : monitor.m_probe = this;
387 : 32 : }
388 : 32 : ~CancelProbe()
389 : : {
390 [ - + ]: 32 : if (m_monitor) m_monitor->promiseDestroyed(*this);
391 : 32 : }
392 : : CancelMonitor* m_monitor;
393 : : };
394 : :
395 : 32 : CancelMonitor::~CancelMonitor()
396 : : {
397 [ + - ]: 32 : if (m_probe) {
398 [ - + ]: 32 : assert(m_probe->m_monitor == this);
399 : 32 : m_probe->m_monitor = nullptr;
400 : 32 : m_probe = nullptr;
401 : : }
402 : 32 : }
403 : :
404 : 0 : void CancelMonitor::promiseDestroyed(CancelProbe& probe)
405 : : {
406 : : // If promise is being destroyed, assume the promise has been canceled. In
407 : : // theory this method could be called when a promise was fulfilled or
408 : : // rejected rather than canceled, but it's safe to assume that's not the
409 : : // case because the CancelMonitor class is meant to be used inside code
410 : : // fulfilling or rejecting the promise and destroyed before doing so.
411 [ # # ]: 0 : assert(m_probe == &probe);
412 : 0 : m_canceled = true;
413 [ # # ]: 0 : if (m_on_cancel) m_on_cancel();
414 : 0 : m_probe = nullptr;
415 : 0 : }
416 : : } // namespace mp
417 : :
418 : : #endif // MP_UTIL_H
|