Description
Use Case: Background Fibers
We sometimes need to execute a process in the background without impacting the priority of foreground fibers. Currently, we lack adequate tooling for this because the only available scheduling mechanism is FIFO. Simply yielding from a fiber is an insufficient solution, as that fiber can resume execution much faster than other fibers that are frequently blocked on I/O operations or mutexes.
Solution
Implement two priority levels for fibers: regular and background.
- Regular fibers: These will operate similarly to the current implementation, maintaining a FIFO order among themselves.
- Background fibers: These will execute only after all regular fibers have finished running, effectively having secondary precedence. They will also maintain a FIFO order among themselves.
To prevent starvation of background fibers, the scheduler will track how long they have been bypassed by foreground fibers. If this duration exceeds a certain threshold, the scheduler will provide an opportunity for background fibers to run. This ensures that even under 100% CPU utilization, background fibers will still make progress. If CPU utilization is less than 100%, foreground fibers will eventually suspend, allowing background fibers to naturally take over execution.