mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
A job queue that will allow us to have a configurable number of threads servicing
arbitrary jobs in priority order, with an easy way to get counts of how many jobs are pending.
This commit is contained in:
80
src/JobQueue.h
Normal file
80
src/JobQueue.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef JOB_QUEUE__H
|
||||
#define JOB_QUEUE__H
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
enum JobType
|
||||
{ // must be in priority order, low to high
|
||||
jtINVALID,
|
||||
jtVALIDATION_ut,
|
||||
jtTRANSACTION,
|
||||
jtPROPOSAL_ut,
|
||||
jtVALIDATION_t,
|
||||
jtPROPOSAL_t,
|
||||
jtADMIN,
|
||||
jtDEATH, // job of death, used internally
|
||||
};
|
||||
|
||||
class Job
|
||||
{
|
||||
protected:
|
||||
JobType mType;
|
||||
uint64 mJobIndex;
|
||||
boost::function<void(void)> mJob;
|
||||
|
||||
public:
|
||||
Job() : mType(jtINVALID), mJobIndex(0) { ; }
|
||||
Job(JobType type, uint64 index) : mType(type), mJobIndex(index) { ; }
|
||||
|
||||
Job(JobType type, uint64 index, const boost::function<void(void)>& job)
|
||||
: mType(type), mJobIndex(index), mJob(job) { ; }
|
||||
|
||||
JobType getType() const { return mType; }
|
||||
void setIndex(uint64 i) { mJobIndex = i; }
|
||||
void doJob(void) { mJob(); }
|
||||
|
||||
bool operator<(const Job& j) const;
|
||||
bool operator>(const Job& j) const;
|
||||
bool operator<=(const Job& j) const;
|
||||
bool operator>=(const Job& j) const;
|
||||
|
||||
static const char* toString(JobType);
|
||||
};
|
||||
|
||||
class JobQueue
|
||||
{
|
||||
protected:
|
||||
boost::mutex mJobLock;
|
||||
boost::condition_variable mJobCond;
|
||||
|
||||
uint64 mLastJob;
|
||||
std::set<Job> mJobSet;
|
||||
std::map<JobType, int> mJobCounts;
|
||||
int mThreadCount;
|
||||
bool mShuttingDown;
|
||||
|
||||
|
||||
void threadEntry(void);
|
||||
|
||||
public:
|
||||
|
||||
JobQueue() : mLastJob(0), mThreadCount(0), mShuttingDown(false) { ; }
|
||||
|
||||
void addJob(JobType type, const boost::function<void(void)>& job);
|
||||
|
||||
int getJobCount(JobType t); // All jobs at or greater than this priority
|
||||
std::vector< std::pair<JobType, int> > getJobCounts();
|
||||
|
||||
void shutdown();
|
||||
void setThreadCount(int c);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user