You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

97 lines
3.8 KiB

#include "scheduler.h"
#include <GLFW/glfw3.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
std::shared_ptr<timer_handle> Scheduler::setTimeout(std::function<void()> callback, unsigned long delay) {
double now = glfwGetTime() * 1000;
std::shared_ptr<timer_handle> handle = std::make_shared<timer_handle>();
handle->callback = callback;
handle->nextAt = now + delay;
handle->interval = 0;
//handle->timeout = true;
this->timers.push_back(handle);
return handle;
}
std::shared_ptr<timer_handle> Scheduler::setInterval(std::function<void()> callback, unsigned long delay) {
double now = glfwGetTime() * 1000;
//std::cout << "Scheduler::setInterval at " << (int)now << std::endl;
std::shared_ptr<timer_handle> handle = std::make_shared<timer_handle>();
handle->callback = callback;
handle->nextAt = now + delay;
handle->interval = delay;
//handle->timeout = false;
this->timers.push_back(handle);
//std::cout << "scheduled at " << (int)handle->nextAt << std::endl;
return handle;
}
unsigned long Scheduler::getNext() {
unsigned long lowest = LONG_MAX;
double now = glfwGetTime() * 1000; // it's number of ms since start up
//std::cout << "there are " << this->timers.size() << std::endl;
for(std::vector<std::shared_ptr<timer_handle>>::iterator it=this->timers.begin(); it!=this->timers.end(); ++it) {
//std::cout << "nextAt " << (int)it->get()->nextAt << " long " << (int)(now - it->get()->nextAt) << std::endl;
//std::cout << "now " << (int)now << " - nextAt: " << (int)it->get()->nextAt << std::endl;
int diff = it->get()->nextAt - now;
if (diff < 0) diff = 0;
//std::cout << "timer fires in " << diff << std::endl;
lowest = std::min(lowest, static_cast<unsigned long>(diff));
}
//std::cout << "returning " << lowest << std::endl;
return lowest;
}
bool Scheduler::fireTimer(std::shared_ptr<timer_handle> timer, double now) {
timer->callback();
if (!timer->interval) {
return true;
}
// schedule the next one
//std::cout << "Scheduling next timer in " << timer->interval << "+" << now << "=" << (now + timer->interval) << std::endl;
timer->nextAt = now + timer->interval;
return false;
}
void Scheduler::fireTimers() {
double now = glfwGetTime() * 1000;
size_t unfired = 0;
int next = INT_MAX;
for(std::vector<std::shared_ptr<timer_handle>>::iterator it=this->timers.begin(); it!=this->timers.end(); ++it) {
// if it's the timer time to fire
int left = abs(static_cast<int>(it->get()->nextAt - now)); // maybe remove the ABS
if (left <= 10 || it->get()->nextAt < now) {
// fire it, do we need to clean it up
if (this->fireTimer(*it, now)) {
// clean up
it = this->timers.erase(it);
if (it == this->timers.end()) break;
}
} else {
//std::cout << "left" << left << std::endl;
next = std::min(left, next);
unfired++;
}
}
if (unfired) {
if (next < 10) {
std::cout << "There are " << unfired << "/" << this->timers.size() << " unfired timers left, next at " << next << std::endl;
std::cout << "Scheduler::fireTimers - failed" << std::endl;
}
}
}
bool Scheduler::clearInterval(std::shared_ptr<timer_handle> timer) {
// find and remove it
//it2 = std::find(this->rootComponent->children.begin(), this->rootComponent->children.end(), it->get()->selectorBox);
std::vector<std::shared_ptr<timer_handle>>::iterator it = std::find(this->timers.begin(), this->timers.end(), timer);
if (it != this->timers.end()) {
//std::cout << "Clearing Interval" << std::endl;
this->timers.erase(it);
} else {
std::cout << "Couldnt find timer" << std::endl;
}
return true;
}