Skip to content

Commit

Permalink
update/rename co_sleep and work_group funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Aug 9, 2024
1 parent 02efe27 commit ba6b3c9
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 44 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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! */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
```
Expand Down
22 changes: 11 additions & 11 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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! */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -634,18 +634,18 @@ 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);
}
wait_result_t *wgr = co_wait(wg);
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;
}
</code></pre>
Expand Down
6 changes: 3 additions & 3 deletions examples/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/go_multi_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions examples/go_sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
8 changes: 4 additions & 4 deletions examples/go_wait_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion examples/test_delay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions include/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit ba6b3c9

Please sign in to comment.