Skip to content

Commit 0d6d2b6

Browse files
scripted-diff: Rename SingleThreadedSchedulerClient to SerialTaskRunner
-BEGIN VERIFY SCRIPT- s() { git grep -l "$1" src | (grep -v "$3" || cat;) | xargs sed -i "s/$1/$2/g"; } s 'SingleThreadedSchedulerClient' 'SerialTaskRunner' '' s 'SinglethreadedSchedulerClient' 'SerialTaskRunner' '' s 'm_schedulerClient' 'm_task_runner' '' s 'AddToProcessQueue' 'insert' '' s 'EmptyQueue' 'flush' '' s 'CallbacksPending' 'size' 'validation' sed -i '109s/CallbacksPending/size/' src/validationinterface.cpp -END VERIFY SCRIPT- Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
1 parent 4abde2c commit 0d6d2b6

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

src/scheduler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool CScheduler::AreThreadsServicingQueue() const
129129
}
130130

131131

132-
void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue()
132+
void SerialTaskRunner::MaybeScheduleProcessQueue()
133133
{
134134
{
135135
LOCK(m_callbacks_mutex);
@@ -142,7 +142,7 @@ void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue()
142142
m_scheduler.schedule([this] { this->ProcessQueue(); }, std::chrono::steady_clock::now());
143143
}
144144

145-
void SingleThreadedSchedulerClient::ProcessQueue()
145+
void SerialTaskRunner::ProcessQueue()
146146
{
147147
std::function<void()> callback;
148148
{
@@ -158,8 +158,8 @@ void SingleThreadedSchedulerClient::ProcessQueue()
158158
// RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
159159
// to ensure both happen safely even if callback() throws.
160160
struct RAIICallbacksRunning {
161-
SingleThreadedSchedulerClient* instance;
162-
explicit RAIICallbacksRunning(SingleThreadedSchedulerClient* _instance) : instance(_instance) {}
161+
SerialTaskRunner* instance;
162+
explicit RAIICallbacksRunning(SerialTaskRunner* _instance) : instance(_instance) {}
163163
~RAIICallbacksRunning()
164164
{
165165
{
@@ -173,7 +173,7 @@ void SingleThreadedSchedulerClient::ProcessQueue()
173173
callback();
174174
}
175175

176-
void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void()> func)
176+
void SerialTaskRunner::insert(std::function<void()> func)
177177
{
178178
{
179179
LOCK(m_callbacks_mutex);
@@ -182,7 +182,7 @@ void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void()> func
182182
MaybeScheduleProcessQueue();
183183
}
184184

185-
void SingleThreadedSchedulerClient::EmptyQueue()
185+
void SerialTaskRunner::flush()
186186
{
187187
assert(!m_scheduler.AreThreadsServicingQueue());
188188
bool should_continue = true;
@@ -193,7 +193,7 @@ void SingleThreadedSchedulerClient::EmptyQueue()
193193
}
194194
}
195195

196-
size_t SingleThreadedSchedulerClient::CallbacksPending()
196+
size_t SerialTaskRunner::size()
197197
{
198198
LOCK(m_callbacks_mutex);
199199
return m_callbacks_pending.size();

src/scheduler.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class CScheduler
120120
* B() will be able to observe all of the effects of callback A() which executed
121121
* before it.
122122
*/
123-
class SingleThreadedSchedulerClient
123+
class SerialTaskRunner
124124
{
125125
private:
126126
CScheduler& m_scheduler;
@@ -133,23 +133,23 @@ class SingleThreadedSchedulerClient
133133
void ProcessQueue() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
134134

135135
public:
136-
explicit SingleThreadedSchedulerClient(CScheduler& scheduler LIFETIMEBOUND) : m_scheduler{scheduler} {}
136+
explicit SerialTaskRunner(CScheduler& scheduler LIFETIMEBOUND) : m_scheduler{scheduler} {}
137137

138138
/**
139139
* Add a callback to be executed. Callbacks are executed serially
140140
* and memory is release-acquire consistent between callback executions.
141141
* Practically, this means that callbacks can behave as if they are executed
142142
* in order by a single thread.
143143
*/
144-
void AddToProcessQueue(std::function<void()> func) EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
144+
void insert(std::function<void()> func) EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
145145

146146
/**
147147
* Processes all remaining queue members on the calling thread, blocking until queue is empty
148148
* Must be called after the CScheduler has no remaining processing threads!
149149
*/
150-
void EmptyQueue() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
150+
void flush() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
151151

152-
size_t CallbacksPending() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
152+
size_t size() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex);
153153
};
154154

155155
#endif // BITCOIN_SCHEDULER_H

src/test/scheduler_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
129129
CScheduler scheduler;
130130

131131
// each queue should be well ordered with respect to itself but not other queues
132-
SingleThreadedSchedulerClient queue1(scheduler);
133-
SingleThreadedSchedulerClient queue2(scheduler);
132+
SerialTaskRunner queue1(scheduler);
133+
SerialTaskRunner queue2(scheduler);
134134

135135
// create more threads than queues
136136
// if the queues only permit execution of one task at once then
@@ -142,20 +142,20 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
142142
threads.emplace_back([&] { scheduler.serviceQueue(); });
143143
}
144144

145-
// these are not atomic, if SinglethreadedSchedulerClient prevents
145+
// these are not atomic, if SerialTaskRunner prevents
146146
// parallel execution at the queue level no synchronization should be required here
147147
int counter1 = 0;
148148
int counter2 = 0;
149149

150150
// just simply count up on each queue - if execution is properly ordered then
151151
// the callbacks should run in exactly the order in which they were enqueued
152152
for (int i = 0; i < 100; ++i) {
153-
queue1.AddToProcessQueue([i, &counter1]() {
153+
queue1.insert([i, &counter1]() {
154154
bool expectation = i == counter1++;
155155
assert(expectation);
156156
});
157157

158-
queue2.AddToProcessQueue([i, &counter2]() {
158+
queue2.insert([i, &counter2]() {
159159
bool expectation = i == counter2++;
160160
assert(expectation);
161161
});

src/validationinterface.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class MainSignalsImpl
4545
// We are not allowed to assume the scheduler only runs in one thread,
4646
// but must ensure all callbacks happen in-order, so we end up creating
4747
// our own queue here :(
48-
SingleThreadedSchedulerClient m_schedulerClient;
48+
SerialTaskRunner m_task_runner;
4949

50-
explicit MainSignalsImpl(CScheduler& scheduler LIFETIMEBOUND) : m_schedulerClient(scheduler) {}
50+
explicit MainSignalsImpl(CScheduler& scheduler LIFETIMEBOUND) : m_task_runner(scheduler) {}
5151

5252
void Register(std::shared_ptr<CValidationInterface> callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
5353
{
@@ -101,12 +101,12 @@ CMainSignals::~CMainSignals() {}
101101

102102
void CMainSignals::FlushBackgroundCallbacks()
103103
{
104-
m_internals->m_schedulerClient.EmptyQueue();
104+
m_internals->m_task_runner.flush();
105105
}
106106

107107
size_t CMainSignals::CallbacksPending()
108108
{
109-
return m_internals->m_schedulerClient.CallbacksPending();
109+
return m_internals->m_task_runner.size();
110110
}
111111

112112
void CMainSignals::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
@@ -140,7 +140,7 @@ void CMainSignals::UnregisterAllValidationInterfaces()
140140

141141
void CMainSignals::CallFunctionInValidationInterfaceQueue(std::function<void()> func)
142142
{
143-
m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
143+
m_internals->m_task_runner.insert(std::move(func));
144144
}
145145

146146
void CMainSignals::SyncWithValidationInterfaceQueue()
@@ -162,7 +162,7 @@ void CMainSignals::SyncWithValidationInterfaceQueue()
162162
do { \
163163
auto local_name = (name); \
164164
LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
165-
m_internals->m_schedulerClient.AddToProcessQueue([=] { \
165+
m_internals->m_task_runner.insert([=] { \
166166
LOG_EVENT(fmt, local_name, __VA_ARGS__); \
167167
event(); \
168168
}); \

0 commit comments

Comments
 (0)