spc476/C-Coroutines
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master
Could not load branches
Nothing to show
Could not load tags
Nothing to show
{{ refName }}
default
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code
-
Clone
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
- Open with GitHub Desktop
- Download ZIP
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
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.
About
Coroutines for C.
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published