Sleep with await

This commit is contained in:
marcelb 2026-04-02 14:29:27 +02:00
parent c8f6aa877c
commit e3574fac17
7 changed files with 137 additions and 105 deletions

View File

@ -26,11 +26,7 @@ using namespace boost::asio;
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
class Sleep;
struct SleepHandle {
std::future<void> future;
std::shared_ptr<Timer> timer;
};
/** /**
* Asynco runtime * Asynco runtime
@ -106,10 +102,14 @@ public:
*/ */
template<typename T> template<typename T>
T await(future<T>& r, uint16_t time_us = 10) { T await(future<T>& r, uint16_t time_us = 10) {
while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) { while (true) {
io_ctx.poll_one(); 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<typename T> template<typename T>
T await(future<T>&& r, uint16_t time_us = 10) { T await(future<T>&& r, uint16_t time_us = 10) {
while (r.wait_for(std::chrono::microseconds(time_us)) != future_status::ready) { while (true) {
io_ctx.poll_one(); 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<void()> callback, uint64_t time); Timer periodic(function<void()> callback, uint64_t time);
/** Sleep sleep(uint64_t time);
* Nonblock time sleep function
*/
void sleep(int _time);
SleepHandle sleep2(int _time);
/** /**
* Initialize trigger (typed event) * Initialize trigger (typed event)
@ -200,6 +197,26 @@ public:
}; };
class Sleep {
Asynco &runtime;
std::promise<void> promise;
std::future<void> future;
std::shared_ptr<Timer> timer;
public:
Sleep(uint64_t _time, Asynco &runtime);
// #ifdef _ASYNCO_DEFAULT_
// Sleep(uint64_t _time);
// #endif
void await();
};
} }
} }

View File

@ -97,12 +97,7 @@ Timer delayed(function<void()> callback, uint64_t time);
Timer periodic(function<void()> callback, uint64_t time); Timer periodic(function<void()> callback, uint64_t time);
/** Sleep sleep(uint64_t time);
* Nonblock time sleep function
*/
void sleep(int _time);
SleepHandle sleep2(int _time);
/** /**
* Initialize trigger (typed event) * Initialize trigger (typed event)

View File

@ -37,23 +37,39 @@ Timer Asynco::periodic(function<void()> callback, uint64_t time) {
return Timer(io_ctx, callback, time, TimerType::Periodic); return Timer(io_ctx, callback, time, TimerType::Periodic);
} }
void Asynco::sleep(int _time) {
promise<void> _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) { // Sleep
auto _promise = std::make_shared<std::promise<void>>();
auto _timer = std::make_shared<Timer>(io_ctx, [_promise]() { Sleep::Sleep(uint64_t _time, Asynco &runtime): runtime(runtime) {
_promise->set_value(); future = promise.get_future();
timer = std::make_shared<Timer>(runtime.io_ctx, [this](){
this->promise.set_value();
}, _time, TimerType::Delayed); }, _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<Timer>(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
} }
}; };

View File

@ -13,15 +13,10 @@ Timer periodic(function<void()> callback, uint64_t time) {
return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic); return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic);
} }
void sleep(int _time) { Sleep sleep(uint64_t time) {
return Asynco_Default_Runtime.sleep(_time); return Sleep(time, Asynco_Default_Runtime);
} }
SleepHandle sleep2(int _time) {
return Asynco_Default_Runtime.sleep2(_time);
}
Asynco& asynco_default_runtime() { Asynco& asynco_default_runtime() {
return Asynco_Default_Runtime; return Asynco_Default_Runtime;
} }

View File

@ -9,6 +9,16 @@ int main() {
Asynco asynco; Asynco asynco;
asynco.run(4); 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 ([&](){ // asynco.async ([&](){
// loop { // loop {
// cout << "Loop 1" << endl; // cout << "Loop 1" << endl;

View File

@ -4,106 +4,105 @@ using namespace marcelb::asynco;
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
asynco_default_run(); asynco_default_run();
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(1000).await();
cout << "Loop 1" << endl; cout << "Loop 1" << endl;
await_(sleep2(1000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(2000).await();
cout << "Loop 2" << endl; cout << "Loop 2" << endl;
await_(sleep2(2000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(2500).await();
cout << "Loop 25" << endl; cout << "Loop 25" << endl;
await_(sleep2(2500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(3000).await();
cout << "Loop 3" << endl; cout << "Loop 3" << endl;
await_(sleep2(3000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(3500).await();
cout << "Loop 35" << endl; cout << "Loop 35" << endl;
await_(sleep2(3500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(4000).await();
cout << "Loop 4" << endl; cout << "Loop 4" << endl;
await_(sleep2(4000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(4500).await();
cout << "Loop 45" << endl; cout << "Loop 45" << endl;
await_(sleep2(4500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(5000).await();
cout << "Loop 5" << endl; cout << "Loop 5" << endl;
await_(sleep2(5000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(5500).await();
cout << "Loop 55" << endl; cout << "Loop 55" << endl;
await_(sleep2(5500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(6000).await();
cout << "Loop 6" << endl; cout << "Loop 6" << endl;
await_(sleep2(6000).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(6500).await();
cout << "Loop 65" << endl; cout << "Loop 65" << endl;
await_(sleep2(6500).future);
} }
}); });
async_ ([](){ async_ ([](){
loop { loop {
marcelb::asynco::sleep(7000).await();
cout << "Loop 7" << endl; cout << "Loop 7" << endl;
await_(sleep2(7000).future);
} }
}); });
loop { // blokira trenutnu loop { // blokira trenutnu
marcelb::asynco::sleep(1500).await();
cout << "Loop 15" << endl; cout << "Loop 15" << endl;
sleep2(1500).future.get();
} }
asynco_default_join(); asynco_default_join();

View File

@ -42,76 +42,76 @@ int main() {
* initialization of typed events * initialization of typed events
*/ */
Trigger<int, int> ev2int = trigger<int, int>(); // Trigger<int, int> ev2int = trigger<int, int>();
Trigger<int, string> evintString = trigger<int, string>(); // Trigger<int, string> evintString = trigger<int, string>();
Trigger<> evoid = trigger<>(); // Trigger<> evoid = trigger<>();
ev2int.on("sum", [](int a, int b) { // ev2int.on("sum", [](int a, int b) {
cout << "Sum " << a+b << endl; // cout << "Sum " << a+b << 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;
}); // });
// multiple listeners // // multiple listeners
string emited2 = "2"; // string emited2 = "2";
evoid.on("void", [&]() { // evoid.on("void", [&]() {
cout << "Void emited " << emited2 << endl; // cout << "Void emited " << emited2 << endl;
}); // });
sleep(1); // sleep(1);
/** // /**
* Emit // * Emit
*/ // */
ev2int.tick("sum", 5, 8); // ev2int.tick("sum", 5, 8);
sleep(1); // sleep(1);
evintString.tick("substract", 3, to_string(2)); // evintString.tick("substract", 3, to_string(2));
sleep(1); // sleep(1);
evoid.tick("void"); // evoid.tick("void");
// Turn off the event listener // Turn off the event listener
evoid.off("void"); // evoid.off("void");
evoid.tick("void"); // nothing is happening // evoid.tick("void"); // nothing is happening
class myOwnClass : public Trigger<int> { // class myOwnClass : public Trigger<int> {
public: // public:
myOwnClass() : Trigger(asynco_default_runtime()) {}; // myOwnClass() : Trigger(asynco_default_runtime()) {};
}; // };
myOwnClass myclass; // myOwnClass myclass;
Timer t = delayed( [&] { // Timer t = delayed( [&] {
myclass.tick("constructed", 1); // myclass.tick("constructed", 1);
}, 200); // }, 200);
myclass.on("constructed", [] (int i) { // myclass.on("constructed", [] (int i) {
cout << "Constructed " << i << endl; // cout << "Constructed " << i << endl;
}); // });
ClassWithTriggers mt; // ClassWithTriggers mt;
mt.on<int>("int", function<void(int)>([&](int i) { // mt.on<int>("int", function<void(int)>([&](int i) {
cout << "Emit int " << i << endl; // cout << "Emit int " << i << endl;
})); // }));
mt.on<string>("string", function<void(string)>([&](string s) { // mt.on<string>("string", function<void(string)>([&](string s) {
cout << "Emit string " << s << endl; // cout << "Emit string " << s << endl;
})); // }));
mt.tick("int", 5); // mt.tick("int", 5);
mt.tick("string", string("Hello world")); // mt.tick("string", string("Hello world"));
asynco_default_join(); asynco_default_join();
return 0; return 0;