https://github.com/firejox/fjx-fiber
thread safe stackful coroutine library
https://github.com/firejox/fjx-fiber
concurrency coroutine-library coroutines csp fiber fibers stackful-coroutines
Last synced: 5 months ago
JSON representation
thread safe stackful coroutine library
- Host: GitHub
- URL: https://github.com/firejox/fjx-fiber
- Owner: firejox
- License: mit
- Created: 2023-04-19T16:53:44.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-18T01:33:14.000Z (about 2 years ago)
- Last Synced: 2024-04-18T02:36:54.132Z (about 2 years ago)
- Topics: concurrency, coroutine-library, coroutines, csp, fiber, fibers, stackful-coroutines
- Language: C
- Homepage:
- Size: 128 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fjx-fiber [](https://github.com/firejox/fjx-fiber/actions/workflows/unit-test.yml)
A thread safe stackful coroutine library in C. It would require thread library installed.
## Features
* stackful coroutines
* multithread features
## Platforms
It runs on Linux x86-64 platform. It may support other platforms in the futures.
## Sample
This is a prime-seive example.
```c
#include "fjx-fiber.h"
#include
#include
struct gen_data {
fjx_fiber_channel *ch;
};
struct filter_data {
fjx_fiber_channel *ch;
fjx_fiber_channel *ch_next;
int prime;
};
void generate(void *data) {
struct gen_data *d = (struct gen_data*)data;
for (int i = 2; ; i++) {
fiber_channel_send(d->ch, &i);
}
}
void filter(void *data) {
struct filter_data *d = (struct filter_data*)data;
int i;
while (1) {
fiber_channel_receive(d->ch, &i);
if (i % d->prime) {
fiber_channel_send(d->ch_next, &i);
}
}
}
int main(void) {
fjx_fiber_channel *ch = NULL, *ch_next = NULL;
fiber_scheduler_init(2);
ch = fiber_channel_create(sizeof(int));
struct gen_data g = {.ch = ch};
struct filter_data f[103];
fiber_spawn(generate, &g);
for (int i = 0; i < 100; i++) {
int prime;
fiber_channel_receive(ch, &prime);
printf("%d\n", prime);
ch_next = fiber_channel_create(sizeof(int));
f[i].ch = ch;
f[i].ch_next = ch_next;
f[i].prime = prime;
fiber_spawn(filter, &f[i]);
ch = ch_next;
}
exit(EXIT_SUCCESS);
}
```
## TODO
* more platform support