Skip to content

Commit 473dd4b

Browse files
committed
[refactor] Prepare for g_signals de-globalization
To this end move some functions into the CMainSignals class.
1 parent 3fba3d5 commit 473dd4b

File tree

2 files changed

+91
-46
lines changed

2 files changed

+91
-46
lines changed

src/validationinterface.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class MainSignalsImpl
9696

9797
static CMainSignals g_signals;
9898

99+
CMainSignals::CMainSignals() {}
100+
101+
CMainSignals::~CMainSignals() {}
102+
99103
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler)
100104
{
101105
assert(!m_internals);
@@ -125,46 +129,46 @@ CMainSignals& GetMainSignals()
125129
return g_signals;
126130
}
127131

128-
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
132+
void CMainSignals::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
129133
{
130134
// Each connection captures the shared_ptr to ensure that each callback is
131135
// executed before the subscriber is destroyed. For more details see #18338.
132-
g_signals.m_internals->Register(std::move(callbacks));
136+
m_internals->Register(std::move(callbacks));
133137
}
134138

135-
void RegisterValidationInterface(CValidationInterface* callbacks)
139+
void CMainSignals::RegisterValidationInterface(CValidationInterface* callbacks)
136140
{
137141
// Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
138142
// is managed by the caller.
139143
RegisterSharedValidationInterface({callbacks, [](CValidationInterface*){}});
140144
}
141145

142-
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
146+
void CMainSignals::UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
143147
{
144148
UnregisterValidationInterface(callbacks.get());
145149
}
146150

147-
void UnregisterValidationInterface(CValidationInterface* callbacks)
151+
void CMainSignals::UnregisterValidationInterface(CValidationInterface* callbacks)
148152
{
149-
if (g_signals.m_internals) {
150-
g_signals.m_internals->Unregister(callbacks);
153+
if (m_internals) {
154+
m_internals->Unregister(callbacks);
151155
}
152156
}
153157

154-
void UnregisterAllValidationInterfaces()
158+
void CMainSignals::UnregisterAllValidationInterfaces()
155159
{
156-
if (!g_signals.m_internals) {
160+
if (!m_internals) {
157161
return;
158162
}
159-
g_signals.m_internals->Clear();
163+
m_internals->Clear();
160164
}
161165

162-
void CallFunctionInValidationInterfaceQueue(std::function<void()> func)
166+
void CMainSignals::CallFunctionInValidationInterfaceQueue(std::function<void()> func)
163167
{
164-
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
168+
m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
165169
}
166170

167-
void SyncWithValidationInterfaceQueue()
171+
void CMainSignals::SyncWithValidationInterfaceQueue()
168172
{
169173
AssertLockNotHeld(cs_main);
170174
// Block until the validation queue drains
@@ -273,3 +277,33 @@ void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared
273277
LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
274278
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
275279
}
280+
281+
// These functions are temporary and will be removed in the following commit
282+
void RegisterValidationInterface(CValidationInterface* callbacks)
283+
{
284+
GetMainSignals().RegisterValidationInterface(callbacks);
285+
}
286+
void UnregisterValidationInterface(CValidationInterface* callbacks)
287+
{
288+
GetMainSignals().UnregisterValidationInterface(callbacks);
289+
}
290+
void UnregisterAllValidationInterfaces()
291+
{
292+
GetMainSignals().UnregisterAllValidationInterfaces();
293+
}
294+
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
295+
{
296+
GetMainSignals().RegisterSharedValidationInterface(callbacks);
297+
}
298+
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
299+
{
300+
GetMainSignals().UnregisterSharedValidationInterface(callbacks);
301+
}
302+
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func)
303+
{
304+
GetMainSignals().CallFunctionInValidationInterfaceQueue(func);
305+
}
306+
void SyncWithValidationInterfaceQueue()
307+
{
308+
GetMainSignals().SyncWithValidationInterfaceQueue();
309+
}

src/validationinterface.h

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
#ifndef BITCOIN_VALIDATIONINTERFACE_H
77
#define BITCOIN_VALIDATIONINTERFACE_H
88

9-
#include <kernel/cs_main.h>
109
#include <kernel/chain.h>
10+
#include <kernel/cs_main.h>
1111
#include <primitives/transaction.h> // CTransaction(Ref)
1212
#include <sync.h>
1313

14+
#include <cstddef>
15+
#include <cstdint>
1416
#include <functional>
1517
#include <memory>
18+
#include <vector>
1619

1720
class BlockValidationState;
1821
class CBlock;
@@ -24,41 +27,14 @@ enum class MemPoolRemovalReason;
2427
struct RemovedMempoolTransactionInfo;
2528
struct NewMempoolTransactionInfo;
2629

27-
/** Register subscriber */
2830
void RegisterValidationInterface(CValidationInterface* callbacks);
29-
/** Unregister subscriber. DEPRECATED. This is not safe to use when the RPC server or main message handler thread is running. */
3031
void UnregisterValidationInterface(CValidationInterface* callbacks);
31-
/** Unregister all subscribers */
3232
void UnregisterAllValidationInterfaces();
3333

34-
// Alternate registration functions that release a shared_ptr after the last
35-
// notification is sent. These are useful for race-free cleanup, since
36-
// unregistration is nonblocking and can return before the last notification is
37-
// processed.
38-
/** Register subscriber */
3934
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
40-
/** Unregister subscriber */
4135
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
4236

43-
/**
44-
* Pushes a function to callback onto the notification queue, guaranteeing any
45-
* callbacks generated prior to now are finished when the function is called.
46-
*
47-
* Be very careful blocking on func to be called if any locks are held -
48-
* validation interface clients may not be able to make progress as they often
49-
* wait for things like cs_main, so blocking until func is called with cs_main
50-
* will result in a deadlock (that DEBUG_LOCKORDER will miss).
51-
*/
5237
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
53-
/**
54-
* This is a synonym for the following, which asserts certain locks are not
55-
* held:
56-
* std::promise<void> promise;
57-
* CallFunctionInValidationInterfaceQueue([&promise] {
58-
* promise.set_value();
59-
* });
60-
* promise.get_future().wait();
61-
*/
6238
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);
6339

