|
typedef void * | co_t |
|
typedef unsigned int | semaphore_t |
|
|
enum | {
CO_READY,
CO_WAIT,
CO_YIELD,
CO_END,
CO_SKIP
} |
|
Coroutine mechanics, implemented using the C language extensions "Labels as Values", "Statements and Declarations in Expressions" and "Locally Declared Labels". Based on Simon Tatham "Coroutines in C".
{
for ( ; ; ) {
}
}
{
for ( ; ; ) {
}
}
int main( void )
{
for ( ; ; ) {
}
return 0;
}
Any local variables which need to be persistent across a coroutine switching must be declared static (CORO_LOCAL).
#define CORO_ALIVE |
( |
|
coro | ) |
(( coro ) < CO_END ) |
Checking the coroutine is not completed.
- Parameters
-
coro | Result of the coroutine call. |
- Returns
- Alive flag.
#define CORO_BEGIN |
( |
|
initial | ) |
|
Value:({ \
initial; \
if ( *co_p ) goto **co_p; \
})
The coroutine beginning.
- Parameters
-
initial | The initial operation, executed whenever enter in the coroutine. |
- Examples:
- drive.c, and remote.c.
#define CORO_CALL |
( |
|
name | ) |
coro_##name( &co_##name ) |
#define CORO_CONTEXT |
( |
|
name | ) |
co_t co_##name |
Define the coroutine context (a pointer to label).
- Parameters
-
- Examples:
- drive.c, and remote.c.
#define CORO_CONTEXT_INIT |
( |
|
name | ) |
|
Value:({ \
co_##name = NULL; \
})
#define CORO_DEFINE |
( |
|
name | ) |
int coro_##name( co_t *co_p ) |
#define CORO_END |
( |
|
final | ) |
|
The coroutine end.
- Parameters
-
final | The final operation, executed whenever exit from the coroutine. |
- Examples:
- drive.c, and remote.c.
#define CORO_QUIT |
( |
|
final | ) |
CORO_END( final ) |
#define CORO_RESTART |
( |
|
final | ) |
|
Value:({ \
*co_p = NULL; \
final; \
return CO_YIELD; \
})
Restart the coroutine.
- Parameters
-
#define CORO_WAIT |
( |
|
cond, |
|
|
|
... |
|
) |
| |
Value:({ \
__label__ L; \
*co_p = &&L; \
L: \
if (!( cond )) { \
__VA_ARGS__; \
return CO_WAIT; \
} \
})
Waiting for the condition is true.
- Parameters
-
cond | Condition. |
... | Final operation. |
- Examples:
- drive.c, and remote.c.
Start and waiting for the child coroutine is completed.
- Parameters
-
coro | Child coroutine. |
... | Final operation. |
#define CORO_YIELD |
( |
|
final | ) |
|
Value:({ \
__label__ L; \
*co_p = &&L; \
final; \
return CO_YIELD; \
L:; \
})
Switching to the next coroutine.
- Parameters
-
- Examples:
- drive.c, and remote.c.
#define SEMAPHORE_ACQUIRE |
( |
|
name, |
|
|
|
... |
|
) |
| |
Value: --name; \
})
#define CORO_WAIT(cond,...)
Waiting for the condition is true.
Definition: coroutine.h:131
Waiting and acquire the semaphore.
- Parameters
-
name | Semaphore name. |
... | Final operation. |
#define SEMAPHORE_INIT |
( |
|
name, |
|
|
|
val |
|
) |
| |
Value:
Initialize the semaphore.
- Parameters
-
name | Semaphore name. |
val | Semaphore capacity. |
#define SEMAPHORE_RELEASE |
( |
|
name | ) |
|
Value:
Release the semaphore.
- Parameters
-