diff options
| author | Tyge Løvset <[email protected]> | 2023-06-13 18:21:04 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-06-13 18:21:04 +0200 |
| commit | 06f3ae1d904d776aea8a78113c16fc30309817ed (patch) | |
| tree | 9dd2332386a4cd4a76000d719bf999d41a05998c | |
| parent | b564ef6bdfcd2437f1b4997f42054c45ccdedbb1 (diff) | |
| download | STC-modified-06f3ae1d904d776aea8a78113c16fc30309817ed.tar.gz STC-modified-06f3ae1d904d776aea8a78113c16fc30309817ed.zip | |
Added cco_closue(Ret, Closure) to define coroutine closure.
Added back cco_await_on(child) calls.
| -rw-r--r-- | docs/ccommon_api.md | 6 | ||||
| -rw-r--r-- | include/stc/algo/coroutine.h | 16 | ||||
| -rw-r--r-- | include/stc/cstr.h | 7 | ||||
| -rw-r--r-- | include/stc/csview.h | 7 | ||||
| -rw-r--r-- | include/stc/utf8.h | 6 | ||||
| -rw-r--r-- | misc/examples/cointerleave.c | 2 | ||||
| -rw-r--r-- | misc/examples/coread.c | 2 |
7 files changed, 29 insertions, 17 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 1f0847da..89a98cbb 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -380,6 +380,9 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_await(promise, ret)` | Await/suspend with ret value | | | `cco_return` | Replaces return. Jump to cco_final: if exist| | | `cco_return_v(val)` | Yield final value, enter final-state | +| | `cco_closure(Ret, Closure, ...)` | Define coroutine closure struct. | +| `void` | `cco_await_on(closure) { }` | Await on closure to finish | +| `void` | `cco_await_on(co, func) { }` | Await on func(co) to finish | | | Semaphores: | | | | `cco_sem` | Semaphore type | | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | @@ -398,7 +401,8 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | -| `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | +| `void` | `cco_block_on(closure) { }` | Run blocking until closure is finished | +| `void` | `cco_block_on(co, func) { }` | Run blocking until func is finished | | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | | | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 5cd6d68f..61581f64 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -84,7 +84,21 @@ enum { case __LINE__: if (!(promise)) {return ret; goto _resume;} \ } while (0) -#define cco_run(co, call) while (call, !cco_done(co)) +#define cco_closure(Ret, Closure, ...) \ + struct Closure { \ + Ret (*coroutine)(struct Closure*); \ + __VA_ARGS__ \ + int cco_state; \ + } + +#define cco_resume(closure) (closure)->coroutine(closure) +#define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) +#define cco_await_on_1(closure) cco_await_on_2(closure, cco_resume(closure)) +#define cco_await_on_2(co, func) cco_await_1((func(co), !cco_done(co))) + +#define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) +#define cco_block_on_1(closure) while (cco_resume(closure), !cco_done(closure)) +#define cco_block_on_2(co, func) while (func(co), !cco_done(co)) #define cco_final \ *_state = cco_state_final; case cco_state_final diff --git a/include/stc/cstr.h b/include/stc/cstr.h index bdfee39b..b9b066ad 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -24,7 +24,7 @@ /* A string type with short string optimization in C99 with good small-string * optimization (22 characters with 24 bytes string). */ -#define _i_nested +#define _i_inc_utf8 #include "utf8.h" #ifndef CSTR_H_INCLUDED @@ -440,7 +440,7 @@ cstr cstr_tocase(csview sv, int k) { #endif // i_import /* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || defined i_implement #ifndef CSTR_C_INCLUDED #define CSTR_C_INCLUDED @@ -655,11 +655,8 @@ intptr_t cstr_printf(cstr* self, const char* fmt, ...) { #if defined __GNUC__ && !defined __clang__ # pragma GCC diagnostic pop #endif -#ifndef _i_nested #undef i_opt #undef i_header #undef i_static #undef i_implement #undef i_import -#endif -#undef _i_nested diff --git a/include/stc/csview.h b/include/stc/csview.h index c16f58bc..07ab4059 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#define _i_nested +#define _i_inc_utf8 #include "utf8.h" #ifndef CSVIEW_H_INCLUDED @@ -150,7 +150,7 @@ STC_INLINE csview cstr_u8_substr(const cstr* self , intptr_t bytepos, intptr_t u #endif /* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || defined i_implement #ifndef CSVIEW_C_INCLUDED #define CSVIEW_C_INCLUDED @@ -203,11 +203,8 @@ csview csview_token(csview sv, const char* sep, intptr_t* start) { } #endif // CSVIEW_C_INCLUDED #endif // i_implement -#ifndef _i_nested #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt -#endif -#undef _i_nested diff --git a/include/stc/utf8.h b/include/stc/utf8.h index 190cc7f3..6d12856f 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -138,14 +138,14 @@ STC_INLINE intptr_t utf8_pos(const char* s, intptr_t index) { return (intptr_t)(utf8_at(s, index) - s); } #endif // UTF8_H_INCLUDED -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || (defined i_implement && !defined _i_inc_utf8) # include "../../src/utf8code.c" #endif -#ifndef _i_nested +#ifndef _i_inc_utf8 #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt #endif -#undef _i_nested +#undef _i_inc_utf8 diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 51b9f39a..1ba7b861 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -48,7 +48,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - cco_run(&g, interleaved(&g)) { + cco_block_on(&g, interleaved) { printf("%d ", g.value); } puts(""); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 63162ba3..622228c0 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -32,7 +32,7 @@ int main(void) { struct file_read g = {__FILE__}; int n = 0; - cco_run(&g, file_read(&g)) + cco_block_on(&g, file_read) { printf("%3d %s\n", ++n, cstr_str(&g.line)); //if (n == 10) cco_stop(&g); |
