b4a3da2642
These are technically static libs, but not in any way shared libs. They are used solely only by this repo and really only by the hub. Most important, no header files are installed and basically none of the normal rules for reusable libraries are applied to these files.
98 lines
3.3 KiB
C++
98 lines
3.3 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2015 The Bitcoin Core developers
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef FLOWEE_SCHEDULER_H
|
|
#define FLOWEE_SCHEDULER_H
|
|
|
|
//
|
|
// NOTE:
|
|
// boost::thread / boost::function / boost::chrono should be ported to
|
|
// std::thread / std::function / std::chrono when we support C++11.
|
|
//
|
|
#include <boost/function.hpp>
|
|
#include <boost/chrono/chrono.hpp>
|
|
#include <boost/thread.hpp>
|
|
#include <map>
|
|
|
|
//
|
|
// Simple class for background tasks that should be run
|
|
// periodically or once "after a while"
|
|
//
|
|
// Usage:
|
|
//
|
|
// CScheduler* s = new CScheduler();
|
|
// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
|
|
// s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
|
|
// boost::thread* t = new boost::thread(std::bind(CScheduler::serviceQueue, s));
|
|
//
|
|
// ... then at program shutdown, clean up the thread running serviceQueue:
|
|
// t->interrupt();
|
|
// t->join();
|
|
// delete t;
|
|
// delete s; // Must be done after thread is interrupted/joined.
|
|
//
|
|
|
|
class CScheduler
|
|
{
|
|
public:
|
|
CScheduler();
|
|
~CScheduler();
|
|
|
|
typedef boost::function<void(void)> Function;
|
|
|
|
// Call func at/after time t
|
|
void schedule(Function f, boost::chrono::system_clock::time_point t);
|
|
|
|
// Convenience method: call f once deltaSeconds from now
|
|
void scheduleFromNow(Function f, int64_t deltaSeconds);
|
|
|
|
// Another convenience method: call f approximately
|
|
// every deltaSeconds forever, starting deltaSeconds from now.
|
|
// To be more precise: every time f is finished, it
|
|
// is rescheduled to run deltaSeconds later. If you
|
|
// need more accurate scheduling, don't use this method.
|
|
void scheduleEvery(Function f, int64_t deltaSeconds);
|
|
|
|
// To keep things as simple as possible, there is no unschedule.
|
|
|
|
// Services the queue 'forever'. Should be run in a thread,
|
|
// and interrupted using boost::interrupt_thread
|
|
void serviceQueue();
|
|
|
|
// Tell any threads running serviceQueue to stop as soon as they're
|
|
// done servicing whatever task they're currently servicing (drain=false)
|
|
// or when there is no work left to be done (drain=true)
|
|
void stop(bool drain=false);
|
|
|
|
// Returns number of tasks waiting to be serviced,
|
|
// and first and last task times
|
|
size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
|
|
boost::chrono::system_clock::time_point &last) const;
|
|
|
|
private:
|
|
std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
|
|
boost::condition_variable newTaskScheduled;
|
|
mutable boost::mutex newTaskMutex;
|
|
int nThreadsServicingQueue;
|
|
bool stopRequested;
|
|
bool stopWhenEmpty;
|
|
bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
|
|
};
|
|
|
|
#endif
|