Skip to content
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

Add async_begin_static to async.h #8

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
7 changes: 6 additions & 1 deletion async/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ struct async { async_state; };
*/
#define async_begin(k) unsigned *_async_k = &(k)->_async_k; switch(*_async_k) { default:

/**
* Mark the start of an async subroutine where the state is abstracted
*/
#define async_begin_static static struct async _async_state = { ASYNC_INIT }; async_begin(&_async_state)

/**
* Mark the end of a async subroutine
*/
Expand Down Expand Up @@ -135,4 +140,4 @@ struct async { async_state; };
*/
#define async_call(f, state) (async_done(state) || (f)(state))

#endif
#endif
22 changes: 9 additions & 13 deletions async/example-small.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ static int async1_flag, async2_flag;
* the code.
*/
static async
async1(struct async *pt)
async1()
{
/* A async function must begin with async_begin() which takes a
pointer to a struct async. */
async_begin(pt);
pointer to a struct async. The async_begin_static macro does
this in the preprocessor*/
async_begin_static;

/* We loop forever here. */
while (1) {
Expand All @@ -51,9 +52,9 @@ async1(struct async *pt)
* first one.
*/
static async
async2(struct async *pt)
async2()
{
async_begin(pt);
async_begin_static;

while (1) {
/* Let the other async run. */
Expand All @@ -75,23 +76,18 @@ async2(struct async *pt)
* Finally, we have the main loop. Here is where the asyncs are
* initialized and scheduled. First, however, we define the
* async state variables pt1 and pt2, which hold the state of
* the two asyncs.
* the two asyncs. This example utilizes async_begin_static
*/
static struct async pt1, pt2;
void
example_small(int i)
{
/* Initialize the async state variables with async_init(). */
async_init(&pt1);
async_init(&pt2);

/*
* Then we schedule the two asyncs by repeatedly calling their
* async functions and passing a pointer to the async
* state variables as arguments.
*/
while (--i >= 0) {
async1(&pt1);
async2(&pt2);
async1();
async2();
}
}