Hello,

I'm new to both asynchronous i/o and fibers. I'm trying to grok on a high-level how fiber scheduling works in vibe-d.

As I understand, Fibers in DLang must be called, and then somewhere within the Fiber it must yield or naturally end if it wants to relinquish control back to the caller. The idea is that a Fiber scheduler could take some aggregate of Fibers and run them in an efficient way.

I'm interested in studying vibe-d's approach to this fiber scheduling problem. However, I'm a bit hazy even after reading the source code – it seems to be above my head.

My understanding, which is most definitely flawed (feel free to correct!), is that

  • There is a queue of Fibers (or some derivative of Fibers)
  • If a Fiber issues a vibe sleep(), a driver timer (say libevent) is created, and during the sleep time, events are processed (the event loop goes through some iterations). When the timer goes off, the Fiber is called again and resumes. Perhaps some other blocking operations are similar.
  • If a Fiber yields or ends, events are processed and the next Fiber in the queue is called. If it was a yield, it is placed in the back of the queue. If it ended, it is removed from the queue.
  • Each Thread probably has its own Fiber scheduler.

And some things I can't figure out:

  • When and where are the new Fibers added to the Fiber 'queue'?
  • Why is there so much timer code in the drivers? I can't think of much use except for making a timer for when Fibers sleep.
  • Does each Thread have its own Fiber queue so to speak?
  • How are Fibers balanced between Threads when worker threads are enabled?

Now obviously I'm not looking for an exacting in-depth technical description, just a high-level description or psuedocode with the major points, because I feel like I'm really missing out on some fundamental understanding. I'm currently more interested in fiber scheduling because as it stands, I know I'm missing some significant chunks.

Regards,
Kelet