MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / serviceQueue

Method serviceQueue

src/scheduler.cpp:23–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21
22
23void CScheduler::serviceQueue()
24{
25 WAIT_LOCK(newTaskMutex, lock);
26 ++nThreadsServicingQueue;
27
28 // newTaskMutex is locked throughout this loop EXCEPT
29 // when the thread is waiting or when the user's function
30 // is called.
31 while (!shouldStop()) {
32 try {
33 while (!shouldStop() && taskQueue.empty()) {
34 // Wait until there is something to do.
35 newTaskScheduled.wait(lock);
36 }
37
38 // Wait until either there is a new task, or until
39 // the time of the first item on the queue:
40
41 while (!shouldStop() && !taskQueue.empty()) {
42 std::chrono::steady_clock::time_point timeToWaitFor = taskQueue.begin()->first;
43 if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
44 break; // Exit loop after timeout, it means we reached the time of the event
45 }
46 }
47
48 // If there are multiple threads, the queue can empty while we're waiting (another
49 // thread may service the task we were waiting on).
50 if (shouldStop() || taskQueue.empty())
51 continue;
52
53 Function f = taskQueue.begin()->second;
54 taskQueue.erase(taskQueue.begin());
55
56 {
57 // Unlock before calling f, so it can reschedule itself or another task
58 // without deadlocking:
59 REVERSE_LOCK(lock, newTaskMutex);
60 f();
61 }
62 } catch (...) {
63 --nThreadsServicingQueue;
64 throw;
65 }
66 }
67 --nThreadsServicingQueue;
68 newTaskScheduled.notify_one();
69}
70
71void CScheduler::schedule(CScheduler::Function f, std::chrono::steady_clock::time_point t)
72{

Callers 4

AppInitMainFunction · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
ScopedSchedulerMethod · 0.80
ChainTestingSetupMethod · 0.80

Calls 5

emptyMethod · 0.45
waitMethod · 0.45
beginMethod · 0.45
wait_untilMethod · 0.45
eraseMethod · 0.45

Tested by 3

BOOST_AUTO_TEST_CASEFunction · 0.64
ScopedSchedulerMethod · 0.64
ChainTestingSetupMethod · 0.64