-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathREADME
68 lines (54 loc) · 2.56 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
An Implementaion of Coroutines for C
This is not based on setjmp()/longjmp() or ucontext.h but is a ground-up
implementation of coroutines for C. This involves the use of assembly to
ensure the coroutine stack intialization and stack switching. The code may
look simple, but it wasn't simple to write. but it is straightforward
(mostly).
There are two example programs, test and iter. test just runs some simple
tests while iter will create a tree from lines of input (either stdin or a
file), then use a coroutine to iterate through the tree.
API:
extern int coroutine_create(
coroutine__s **pco,
size_t stsize,
uintptr_t (*fun)(coroutine__s *,uintptr_t)
);
Creates a coroutine context, which will include a stack of stsize;
if stsize is 0, a default size is automatically provided (which
*should* be big enough for most uses). This will return 0 on
success, non-zero on failure.
The function passed in takes two parameters, a coroutine__s * (which
will refer to its own context when called) and a uintptr_t (an
integer quantity large enough to hold a pointer). It can return
values back via coroutine_yield() or simply returning a value. Once
the function returns, it will not be called on subsequent calls to
coroutine_yield() (see coroutine_yield() for more information).
extern void coroutine_init(
coroutine__s *co,
uintptr_t (*fun)(coroutine__s *,uintptr_t),
void *stack
);
Initializes the coroutine stack so that the first call to
coroutine_yield() will call the given function. This is not meant
to be called from user code but is rather an internal method to
coroutine_create(). The stack paramter should point to the stack
top (highest memory address on most systems).
extern uintptr_t coroutine_yield(
coroutine__s *co,
uintptr_t data
);
Passes control to the coroutine, passing data to it. This call is
symetrical, being used on both sides to pass control back and forth.
Once the coroutine's main function does an actual return, further
calls to coroutine_yield() to said coroutine will immediately yield
back, returning data directly.
NOTE: setjmp()/longjmp() will *NOT* work across coroutine stacks.
setjmp()/longjmp() will work within a coroutine stack.
extern int coroutine_free(
coroutine__s *co
);
Free up the memory used for co and its stack. No other resources
are reclaimed (like open files). calling coroutine_free() on a
coroutine currently running is undefined (if you are lucky, the
program will crash immediate). The coroutine need not be finished
running when this is called.