PIVX Core  5.6.99
P2P Digital Currency
scheduler.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "scheduler.h"
7 
8 #include "random.h"
9 
10 #include <assert.h>
11 #include <utility>
12 
13 CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
14 {
15 }
16 
18 {
19  assert(nThreadsServicingQueue == 0);
20 }
21 
23 {
24  WAIT_LOCK(newTaskMutex, lock);
25  ++nThreadsServicingQueue;
26 
27  // newTaskMutex is locked throughout this loop EXCEPT
28  // when the thread is waiting or when the user's function
29  // is called.
30  while (!shouldStop()) {
31  try {
32  if (!shouldStop() && taskQueue.empty()) {
33  REVERSE_LOCK(lock);
34  }
35  while (!shouldStop() && taskQueue.empty()) {
36  // Wait until there is something to do.
37  newTaskScheduled.wait(lock);
38  }
39 
40  // Wait until either there is a new task, or until
41  // the time of the first item on the queue:
42 
43  while (!shouldStop() && !taskQueue.empty()) {
44  std::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
45  if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
46  break; // Exit loop after timeout, it means we reached the time of the event
47  }
48  }
49 
50  // If there are multiple threads, the queue can empty while we're waiting (another
51  // thread may service the task we were waiting on).
52  if (shouldStop() || taskQueue.empty())
53  continue;
54 
55  Function f = taskQueue.begin()->second;
56  taskQueue.erase(taskQueue.begin());
57 
58  {
59  // Unlock before calling f, so it can reschedule itself or another task
60  // without deadlocking:
61  REVERSE_LOCK(lock);
62  f();
63  }
64  } catch (...) {
65  --nThreadsServicingQueue;
66  throw;
67  }
68  }
69  --nThreadsServicingQueue;
70  newTaskScheduled.notify_one();
71 }
72 
73 void CScheduler::stop(bool drain)
74 {
75  {
77  if (drain)
78  stopWhenEmpty = true;
79  else
80  stopRequested = true;
81  }
82  newTaskScheduled.notify_all();
83 }
84 
86 {
87  {
89  taskQueue.emplace(t, f);
90  }
91  newTaskScheduled.notify_one();
92 }
93 
94 void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSeconds)
95 {
96  schedule(f, std::chrono::system_clock::now() + std::chrono::milliseconds(deltaMilliSeconds));
97 }
98 
99 static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
100 {
101  f();
102  s->scheduleFromNow(std::bind(&Repeat, s, f, deltaMilliSeconds), deltaMilliSeconds);
103 }
104 
105 void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds)
106 {
107  scheduleFromNow(std::bind(&Repeat, this, f, deltaMilliSeconds), deltaMilliSeconds);
108 }
109 
112 {
114  size_t result = taskQueue.size();
115  if (!taskQueue.empty()) {
116  first = taskQueue.begin()->first;
117  last = taskQueue.rbegin()->first;
118  }
119  return result;
120 }
121 
124  return nThreadsServicingQueue;
125 }
126 
128  {
130  // Try to avoid scheduling too many copies here, but if we
131  // accidentally have two ProcessQueue's scheduled at once its
132  // not a big deal.
133  if (m_are_callbacks_running) return;
134  if (m_callbacks_pending.empty()) return;
135  }
136  m_pscheduler->schedule(std::bind(&SingleThreadedSchedulerClient::ProcessQueue, this), std::chrono::system_clock::now());
137 }
138 
140  std::function<void (void)> callback;
141  {
143  if (m_are_callbacks_running) return;
144  if (m_callbacks_pending.empty()) return;
146 
147  callback = std::move(m_callbacks_pending.front());
148  m_callbacks_pending.pop_front();
149  }
150 
151  // RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
152  // to ensure both happen safely even if callback() throws.
153  struct RAIICallbacksRunning {
155  explicit RAIICallbacksRunning(SingleThreadedSchedulerClient* _instance) : instance(_instance) {}
156  ~RAIICallbacksRunning() {
157  {
158  LOCK(instance->m_cs_callbacks_pending);
159  instance->m_are_callbacks_running = false;
160  }
161  instance->MaybeScheduleProcessQueue();
162  }
163  } raiicallbacksrunning(this);
164 
165  callback();
166 }
167 
168 void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void (void)> func) {
169  assert(m_pscheduler);
170 
171  {
173  m_callbacks_pending.emplace_back(std::move(func));
174  }
176 }
177 
180  bool should_continue = true;
181  while (should_continue) {
182  ProcessQueue();
184  should_continue = !m_callbacks_pending.empty();
185  }
186 }
187 
190  return m_callbacks_pending.size();
191 }
false
Definition: bls_dkg.cpp:151
void serviceQueue()
Definition: scheduler.cpp:22
void stop(bool drain=false)
Definition: scheduler.cpp:73
void schedule(Function f, std::chrono::system_clock::time_point t)
Definition: scheduler.cpp:85
std::function< void(void)> Function
Definition: scheduler.h:45
void scheduleFromNow(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:94
size_t getQueueInfo(std::chrono::system_clock::time_point &first, std::chrono::system_clock::time_point &last) const
Definition: scheduler.cpp:110
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:105
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
Definition: scheduler.h:86
bool AreThreadsServicingQueue() const
Definition: scheduler.cpp:122
std::condition_variable newTaskScheduled
Definition: scheduler.h:81
Mutex newTaskMutex
Definition: scheduler.h:80
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:99
RecursiveMutex m_cs_callbacks_pending
Definition: scheduler.h:103
std::list< std::function< void(void)> > m_callbacks_pending
Definition: scheduler.h:104
void AddToProcessQueue(std::function< void(void)> func)
Add a callback to be executed.
Definition: scheduler.cpp:168
@ LOCK
Definition: lockunlock.h:16
clock::time_point time_point
Definition: bench.h:48
#define WAIT_LOCK(cs, name)
Definition: sync.h:225
#define REVERSE_LOCK(g)
Definition: sync.h:215