From 4384b0b3118eecbd73c66e66764e1baf33bb39b6 Mon Sep 17 00:00:00 2001 From: marcelb Date: Thu, 12 Jun 2025 08:45:30 +0200 Subject: [PATCH] Fix on multiple await, is block thread/loop: replace get with await. Add some comments --- lib/asynco.hpp | 50 ++++++++++++++++++++++++++---------- lib/asynco_default.hpp | 58 ++++++++++++++++++++++++++++++++++-------- lib/timers.hpp | 1 + 3 files changed, 85 insertions(+), 24 deletions(-) diff --git a/lib/asynco.hpp b/lib/asynco.hpp index 3176dc6..f70c97e 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -26,6 +26,8 @@ namespace asynco { /** * Asynco runtime + * Used for all asynchronous capabilities of this wrapper + * Initializes threads and boost::asio::io_context */ class Asynco { vector _runners; @@ -36,14 +38,26 @@ class Asynco { public: io_context io_ctx; + /** + * It starts the thread initialization and the Boost::Asio event loop in each of them + */ + void run(uint8_t threads = thread::hardware_concurrency()); + /** + * Starts Boost::Asio event loop in the current thread + */ + void run_on_this(); + /** + * Waits until all threads have finished working + */ + void join(); /** - * Run the function asynchronously + * Run the function asynchronously in runtime */ template auto async(F&& f, Args&&... args) -> future> { @@ -54,7 +68,7 @@ public: #if __cplusplus >= 202002L /** - * Run the coroutine + * Run the coroutine in runtime */ template future async(boost::asio::awaitable _coroutine) { @@ -80,7 +94,7 @@ public: #endif /** - * Block until the asynchronous call completes - dont block asynco engine loop + * Wait until the asynchronous call completes */ template T await(future& r, uint16_t time_us = 10) { @@ -91,7 +105,7 @@ public: } /** - * Block until the asynchronous call completes - dont block asynco engine loop + * Wait until the asynchronous call completes */ template T await(future&& r, uint16_t time_us = 10) { @@ -102,7 +116,7 @@ public: } /** - * Run the function asynchronously an block until completes + * Run the function asynchronously an wait until completes */ template auto await(F&& f, Args&&... args) -> invoke_result_t { @@ -125,17 +139,17 @@ public: #endif /** - * Block until the multiple asynchronous call completes + * Wait until the multiple asynchronous call completes * Use only on no-void calls */ template auto await(F&&... f) -> tuple::type...> { - return make_tuple(move(f).get()...); + return make_tuple(await(f)...); } /** - * Block until the multiple asynchronous call completes + * Wait until the multiple asynchronous call completes * Use only on no-void calls */ @@ -144,13 +158,21 @@ public: return make_tuple(await(f)...); } - Timer delayed(function callback, uint64_t time) ;/*{ - return Timer(io_ctx, callback, time, TimerType::Delayed); - }*/ + /** + * Initialize the delayed timer + */ - Timer periodic(function callback, uint64_t time) ;/*{ - return Timer(io_ctx, callback, time, TimerType::Periodic); - }*/ + Timer delayed(function callback, uint64_t time); + + /** + * Initialize the periodic timer + */ + + Timer periodic(function callback, uint64_t time); + + /** + * Initialize trigger (typed event) + */ template Trigger trigger() { diff --git a/lib/asynco_default.hpp b/lib/asynco_default.hpp index 246fb94..441259e 100644 --- a/lib/asynco_default.hpp +++ b/lib/asynco_default.hpp @@ -6,11 +6,14 @@ namespace marcelb { namespace asynco { +/** + * Default runtime + */ extern Asynco Asynco_Default_Runtime; /** - * Run the function asynchronously + * Run the function asynchronously in default runtime */ template auto async_(F&& f, Args&&... args) -> future> { @@ -19,7 +22,7 @@ auto async_(F&& f, Args&&... args) -> future> { #if __cplusplus >= 202002L /** - * Run the coroutine + * Run the coroutine in default runtime */ template std::future async_(boost::asio::awaitable _coroutine) { @@ -28,7 +31,7 @@ std::future async_(boost::asio::awaitable _coroutine) { #endif /** - * Block until the asynchronous call completes - dont block asynco engine loop + * Wait until the asynchronous call completes */ template T await_(future& r, uint16_t time_us = 10) { @@ -36,7 +39,7 @@ T await_(future& r, uint16_t time_us = 10) { } /** - * Block until the asynchronous call completes - dont block asynco engine loop + * Wait until the asynchronous call completes */ template T await_(future&& r, uint16_t time_us = 10) { @@ -44,7 +47,7 @@ T await_(future&& r, uint16_t time_us = 10) { } /** - * Run the function asynchronously an block until completes + * Run the function asynchronously an wait until completes */ template auto await_(F&& f, Args&&... args) -> invoke_result_t { @@ -63,7 +66,7 @@ T await_(boost::asio::awaitable _coroutine) { #endif /** - * Block until the multiple asynchronous call completes + * Wait until the multiple asynchronous call completes * Use only on no-void calls */ @@ -73,7 +76,7 @@ auto await_(F&&... f) -> std::tuple std::tuple callback, uint64_t time); +/** + * Initialize the periodic timer + */ + Timer periodic(function callback, uint64_t time); +/** + * Initialize trigger (typed event) + */ + template Trigger trigger() { return Trigger(Asynco_Default_Runtime); } /** - * Alternative names of functions - mostly for the sake of more beautiful coloring of the code + * Get reference of default runtime */ -#define async_ marcelb::asynco::async_ -#define await_ marcelb::asynco::await_ Asynco& asynco_default_runtime(); +/** + * Run default runtime + */ + void asynco_default_run(); +/** + * Run default runtime in this thread + */ + void asynco_default_run_on_this(); +/** + * Waits until all threads have finished working + */ + void asynco_default_join(); +/** + * Get reference of boost::asio::io_context + */ + io_context& asynco_default_io_context(); +/** + * Run the function asynchronously in default runtime +*/ +#define async_ marcelb::asynco::async_ + +/** + * Wait until the asynchronous call completes +*/ +#define await_ marcelb::asynco::await_ } diff --git a/lib/timers.hpp b/lib/timers.hpp index 2196811..51a94b8 100644 --- a/lib/timers.hpp +++ b/lib/timers.hpp @@ -77,6 +77,7 @@ class Timer { * The logic status of the timer stop state */ bool stoped(); + /** * The destructor stops the timer */