66#include " scheduler.h"
77
88#include " random.h"
9- #include " reverselock.h"
109
1110#include < assert.h>
1211#include < utility>
@@ -22,7 +21,7 @@ CScheduler::~CScheduler()
2221
2322void CScheduler::serviceQueue ()
2423{
25- boost::unique_lock<boost::mutex> lock (newTaskMutex);
24+ WAIT_LOCK (newTaskMutex, lock );
2625 ++nThreadsServicingQueue;
2726
2827 // newTaskMutex is locked throughout this loop EXCEPT
@@ -31,7 +30,7 @@ void CScheduler::serviceQueue()
3130 while (!shouldStop ()) {
3231 try {
3332 if (!shouldStop () && taskQueue.empty ()) {
34- reverse_lock<boost::unique_lock<boost::mutex> > rlock (lock);
33+ REVERSE_LOCK (lock);
3534 }
3635 while (!shouldStop () && taskQueue.empty ()) {
3736 // Wait until there is something to do.
@@ -41,12 +40,13 @@ void CScheduler::serviceQueue()
4140 // Wait until either there is a new task, or until
4241 // the time of the first item on the queue:
4342
44- // Some boost versions have a conflicting overload of wait_until that returns void.
45- // Explicitly use a template here to avoid hitting that overload.
46- while (! shouldStop () && !taskQueue. empty () &&
47- newTaskScheduled. wait_until <>(lock, taskQueue. begin ()-> first ) != boost::cv_status:: timeout) {
48- // Keep waiting until timeout
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+ }
4948 }
49+
5050 // If there are multiple threads, the queue can empty while we're waiting (another
5151 // thread may service the task we were waiting on).
5252 if (shouldStop () || taskQueue.empty ())
@@ -58,7 +58,7 @@ void CScheduler::serviceQueue()
5858 {
5959 // Unlock before calling f, so it can reschedule itself or another task
6060 // without deadlocking:
61- reverse_lock<boost::unique_lock<boost::mutex> > rlock (lock);
61+ REVERSE_LOCK (lock);
6262 f ();
6363 }
6464 } catch (...) {
@@ -73,7 +73,7 @@ void CScheduler::serviceQueue()
7373void CScheduler::stop (bool drain)
7474{
7575 {
76- boost::unique_lock<boost::mutex> lock (newTaskMutex);
76+ LOCK (newTaskMutex);
7777 if (drain)
7878 stopWhenEmpty = true ;
7979 else
@@ -82,18 +82,18 @@ void CScheduler::stop(bool drain)
8282 newTaskScheduled.notify_all ();
8383}
8484
85- void CScheduler::schedule (CScheduler::Function f, boost ::chrono::system_clock::time_point t)
85+ void CScheduler::schedule (CScheduler::Function f, std ::chrono::system_clock::time_point t)
8686{
8787 {
88- boost::unique_lock<boost::mutex> lock (newTaskMutex);
88+ LOCK (newTaskMutex);
8989 taskQueue.emplace (t, f);
9090 }
9191 newTaskScheduled.notify_one ();
9292}
9393
9494void CScheduler::scheduleFromNow (CScheduler::Function f, int64_t deltaMilliSeconds)
9595{
96- schedule (f, boost ::chrono::system_clock::now () + boost ::chrono::milliseconds (deltaMilliSeconds));
96+ schedule (f, std ::chrono::system_clock::now () + std ::chrono::milliseconds (deltaMilliSeconds));
9797}
9898
9999static void Repeat (CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
@@ -107,10 +107,10 @@ void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds
107107 scheduleFromNow (std::bind (&Repeat, this , f, deltaMilliSeconds), deltaMilliSeconds);
108108}
109109
110- size_t CScheduler::getQueueInfo (boost ::chrono::system_clock::time_point &first,
111- boost ::chrono::system_clock::time_point &last) const
110+ size_t CScheduler::getQueueInfo (std ::chrono::system_clock::time_point &first,
111+ std ::chrono::system_clock::time_point &last) const
112112{
113- boost::unique_lock<boost::mutex> lock (newTaskMutex);
113+ LOCK (newTaskMutex);
114114 size_t result = taskQueue.size ();
115115 if (!taskQueue.empty ()) {
116116 first = taskQueue.begin ()->first ;
@@ -120,7 +120,7 @@ size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
120120}
121121
122122bool CScheduler::AreThreadsServicingQueue () const {
123- boost::unique_lock<boost::mutex> lock (newTaskMutex);
123+ LOCK (newTaskMutex);
124124 return nThreadsServicingQueue;
125125}
126126
@@ -133,7 +133,7 @@ void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue() {
133133 if (m_are_callbacks_running) return ;
134134 if (m_callbacks_pending.empty ()) return ;
135135 }
136- m_pscheduler->schedule (std::bind (&SingleThreadedSchedulerClient::ProcessQueue, this ));
136+ m_pscheduler->schedule (std::bind (&SingleThreadedSchedulerClient::ProcessQueue, this ), std::chrono::system_clock::now () );
137137}
138138
139139void SingleThreadedSchedulerClient::ProcessQueue () {
0 commit comments