|
| 1 | +// Copyright (c) 2015 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "scheduler.h" |
| 6 | + |
| 7 | +#include <assert.h> |
| 8 | +#include <boost/bind.hpp> |
| 9 | +#include <utility> |
| 10 | + |
| 11 | +CScheduler::CScheduler() : nThreadsServicingQueue(0) |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +CScheduler::~CScheduler() |
| 16 | +{ |
| 17 | + assert(nThreadsServicingQueue == 0); |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +#if BOOST_VERSION < 105000 |
| 22 | +static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t) |
| 23 | +{ |
| 24 | + boost::chrono::system_clock::duration d = t.time_since_epoch(); |
| 25 | + boost::chrono::microseconds usecs = boost::chrono::duration_cast<boost::chrono::microseconds>(d); |
| 26 | + boost::system_time result = boost::posix_time::from_time_t(0) + |
| 27 | + boost::posix_time::microseconds(usecs.count()); |
| 28 | + return result; |
| 29 | +} |
| 30 | +#endif |
| 31 | + |
| 32 | +void CScheduler::serviceQueue() |
| 33 | +{ |
| 34 | + boost::unique_lock<boost::mutex> lock(newTaskMutex); |
| 35 | + ++nThreadsServicingQueue; |
| 36 | + |
| 37 | + // newTaskMutex is locked throughout this loop EXCEPT |
| 38 | + // when the thread is waiting or when the user's function |
| 39 | + // is called. |
| 40 | + while (1) { |
| 41 | + try { |
| 42 | + while (taskQueue.empty()) { |
| 43 | + // Wait until there is something to do. |
| 44 | + newTaskScheduled.wait(lock); |
| 45 | + } |
| 46 | +// Wait until either there is a new task, or until |
| 47 | +// the time of the first item on the queue: |
| 48 | + |
| 49 | +// wait_until needs boost 1.50 or later; older versions have timed_wait: |
| 50 | +#if BOOST_VERSION < 105000 |
| 51 | + while (!taskQueue.empty() && newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) { |
| 52 | + // Keep waiting until timeout |
| 53 | + } |
| 54 | +#else |
| 55 | + while (!taskQueue.empty() && newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { |
| 56 | + // Keep waiting until timeout |
| 57 | + } |
| 58 | +#endif |
| 59 | + // If there are multiple threads, the queue can empty while we're waiting (another |
| 60 | + // thread may service the task we were waiting on). |
| 61 | + if (taskQueue.empty()) |
| 62 | + continue; |
| 63 | + |
| 64 | + Function f = taskQueue.begin()->second; |
| 65 | + taskQueue.erase(taskQueue.begin()); |
| 66 | + |
| 67 | + // Unlock before calling f, so it can reschedule itself or another task |
| 68 | + // without deadlocking: |
| 69 | + lock.unlock(); |
| 70 | + f(); |
| 71 | + lock.lock(); |
| 72 | + } catch (...) { |
| 73 | + --nThreadsServicingQueue; |
| 74 | + throw; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t) |
| 80 | +{ |
| 81 | + { |
| 82 | + boost::unique_lock<boost::mutex> lock(newTaskMutex); |
| 83 | + taskQueue.insert(std::make_pair(t, f)); |
| 84 | + } |
| 85 | + newTaskScheduled.notify_one(); |
| 86 | +} |
| 87 | + |
| 88 | +void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds) |
| 89 | +{ |
| 90 | + schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds)); |
| 91 | +} |
| 92 | + |
| 93 | +static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds) |
| 94 | +{ |
| 95 | + f(); |
| 96 | + s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds); |
| 97 | +} |
| 98 | + |
| 99 | +void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds) |
| 100 | +{ |
| 101 | + scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds); |
| 102 | +} |
0 commit comments