Compare commits

...

1 Commits

3 changed files with 85 additions and 24 deletions

View File

@ -26,6 +26,8 @@ namespace asynco {
/** /**
* Asynco runtime * Asynco runtime
* Used for all asynchronous capabilities of this wrapper
* Initializes threads and boost::asio::io_context
*/ */
class Asynco { class Asynco {
vector<thread> _runners; vector<thread> _runners;
@ -36,14 +38,26 @@ class Asynco {
public: public:
io_context io_ctx; 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()); void run(uint8_t threads = thread::hardware_concurrency());
/**
* Starts Boost::Asio event loop in the current thread
*/
void run_on_this(); void run_on_this();
/**
* Waits until all threads have finished working
*/
void join(); void join();
/** /**
* Run the function asynchronously * Run the function asynchronously in runtime
*/ */
template<class F, class... Args> template<class F, class... Args>
auto async(F&& f, Args&&... args) -> future<invoke_result_t<F, Args...>> { auto async(F&& f, Args&&... args) -> future<invoke_result_t<F, Args...>> {
@ -54,7 +68,7 @@ public:
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
/** /**
* Run the coroutine * Run the coroutine in runtime
*/ */
template <typename T> template <typename T>
future<T> async(boost::asio::awaitable<T> _coroutine) { future<T> async(boost::asio::awaitable<T> _coroutine) {
@ -80,7 +94,7 @@ public:
#endif #endif
/** /**
* Block until the asynchronous call completes - dont block asynco engine loop * Wait until the asynchronous call completes
*/ */
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) {
@ -91,7 +105,7 @@ public:
} }
/** /**
* Block until the asynchronous call completes - dont block asynco engine loop * Wait until the asynchronous call completes
*/ */
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) {
@ -102,7 +116,7 @@ public:
} }
/** /**
* Run the function asynchronously an block until completes * Run the function asynchronously an wait until completes
*/ */
template<class F, class... Args> template<class F, class... Args>
auto await(F&& f, Args&&... args) -> invoke_result_t<F, Args...> { auto await(F&& f, Args&&... args) -> invoke_result_t<F, Args...> {
@ -125,17 +139,17 @@ public:
#endif #endif
/** /**
* Block until the multiple asynchronous call completes * Wait until the multiple asynchronous call completes
* Use only on no-void calls * Use only on no-void calls
*/ */
template<typename... F> template<typename... F>
auto await(F&&... f) -> tuple<typename decay<decltype(await(f))>::type...> { auto await(F&&... f) -> tuple<typename decay<decltype(await(f))>::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 * Use only on no-void calls
*/ */
@ -144,13 +158,21 @@ public:
return make_tuple(await(f)...); return make_tuple(await(f)...);
} }
Timer delayed(function<void()> callback, uint64_t time) ;/*{ /**
return Timer(io_ctx, callback, time, TimerType::Delayed); * Initialize the delayed timer
}*/ */
Timer periodic(function<void()> callback, uint64_t time) ;/*{ Timer delayed(function<void()> callback, uint64_t time);
return Timer(io_ctx, callback, time, TimerType::Periodic);
}*/ /**
* Initialize the periodic timer
*/
Timer periodic(function<void()> callback, uint64_t time);
/**
* Initialize trigger (typed event)
*/
template<typename... T> template<typename... T>
Trigger<T...> trigger() { Trigger<T...> trigger() {

View File

@ -6,11 +6,14 @@
namespace marcelb { namespace marcelb {
namespace asynco { namespace asynco {
/**
* Default runtime
*/
extern Asynco Asynco_Default_Runtime; extern Asynco Asynco_Default_Runtime;
/** /**
* Run the function asynchronously * Run the function asynchronously in default runtime
*/ */
template<class F, class... Args> template<class F, class... Args>
auto async_(F&& f, Args&&... args) -> future<invoke_result_t<F, Args...>> { auto async_(F&& f, Args&&... args) -> future<invoke_result_t<F, Args...>> {
@ -19,7 +22,7 @@ auto async_(F&& f, Args&&... args) -> future<invoke_result_t<F, Args...>> {
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
/** /**
* Run the coroutine * Run the coroutine in default runtime
*/ */
template <typename T> template <typename T>
std::future<T> async_(boost::asio::awaitable<T> _coroutine) { std::future<T> async_(boost::asio::awaitable<T> _coroutine) {
@ -28,7 +31,7 @@ std::future<T> async_(boost::asio::awaitable<T> _coroutine) {
#endif #endif
/** /**
* Block until the asynchronous call completes - dont block asynco engine loop * Wait until the asynchronous call completes
*/ */
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) {
@ -36,7 +39,7 @@ T await_(future<T>& r, uint16_t time_us = 10) {
} }
/** /**
* Block until the asynchronous call completes - dont block asynco engine loop * Wait until the asynchronous call completes
*/ */
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) {
@ -44,7 +47,7 @@ T await_(future<T>&& r, uint16_t time_us = 10) {
} }
/** /**
* Run the function asynchronously an block until completes * Run the function asynchronously an wait until completes
*/ */
template<class F, class... Args> template<class F, class... Args>
auto await_(F&& f, Args&&... args) -> invoke_result_t<F, Args...> { auto await_(F&& f, Args&&... args) -> invoke_result_t<F, Args...> {
@ -63,7 +66,7 @@ T await_(boost::asio::awaitable<T> _coroutine) {
#endif #endif
/** /**
* Block until the multiple asynchronous call completes * Wait until the multiple asynchronous call completes
* Use only on no-void calls * Use only on no-void calls
*/ */
@ -73,7 +76,7 @@ auto await_(F&&... f) -> std::tuple<typename std::decay<decltype(Asynco_Default_
} }
/** /**
* Block until the multiple asynchronous call completes * Wait until the multiple asynchronous call completes
* Use only on no-void calls * Use only on no-void calls
*/ */
@ -82,31 +85,66 @@ auto await_(F&... f) -> std::tuple<typename std::decay<decltype(Asynco_Default_R
return Asynco_Default_Runtime.await(f...);; return Asynco_Default_Runtime.await(f...);;
} }
/**
* Initialize the delayed timer
*/
Timer delayed(function<void()> callback, uint64_t time); Timer delayed(function<void()> callback, uint64_t time);
/**
* Initialize the periodic timer
*/
Timer periodic(function<void()> callback, uint64_t time); Timer periodic(function<void()> callback, uint64_t time);
/**
* Initialize trigger (typed event)
*/
template<typename... T> template<typename... T>
Trigger<T...> trigger() { Trigger<T...> trigger() {
return Trigger<T...>(Asynco_Default_Runtime); return Trigger<T...>(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(); Asynco& asynco_default_runtime();
/**
* Run default runtime
*/
void asynco_default_run(); void asynco_default_run();
/**
* Run default runtime in this thread
*/
void asynco_default_run_on_this(); void asynco_default_run_on_this();
/**
* Waits until all threads have finished working
*/
void asynco_default_join(); void asynco_default_join();
/**
* Get reference of boost::asio::io_context
*/
io_context& asynco_default_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_
} }

View File

@ -77,6 +77,7 @@ class Timer {
* The logic status of the timer stop state * The logic status of the timer stop state
*/ */
bool stoped(); bool stoped();
/** /**
* The destructor stops the timer * The destructor stops the timer
*/ */