From 14663d631ad4442d0ba631b45b4fd193167815bf Mon Sep 17 00:00:00 2001 From: marcelb Date: Thu, 28 Mar 2024 23:33:02 +0100 Subject: [PATCH] Switch atask, events, timers to asio --- test/asio.cpp | 129 -------------------------------------------------- test/test.cpp | 76 ++++++++++++++++++----------- 2 files changed, 49 insertions(+), 156 deletions(-) delete mode 100644 test/asio.cpp diff --git a/test/asio.cpp b/test/asio.cpp deleted file mode 100644 index b485392..0000000 --- a/test/asio.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; - -int64_t rtime_ms() { - return chrono::duration_cast(chrono::system_clock::now() - .time_since_epoch()) - .count(); -} - - -// globals -int64_t start = rtime_ms();; -// timer global -boost::asio::io_context io_context; -unique_ptr work(new boost::asio::io_service::work(io_context)); - - -class timer { - boost::asio::steady_timer st; - bool stop = false; - bool repeate; - function callback; - uint64_t time; - - void init() { - st.async_wait( [this] (const boost::system::error_code&) { - if (!stop) { - callback(); - if (repeate) { - reinit(); - } - } - }); - } - - void reinit() { - st = boost::asio::steady_timer(io_context, boost::asio::chrono::milliseconds(time)); - init(); - } - - public: - timer (function _callback, uint64_t _time, bool _repeate) - : st(io_context, boost::asio::chrono::milliseconds(_time)), - stop(false), - repeate(_repeate), - callback(_callback), - time(_time) { - - init(); - } - - void clear() { - stop = true; - st.cancel(); - } - - ~timer() { - clear(); - } -}; - -vector> timer_container; - -timer setInterval(int interval, std::function func) { - cout << "set interval construction call" << endl; - // return make_shared (func, interval, true); - return timer (func, interval, true); -}; - -class interval { - shared_ptr _timer; - - public: - interval(function callback, uint64_t time) : _timer(make_shared (callback, time, true)) { - timer_container.push_back(_timer); - } - -}; - - -timer setTimeout(int interval, std::function func) { - cout << "set timeout construction call" << endl; - // return make_shared (func, interval, false); - return timer(func, interval, false); -}; - -// void clearTimer(shared_ptr _timer) { -// _timer->stop = true; -// _timer->st.cancel(); -// } - -int main() { - - vector loops; - - for (int i =0; i<8; i++) - loops.push_back( thread([&]() { - while (true) {io_context.run();} - })); - - - timer timer1 = setInterval(1000, []() { - // cout << "Interval end " << rtime_ms() - start << endl; - cout << "Interval 1 " << this_thread::get_id() << endl; - }); - - interval([]() { - // cout << "Interval end " << rtime_ms() - start << endl; - cout << "Interval 2 " << this_thread::get_id() << endl; - - }, 1500); - - // timer timer2 = setTimeout(5000, [&]() { - // cout << "Timeout end " << rtime_ms() - start << endl; - // timer1.clear(); - // }); - - for (int i =0; i<4; i++) - loops[i].join(); - - - return 0; -} - diff --git a/test/test.cpp b/test/test.cpp index d698a30..381e50b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,12 +1,14 @@ -#define NUM_OF_RUNNERS 2 +// #define NUM_OF_RUNNERS 2 #include "../lib/asynco.hpp" #include "../lib/event.hpp" -#include "../lib/rotor.hpp" +// #include "../lib/rotor.hpp" #include "../lib/filesystem.hpp" +#include "../lib/timers.hpp" #include #include +#include using namespace std; using namespace marcelb::asynco; @@ -67,26 +69,15 @@ int main () { * Init interval and timeout; clear interval and timeout */ - // interval( [&] () { - // cout << "interval 1: " << rtime_ms() - start << endl; - // }, 50); - // interval( [&] () { - // cout << "interval 1: " << rtime_ms() - start << endl; - // }, 100); - - // interval( [&] () { - // cout << "interval 2: " << rtime_ms() - start << endl; - // }, 200); - - // interval( [&] () { - // cout << "interval 3: " << rtime_ms() - start << endl; - // }, 300); + // vector intervals; + // for(int i=0; i<1000; i++) { + // intervals.push_back(interval( [i, &start]() { + // cout << "interval " << i << " end: " << rtime_ms() - start << endl; + // }, (i%5 +1)*1000)); + // } - // interval( [&] () { - // cout << "interval 4: " << rtime_ms() - start << endl; - // }, 400); // interval inter1 ([&]() { // cout << "interval prvi " << rtime_ms() - start << endl; @@ -114,19 +105,24 @@ int main () { // timeout time1 ( [&] () { // cout << "Close interval 1 i 2 " << rtime_ms() - start << endl; - // // inter1.clear(); - // // cout << "inter1.stop " << inter1.stop << endl; - // // inter2.clear(); - // // cout << "inter2.stop " << inter2.stop << endl; - // }, 5000); + // // inter1.stop(); + // cout << "inter1.stop " << endl; + // inter2.stop(); + // cout << "inter2.stop " << endl; + // }, 8000); // timeout time2 ([&] () { - // cout << "Close interval 3 " << rtime_ms() - start << endl; - // // inter3.clear(); - // time1.clear(); + // // cout << "Close interval 3 " << rtime_ms() - start << endl; + // inter3.stop(); + // // time1.stop(); // }, 2000); + + // interval ( [] () { + // cout << "BROJ TAJMERA " << _intern_asynco_timer_globals.timers.size() << endl; + // }, 5000); + // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // /** @@ -293,6 +289,32 @@ int main () { // cout << err.what() << endl; // } + + 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::read2("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; + } + }); + cout << "Sleep" << endl; sleep(100000); // only for testing