Skip to content

Add ASYNC wrapper macro #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Function|Description
--------|-----------
*async_begin(state)*|Mark the beginning of an async subroutine
*async_end*|Mark the end of an async subroutine
*ASYNC(state, ...code block...)*|Wrap the code block with async_begin and async_end automatically
*async_yield*|Yield execution until it's invoked again
*await(cond)*|Block progress until `cond` is true
*await_while(cond)*|Block progress while `cond` is true
Expand Down Expand Up @@ -94,29 +95,29 @@ typedef struct {
example_state pt;

async nested(struct async *pt){
async_begin(pt);
ASYNC(pt, {
...
async_end;
});
}

async example(example_state *pt) {
async_begin(pt);

// fork two nested async subroutines and wait until both complete
async_init(&pt->nested1);
async_init(&pt->nested2);
await(async_call(nested, &pt->nested1) & async_call(nested, &pt->nested2));
// OR call directly:
//await(nested(&pt->nested1) & nested(&pt->nested2));

// fork two nested async subroutines and wait until at least one completes
async_init(&pt->nested1);
async_init(&pt->nested2);
await(async_call(nested, &pt->nested1) | async_call(nested, &pt->nested2));
// OR call the subroutines directly:
//await(nested(&pt->nested1) | nested(&pt->nested2));

async_end;
ASYNC(pt, {

// fork two nested async subroutines and wait until both complete
async_init(&pt->nested1);
async_init(&pt->nested2);
await(async_call(nested, &pt->nested1) & async_call(nested, &pt->nested2));
// OR call directly:
//await(nested(&pt->nested1) & nested(&pt->nested2));
// fork two nested async subroutines and wait until at least one completes
async_init(&pt->nested1);
async_init(&pt->nested2);
await(async_call(nested, &pt->nested1) | async_call(nested, &pt->nested2));
// OR call the subroutines directly:
//await(nested(&pt->nested1) | nested(&pt->nested2));

});
}
```

Expand Down
8 changes: 8 additions & 0 deletions async/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ struct async { async_state; };
*/
#define async_end *_async_k=ASYNC_DONE; case ASYNC_DONE: return ASYNC_DONE; }

/**
* Wrap code with async_begin/async_end.
*
* @param state The async procedure state
* @param ... The code block to wrap
*/
#define ASYNC(state, ...) async_begin(state); __VA_ARGS__ async_end

/**
* Wait until the condition succeeds
* @param cond The condition that must be satisfied before execution can proceed
Expand Down