From 749b33c586bcb492c8c422188a545d1fa8ae0031 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Thu, 25 Aug 2022 01:38:09 +0400 Subject: [async_function] Static coroutines --- source/kit/async_function.h | 9 +++++++++ source/test/unittests/async_function.test.c | 23 +++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'source') diff --git a/source/kit/async_function.h b/source/kit/async_function.h index 5ae9ade..3954aca 100644 --- a/source/kit/async_function.h +++ b/source/kit/async_function.h @@ -90,6 +90,13 @@ typedef struct { #define KIT_CORO_VOID(name_, ...) \ KIT_CORO(kit_af_void, name_, __VA_ARGS__) +#define KIT_STATIC_CORO(ret_type_, name_, ...) \ + KIT_AF_STATE(ret_type_, name_, __VA_ARGS__); \ + static KIT_CORO_IMPL(name_) + +#define KIT_STATIC_CORO_VOID(name_, ...) \ + KIT_STATIC_CORO(kit_af_void, name_, __VA_ARGS__) + #define KIT_AF_YIELD(...) \ do { \ self->_index = KIT_AF_LINE_(); \ @@ -277,6 +284,8 @@ typedef struct { # define CORO_DECL KIT_CORO_DECL # define CORO KIT_CORO # define CORO_DECL_VOID KIT_CORO_DECL_VOID +# define STATIC_CORO KIT_STATIC_CORO +# define STATIC_CORO_VOID KIT_STATIC_CORO_VOID # define CORO_VOID KIT_CORO_VOID # define AF_YIELD KIT_AF_YIELD # define AF_YIELD_VOID KIT_AF_YIELD_VOID diff --git a/source/test/unittests/async_function.test.c b/source/test/unittests/async_function.test.c index 1ff3031..5816bfc 100644 --- a/source/test/unittests/async_function.test.c +++ b/source/test/unittests/async_function.test.c @@ -3,31 +3,37 @@ #define KIT_TEST_FILE async_function #include "../../kit_test/test.h" -CORO(int, test_foo) { +AF_STATE(int, test_foo); +static AF_DECL(test_foo); + +CORO_IMPL(test_foo) { AF_RETURN(42); } CORO_END -CORO(int, test_bar) { +AF_STATE(int, test_bar); +static AF_DECL(test_bar); + +CORO_IMPL(test_bar) { AF_YIELD_VOID; AF_RETURN(42); } CORO_END -CORO(int, test_gen, int i; int min; int max;) { +STATIC_CORO(int, test_gen, int i; int min; int max;) { for (af i = af min; af i < af max; af i++) AF_YIELD(af i); AF_RETURN(af max); } CORO_END -CORO_VOID(test_task) { +STATIC_CORO_VOID(test_task) { AF_YIELD_VOID; AF_YIELD_VOID; AF_RETURN_VOID; } CORO_END -CORO_VOID(test_nest_task, AF_TYPE(test_task) promise;) { +STATIC_CORO_VOID(test_nest_task, AF_TYPE(test_task) promise;) { AF_INIT(af promise, test_task); AF_AWAIT(af promise); AF_AWAIT(af promise); @@ -35,13 +41,13 @@ CORO_VOID(test_nest_task, AF_TYPE(test_task) promise;) { } CORO_END -CORO(int, test_nest_generator, AF_TYPE(test_gen) promise;) { +STATIC_CORO(int, test_nest_generator, AF_TYPE(test_gen) promise;) { AF_INIT(af promise, test_gen, .min = 1, .max = 3); AF_YIELD_AWAIT(af promise); } CORO_END -CORO(int, test_join_multiple, AF_TYPE(test_bar) promises[3];) { +STATIC_CORO(int, test_join_multiple, AF_TYPE(test_bar) promises[3];) { for (int i = 0; i < 3; i++) AF_INIT(af promises[i], test_bar, .return_value = 0); AF_RESUME_AND_JOIN_ALL(af promises); @@ -51,7 +57,8 @@ CORO(int, test_join_multiple, AF_TYPE(test_bar) promises[3];) { } CORO_END -CORO(int, test_await_multiple, AF_TYPE(test_bar) promises[3];) { +STATIC_CORO(int, test_await_multiple, + AF_TYPE(test_bar) promises[3];) { for (int i = 0; i < 3; i++) AF_INIT(af promises[i], test_bar, .return_value = 0); AF_AWAIT_ALL(af promises); -- cgit v1.2.3