diff --git a/lib/rotor.hpp b/lib/rotor.hpp deleted file mode 100644 index 3b86959..0000000 --- a/lib/rotor.hpp +++ /dev/null @@ -1,239 +0,0 @@ -#ifndef _ROTOR_ -#define _ROTOT_ - -#include "runner.hpp" -#include "chrono" -#include - -#include "iostream" - -using namespace std; -using namespace marcelb; -using namespace asynco; - -namespace marcelb { -namespace asynco { - -/** - * Get the time in ms from the epoch -*/ - -int64_t rtime_ms() { - return chrono::duration_cast(chrono::system_clock::now() - .time_since_epoch()) - .count(); -} - -int64_t rtime_us() { - return chrono::duration_cast(chrono::system_clock::now() - .time_since_epoch()) - .count(); -} - -namespace { - -/** - * Intern class for timer async loop -*/ -class timer_core { - public: - mutex hangon; - condition_variable cv; - function callback; - int64_t time; - int64_t next; - bool repeat; - bool stop; - - /** - * Timer constructor, receives a callback function and time - */ - timer_core( function _callback, int64_t _time, bool _repeat): - callback(_callback), time(_time*1000), repeat(_repeat), stop(false) { - next = rtime_us() + time; - } - - /** - * Stop timer - */ - void clear() { - // lock_guard hang(hangon); - stop = true; - cv.notify_one(); - } - - /** - * Destruktor of timer, call stop - */ - ~timer_core() { - clear(); - } -}; - -/** - * Event loop for time events -*/ - -class rotor { - vector> tcores; - mutex te_m; - bool rotating = true; - int64_t sampling; - - condition_variable te_cv; - - /** - * 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 - */ - void loop() { - while (rotating) { - vector>::iterator next_tc; - shared_ptr next_ptr; - - { - unique_lock te_l(te_m); - te_cv.wait(te_l, [this]{ return !tcores.empty() || rotating; }); - if (!rotating) { - break; - } - - next_tc = min_element( tcores.begin(), tcores.end(), - [](shared_ptr a, shared_ptr b ) { - return a->next < b->next; - } - ); - - next_ptr = *next_tc; - } - - unique_lock next_l(next_ptr->hangon); - next_ptr->cv.wait_for(next_l, chrono::microseconds(next_ptr->next - rtime_us()), [&next_ptr] () { - return next_ptr->stop; - }); - - 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); - } - } - - } - } - - /** - * The method deletes a non-repeating or stopped event from the stack - */ - void remove(vector>::iterator it) { - lock_guard lock(te_m); - tcores.erase(it); - // te_cv.notify_one(); - } - - public: - - /** - * Constructor for the rotor, starts the given loop by occupying one runner - */ - rotor() { - _asyncon.put_task( [&] () { - loop(); - }); - }; - - /** - * Adds a time event to the stack - */ - void insert(shared_ptr tcore) { - lock_guard lock(te_m); - tcores.push_back(tcore); - te_cv.notify_one(); - }; - - /** - * Returns the number of active events - */ - int active() { - return tcores.size(); - } - - /** - * Stops all active events and stops the rotor - */ - ~rotor() { - for (int i=0; iclear(); - } - rotating = false; - } - -}; - -} - - -/** - * It is intended that there is only one global declaration -*/ -static rotor _rotor; - -/** - * Core class for pure async timer functions -*/ - -class _timer_intern { - shared_ptr tcore; - public: - - _timer_intern(function _callback, int64_t _time, bool repeat) { - tcore = make_shared(_callback, _time, repeat); - _rotor.insert(tcore); - } - - /** - * Stop interval - */ - void clear() { - tcore->clear(); - } -}; - -/** - * Class interval for periodic execution of the callback in time in ms -*/ -class interval : public _timer_intern { - public: - - /** - * The constructor receives a callback function and an interval time - */ - interval( function _callback, int64_t _time): - _timer_intern(_callback, _time, true) { - } -}; - -/** - * Class interval for delayed callback execution in ms -*/ -class timeout : public _timer_intern { - public: - - /** - * The constructor receives a callback function and a delay time - */ - timeout( function _callback, int64_t delay): - _timer_intern(_callback, delay, false) { - } - -}; - -} -} - -#endif diff --git a/test/test.cpp b/test/test.cpp index ed6c04c..297949d 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -65,246 +65,246 @@ int main () { // --------------- TIME ASYNCHRONOUS FUNCTIONS -------------- - // /** - // * Init interval and timeout; clear interval and timeout - // */ - - // interval inter1 ([&]() { - // cout << "interval prvi " << rtime_ms() - start << endl; - // }, 1000); - - // interval inter2 ([&]() { - // cout << "interval drugi " << rtime_ms() - start << endl; - // }, 2000); - - // interval inter3 ([&]() { - // cout << "interval treći " << rtime_ms() - start << endl; - // }, 1000); - - // interval inter4 ([&]() { - // // cout << "interval cetvrti " << rtime_ms() - start << endl; - // cout << "Ticks " << inter3.ticks() << endl; - // }, 500); - - // interval inter5 ([&]() { - // cout << "interval peti " << rtime_ms() - start << endl; - // }, 2000); - - // interval inter6 ([&]() { - // cout << "interval sesti " << rtime_ms() - start << endl; - // }, 3000); - - // timeout time1 ( [&] () { - // cout << "Close interval 1 i 2 " << rtime_ms() - start << endl; - // inter1.stop(); - // cout << "inter1.stop " << endl; - // inter2.stop(); - // cout << "inter2.stop " << endl; - // }, 8000); - - - // timeout time2 ([&] () { - // cout << "Close interval 3 " << rtime_ms() - start << endl; - // inter3.stop(); - // cout << "Stoped " << inter3.stoped() << endl; - // // time1.stop(); - // }, 5000); - - - // if (time2.expired()) { - // cout << "isteko " << endl; - // } else { - // cout << "nije isteko " << endl; - // } + /** + * Init interval and timeout; clear interval and timeout + */ + + interval inter1 ([&]() { + cout << "interval prvi " << rtime_ms() - start << endl; + }, 1000); + + interval inter2 ([&]() { + cout << "interval drugi " << rtime_ms() - start << endl; + }, 2000); + + interval inter3 ([&]() { + cout << "interval treći " << rtime_ms() - start << endl; + }, 1000); + + interval inter4 ([&]() { + // cout << "interval cetvrti " << rtime_ms() - start << endl; + cout << "Ticks " << inter3.ticks() << endl; + }, 500); + + interval inter5 ([&]() { + cout << "interval peti " << rtime_ms() - start << endl; + }, 2000); + + interval inter6 ([&]() { + cout << "interval sesti " << rtime_ms() - start << endl; + }, 3000); + + timeout time1 ( [&] () { + cout << "Close interval 1 i 2 " << rtime_ms() - start << endl; + inter1.stop(); + cout << "inter1.stop " << endl; + inter2.stop(); + cout << "inter2.stop " << endl; + }, 8000); + + + timeout time2 ([&] () { + cout << "Close interval 3 " << rtime_ms() - start << endl; + inter3.stop(); + cout << "Stoped " << inter3.stoped() << endl; + // time1.stop(); + }, 5000); + + + if (time2.expired()) { + cout << "isteko " << endl; + } else { + cout << "nije isteko " << endl; + } - // // sleep(6); + // sleep(6); - // if (time2.expired()) { - // cout << "isteko " << endl; - // } else { - // cout << "nije isteko " << endl; - // } + if (time2.expired()) { + cout << "isteko " << endl; + } else { + cout << "nije isteko " << endl; + } - // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- + // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- - // /** - // * Run an function asyncronic - // */ + /** + * Run an function asyncronic + */ - // atask( []() { - // sleep_for(2s); // only for simulate log duration function - // cout << "atask 1" << endl; - // return 5; - // }); + atask( []() { + sleep_for(2s); // only for simulate log duration function + cout << "atask 1" << endl; + return 5; + }); - // /** - // * Call not lambda function - // */ + /** + * Call not lambda function + */ - // atask (notLambdaFunction); + atask (notLambdaFunction); - // wait ( - // atask ( - // notLambdaFunction - // ) - // ); + wait ( + atask ( + notLambdaFunction + ) + ); - // /** - // * Call class method - // */ + /** + * Call class method + */ - // clm classes; - // atask( [&classes] () { - // classes.classMethode(); - // }); + clm classes; + atask( [&classes] () { + classes.classMethode(); + }); - // sleep(5); + sleep(5); - // /** - // * Wait after runned as async - // */ + /** + * Wait after runned as async + */ - // auto a = atask( []() { - // sleep_for(2s); // only for simulate log duration function - // cout << "atask 2" << endl; - // return 5; - // }); + auto a = atask( []() { + sleep_for(2s); // only for simulate log duration function + cout << "atask 2" << endl; + return 5; + }); - // cout << wait(a) << endl; - // cout << "print after atask 2" << endl; + cout << wait(a) << endl; + cout << "print after atask 2" << endl; - // /** - // * Wait async function call and use i cout - // */ + /** + * Wait async function call and use i cout + */ - // cout << wait(atask( [] () { - // sleep_for(chrono::seconds(1)); // only for simulate log duration function - // cout << "wait end" << endl; - // return 4; - // })) << endl; + cout << wait(atask( [] () { + sleep_for(chrono::seconds(1)); // only for simulate log duration function + cout << "wait end" << endl; + return 4; + })) << endl; - // /** - // * Sleep with timeout sleep implement - // */ + /** + * Sleep with timeout sleep implement + */ - // sleep_to(3000); - // cout << "sleep_to " << rtime_ms() - start << endl; + sleep_to(3000); + cout << "sleep_to " << rtime_ms() - start << endl; - // /** - // * Catch promise reject - // */ + /** + * Catch promise reject + */ - // try { - // promise_reject(3000); - // } catch (runtime_error err) { - // cout<< err.what() << endl; - // } + try { + promise_reject(3000); + } catch (runtime_error err) { + cout<< err.what() << endl; + } - // cout << "promise_reject " << rtime_ms() - start << endl; + cout << "promise_reject " << rtime_ms() - start << endl; - // /** - // * Nested asynchronous invocation - // */ + /** + * Nested asynchronous invocation + */ - // atask( [] { - // cout << "idemo ..." << endl; - // atask( [] { - // cout << "ugdnježdena async funkcija " << endl; - // }); - // }); + atask( [] { + cout << "idemo ..." << endl; + atask( [] { + cout << "ugdnježdena async funkcija " << endl; + }); + }); - // // // --------------- EVENTS ------------------- + // // --------------- EVENTS ------------------- - // /** - // * initialization of typed events - // */ + /** + * initialization of typed events + */ - // event ev2int; - // event evintString; - // event<> evoid; + event ev2int; + event evintString; + event<> evoid; - // ev2int.on("sum", [](int a, int b) { - // cout << "Sum " << a+b << endl; - // }); + ev2int.on("sum", [](int a, int b) { + cout << "Sum " << a+b << endl; + }); - // ev2int.on("sum", [](int a, int b) { - // cout << "Sum done" << endl; - // }); + ev2int.on("sum", [](int a, int b) { + cout << "Sum done" << endl; + }); - // evintString.on("substract", [](int a, string b) { - // cout << "Substract " << a-stoi(b) << endl; - // }); + evintString.on("substract", [](int a, string b) { + cout << "Substract " << a-stoi(b) << endl; + }); - // evoid.on("void", []() { - // cout << "Void emited" << endl; - // }); + evoid.on("void", []() { + cout << "Void emited" << endl; + }); - // string emited2 = "2"; + string emited2 = "2"; - // evoid.on("void", [&]() { - // cout << "Void emited " << emited2 << endl; - // }); + evoid.on("void", [&]() { + cout << "Void emited " << emited2 << endl; + }); - // evoid.emit("void"); - // sleep(1); + evoid.emit("void"); + sleep(1); - // /** - // * Emit - // */ + /** + * Emit + */ - // ev2int.emit("sum", 5, 8); + ev2int.emit("sum", 5, 8); - // sleep(1); - // evintString.emit("substract", 3, to_string(2)); + sleep(1); + evintString.emit("substract", 3, to_string(2)); - // sleep(1); - // evoid.off("void"); - // evoid.emit("void"); + sleep(1); + evoid.off("void"); + evoid.emit("void"); - // /** - // * Own class - // */ + /** + * Own class + */ - // myOwnClass myclass; + myOwnClass myclass; - // timeout t( [&] { - // myclass.emit("constructed", 1); - // }, 200); + timeout t( [&] { + myclass.emit("constructed", 1); + }, 200); - // myclass.on("constructed", [] (int i) { - // cout << "Constructed " << i << endl; - // }); + myclass.on("constructed", [] (int i) { + cout << "Constructed " << i << endl; + }); - // auto status = fs::read("test1.txt"); + auto status = fs::read("test1.txt"); - // try { - // auto data = wait(status); - // cout << data; - // } catch (exception& err) { - // cout << err.what() << endl; - // } + try { + auto data = wait(status); + cout << data; + } catch (exception& err) { + cout << err.what() << endl; + } - // string data_; - // auto start_read = rtime_us(); + string data_; + auto start_read = rtime_us(); - // fs::read("test1.txt", [&data_, &start_read] (string data, exception* error) { - // if (error) { - // cout << "Error " << error->what() << endl; - // } else { - // // cout << "Data " << endl << data << endl; - // // data_ = data; - // // cout << "Data_" << data_ << endl; - // cout << "read " << rtime_us() - start_read << endl; - // } - // }); + fs::read("test1.txt", [&data_, &start_read] (string data, exception* error) { + if (error) { + cout << "Error " << error->what() << endl; + } else { + // cout << "Data " << endl << data << endl; + // data_ = data; + // cout << "Data_" << data_ << endl; + cout << "read " << rtime_us() - start_read << endl; + } + }); // ----------------------------------------------------------------------------------------------------