2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2015-04-02 16:33:45 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <scheduler.h>
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <random.h>
|
Merge bitcoin/bitcoin#21948: test: Fix off-by-one in mockscheduler test RPC
fa2e614d16af84327adf1c02746d0f73e0f48111 test: Fix off-by-one in mockscheduler test RPC (MarcoFalke)
Pull request description:
Fixes:
```
fuzz: scheduler.cpp:83: void CScheduler::MockForward(std::chrono::seconds): Assertion `delta_seconds.count() > 0 && delta_seconds < std::chrono::hours{1}' failed.
==1059066== ERROR: libFuzzer: deadly signal
#0 0x558f75449c10 in __sanitizer_print_stack_trace (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x5fec10)
#1 0x558f753f32b8 in fuzzer::PrintStackTrace() fuzzer.o
#2 0x558f753d68d3 in fuzzer::Fuzzer::CrashCallback() fuzzer.o
#3 0x7f4a3cbbb3bf (/lib/x86_64-linux-gnu/libpthread.so.0+0x153bf)
#4 0x7f4a3c7ff18a in raise (/lib/x86_64-linux-gnu/libc.so.6+0x4618a)
#5 0x7f4a3c7de858 in abort (/lib/x86_64-linux-gnu/libc.so.6+0x25858)
#6 0x7f4a3c7de728 (/lib/x86_64-linux-gnu/libc.so.6+0x25728)
#7 0x7f4a3c7eff35 in __assert_fail (/lib/x86_64-linux-gnu/libc.so.6+0x36f35)
#8 0x558f7588a913 in CScheduler::MockForward(std::chrono::duration<long, std::ratio<1l, 1l> >) scheduler.cpp:83:5
#9 0x558f75b0e5b1 in mockscheduler()::$_7::operator()(RPCHelpMan const&, JSONRPCRequest const&) const rpc/misc.cpp:435:30
#10 0x558f75b0e5b1 in std::_Function_handler<UniValue (RPCHelpMan const&, JSONRPCRequest const&), mockscheduler()::$_7>::_M_invoke(std::_Any_data const&, RPCHelpMan const&, JSONRPCRequest const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:285:9
#11 0x558f7587a141 in std::function<UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator()(RPCHelpMan const&, JSONRPCRequest const&) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#12 0x558f7587a141 in RPCHelpMan::HandleRequest(JSONRPCRequest const&) const rpc/util.cpp:565:26
#13 0x558f756c0086 in CRPCCommand::CRPCCommand(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)::operator()(JSONRPCRequest const&, UniValue&, bool) const ./rpc/server.h:110:91
#14 0x558f756c0086 in std::_Function_handler<bool (JSONRPCRequest const&, UniValue&, bool), CRPCCommand::CRPCCommand(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)>::_M_invoke(std::_Any_data const&, JSONRPCRequest const&, UniValue&, bool&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:285:9
#15 0x558f756b8592 in std::function<bool (JSONRPCRequest const&, UniValue&, bool)>::operator()(JSONRPCRequest const&, UniValue&, bool) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#16 0x558f756b8592 in ExecuteCommand(CRPCCommand const&, JSONRPCRequest const&, UniValue&, bool) rpc/server.cpp:480:20
#17 0x558f756b8592 in ExecuteCommands(std::vector<CRPCCommand const*, std::allocator<CRPCCommand const*> > const&, JSONRPCRequest const&, UniValue&) rpc/server.cpp:444:13
#18 0x558f756b8017 in CRPCTable::execute(JSONRPCRequest const&) const rpc/server.cpp:464:13
#19 0x558f7552457a in (anonymous namespace)::RPCFuzzTestingSetup::CallRPC(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) test/fuzz/rpc.cpp:50:25
#20 0x558f7552457a in rpc_fuzz_target(Span<unsigned char const>) test/fuzz/rpc.cpp:354:28
#21 0x558f7544cf0f in std::_Function_handler<void (Span<unsigned char const>), void (*)(Span<unsigned char const>)>::_M_invoke(std::_Any_data const&, Span<unsigned char const>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:300:2
#22 0x558f75c05197 in std::function<void (Span<unsigned char const>)>::operator()(Span<unsigned char const>) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#23 0x558f75c05197 in LLVMFuzzerTestOneInput test/fuzz/fuzz.cpp:74:5
#24 0x558f753d8073 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) fuzzer.o
#25 0x558f753c1f72 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) fuzzer.o
#26 0x558f753c7d6a in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) fuzzer.o
#27 0x558f753f3a92 in main (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x5a8a92)
#28 0x7f4a3c7e00b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#29 0x558f7539cc9d in _start (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x551c9d)
ACKs for top commit:
practicalswift:
cr ACK fa2e614d16af84327adf1c02746d0f73e0f48111
Tree-SHA512: cfa120265261f0ad019b46c426b915c1c007806b37aecb27016ce780a0ddea5e6fc9b09065fd40684b11183dcd3bf543558d7a655e604695021653540266baf7
2021-05-15 09:54:54 +02:00
|
|
|
#include <util/time.h>
|
2015-09-03 18:53:00 +02:00
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
#include <assert.h>
|
2021-01-26 11:14:06 +01:00
|
|
|
#include <functional>
|
2015-04-02 16:33:45 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
CScheduler::CScheduler()
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CScheduler::~CScheduler()
|
|
|
|
{
|
|
|
|
assert(nThreadsServicingQueue == 0);
|
2023-01-15 12:04:56 +01:00
|
|
|
if (stopWhenEmpty) assert(taskQueue.empty());
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CScheduler::serviceQueue()
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
WAIT_LOCK(newTaskMutex, lock);
|
2015-04-02 16:33:45 +02:00
|
|
|
++nThreadsServicingQueue;
|
|
|
|
|
|
|
|
// newTaskMutex is locked throughout this loop EXCEPT
|
|
|
|
// when the thread is waiting or when the user's function
|
|
|
|
// is called.
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
try {
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop() && taskQueue.empty()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
// Wait until there is something to do.
|
|
|
|
newTaskScheduled.wait(lock);
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
|
|
|
// Wait until either there is a new task, or until
|
|
|
|
// the time of the first item on the queue:
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2016-11-19 16:18:19 +01:00
|
|
|
while (!shouldStop() && !taskQueue.empty()) {
|
2020-03-06 20:47:49 +01:00
|
|
|
std::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
|
|
|
|
if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
|
2016-11-19 16:18:19 +01:00
|
|
|
break; // Exit loop after timeout, it means we reached the time of the event
|
2020-03-06 20:47:49 +01:00
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
2020-03-06 20:47:49 +01:00
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
// If there are multiple threads, the queue can empty while we're waiting (another
|
|
|
|
// thread may service the task we were waiting on).
|
2015-05-15 18:40:36 +02:00
|
|
|
if (shouldStop() || taskQueue.empty())
|
2015-04-02 16:33:45 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Function f = taskQueue.begin()->second;
|
|
|
|
taskQueue.erase(taskQueue.begin());
|
|
|
|
|
2015-08-17 23:30:46 +02:00
|
|
|
{
|
|
|
|
// Unlock before calling f, so it can reschedule itself or another task
|
|
|
|
// without deadlocking:
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2015-08-17 23:30:46 +02:00
|
|
|
f();
|
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
} catch (...) {
|
|
|
|
--nThreadsServicingQueue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
--nThreadsServicingQueue;
|
2016-05-06 11:00:01 +02:00
|
|
|
newTaskScheduled.notify_one();
|
2015-05-15 18:40:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
void CScheduler::schedule(CScheduler::Function f, std::chrono::system_clock::time_point t)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2015-04-02 16:33:45 +02:00
|
|
|
taskQueue.insert(std::make_pair(t, f));
|
|
|
|
}
|
|
|
|
newTaskScheduled.notify_one();
|
|
|
|
}
|
|
|
|
|
2022-01-09 18:03:26 +01:00
|
|
|
void CScheduler::MockForward(std::chrono::seconds delta_seconds)
|
|
|
|
{
|
Merge bitcoin/bitcoin#21948: test: Fix off-by-one in mockscheduler test RPC
fa2e614d16af84327adf1c02746d0f73e0f48111 test: Fix off-by-one in mockscheduler test RPC (MarcoFalke)
Pull request description:
Fixes:
```
fuzz: scheduler.cpp:83: void CScheduler::MockForward(std::chrono::seconds): Assertion `delta_seconds.count() > 0 && delta_seconds < std::chrono::hours{1}' failed.
==1059066== ERROR: libFuzzer: deadly signal
#0 0x558f75449c10 in __sanitizer_print_stack_trace (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x5fec10)
#1 0x558f753f32b8 in fuzzer::PrintStackTrace() fuzzer.o
#2 0x558f753d68d3 in fuzzer::Fuzzer::CrashCallback() fuzzer.o
#3 0x7f4a3cbbb3bf (/lib/x86_64-linux-gnu/libpthread.so.0+0x153bf)
#4 0x7f4a3c7ff18a in raise (/lib/x86_64-linux-gnu/libc.so.6+0x4618a)
#5 0x7f4a3c7de858 in abort (/lib/x86_64-linux-gnu/libc.so.6+0x25858)
#6 0x7f4a3c7de728 (/lib/x86_64-linux-gnu/libc.so.6+0x25728)
#7 0x7f4a3c7eff35 in __assert_fail (/lib/x86_64-linux-gnu/libc.so.6+0x36f35)
#8 0x558f7588a913 in CScheduler::MockForward(std::chrono::duration<long, std::ratio<1l, 1l> >) scheduler.cpp:83:5
#9 0x558f75b0e5b1 in mockscheduler()::$_7::operator()(RPCHelpMan const&, JSONRPCRequest const&) const rpc/misc.cpp:435:30
#10 0x558f75b0e5b1 in std::_Function_handler<UniValue (RPCHelpMan const&, JSONRPCRequest const&), mockscheduler()::$_7>::_M_invoke(std::_Any_data const&, RPCHelpMan const&, JSONRPCRequest const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:285:9
#11 0x558f7587a141 in std::function<UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator()(RPCHelpMan const&, JSONRPCRequest const&) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#12 0x558f7587a141 in RPCHelpMan::HandleRequest(JSONRPCRequest const&) const rpc/util.cpp:565:26
#13 0x558f756c0086 in CRPCCommand::CRPCCommand(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)::operator()(JSONRPCRequest const&, UniValue&, bool) const ./rpc/server.h:110:91
#14 0x558f756c0086 in std::_Function_handler<bool (JSONRPCRequest const&, UniValue&, bool), CRPCCommand::CRPCCommand(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)>::_M_invoke(std::_Any_data const&, JSONRPCRequest const&, UniValue&, bool&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:285:9
#15 0x558f756b8592 in std::function<bool (JSONRPCRequest const&, UniValue&, bool)>::operator()(JSONRPCRequest const&, UniValue&, bool) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#16 0x558f756b8592 in ExecuteCommand(CRPCCommand const&, JSONRPCRequest const&, UniValue&, bool) rpc/server.cpp:480:20
#17 0x558f756b8592 in ExecuteCommands(std::vector<CRPCCommand const*, std::allocator<CRPCCommand const*> > const&, JSONRPCRequest const&, UniValue&) rpc/server.cpp:444:13
#18 0x558f756b8017 in CRPCTable::execute(JSONRPCRequest const&) const rpc/server.cpp:464:13
#19 0x558f7552457a in (anonymous namespace)::RPCFuzzTestingSetup::CallRPC(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) test/fuzz/rpc.cpp:50:25
#20 0x558f7552457a in rpc_fuzz_target(Span<unsigned char const>) test/fuzz/rpc.cpp:354:28
#21 0x558f7544cf0f in std::_Function_handler<void (Span<unsigned char const>), void (*)(Span<unsigned char const>)>::_M_invoke(std::_Any_data const&, Span<unsigned char const>&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:300:2
#22 0x558f75c05197 in std::function<void (Span<unsigned char const>)>::operator()(Span<unsigned char const>) const /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/std_function.h:688:14
#23 0x558f75c05197 in LLVMFuzzerTestOneInput test/fuzz/fuzz.cpp:74:5
#24 0x558f753d8073 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) fuzzer.o
#25 0x558f753c1f72 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) fuzzer.o
#26 0x558f753c7d6a in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) fuzzer.o
#27 0x558f753f3a92 in main (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x5a8a92)
#28 0x7f4a3c7e00b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#29 0x558f7539cc9d in _start (/root/fuzz_dir/scratch/fuzz_gen/code/src/test/fuzz/fuzz+0x551c9d)
ACKs for top commit:
practicalswift:
cr ACK fa2e614d16af84327adf1c02746d0f73e0f48111
Tree-SHA512: cfa120265261f0ad019b46c426b915c1c007806b37aecb27016ce780a0ddea5e6fc9b09065fd40684b11183dcd3bf543558d7a655e604695021653540266baf7
2021-05-15 09:54:54 +02:00
|
|
|
assert(delta_seconds > 0s && delta_seconds <= 1h);
|
2022-01-09 18:03:26 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(newTaskMutex);
|
|
|
|
|
|
|
|
// use temp_queue to maintain updated schedule
|
|
|
|
std::multimap<std::chrono::system_clock::time_point, Function> temp_queue;
|
|
|
|
|
|
|
|
for (const auto& element : taskQueue) {
|
|
|
|
temp_queue.emplace_hint(temp_queue.cend(), element.first - delta_seconds, element.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// point taskQueue to temp_queue
|
|
|
|
taskQueue = std::move(temp_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify that the taskQueue needs to be processed
|
|
|
|
newTaskScheduled.notify_one();
|
|
|
|
}
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
static void Repeat(CScheduler& s, CScheduler::Function f, std::chrono::milliseconds delta)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
f();
|
2023-01-15 12:04:56 +01:00
|
|
|
s.scheduleFromNow([=, &s] { Repeat(s, f, delta); }, delta);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
void CScheduler::scheduleEvery(CScheduler::Function f, std::chrono::milliseconds delta)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
2023-01-15 12:04:56 +01:00
|
|
|
scheduleFromNow([=] { Repeat(*this, f, delta); }, delta);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
size_t CScheduler::getQueueInfo(std::chrono::system_clock::time_point& first,
|
|
|
|
std::chrono::system_clock::time_point& last) const
|
2015-05-15 18:40:36 +02:00
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2015-05-15 18:40:36 +02:00
|
|
|
size_t result = taskQueue.size();
|
|
|
|
if (!taskQueue.empty()) {
|
|
|
|
first = taskQueue.begin()->first;
|
|
|
|
last = taskQueue.rbegin()->first;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-07-11 09:30:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
bool CScheduler::AreThreadsServicingQueue() const
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
return nThreadsServicingQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue()
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
// Try to avoid scheduling too many copies here, but if we
|
|
|
|
// accidentally have two ProcessQueue's scheduled at once its
|
|
|
|
// not a big deal.
|
|
|
|
if (m_are_callbacks_running) return;
|
|
|
|
if (m_callbacks_pending.empty()) return;
|
|
|
|
}
|
2022-05-04 10:54:19 +02:00
|
|
|
m_scheduler.schedule([this] { this->ProcessQueue(); }, std::chrono::system_clock::now());
|
2017-07-11 09:30:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::ProcessQueue()
|
|
|
|
{
|
|
|
|
std::function<void()> callback;
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
if (m_are_callbacks_running) return;
|
|
|
|
if (m_callbacks_pending.empty()) return;
|
|
|
|
m_are_callbacks_running = true;
|
|
|
|
|
|
|
|
callback = std::move(m_callbacks_pending.front());
|
|
|
|
m_callbacks_pending.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
// RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
|
|
|
|
// to ensure both happen safely even if callback() throws.
|
|
|
|
struct RAIICallbacksRunning {
|
|
|
|
SingleThreadedSchedulerClient* instance;
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit RAIICallbacksRunning(SingleThreadedSchedulerClient* _instance) : instance(_instance) {}
|
2020-05-28 14:04:40 +02:00
|
|
|
~RAIICallbacksRunning()
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(instance->m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
instance->m_are_callbacks_running = false;
|
|
|
|
}
|
|
|
|
instance->MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
} raiicallbacksrunning(this);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void()> func)
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
m_callbacks_pending.emplace_back(std::move(func));
|
|
|
|
}
|
|
|
|
MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::EmptyQueue()
|
|
|
|
{
|
2022-05-04 10:54:19 +02:00
|
|
|
assert(!m_scheduler.AreThreadsServicingQueue());
|
2017-07-11 09:30:36 +02:00
|
|
|
bool should_continue = true;
|
|
|
|
while (should_continue) {
|
|
|
|
ProcessQueue();
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
should_continue = !m_callbacks_pending.empty();
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 15:21:59 +01:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
size_t SingleThreadedSchedulerClient::CallbacksPending()
|
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2020-03-24 15:21:59 +01:00
|
|
|
return m_callbacks_pending.size();
|
|
|
|
}
|