| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#pragma once |
| 23 |
|
| 24 |
#if !defined (DEX_INSIDE) && !defined (DEX_COMPILATION) |
| 25 |
# error "Only <libdex.h> can be included directly." |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "dex-future.h" |
| 29 |
#include "dex-scheduler.h" |
| 30 |
#include "dex-version-macros.h" |
| 31 |
|
| 32 |
G_BEGIN_DECLS |
| 33 |
|
| 34 |
typedef struct _DexCoroutine DexCoroutine; |
| 35 |
typedef struct _DexCoroutineClass DexCoroutineClass; |
| 36 |
|
| 37 |
#define DEX_TYPE_COROUTINE (dex_coroutine_get_type()) |
| 38 |
#define DEX_COROUTINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEX_TYPE_COROUTINE, DexCoroutine)) |
| 39 |
#define DEX_IS_COROUTINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEX_TYPE_COROUTINE)) |
| 40 |
|
| 41 |
DEX_AVAILABLE_IN_ALL |
| 42 |
GType dex_coroutine_get_type (void); |
| 43 |
DEX_AVAILABLE_IN_ALL |
| 44 |
void dex_coroutine_context_suspend (DexCoroutineContext *context, |
| 45 |
guint pc, |
| 46 |
DexFuture *future); |
| 47 |
DEX_AVAILABLE_IN_ALL |
| 48 |
void dex_coroutine_context_resume (DexCoroutineContext *context, |
| 49 |
guint *pc, |
| 50 |
DexFuture **future); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#define DEX_COROUTINE_BEGIN(context) \ |
| 64 |
{ DexCoroutineContext *__dex_coro = (DexCoroutineContext *)(context); \ |
| 65 |
guint __dex_coro_pc = 0; \ |
| 66 |
DexFuture *__dex_coro_pending = NULL; \ |
| 67 |
g_return_val_if_fail (__dex_coro != NULL, NULL); \ |
| 68 |
dex_coroutine_context_resume (__dex_coro, &__dex_coro_pc, &__dex_coro_pending); \ |
| 69 |
switch (__dex_coro_pc) { case 0: |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#define DEX_COROUTINE_END \ |
| 77 |
default: \ |
| 78 |
g_assert_not_reached (); \ |
| 79 |
return NULL; \ |
| 80 |
} \ |
| 81 |
return NULL; } |
| 82 |
|
| 83 |
#define _DEX_COROUTINE_SUSPEND(_out, _error, _future, _await, _id) \ |
| 84 |
G_GNUC_FALLTHROUGH; \ |
| 85 |
case _id: \ |
| 86 |
{ \ |
| 87 |
gboolean __resumed = __dex_coro_pc == (_id); \ |
| 88 |
DexFuture *__future = __resumed ? __dex_coro_pending : (_future); \ |
| 89 |
if (dex_future_is_pending (__future)) \ |
| 90 |
{ \ |
| 91 |
dex_coroutine_context_suspend (__dex_coro, (_id), g_steal_pointer (&__future)); \ |
| 92 |
return NULL; \ |
| 93 |
} \ |
| 94 |
__dex_coro_pc = 0; \ |
| 95 |
*(_out) = (__typeof__(*(_out))) (_await (g_steal_pointer (&__future), (_error))); \ |
| 96 |
} |
| 97 |
|
| 98 |
#define _DEX_COROUTINE_SUSPEND_VOID(_error, _future, _id) \ |
| 99 |
G_GNUC_FALLTHROUGH; \ |
| 100 |
case _id: \ |
| 101 |
{ \ |
| 102 |
gboolean __resumed = __dex_coro_pc == (_id); \ |
| 103 |
DexFuture *__future = __resumed ? __dex_coro_pending : (_future); \ |
| 104 |
if (dex_future_is_pending (__future)) \ |
| 105 |
{ \ |
| 106 |
dex_coroutine_context_suspend (__dex_coro, (_id), g_steal_pointer (&__future)); \ |
| 107 |
return NULL; \ |
| 108 |
} \ |
| 109 |
__dex_coro_pc = 0; \ |
| 110 |
(void) dex_await (g_steal_pointer (&__future), (_error)); \ |
| 111 |
} |
| 112 |
|
| 113 |
#define _DEX_COROUTINE_LABEL_ID() (__COUNTER__ + 1) |
| 114 |
|
| 115 |
#define DEX_COROUTINE_SUSPEND(_future, _error) \ |
| 116 |
_DEX_COROUTINE_SUSPEND_VOID (_error, _future, _DEX_COROUTINE_LABEL_ID ()) |
| 117 |
|
| 118 |
#define DEX_COROUTINE_SUSPEND_BOOLEAN(_out, _error, _future) \ |
| 119 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_boolean, _DEX_COROUTINE_LABEL_ID ()) |
| 120 |
|
| 121 |
#define DEX_COROUTINE_SUSPEND_ENUM(_out, _error, _future) \ |
| 122 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_enum, _DEX_COROUTINE_LABEL_ID ()) |
| 123 |
|
| 124 |
#define DEX_COROUTINE_SUSPEND_FLAGS(_out, _error, _future) \ |
| 125 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_flags, _DEX_COROUTINE_LABEL_ID ()) |
| 126 |
|
| 127 |
#define DEX_COROUTINE_SUSPEND_OBJECT(_out, _error, _future) \ |
| 128 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_object, _DEX_COROUTINE_LABEL_ID ()) |
| 129 |
|
| 130 |
#define DEX_COROUTINE_SUSPEND_BOXED(_out, _error, _future) \ |
| 131 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_boxed, _DEX_COROUTINE_LABEL_ID ()) |
| 132 |
|
| 133 |
#define DEX_COROUTINE_SUSPEND_POINTER(_out, _error, _future) \ |
| 134 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_pointer, _DEX_COROUTINE_LABEL_ID ()) |
| 135 |
|
| 136 |
#define DEX_COROUTINE_SUSPEND_INT(_out, _error, _future) \ |
| 137 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_int, _DEX_COROUTINE_LABEL_ID ()) |
| 138 |
|
| 139 |
#define DEX_COROUTINE_SUSPEND_UINT(_out, _error, _future) \ |
| 140 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_uint, _DEX_COROUTINE_LABEL_ID ()) |
| 141 |
|
| 142 |
#define DEX_COROUTINE_SUSPEND_INT64(_out, _error, _future) \ |
| 143 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_int64, _DEX_COROUTINE_LABEL_ID ()) |
| 144 |
|
| 145 |
#define DEX_COROUTINE_SUSPEND_UINT64(_out, _error, _future) \ |
| 146 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_uint64, _DEX_COROUTINE_LABEL_ID ()) |
| 147 |
|
| 148 |
#define DEX_COROUTINE_SUSPEND_DOUBLE(_out, _error, _future) \ |
| 149 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_double, _DEX_COROUTINE_LABEL_ID ()) |
| 150 |
|
| 151 |
#define DEX_COROUTINE_SUSPEND_FLOAT(_out, _error, _future) \ |
| 152 |
_DEX_COROUTINE_SUSPEND (_out, _error, _future, dex_await_float, _DEX_COROUTINE_LABEL_ID ()) |
| 153 |
|
| 154 |
G_END_DECLS |