Threads and fibers
Posted: Thu Apr 23, 2020 3:50 pm
I was thinking about scheduling, and how it would be useful to have both threads and fibers. The idea I'm toying with is:
Threads are preemptively multitasked, and fibers are cooperatively multitasked (within the same process.)
Threads (within the same process) get scheduled in a round robin fashion, while there will only be one awake fiber per process per core. The other fibers will be "pending" (awake but won't get scheduled) unless there are no other fibers currently running in that core for that process.
Anything code that runs in a fiber could run in a thread, but I thought there could be performance advantages if you kicked off task-sized bites that would not get interrupted by another fiber unless explicitly giving up control (by terminating, or sleeping because we're waiting on IO or a mutex.) If your process wanted to do something CPU intensive, you'd explicitly create a thread instead of a fiber.
Or, we could just implement fibers in userland?
Has anyone implemented fibers in their kernel?
I'm also imagining a situation where if there is only one awake process with one awake thread or fiber, we could switch off the timer interrupt, giving the task at hand a little more CPU time. (Maybe this is risky.)
Threads are preemptively multitasked, and fibers are cooperatively multitasked (within the same process.)
Threads (within the same process) get scheduled in a round robin fashion, while there will only be one awake fiber per process per core. The other fibers will be "pending" (awake but won't get scheduled) unless there are no other fibers currently running in that core for that process.
Anything code that runs in a fiber could run in a thread, but I thought there could be performance advantages if you kicked off task-sized bites that would not get interrupted by another fiber unless explicitly giving up control (by terminating, or sleeping because we're waiting on IO or a mutex.) If your process wanted to do something CPU intensive, you'd explicitly create a thread instead of a fiber.
Or, we could just implement fibers in userland?
Has anyone implemented fibers in their kernel?
I'm also imagining a situation where if there is only one awake process with one awake thread or fiber, we could switch off the timer interrupt, giving the task at hand a little more CPU time. (Maybe this is risky.)