Skip to content

Commit

Permalink
refactored threading wait group handling
Browse files Browse the repository at this point in the history
- added `async_for` and `go_for` allows passing multi arguments directly, by converting macro to func, and update examples
- add `grouping` field to wait group type pointer to hold wait result pointer
- fix various atomic race conditions,  example `benchmark.c` now execute and complete as expected,
  • Loading branch information
TheTechsTech committed Nov 5, 2024
1 parent 5a13aa9 commit b98dae1
Show file tree
Hide file tree
Showing 9 changed files with 528 additions and 414 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ a terminated/finish status.

The initialization ends when `wait_for()` 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 *wait_group(void);
C_API wait_group_t wait_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 wait_for(wait_group_t *);
C_API wait_result_t wait_for(wait_group_t);

/* Returns results of the given completed coroutine id, value in union value_t storage format. */
C_API value_t wait_result(wait_result_t *, int);
C_API value_t wait_result(wait_result_t, int);

/* Creates an unbuffered channel, similar to golang channels. */
C_API channel_t *channel(void);
Expand Down Expand Up @@ -619,12 +619,12 @@ void_t worker(void_t arg)
int co_main(int argc, char **argv)
{
int cid[5];
wait_group_t *wg = wait_group();
wait_group_t wg = wait_group();
for (int i = 1; i <= 5; i++)
{
cid[i-1] = go(worker, &i);
}
wait_result_t *wgr = wait_for(wg);
wait_result_t wgr = wait_for(wg);

printf("\nWorker # %d returned: %d\n",
cid[2],
Expand Down
10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ a terminated/finish status.

The initialization ends when `wait_for()` 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 *wait_group(void);
C_API wait_group_t wait_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 wait_for(wait_group_t *);
C_API wait_result_t wait_for(wait_group_t);

/* Returns results of the given completed coroutine id, value in union value_t storage format. */
C_API value_t wait_result(wait_result_t *, int);
C_API value_t wait_result(wait_result_t, int);

/* Creates an unbuffered channel, similar to golang channels. */
C_API channel_t *channel(void);
Expand Down Expand Up @@ -634,11 +634,11 @@ void_t worker(void_t arg) {
int co_main(int argc, char **argv)
{
int cid[5];
wait_group_t *wg = wait_group();
wait_group_t wg = wait_group();
for (int i = 1; i <= 5; i++) {
cid[i-1] = go(worker, &i);
}
wait_result_t *wgr = wait_for(wg);
wait_result_t wgr = wait_for(wg);
printf("\nWorker # %d returned: %d\n",
cid[2],
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int co_main(int argc, char **argv) {
numRoutines = (u32)atoi(argv[1]);

co_interrupt_off();
wait_group_t *wg = wait_group_by(numRoutines);
wait_group_t wg = wait_group_by(numRoutines);
for (i = 0; i < numRoutines; i++) {
go(func, NULL);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/go_multi_args.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "coroutine.h"

void *worker(void *arg) {
void *worker(args_t *arg) {
int i, count = args_get(arg, 0).integer;
char *text = args_get(arg, 1).char_ptr;

Expand All @@ -13,9 +13,9 @@ void *worker(void *arg) {
}

int co_main(int argc, char **argv) {
go(worker, args_for("is", 4, "a"));
go(worker, args_for("is", 2, "b"));
go(worker, args_for("is", 3, "c"));
go_for(worker, "is", 4, "a");
go_for(worker, "is", 2, "b");
go_for(worker, "is", 3, "c");

sleep_for(100);

Expand Down
4 changes: 2 additions & 2 deletions examples/go_wait_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ void *worker(void *arg) {
int co_main(int argc, char **argv) {
int cid[50], i;

wait_group_t *wg = wait_group_by(50);
wait_group_t wg = wait_group_by(50);
for (i = 0; i < 50; i++) {
cid[i] = go(worker, args_for("i", i));
}
wait_result_t *wgr = wait_for(wg);
wait_result_t wgr = wait_for(wg);

printf("\nWorker # %d returned: %d\n", cid[2], wait_result(wgr, cid[2]).integer);
printf("\nWorker # %d returned: %s\n", cid[1], wait_result(wgr, cid[1]).char_ptr);
Expand Down
Loading

0 comments on commit b98dae1

Please # to comment.