summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-04-08 02:24:20 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-04-08 02:24:20 +0200
commite00840dd0a5e3ce22468b0b46cc8a36e3b0e80f9 (patch)
tree992cd761f4e3d20985dde1b98baad9425e35279c /source
parentd747d4b6c57cc0ac42e0da370f03704d8e5927f9 (diff)
downloadkit-e00840dd0a5e3ce22468b0b46cc8a36e3b0e80f9.zip
Add coroutine id
Diffstat (limited to 'source')
-rw-r--r--source/kit/async_function.h20
-rw-r--r--source/test/unittests/async_function.test.c6
2 files changed, 13 insertions, 13 deletions
diff --git a/source/kit/async_function.h b/source/kit/async_function.h
index 0389485..45e1089 100644
--- a/source/kit/async_function.h
+++ b/source/kit/async_function.h
@@ -1,6 +1,7 @@
#ifndef KIT_ASYNC_FUNCTION_H
#define KIT_ASYNC_FUNCTION_H
+#include <stddef.h>
#include <string.h>
#ifdef __cplusplus
@@ -22,6 +23,7 @@ typedef void (*kit_af_state_machine)(void *self_void_);
#define KIT_AF_STATE_DATA \
struct { \
int _index; \
+ ptrdiff_t _id; \
kit_af_state_machine _state_machine; \
}
@@ -152,11 +154,16 @@ static void kit_async_function_dispatch(void *promise) {
#define KIT_AF_TYPE(coro_) struct coro_##_coro_state_
-#define KIT_AF_INITIAL(coro_) ._index = 0, ._state_machine = (coro_)
+#define KIT_AF_INITIAL(id_, coro_) \
+ ._index = 0, ._id = (id_), ._state_machine = (coro_)
#define KIT_AF_CREATE(promise_, coro_, ...) \
KIT_AF_TYPE(coro_) \
- promise_ = { KIT_AF_INITIAL(coro_), __VA_ARGS__ }
+ promise_ = { KIT_AF_INITIAL(0, coro_), __VA_ARGS__ }
+
+#define KIT_AF_CREATE_ID(promise_, id_, ...) \
+ KIT_AF_TYPE(coro_) \
+ promise_ = { KIT_AF_INITIAL(id_, NULL), __VA_ARGS__ }
#define KIT_AF_INIT(promise_, coro_, ...) \
do { \
@@ -164,10 +171,10 @@ static void kit_async_function_dispatch(void *promise) {
memcpy(&(promise_), &kit_af_temp_, sizeof kit_af_temp_); \
} while (0)
-#define KIT_AF_INIT_EXPLICIT(promise_, size_, coro_func_) \
- do { \
- memset(&(promise_), 0, size_); \
- (promise_)._state_machine = (coro_func_); \
+#define KIT_AF_INIT_ID(promise_, id_, ...) \
+ do { \
+ KIT_AF_CREATE_ID(kit_af_temp_, id_, __VA_ARGS__); \
+ memcpy(&(promise_), &kit_af_temp_, sizeof kit_af_temp_); \
} while (0)
#define KIT_AF_FINISHED(promise_) ((promise_)._index == -1)
@@ -216,7 +223,6 @@ static void kit_async_function_dispatch(void *promise) {
# define AF_INITIAL KIT_AF_INITIAL
# define AF_CREATE KIT_AF_CREATE
# define AF_INIT KIT_AF_INIT
-# define AF_INIT_EXPLICIT KIT_AF_INIT_EXPLICIT
# define AF_FINISHED KIT_AF_FINISHED
# define AF_FINISHED_N KIT_AF_FINISHED_N
# define AF_FINISHED_ALL KIT_AF_FINISHED_ALL
diff --git a/source/test/unittests/async_function.test.c b/source/test/unittests/async_function.test.c
index a10dbff..32aff4e 100644
--- a/source/test/unittests/async_function.test.c
+++ b/source/test/unittests/async_function.test.c
@@ -59,12 +59,6 @@ TEST("coroutine init") {
REQUIRE(!AF_FINISHED(promise));
}
-TEST("coroutine init explicit") {
- AF_TYPE(test_foo) promise;
- AF_INIT_EXPLICIT(promise, sizeof promise, test_foo);
- REQUIRE(!AF_FINISHED(promise));
-}
-
TEST("coroutine init with value") {
AF_TYPE(test_foo) promise;
AF_INIT(promise, test_foo, .return_value = 42);