6440
/**
@@ -194,12 +170,10 @@ class CMainSignals {
194170
private:
195171
std::unique_ptr<MainSignalsImpl> m_internals;
196172

197-
friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
198-
friend void ::UnregisterValidationInterface(CValidationInterface*);
199-
friend void ::UnregisterAllValidationInterfaces();
200-
friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
201-
202173
public:
174+
CMainSignals();
175+
~CMainSignals();
176+
203177
/** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
204178
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
205179
/** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
@@ -209,6 +183,43 @@ class CMainSignals {
209183

210184
size_t CallbacksPending();
211185

186+
/** Register subscriber */
187+
void RegisterValidationInterface(CValidationInterface* callbacks);
188+
/** Unregister subscriber. DEPRECATED. This is not safe to use when the RPC server or main message handler thread is running. */
189+
void UnregisterValidationInterface(CValidationInterface* callbacks);
190+
/** Unregister all subscribers */
191+
void UnregisterAllValidationInterfaces();
192+
193+
// Alternate registration functions that release a shared_ptr after the last
194+
// notification is sent. These are useful for race-free cleanup, since
195+
// unregistration is nonblocking and can return before the last notification is
196+
// processed.
197+
/** Register subscriber */
198+
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
199+
/** Unregister subscriber */
200+
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
201+
202+
/**
203+
* Pushes a function to callback onto the notification queue, guaranteeing any
204+
* callbacks generated prior to now are finished when the function is called.
205+
*
206+
* Be very careful blocking on func to be called if any locks are held -
207+
* validation interface clients may not be able to make progress as they often
208+
* wait for things like cs_main, so blocking until func is called with cs_main
209+
* will result in a deadlock (that DEBUG_LOCKORDER will miss).
210+
*/
211+
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
212+
213+
/**
214+
* This is a synonym for the following, which asserts certain locks are not
215+
* held:
216+
* std::promise<void> promise;
217+
* CallFunctionInValidationInterfaceQueue([&promise] {
218+
* promise.set_value();
219+
* });
220+
* promise.get_future().wait();
221+
*/
222+
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);
212223

213224
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
214225
void TransactionAddedToMempool(const NewMempoolTransactionInfo&, uint64_t mempool_sequence);

0 commit comments

Comments
 (0)