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

Method serviceQueue

src/scheduler.cpp:33–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31#endif
32
33void CScheduler::serviceQueue()
34{
35 boost::unique_lock<boost::mutex> lock(newTaskMutex);
36 ++nThreadsServicingQueue;
37
38 // newTaskMutex is locked throughout this loop EXCEPT
39 // when the thread is waiting or when the user's function
40 // is called.
41 while (!shouldStop()) {
42 try {
43 if (!shouldStop() && taskQueue.empty()) {
44 reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
45 // Use this chance to get a tiny bit more entropy
46 RandAddSeedSleep();
47 }
48 while (!shouldStop() && taskQueue.empty()) {
49 // Wait until there is something to do.
50 newTaskScheduled.wait(lock);
51 }
52
53 // Wait until either there is a new task, or until
54 // the time of the first item on the queue:
55
56// wait_until needs boost 1.50 or later; older versions have timed_wait:
57#if BOOST_VERSION < 105000
58 while (!shouldStop() && !taskQueue.empty() &&
59 newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
60 // Keep waiting until timeout
61 }
62#else
63 // Some boost versions have a conflicting overload of wait_until that returns void.
64 // Explicitly use a template here to avoid hitting that overload.
65 while (!shouldStop() && !taskQueue.empty()) {
66 boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
67 if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout)
68 break; // Exit loop after timeout, it means we reached the time of the event
69 }
70#endif
71 // If there are multiple threads, the queue can empty while we're waiting (another
72 // thread may service the task we were waiting on).
73 if (shouldStop() || taskQueue.empty())
74 continue;
75
76 Function f = taskQueue.begin()->second;
77 taskQueue.erase(taskQueue.begin());
78
79 {
80 // Unlock before calling f, so it can reschedule itself or another task
81 // without deadlocking:
82 reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
83 f();
84 }
85 } catch (...) {
86 --nThreadsServicingQueue;
87 throw;
88 }
89 }
90 --nThreadsServicingQueue;

Callers

nothing calls this directly

Calls 6

RandAddSeedSleepFunction · 0.85
toPosixTimeFunction · 0.85
waitMethod · 0.80
emptyMethod · 0.45
beginMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected