Much more precise timer functions

dev-own-engine
mbandic 6 months ago
parent 105c15b06b
commit 4d79b1459a
  1. 88
      lib/rotor.hpp
  2. 121
      test/test.cpp

@ -38,9 +38,10 @@ namespace {
class timer_core { class timer_core {
public: public:
mutex hangon; mutex hangon;
condition_variable cv;
function<void()> callback; function<void()> callback;
int64_t init;
int64_t time; int64_t time;
int64_t next;
bool repeat; bool repeat;
bool stop; bool stop;
@ -48,15 +49,17 @@ class timer_core {
* Timer constructor, receives a callback function and time * Timer constructor, receives a callback function and time
*/ */
timer_core( function<void()> _callback, int64_t _time, bool _repeat): timer_core( function<void()> _callback, int64_t _time, bool _repeat):
callback(_callback), init(rtime_us()), time(_time*1000), repeat(_repeat), stop(false) { callback(_callback), time(_time*1000), repeat(_repeat), stop(false) {
next = rtime_us() + time;
} }
/** /**
* Stop timer * Stop timer
*/ */
void clear() { void clear() {
lock_guard<mutex> hang(hangon); // lock_guard<mutex> hang(hangon);
stop = true; stop = true;
cv.notify_one();
} }
/** /**
@ -77,65 +80,58 @@ class rotor {
bool rotating = true; bool rotating = true;
int64_t sampling; int64_t sampling;
condition_variable te_cv;
/** /**
* Loop method, started by the constructor in a separate runner * Loop method, started by the constructor in a separate runner
* It checks the events on the stack and sends the expired ones to the runner * It checks the events on the stack and sends the expired ones to the runner
*/ */
void loop() { void loop() {
while (rotating) { while (rotating) {
for (int i=0; i<tcores.size(); i++) { vector<shared_ptr<timer_core>>::iterator next_tc;
shared_ptr<timer_core> next_ptr;
{
unique_lock<mutex> te_l(te_m);
te_cv.wait(te_l, [this]{ return !tcores.empty(); });
// calc_next();
if (tcores[i]->stop) { next_tc = min_element( tcores.begin(), tcores.end(),
remove(i); [](shared_ptr<timer_core> a, shared_ptr<timer_core> b ) {
i--; return a->next < b->next;
} }
);
else if (expired(tcores[i])) { next_ptr = *next_tc;
_asyncon.put_task(tcores[i]->callback); }
if (tcores[i]->repeat) {
tcores[i]->init = rtime_us(); unique_lock<mutex> next_l(next_ptr->hangon);
} next_ptr->cv.wait_for(next_l, chrono::microseconds(next_ptr->next - rtime_us()), [&next_ptr] () {
else { return next_ptr->stop;
remove(i); });
i--;
} if (next_ptr->stop) {
remove(next_tc);
} else {
_asyncon.put_task(next_ptr->callback);
if (next_ptr->repeat) {
next_ptr->next += next_ptr->time;
}
else {
remove(next_tc);
} }
} }
this_thread::sleep_for(chrono::microseconds(sampling));
}
}
/** }
* The method checks whether the time event has expired
*/
bool expired(shared_ptr<timer_core> tcore) {
return rtime_us() - tcore->init >= tcore->time;
} }
/** /**
* The method deletes a non-repeating or stopped event from the stack * The method deletes a non-repeating or stopped event from the stack
*/ */
void remove(const int& position) { void remove(vector<shared_ptr<timer_core>>::iterator it) {
lock_guard<mutex> lock(te_m); lock_guard<mutex> lock(te_m);
tcores.erase(tcores.begin()+position); tcores.erase(it);
update_sampling(); te_cv.notify_one();
}
/**
* Updates the idle time of the loop, according to twice the frequency of available events
*/
void update_sampling() {
if (tcores.empty()) {
sampling = 100;
return;
}
sampling = tcores[0]->time;
for (int i=0; i<tcores.size(); i++) {
if (sampling > tcores[i]->time) {
sampling = tcores[i]->time;
}
}
sampling /= tcores.size()*2;
} }
public: public:
@ -155,7 +151,7 @@ class rotor {
void insert(shared_ptr<timer_core> tcore) { void insert(shared_ptr<timer_core> tcore) {
lock_guard<mutex> lock(te_m); lock_guard<mutex> lock(te_m);
tcores.push_back(tcore); tcores.push_back(tcore);
update_sampling(); te_cv.notify_one();
}; };
/** /**

@ -61,11 +61,11 @@ int main () {
auto start = rtime_ms(); auto start = rtime_ms();
// // --------------- TIME ASYNCHRONOUS FUNCTIONS -------------- // --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
// /** /**
// * Init interval and timeout; clear interval and timeout * Init interval and timeout; clear interval and timeout
// */ */
// interval( [&] () { // interval( [&] () {
// cout << "interval 1: " << rtime_ms() - start << endl; // cout << "interval 1: " << rtime_ms() - start << endl;
@ -88,17 +88,17 @@ int main () {
// cout << "interval 4: " << rtime_ms() - start << endl; // cout << "interval 4: " << rtime_ms() - start << endl;
// }, 400); // }, 400);
// interval inter1 ([&]() { interval inter1 ([&]() {
// cout << "interval prvi " << rtime_ms() - start << endl; cout << "interval prvi " << rtime_ms() - start << endl;
// }, 1000); }, 1000);
// interval inter2 ([&]() { interval inter2 ([&]() {
// cout << "interval drugi " << rtime_ms() - start << endl; cout << "interval drugi " << rtime_ms() - start << endl;
// }, 2000); }, 2000);
// interval inter3 ([&]() { interval inter3 ([&]() {
// cout << "interval treći " << rtime_ms() - start << endl; cout << "interval treći " << rtime_ms() - start << endl;
// }, 3000); }, 3000);
// interval inter4 ([&]() { // interval inter4 ([&]() {
// cout << "interval cetvrti " << rtime_ms() - start << endl; // cout << "interval cetvrti " << rtime_ms() - start << endl;
@ -114,17 +114,18 @@ int main () {
// timeout time1 ( [&] () { // timeout time1 ( [&] () {
// cout << "Close interval 1 i 2 " << rtime_ms() - start << endl; // cout << "Close interval 1 i 2 " << rtime_ms() - start << endl;
// // inter1.clear(); // inter1.clear();
// // cout << "inter1.stop " << inter1.stop << endl; // // cout << "inter1.stop " << inter1.stop << endl;
// // inter2.clear(); // inter2.clear();
// // cout << "inter2.stop " << inter2.stop << endl; // // cout << "inter2.stop " << inter2.stop << endl;
// }, 5000); // }, 5000);
// timeout time2 ([&] () { // timeout time2 ([&] () {
// cout << "Close interval 3 " << rtime_ms() - start << endl; // cout << "Close interval 3 " << rtime_ms() - start << endl;
// // inter3.clear(); // // inter3.clear();
// // time1.clear(); // time1.clear();
// }, 2000); // }, 2000);
// // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
@ -218,68 +219,68 @@ int main () {
// }); // });
// }); // });
// --------------- EVENTS ------------------- // // --------------- EVENTS -------------------
/** // /**
* initialization of typed events // * initialization of typed events
*/ // */
event<int, int> ev2int; // event<int, int> ev2int;
event<int, string> evintString; // event<int, string> evintString;
event<> evoid; // event<> evoid;
ev2int.on("sum", [](int a, int b) { // ev2int.on("sum", [](int a, int b) {
cout << "Sum " << a+b << endl; // cout << "Sum " << a+b << endl;
}); // });
ev2int.on("sum", [](int a, int b) { // ev2int.on("sum", [](int a, int b) {
cout << "Sum done" << endl; // cout << "Sum done" << endl;
}); // });
evintString.on("substract", [](int a, string b) { // evintString.on("substract", [](int a, string b) {
cout << "Substract " << a-stoi(b) << endl; // cout << "Substract " << a-stoi(b) << endl;
}); // });
evoid.on("void", []() { // evoid.on("void", []() {
cout << "Void emited" << endl; // cout << "Void emited" << endl;
}); // });
string emited2 = "2"; // string emited2 = "2";
evoid.on("void", [&]() { // evoid.on("void", [&]() {
cout << "Void emited " << emited2 << endl; // cout << "Void emited " << emited2 << endl;
}); // });
evoid.emit("void"); // evoid.emit("void");
sleep(1); // sleep(1);
/** // /**
* Emit // * Emit
*/ // */
ev2int.emit("sum", 5, 8); // ev2int.emit("sum", 5, 8);
sleep(1); // sleep(1);
evintString.emit("substract", 3, to_string(2)); // evintString.emit("substract", 3, to_string(2));
sleep(1); // sleep(1);
evoid.off("void"); // evoid.off("void");
evoid.emit("void"); // evoid.emit("void");
/** // /**
* Own class // * Own class
*/ // */
myOwnClass myclass; // myOwnClass myclass;
timeout t( [&] { // timeout t( [&] {
myclass.emit("constructed", 1); // myclass.emit("constructed", 1);
}, 200); // }, 200);
myclass.on("constructed", [] (int i) { // myclass.on("constructed", [] (int i) {
cout << "Constructed " << i << endl; // cout << "Constructed " << i << endl;
}); // });

Loading…
Cancel
Save