diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 4dff705..fac3aae 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -26,11 +26,7 @@ using namespace boost::asio; namespace marcelb { namespace asynco { - -struct SleepHandle { - std::future future; - std::shared_ptr timer; -}; +class Sleep; /** * Asynco runtime @@ -106,10 +102,14 @@ public: */ template T await(future& r, uint16_t time_us = 10) { - while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) { - io_ctx.poll_one(); + while (true) { + if (r.wait_for(std::chrono::microseconds(0)) == std::future_status::ready) { + return r.get(); + } + if (io_ctx.poll_one() == 0) { + std::this_thread::sleep_for(std::chrono::microseconds(time_us)); + } } - return r.get(); } /** @@ -117,10 +117,14 @@ public: */ template T await(future&& r, uint16_t time_us = 10) { - while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) { - io_ctx.poll_one(); + while (true) { + if (r.wait_for(std::chrono::microseconds(0)) == std::future_status::ready) { + return std::move(r).get(); + } + if (io_ctx.poll_one() == 0) { + std::this_thread::sleep_for(std::chrono::microseconds(time_us)); + } } - return move(r).get(); } /** @@ -178,14 +182,7 @@ public: Timer periodic(function callback, uint64_t time); - /** - * Nonblock time sleep function - */ - - void sleep(int _time); - - SleepHandle sleep2(int _time); - + Sleep sleep(uint64_t time); /** * Initialize trigger (typed event) @@ -200,6 +197,26 @@ public: }; +class Sleep { + Asynco &runtime; + std::promise promise; + std::future future; + std::shared_ptr timer; + + public: + + Sleep(uint64_t _time, Asynco &runtime); + + +// #ifdef _ASYNCO_DEFAULT_ +// Sleep(uint64_t _time); +// #endif + + + void await(); + +}; + } } diff --git a/lib/asynco_default.hpp b/lib/asynco_default.hpp index f5fbdae..57daf88 100644 --- a/lib/asynco_default.hpp +++ b/lib/asynco_default.hpp @@ -97,12 +97,7 @@ Timer delayed(function callback, uint64_t time); Timer periodic(function callback, uint64_t time); -/** - * Nonblock time sleep function - */ - -void sleep(int _time); -SleepHandle sleep2(int _time); +Sleep sleep(uint64_t time); /** * Initialize trigger (typed event) diff --git a/src/asynco.cpp b/src/asynco.cpp index 30a9fe3..8245d6a 100644 --- a/src/asynco.cpp +++ b/src/asynco.cpp @@ -37,23 +37,39 @@ Timer Asynco::periodic(function callback, uint64_t time) { return Timer(io_ctx, callback, time, TimerType::Periodic); } -void Asynco::sleep(int _time) { - promise _promise; - Timer t = delayed( [&]() { - _promise.set_value(); - }, _time); - return await(_promise.get_future()); +Sleep Asynco::sleep(uint64_t time) { + return Sleep(time, *this); } -SleepHandle Asynco::sleep2(int _time) { - auto _promise = std::make_shared>(); +// Sleep - auto _timer = std::make_shared(io_ctx, [_promise]() { - _promise->set_value(); +Sleep::Sleep(uint64_t _time, Asynco &runtime): runtime(runtime) { + future = promise.get_future(); + + timer = std::make_shared(runtime.io_ctx, [this](){ + this->promise.set_value(); }, _time, TimerType::Delayed); +} - return { _promise->get_future(), _timer }; + +// #ifdef _ASYNCO_DEFAULT_ +// Sleep::Sleep(uint64_t _time): runtime(asynco_default_runtime()) { +// future = promise.get_future(); + +// timer = std::make_shared(runtime.io_ctx, [this](){ +// this->promise.set_value(); +// }, _time, TimerType::Delayed); +// } +// #endif + + +void Sleep::await() { +#ifdef _ASYNCO_DEFAULT_ + await_(future); +#else + runtime.await(future); +#endif } }; diff --git a/src/asynco_default.cpp b/src/asynco_default.cpp index bf3b3ce..15188b4 100644 --- a/src/asynco_default.cpp +++ b/src/asynco_default.cpp @@ -13,15 +13,10 @@ Timer periodic(function callback, uint64_t time) { return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic); } -void sleep(int _time) { - return Asynco_Default_Runtime.sleep(_time); +Sleep sleep(uint64_t time) { + return Sleep(time, Asynco_Default_Runtime); } -SleepHandle sleep2(int _time) { - return Asynco_Default_Runtime.sleep2(_time); -} - - Asynco& asynco_default_runtime() { return Asynco_Default_Runtime; } diff --git a/test/main_infinit_loop.cpp b/test/main_infinit_loop.cpp index 7391f19..35bd974 100644 --- a/test/main_infinit_loop.cpp +++ b/test/main_infinit_loop.cpp @@ -9,6 +9,16 @@ int main() { Asynco asynco; asynco.run(4); + asynco.async ([&](){ + loop { + cout << "Loop 1" << endl; + // auto timer = Sleep3(1000, asynco); + // timer.await(); + // Sleep(1000, asynco).await(); + asynco.sleep(1000).await(); + } + }); + // asynco.async ([&](){ // loop { // cout << "Loop 1" << endl; diff --git a/test/main_infinit_loop_default.cpp b/test/main_infinit_loop_default.cpp index ac2b83d..ca3a1d6 100644 --- a/test/main_infinit_loop_default.cpp +++ b/test/main_infinit_loop_default.cpp @@ -4,106 +4,105 @@ using namespace marcelb::asynco; #include using namespace std; - int main() { asynco_default_run(); async_ ([](){ loop { + marcelb::asynco::sleep(1000).await(); cout << "Loop 1" << endl; - await_(sleep2(1000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(2000).await(); cout << "Loop 2" << endl; - await_(sleep2(2000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(2500).await(); cout << "Loop 25" << endl; - await_(sleep2(2500).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(3000).await(); cout << "Loop 3" << endl; - await_(sleep2(3000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(3500).await(); cout << "Loop 35" << endl; - await_(sleep2(3500).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(4000).await(); cout << "Loop 4" << endl; - await_(sleep2(4000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(4500).await(); cout << "Loop 45" << endl; - await_(sleep2(4500).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(5000).await(); cout << "Loop 5" << endl; - await_(sleep2(5000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(5500).await(); cout << "Loop 55" << endl; - await_(sleep2(5500).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(6000).await(); cout << "Loop 6" << endl; - await_(sleep2(6000).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(6500).await(); cout << "Loop 65" << endl; - await_(sleep2(6500).future); } }); async_ ([](){ loop { + marcelb::asynco::sleep(7000).await(); cout << "Loop 7" << endl; - await_(sleep2(7000).future); } }); loop { // blokira trenutnu + marcelb::asynco::sleep(1500).await(); cout << "Loop 15" << endl; - sleep2(1500).future.get(); } asynco_default_join(); diff --git a/test/main_trigger_default.cpp b/test/main_trigger_default.cpp index c4ebe6a..0c8786a 100644 --- a/test/main_trigger_default.cpp +++ b/test/main_trigger_default.cpp @@ -42,76 +42,76 @@ int main() { * initialization of typed events */ - Trigger ev2int = trigger(); - Trigger evintString = trigger(); - Trigger<> evoid = trigger<>(); + // Trigger ev2int = trigger(); + // Trigger evintString = trigger(); + // Trigger<> evoid = trigger<>(); - ev2int.on("sum", [](int a, int b) { - cout << "Sum " << a+b << endl; - }); + // ev2int.on("sum", [](int a, int b) { + // cout << "Sum " << a+b << 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; + // }); - // multiple listeners + // // multiple listeners - string emited2 = "2"; + // string emited2 = "2"; - evoid.on("void", [&]() { - cout << "Void emited " << emited2 << endl; - }); + // evoid.on("void", [&]() { + // cout << "Void emited " << emited2 << endl; + // }); - sleep(1); + // sleep(1); - /** - * Emit - */ + // /** + // * Emit + // */ - ev2int.tick("sum", 5, 8); + // ev2int.tick("sum", 5, 8); - sleep(1); - evintString.tick("substract", 3, to_string(2)); + // sleep(1); + // evintString.tick("substract", 3, to_string(2)); - sleep(1); - evoid.tick("void"); + // sleep(1); + // evoid.tick("void"); // Turn off the event listener - evoid.off("void"); - evoid.tick("void"); // nothing is happening + // evoid.off("void"); + // evoid.tick("void"); // nothing is happening - class myOwnClass : public Trigger { - public: - myOwnClass() : Trigger(asynco_default_runtime()) {}; - }; + // class myOwnClass : public Trigger { + // public: + // myOwnClass() : Trigger(asynco_default_runtime()) {}; + // }; - myOwnClass myclass; + // myOwnClass myclass; - Timer t = delayed( [&] { - myclass.tick("constructed", 1); - }, 200); + // Timer t = delayed( [&] { + // myclass.tick("constructed", 1); + // }, 200); - myclass.on("constructed", [] (int i) { - cout << "Constructed " << i << endl; - }); + // myclass.on("constructed", [] (int i) { + // cout << "Constructed " << i << endl; + // }); - ClassWithTriggers mt; + // ClassWithTriggers mt; - mt.on("int", function([&](int i) { - cout << "Emit int " << i << endl; - })); + // mt.on("int", function([&](int i) { + // cout << "Emit int " << i << endl; + // })); - mt.on("string", function([&](string s) { - cout << "Emit string " << s << endl; - })); + // mt.on("string", function([&](string s) { + // cout << "Emit string " << s << endl; + // })); - mt.tick("int", 5); - mt.tick("string", string("Hello world")); + // mt.tick("int", 5); + // mt.tick("string", string("Hello world")); asynco_default_join(); return 0;