25 #ifndef __I6ENGINE_CORE_SCHEDULER_H__
26 #define __I6ENGINE_CORE_SCHEDULER_H__
37 #include "boost/thread.hpp"
42 template<
class ClockUpdater>
58 boost::function<bool(void)>
func;
84 if (time != other.
time) {
85 return time > other.
time;
110 while (!_queue.empty()) {
116 _workerThreads[i].second->join();
117 delete _workerThreads[i].second;
134 Job j(f, _clock.
getTime() + time, priority, _id++);
135 boost::mutex::scoped_lock sl(_lock);
137 if (_queue.top().time == j.time) {
156 ISIXE_THROW_API(
"Scheduler",
"interval has to be greater than 0, otherwise there would be an instant call");
159 Job j(f, _clock.
getTime() + interval, priority, _id++, interval);
160 boost::mutex::scoped_lock sl(_lock);
162 if (_queue.top().time == j.time) {
176 std::priority_queue<Job> copy = _queue;
179 while (!copy.empty()) {
195 boost::mutex::scoped_lock sl(_lock);
196 std::priority_queue<Job> copy = _queue;
198 while (!_queue.empty()) {
202 while (!copy.empty()) {
217 boost::mutex::scoped_lock sl(_lock);
218 _removeIDs.insert(
id);
227 void worker(uint64_t tid) {
230 while (!_queue.empty() && _queue.top().time <= _clock.
getTime()) {
231 Job j = _queue.top();
233 if (_removeIDs.find(j.id) != _removeIDs.end()) {
234 _removeIDs.erase(j.id);
240 if (j.interval != UINT64_MAX && b) {
241 j.time = _clock.
getTime() + j.interval;
242 if (_removeIDs.find(j.id) != _removeIDs.end()) {
243 _removeIDs.erase(j.id);
249 uint64_t t = _clock.
getTime() + 1000000;
250 if (!_queue.empty()) {
251 t = _queue.top().time;
263 std::atomic<bool> _running;
268 utils::Clock<ClockUpdater> & _clock;
273 std::priority_queue<Job> _queue;
278 mutable boost::mutex _lock;
280 std::atomic<uint64_t> _id;
285 std::vector<std::pair<uint64_t, boost::thread *>> _workerThreads;
287 std::set<uint64_t> _removeIDs;
const uint16_t SCHEDULER_THREAD_AMOUNT
uint64_t id
id of this job
uint64_t runRepeated(uint64_t interval, const boost::function< bool(void)> &f, JobPriorities priority)
starts a timer repeating in the given interval
uint64_t interval
interval in which this job is repeated, LONG_MAX if only once
#define ISIXE_THROW_API(module, message)
void updateWaitTime(uint64_t timerID, uint64_t time)
updates the time a timer is waiting for
bool operator<(const Job &other) const
operator for lower than, needed for priority_queue
uint64_t getTimeLeft(uint64_t id) const
returns time left until execution of the job if no job with given name found, -1 is returned ...
uint64_t runOnce(uint64_t time, const boost::function< bool(void)> &f, JobPriorities priority)
starts a timer beeing scheduled after the given time
uint64_t registerTimer()
registers a new timer waiting for this clock
bool waitForTime(uint64_t timerID, uint64_t time)
let's a timer wait for the given time
this struct represents a single timer event
uint64_t time
time this job will be scheduled
struct i6e::core::Scheduler::Job Job
this struct represents a single timer event
Job(const boost::function< bool(void)> &f, uint64_t t, JobPriorities p, uint64_t d, uint64_t i=UINT64_MAX)
constructor of the timer
uint64_t getTime() const
Will return the time since the Clock has been started.
bool stop(uint64_t id)
removes Timer registered for given id returns true, if Timer was found, otherwise false ...
Scheduler(utils::Clock< ClockUpdater > &c)
constructor for scheduler taking a clock
boost::function< bool(void)> func
method called by this timer
void removeTimer(JobPriorities priority)
removes all timers with given priority
JobPriorities priority
priority of this job