diff --git a/README.md b/README.md index 466b348..1dfe5ef 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,11 @@ There are five simple ways to create coroutines: this is a shortcut to `coroutine_create(callable, *args, CO_STACK_SIZE)`. 2. `co_await(callable, *args);` returns your _value_ inside a generic union **value_t** type, after coroutine fully completes. - This is a combine shortcut to four functions: - 1. `co_wait_group();` returns **hash-table** storing _coroutine-id's_ of any future created, + 1. `work_group();` returns **hash-table** storing _coroutine-id's_ of any future created, 2. `co_go(callable, *args);` calls, will end with a call to, 3. `co_wait(hash-table);` will suspend current coroutine, process coroutines until all are completed, returns **hash-table** of results for, - 4. `co_group_get_result(hash-table, coroutine-id);` returns your _value_ inside a generic union **value_t** type. + 4. `work_group_result(hash-table, coroutine-id);` returns your _value_ inside a generic union **value_t** type. 3. `co_execute(function, *args)` creates coroutine and immediately execute, does not return any value. 4. `co_event(callable, *args)` same as `co_await()` but for **libuv** or any event driven like library. 5. `co_handler(function, *handle, destructor)` initial setup for coroutine background handling of **http** _request/response_, @@ -110,14 +110,14 @@ a terminated/finish status. The initialization ends when `co_wait()` is called, as such current coroutine will pause, and execution will begin for the group of coroutines, and wait for all to finished. */ -C_API wait_group_t *co_wait_group(void); +C_API wait_group_t *work_group(void); /* Pauses current coroutine, and begin execution for given coroutine wait group object, will wait for all to finished. Returns hast table of results, accessible by coroutine id. */ C_API wait_result_t co_wait(wait_group_t *); /* Returns results of the given completed coroutine id, value in union value_t storage format. */ -C_API value_t co_group_get_result(wait_result_t *, int); +C_API value_t work_group_result(wait_result_t *, int); /* Creates an unbuffered channel, similar to golang channels. */ C_API channel_t *channel(void); @@ -163,7 +163,7 @@ C_API void co_execute(co_call_t, void_t); /* Explicitly give up the CPU for at least ms milliseconds. Other tasks continue to run during this time. */ -C_API unsigned int co_sleep(unsigned int ms); +C_API unsigned int sleep_for(unsigned int ms); /* Call `CO_MALLOC` to allocate memory of given size in current coroutine, will auto free `LIFO` on function exit/return, do not free! */ @@ -278,7 +278,7 @@ void_t greetings(void_t arg) for (int i = 0; i < 3; i++) { printf("%d ==> %s\n", i, name); - co_sleep(1); + sleep_for(1); } return 0; } @@ -288,7 +288,7 @@ int co_main(int argc, char **argv) puts("Start of main Goroutine"); co_go(greetings, "John"); co_go(greetings, "Mary"); - co_sleep(1000); + sleep_for(1000); puts("End of main Goroutine"); return 0; } @@ -606,7 +606,7 @@ void_t worker(void_t arg) int id = co_id(); printf("Worker %d starting\n", id); - co_sleep(1000); + sleep_for(1000); printf("Worker %d done\n", id); if (id == 4) return (void_t)32; @@ -619,7 +619,7 @@ void_t worker(void_t arg) int co_main(int argc, char **argv) { int cid[5]; - wait_group_t *wg = co_wait_group(); + wait_group_t *wg = work_group(); for (int i = 1; i <= 5; i++) { cid[i-1] = co_go(worker, &i); @@ -628,10 +628,10 @@ int co_main(int argc, char **argv) printf("\nWorker # %d returned: %d\n", cid[2], - co_group_get_result(wgr, cid[2]).integer); + work_group_result(wgr, cid[2]).integer); printf("\nWorker # %d returned: %s\n", cid[1], - co_group_get_result(wgr, cid[1]).string); + work_group_result(wgr, cid[1]).string); return 0; } ``` diff --git a/docs/index.md b/docs/index.md index 53eded6..cdd5679 100644 --- a/docs/index.md +++ b/docs/index.md @@ -69,11 +69,11 @@ There are five simple ways to create coroutines: this is a shortcut to `coroutine_create(callable, *args, CO_STACK_SIZE)`. 2. `co_await(callable, *args);` returns your _value_ inside a generic union **value_t** type, after coroutine fully completes. - This is a combine shortcut to four functions: - 1. `co_wait_group();` returns **hash-table** storing _coroutine-id's_ of any future created, + 1. `work_group();` returns **hash-table** storing _coroutine-id's_ of any future created, 2. `co_go(callable, *args);` calls, will end with a call to, 3. `co_wait(hash-table);` will suspend current coroutine, process coroutines until all are completed, returns **hash-table** of results for, - 4. `co_group_get_result(hash-table, coroutine-id);` returns your _value_ inside a generic union **value_t** type. + 4. `work_group_result(hash-table, coroutine-id);` returns your _value_ inside a generic union **value_t** type. 3. `co_execute(function, *args)` creates coroutine and immediately execute, does not return any value. 4. `co_event(callable, *args)` same as `co_await()` but for **libuv** or any event driven like library. 5. `co_handler(function, *handle, destructor)` initial setup for coroutine background handling of **http** _request/response_, @@ -110,14 +110,14 @@ a terminated/finish status. The initialization ends when `co_wait()` is called, as such current coroutine will pause, and execution will begin for the group of coroutines, and wait for all to finished. */ -C_API wait_group_t *co_wait_group(void); +C_API wait_group_t *work_group(void); /* Pauses current coroutine, and begin execution for given coroutine wait group object, will wait for all to finished. Returns hast table of results, accessible by coroutine id. */ C_API wait_result_t co_wait(wait_group_t *); /* Returns results of the given completed coroutine id, value in union value_t storage format. */ -C_API value_t co_group_get_result(wait_result_t *, int); +C_API value_t work_group_result(wait_result_t *, int); /* Creates an unbuffered channel, similar to golang channels. */ C_API channel_t *channel(void); @@ -163,7 +163,7 @@ C_API void co_execute(co_call_t, void_t); /* Explicitly give up the CPU for at least ms milliseconds. Other tasks continue to run during this time. */ -C_API unsigned int co_sleep(unsigned int ms); +C_API unsigned int sleep_for(unsigned int ms); /* Call `CO_MALLOC` to allocate memory of given size in current coroutine, will auto free `LIFO` on function exit/return, do not free! */ @@ -275,7 +275,7 @@ void_t greetings(void_t arg) for (int i = 0; i < 3; i++) { printf("%d ==> %s\n", i, name); - co_sleep(1); + sleep_for(1); } return 0; } @@ -285,7 +285,7 @@ int co_main(int argc, char **argv) puts("Start of main Goroutine"); co_go(greetings, "John"); co_go(greetings, "Mary"); - co_sleep(1000); + sleep_for(1000); puts("End of main Goroutine"); return 0; } @@ -621,7 +621,7 @@ void_t worker(void_t arg) { int id = co_id(); printf("Worker %d starting\n", id); - co_sleep(1000); + sleep_for(1000); printf("Worker %d done\n", id); if (id == 4) return (void_t)32; @@ -634,7 +634,7 @@ void_t worker(void_t arg) { int co_main(int argc, char **argv) { int cid[5]; - wait_group_t *wg = co_wait_group(); + wait_group_t *wg = work_group(); for (int i = 1; i <= 5; i++) { cid[i-1] = co_go(worker, &i); } @@ -642,10 +642,10 @@ int co_main(int argc, char **argv) printf("\nWorker # %d returned: %d\n", cid[2], - co_group_get_result(wgr, cid[2]).integer); + work_group_result(wgr, cid[2]).integer); printf("\nWorker # %d returned: %s\n", cid[1], - co_group_get_result(wgr, cid[1]).string); + work_group_result(wgr, cid[1]).string); return 0; } diff --git a/examples/benchmark.c b/examples/benchmark.c index 232e38c..3d88ab6 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -37,7 +37,7 @@ func main() { #include "coroutine.h" void *func(void *arg) { - co_sleep(10 * time_Second); + sleep_for(10 * time_Second); return 0; } @@ -46,8 +46,8 @@ int co_main(int argc, char **argv) { if (argc > 1) numRoutines = (u32)atoi(argv[1]); - co_wait_group_capacity(Kb(25)); - wait_group_t *wg = co_wait_group(); + work_group_capacity(Kb(25)); + wait_group_t *wg = work_group(); for (i = 0; i < numRoutines; i++) { co_go(func, NULL); } diff --git a/examples/go_multi_args.c b/examples/go_multi_args.c index 3193481..212005e 100644 --- a/examples/go_multi_args.c +++ b/examples/go_multi_args.c @@ -7,7 +7,7 @@ void *worker(void *arg) { for (int i = 0; i < count; i++) { printf("%s\n", text); - co_sleep(10); + sleep_for(10); } return 0; } @@ -17,7 +17,7 @@ int co_main(int argc, char **argv) { co_go(worker, args_for("is", 2, "b")); co_go(worker, args_for("is", 3, "c")); - co_sleep(100); + sleep_for(100); return 0; } diff --git a/examples/go_sleep.c b/examples/go_sleep.c index 5a889eb..a19669e 100644 --- a/examples/go_sleep.c +++ b/examples/go_sleep.c @@ -5,7 +5,7 @@ void *greetings(void *arg) { const char *name = c_const_char(arg); for (int i = 0; i < 3; i++) { printf("%d ==> %s\n", i, name); - co_sleep(1); + sleep_for(1); } return 0; } @@ -14,7 +14,7 @@ int co_main(int argc, char **argv) { puts("Start of main Goroutine"); co_go(greetings, "John"); co_go(greetings, "Mary"); - co_sleep(1000); + sleep_for(1000); puts("\nEnd of main Goroutine"); return 0; } diff --git a/examples/go_wait_group.c b/examples/go_wait_group.c index 05e1c42..9c75b59 100644 --- a/examples/go_wait_group.c +++ b/examples/go_wait_group.c @@ -7,7 +7,7 @@ void *worker(void *arg) { printf("Worker %d starting\n", wid); co_info_active(); - co_sleep(1000); + sleep_for(1000); printf("Worker %d done\n", wid); co_info_active(); @@ -22,13 +22,13 @@ void *worker(void *arg) { int co_main(int argc, char **argv) { int cid[5], i; - wait_group_t *wg = co_wait_group(); + wait_group_t *wg = work_group(); for (i = 1; i <= 5; i++) { cid[i - 1] = co_go(worker, args_for("i", i)); } wait_result_t *wgr = co_wait(wg); - printf("\nWorker # %d returned: %d\n", cid[2], co_group_get_result(wgr, cid[2]).integer); - printf("\nWorker # %d returned: %s\n", cid[1], co_group_get_result(wgr, cid[1]).char_ptr); + printf("\nWorker # %d returned: %d\n", cid[2], work_group_result(wgr, cid[2]).integer); + printf("\nWorker # %d returned: %s\n", cid[1], work_group_result(wgr, cid[1]).char_ptr); return 0; } diff --git a/examples/test_delay.c b/examples/test_delay.c index d3a0f44..fae3786 100644 --- a/examples/test_delay.c +++ b/examples/test_delay.c @@ -4,7 +4,7 @@ channel_t *c; void *delay_co(void *arg) { int v = atoi(c_char_ptr(arg)); - co_sleep(v); + sleep_for(v); printf("awake after %d ms\n", v); co_send(c, 0); diff --git a/include/coroutine.h b/include/coroutine.h index dfd6b92..f6d6bd4 100644 --- a/include/coroutine.h +++ b/include/coroutine.h @@ -772,7 +772,7 @@ C_API void co_process(func_t fn, void_t args); /* Explicitly give up the CPU for at least ms milliseconds. Other tasks continue to run during this time. */ -C_API u32 co_sleep(u32 ms); +C_API u32 sleep_for(u32 ms); /* Print coroutine internal data state, only active in `debug` builds. */ C_API void co_info(routine_t *t); @@ -908,17 +908,17 @@ C_API ht_string_t *ht_string_init(void); All coroutines here behaves like regular functions, meaning they return values, and indicate a terminated/finish status. The initialization ends when `co_wait()` is called, as such current coroutine will pause, and execution will begin for the group of coroutines, and wait for all to finished. */ -C_API wait_group_t *co_wait_group(void); +C_API wait_group_t *work_group(void); /* Set global wait group `hash table` initial capacity. */ -C_API void co_wait_group_capacity(u32); +C_API void work_group_capacity(u32); /* Pauses current coroutine, and begin execution for given coroutine wait group object, will wait for all to finish. Returns hast table of results, accessible by coroutine id. */ C_API wait_result_t *co_wait(wait_group_t *); /* Returns results of the given completed coroutine id, value in union value_t storage format. */ -C_API value_t co_group_get_result(wait_result_t *, u32); +C_API value_t work_group_result(wait_result_t *, u32); C_API void co_result_set(routine_t *, void_t); C_API void co_plain_set(routine_t *, size_t); diff --git a/src/coroutine.c b/src/coroutine.c index 8056613..11fe16e 100644 --- a/src/coroutine.c +++ b/src/coroutine.c @@ -128,13 +128,13 @@ CO_FORCE_INLINE bool co_terminated(routine_t *co) { } value_t co_await(callable_t fn, void_t arg) { - wait_group_t *wg = co_wait_group(); + wait_group_t *wg = work_group(); u32 cid = co_go(fn, arg); wait_result_t *wgr = co_wait(wg); if (is_empty(wgr)) return; - return co_group_get_result(wgr, cid); + return work_group_result(wgr, cid); } value_t co_event(callable_t fn, void_t arg) { @@ -185,11 +185,11 @@ void co_process(func_t fn, void_t args) { hash_free(eg); } -CO_FORCE_INLINE void co_wait_group_capacity(u32 size) { +CO_FORCE_INLINE void work_group_capacity(u32 size) { hash_capacity(size + (size * 0.333334)); } -wait_group_t *co_wait_group(void) { +wait_group_t *work_group(void) { routine_t *c = co_active(); wait_group_t *wg = ht_group_init(); c->wait_active = true; @@ -259,7 +259,7 @@ wait_result_t *co_wait(wait_group_t *wg) { return has_erred ? NULL : wgr; } -value_t co_group_get_result(wait_result_t *wgr, u32 cid) { +value_t work_group_result(wait_result_t *wgr, u32 cid) { if (is_empty(wgr)) return; diff --git a/src/scheduler.c b/src/scheduler.c index 55b93de..97a45a9 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -1355,7 +1355,7 @@ static void_t coroutine_wait(void_t v) { } } -u32 co_sleep(u32 ms) { +u32 sleep_for(u32 ms) { size_t when, now; routine_t *t;