Fix block loop in await_

This commit is contained in:
marcelb 2025-06-10 15:07:54 +02:00
parent d37f85f728
commit 8d7796998f
2 changed files with 54 additions and 68 deletions

View File

@ -53,26 +53,10 @@ std::future<T> async_(boost::asio::awaitable<T> _coroutine) {
#endif #endif
/** /**
* Block until the asynchronous call completes * Block until the asynchronous call completes - dont block asynco engine loop
*/ */
template<typename T> template<typename T>
T await_(future<T>& r) { T await_(future<T>& r, uint16_t time_us = 10) {
return r.get();
}
/**
* Block until the asynchronous call completes
*/
template<typename T>
T await_(future<T>&& r) {
return move(r).get();
}
/**
* Block until the asynchronous call completes
*/
template<typename T>
T await_(future<T>& r, uint32_t time_us = 10) {
while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) {
_asynco_engine.io_context.poll_one(); _asynco_engine.io_context.poll_one();
} }
@ -80,10 +64,10 @@ T await_(future<T>& r, uint32_t time_us = 10) {
} }
/** /**
* Block until the asynchronous call completes * Block until the asynchronous call completes - dont block asynco engine loop
*/ */
template<typename T> template<typename T>
T await_(future<T>&& r, uint32_t time_us = 10) { T await_(future<T>&& r, uint16_t time_us = 10) {
while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) { while (r.wait_for(std::chrono::microseconds(time_us)) != std::future_status::ready) {
_asynco_engine.io_context.poll_one(); _asynco_engine.io_context.poll_one();
} }
@ -116,29 +100,6 @@ T await_(boost::asio::awaitable<T> _coroutine) {
#endif #endif
/**
* Block until the asynchronous call completes or time expired
*/
// template<typename T>
// T await_(future<T>& r, uint64_t time) {
// if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
// throw runtime_error("Asynchronous execution timed out");
// }
// return r.get();
// }
/**
* Block until the asynchronous call completes or time expired
*/
// template<typename T>
// T await_(future<T>&& r, uint64_t time) {
// if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) {
// throw runtime_error("Asynchronous execution timed out");
// }
// return move(r).get();
// }
} }
} }

View File

@ -45,6 +45,25 @@ using namespace this_thread;
// return _promise.get_future().get(); // return _promise.get_future().get();
// } // }
// void sleep_to (int _time) {
// promise<void> _promise;
// Delayed t( [&]() {
// _promise.set_value();
// }, _time);
// await_ (_promise.get_future(), 100);
// }
// future<void> sleep_to (int _time) {
// promise<void> _promise;
// future<void> _future = _promise.get_future();
// Delayed t( [&]() {
// _promise.set_value();
// }, _time);
// return _future;
// }
// void promise_reject (int _time) { // void promise_reject (int _time) {
// promise<void> _promise; // promise<void> _promise;
// Delayed t( [&]() { // Delayed t( [&]() {
@ -71,14 +90,14 @@ using namespace this_thread;
// } // }
// }; // };
// // ------------------ EXTEND OWN CLASS WITH EVENTS ------------------- // // // ------------------ EXTEND OWN CLASS WITH EVENTS -------------------
// class myOwnClass : public Trigger<int> { // class myOwnClass : public Trigger<int> {
// public: // public:
// myOwnClass() : Trigger() {}; // myOwnClass() : Trigger() {};
// }; // };
// ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------ // // ----------------- MULTIPLE TRIGGERS IN ONE CLASS ------------------
// class ClassWithTriggers { // class ClassWithTriggers {
// Trigger<int> emitter1; // Trigger<int> emitter1;
@ -176,25 +195,6 @@ int main () {
// cout << "nije isteko " << endl; // cout << "nije isteko " << endl;
// } // }
// auto d = Delayed( [](){
// cout << "Delayed" << endl;
// }, 2000);
// auto p = Periodic( [](){
// cout << "Periodic" << endl;
// }, 700);
// Periodic( [&] (){
// cout << "Delayed expire " << d->expired() << endl;
// cout << "Periodic ticks " << p->ticks() << endl;
// cout << "Delayed stoped " << d->stoped() << endl;
// cout << "Periodic stoped " << p->stoped() << endl;
// }, 1000);
// Delayed( [&](){
// p->stop();
// }, 10000);
// // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS ------------------------- // // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// // /** // // /**
@ -460,7 +460,7 @@ int main () {
// }); // });
// // ---------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------
// auto i = async_ ( []() -> asyncable<int> { // auto i = async_ ( []() -> asyncable<int> {
@ -481,7 +481,7 @@ int main () {
// }, 2000); // }, 2000);
// Periodic b( []() { // Periodic b_( []() {
// cout << "funckija" << endl; // cout << "funckija" << endl;
// }, 2000); // }, 2000);
@ -565,7 +565,7 @@ int main () {
// ); // );
// }, 2000); // }, 2000);
// await_( async_co2 ( [c1 = move(c1)]() -> asyncable<void> { // await_( async_ ( [c1 = move(c1)]() -> asyncable<void> {
// cout << "Baba roga" << endl; // cout << "Baba roga" << endl;
// co_await c1(); // co_await c1();
// })); // }));
@ -578,7 +578,32 @@ int main () {
// evoid.tick("void"); // evoid.tick("void");
cout << "-------------end main-------------" << endl;
// vector<future<void>> futures;
// for (int i=0; i<20; i++) {
// futures.push_back(
// async_([a = i](){
// for (int i=0; i<1000; i++) {
// cout << a << " " << i << endl;
// // sleep_to(i);
// }
// })
// );
// }
// for (int i=0; i<20; i++) {
// await_(futures[i]);
// // await_(futures[i]);
// }
cout << "-------------end main------------- " << rtime_ms() - start << endl;
_asynco_engine.run(); _asynco_engine.run();
return 0; return 0;
} }