mirror of
https://github.com/jrcutler/threadless.io.git
synced 2024-07-07 10:35:49 +00:00
Adding initial coroutine library with simple example/test.
This commit is contained in:
92
test/coroutine.c
Normal file
92
test/coroutine.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* printf, perror */
|
||||
#include <stdio.h>
|
||||
/* EXIT_SUCCESS, EXIT_FAILURE */
|
||||
#include <stdlib.h>
|
||||
|
||||
/* ... */
|
||||
#include <threadless/coroutine.h>
|
||||
|
||||
|
||||
static void *fibonacci_generator(coroutine *coro, void *data)
|
||||
{
|
||||
size_t x = 0;
|
||||
size_t y = 1;
|
||||
|
||||
/* ignore initial input data */
|
||||
(void) data;
|
||||
|
||||
/* generate Fibonacci sequence until overflow */
|
||||
while (x <= y) {
|
||||
size_t tmp = x;
|
||||
/* yield next value, ignore new input data */
|
||||
(void) coroutine_yield(coro, &tmp);
|
||||
/* x, y = y, x + y */
|
||||
tmp = y;
|
||||
y += x;
|
||||
x = tmp;
|
||||
}
|
||||
|
||||
/* no more values */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void *output_coroutine(coroutine *coro, void *data)
|
||||
{
|
||||
size_t *value = data;
|
||||
|
||||
while (NULL != value) {
|
||||
printf("%zu\n", *value);
|
||||
value = coroutine_yield(coro, value);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static int run(void)
|
||||
{
|
||||
int error = -1;
|
||||
coroutine *fibonacci = NULL;
|
||||
coroutine *output = NULL;
|
||||
|
||||
fibonacci = coroutine_create(fibonacci_generator, 1);
|
||||
if (NULL == fibonacci) {
|
||||
perror("coroutine_create");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
output = coroutine_create(output_coroutine, 1);
|
||||
if (NULL == output) {
|
||||
perror("coroutine_create");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
void *data;
|
||||
|
||||
while (!(coroutine_ended(fibonacci) || coroutine_ended(output))) {
|
||||
data = coroutine_resume(fibonacci, NULL);
|
||||
data = coroutine_resume(output, data);
|
||||
}
|
||||
|
||||
error = 0;
|
||||
|
||||
fail:
|
||||
coroutine_destroy(output);
|
||||
coroutine_destroy(fibonacci);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int error;
|
||||
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
error = run();
|
||||
|
||||
return !error ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
Reference in New Issue
Block a user