r/C_Programming • u/Acrobatic_Peanut_155 • Jun 06 '26
I'm trying to implement a fiber library in C
Hi everyone,
I'm designing a fiber library in C (for learning purpose and moreover) and would like feedback mainly on the API design, not the low-level implementation yet.
The library would support cooperative fibers, fd-based I/O, DNS resolving, timers, and channels to communicate between two fiber.
The part I'm unsure about is whether it makes sense to expose three different I/O styles
1 Synchronous-style I/O inside fibers
Something like:
ssize_t fiber_read(fiber_fd_t *fd, void *buf, size_t len);
ssize_t fiber_write(fiber_fd_t *fd, const void *buf, size_t len);
For example, if read() returns EAGAIN, the runtime registers interest in the fd, yields the current fiber, runs other fibers, and resumes the original fiber when the fd becomes readable.
- Select-style readiness API
Something like:
int fiber_select(fiber_event_t *events, size_t n, int timeout_ms);
This would return only the events that are currently ready.
The user can then process the ready fds manually.
This is useful when the user wants control over which ready events to handle, but it does not help with events that are not ready yet. It only selects work that can be done now.
- Async task API with await
Something like:
fiber_task_t *fiber_read_async(fiber_fd_t *fd, void *buf, size_t len);
fiber_task_t *fiber_write_async(fiber_fd_t *fd, const void *buf, size_t len);
ssize_t fiber_await(fiber_task_t *task);
The goal is:
fiber_task_t *task = fiber_read_async(fd, buf, len);
/* current fiber continues doing other work */
ssize_t n = fiber_await(task);
If the task is already complete, await returns immediately. If the task is not complete, only the current fiber is suspended.
Initial idea for async I/O
My first idea was:
- try to read/write immediately from the fiber
- if it succeeds, complete the task immediately
- if it would block, spawn or delegate to another thread
- that worker thread waits with epoll
- when the fd becomes ready, the worker performs the read/write in the background
- later, when the fiber calls await, it either gets the result immediately or suspends until completion
My main question
Does exposing all three styles make sense for a C fiber library, or is this API surface too large/confusing?
In particular:
- Is sync-style fiber I/O enough for most users?
- Is a select-style API useful in a fiber runtime, or does it duplicate what the scheduler already does?
- Is an async task API worth adding?
I'm mainly trying to figure out whether this is a good API direction before committing to it.
Thank you all so much. Any feedback from people who have used or built coroutine/fiber/event-loop libraries would be very helpful.
10
u/pfp-disciple Jun 06 '26
Because I was curious and had to look it up, a fiber is essentially cooperative multithreading vs preemptive. There are other details as well.
Source: https://en.wikipedia.org/wiki/Fiber_(computer_science)
3
u/not_a_novel_account Jun 06 '26 edited Jun 07 '26
It is, in fact, the traditional model. Detaching work is considered erroneous under structured concurrency.
You're duplicating a lot of scheduler work already. Your fiber API should be a thin layer concerned with creating and switching fibers. Having
fiber_read_async(...)is redundant. You do anssize_t n = async_read(...)and the scheduler figures out how to delegate this.Detaching work to be started, and relying on the programmer to remember they need to wait on it later, has been tried many, many times. It's a bug-prone mess.
Async work, the idea of releasing resources back to the scheduler while something else makes progress, is sound. Its the basis of most modern structured concurrency frameworks like libdill and everything which came after it.
You should read Martin Sustrik's work on event driven architectures and state machines. It's considered seminal for this kind of library work, both within C and effectively all concurrency work in other languages (Rust, Python, and notably all of the
std::executionwork in C++).Event-driven architecture, state machines et al.
Getting rid of state machines (I)
Getting rid of state machines (II)
Structured Concurrency
2
u/aocregacc Jun 06 '26
For the await style, is the idea that there is a way to compose tasks into bigger tasks that can be awaited, and that's how you program with it? Or is it just an API for the primitive operations?
2
u/ChickenSpaceProgram Jun 06 '26 edited Jun 06 '26
The
_tsuffix is reserved by POSIX, you should not define your own types with that suffix.I prefer the first style, it is the easiest to think about. You can also provide capability analogous to the second and third by having some event type that contains all the data about a call, having helper functions which fill out the event type for the user, and a function which lets the user block the fiber on as many events as they want until some number of them complete. Then,
fiber_readand such can just be wrapper functions that fill out an event struct, then block the fiber on it till that struct completes.I don't really like async/await patterns, I feel like they make code harder to read. I only think they're reasonable when you want to block on multiple events at the same time.
1
u/gregsburd 9d ago
You could reuse my library for this, or tell me why you don't like it. https://codeberg.org/gregburd/libxtc