Closed 315567599 closed 1 year ago
Hello,
Does https://github.com/google/marl/blob/main/docs/scheduler.md help answer your question?
Cheers, Ben
Hello Ben,
A task is created by marl::schedule(). My question is how to create a fiber?
Thanks & Best Regards
when a task is blocked (marl::Event::wait), how the task become a fiber? I can't understand the detail.
A task is created by marl::schedule(). My question is how to create a fiber?
Fibers are internally created by the scheduler. There is no public API to create them yourself. The public API allows you to schedule tasks, and the scheduler takes care of ensuring that other tasks can be executed when other tasks become blocked.
when a task is blocked (marl::Event::wait), how the task become a fiber? I can't understand the detail.
Tasks don't "become a fiber", but tasks are executed on fibers. When tasks become blocked, the scheduler attempts to resume execution with another fiber for the given thread. If the scheduler has no fibers to resume, and there's outstanding tasks to being execution, then a new fiber is created, which will begin execution of the next schedule task.
A fiber switch is a relatively simple operation: all registers, including the program counter are stored to the fiber's allocated state, then the target fiber's state is loaded in to the registers. The thread then continues execution of the target fiber, resuming from where the program counter was at the time of suspension. See the src/osfiber_asm_*
files for the platform-dependent implementations of fibers.
Marl made the decision that fibers are owned and only executed by a specific thread, so a task that begins execution on a given thread, will only continue to be executed on that thread. This ensures that tasks that use thread-local-storage behave correctly.
Does this help?
Thanks very much for help。but i still don't understand when a fiber was created. and why at that time?
By reading code, Scheduler:: Worker:: suspend may be the key to understanding. Can you explain this function in an easy way?
If the fiber running on a thread becomes blocked, and there's additional tasks to run, you want to have the thread be able to suspend the execution of the currently executing & blocked task, and have the thread run another task. To do this without requiring a callback or promise pattern (the main purpose of marl), we require another fiber of execution. Fibers are pooled. If the pool has no free fibers available, then we construct a new one to start executing the next task.
By reading code, Scheduler:: Worker:: suspend may be the key to understanding. Can you explain this function in an easy way?
I don't think I can describe here in any more detail than what I wrote in the documentation:
https://github.com/google/marl/blob/main/docs/scheduler.md#marlschedulerworkersuspend
If the fiber running on a thread becomes blocked, and there's additional tasks to run, you want to have the thread be able to suspend the execution of the currently executing & blocked task, and have the thread run another task. To do this without requiring a callback or promise pattern (the main purpose of marl), we require another fiber of execution. Fibers are pooled. If the pool has no free fibers available, then "we construct a new one to start executing the next task".
we construct a new one to start executing the next task which function does above job?
which function does above job?
It's constructed here: https://github.com/google/marl/blob/a47a3a5c54435751d30e3154fdf17e3b29fc6796/src/scheduler.cpp#L496
very thanks, your kindly answer help me a lot. I try to continue read and debug the code.
No worries. Closing for now.
Please don't hesitate to file more issues if you have further questions.
rt