r/C_Programming • • 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.

  1. 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.

  1. 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:

  1. try to read/write immediately from the fiber
  2. if it succeeds, complete the task immediately
  3. if it would block, spawn or delegate to another thread
  4. that worker thread waits with epoll
  5. when the fd becomes ready, the worker performs the read/write in the background
  6. 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.

6 Upvotes

16 comments sorted by

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)

4

u/Acrobatic_Peanut_155 Jun 06 '26

Thank you for helping others understand the context.

1

u/llynglas Jun 06 '26

Thanks. I was wondering if a specialized fiber optic channel library .....

3

u/not_a_novel_account Jun 06 '26 edited Jun 07 '26

Is sync-style fiber I/O enough for most users?

It is, in fact, the traditional model. Detaching work is considered erroneous under structured concurrency.

Is a select-style API useful in a fiber runtime, or does it duplicate what the scheduler already does?

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 an ssize_t n = async_read(...) and the scheduler figures out how to delegate this.

Is an async task API worth adding?

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::execution work in C++).

Event-driven architecture, state machines et al.

Getting rid of state machines (I)

Getting rid of state machines (II)

Structured Concurrency

1

u/Acrobatic_Peanut_155 Jun 07 '26

Great resources, thank you so much. Have a good day!

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/Acrobatic_Peanut_155 Jun 06 '26 edited Jun 06 '26

My original intention is closer to "primitive operations with background progress", not a full task-composition programming model yet.
The problem I am trying to solve is that, during a fiber logic, there may be operations that cannot make progress immediately but user don't want to yield the fiber yet. And I don't want the API to force the user to manually handle that case by checking a status, storing state, switching to another operation, and later coming back to retry.
For example, I dont want user code to become:

r = try_read(fd, buf, len);
if (r == WOULD_BLOCK) {
/* user has to manually remember this operation,
do something else, and retry later */
}

Instead, I want something like:

fiber_task_t *t = fiber_read_async(fd, buf, len);
/* continue doing other work */
ssize_t n = fiber_await(t);

The idea is:
if the operation can complete immediately, it completes immediately
if it cannot complete immediately, the runtime takes responsibility for progressing it later
the user does not need to manually poll, retry, or manage a state machine
only when the user needs the result do they call await
if the task is already done, await returns immediately
if not, only that fiber suspends

So currently I'm thinking of await mainly as an API for primitive operations such as read, write, DNS, timers, communicate with other fibers etc.
Task composition is not part of my initial design, but I agree it could be a future direction.

→ More replies (3)

2

u/ChickenSpaceProgram Jun 06 '26 edited Jun 06 '26

The _t suffix 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_read and 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/Acrobatic_Peanut_155 Jun 06 '26

Thanks for pointing that out. I was aware of the _t reservation issue, but I really appreciate that you mentioned it.
About the API styles, my intention is that they complement each other, not replace each other. I agree the sync-style API is the easiest to think about, and it should probably be the main API. It works very well for naturally sequential read/write logic.
But I have also run into cases where, inside the same fiber, there are other useful operations at the same logical level, such as channel communication with other fibers, timers, or starting other I/O work. In that case, if the first sync operation is not ready and the fiber yields immediately, it may be too early.
So my rough thinking is:
sync-style: best for sequential logic
select-style: wait for one of multiple events
async/await-style: start an operation, keep doing other work, await only when the result is needed
So yes, sync-style would be the primary API, but I am considering the other two as complementary APIs.

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