lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Sep 21, 2022 at 3:16 PM bil til <biltil52@gmail.com> wrote:
Where does Lua exactly do the yielding between the different coroutines?

(I think generally in such a coroutine approach it is nice, if the Lua
"end user" somehow has a possibility to decide where the yielding is
done).

Yes, the intention is for context switching to be explicit.  That's important for keeping a program easy to reason about and eliminating the need for locks.

By convention, in these articles, any function starting with "async_" can yield to the scheduler, so those calls are the point of context switch.  (Detail: such functions should ensure they yield at least once, regardless of the code path. Otherwise, it's easy to mistakenly starve the scheduler with busy loops.)  Another potential point of context switching is more subtle, but you still have explicit control by way of the code's structure:  when execution reaches the end of a nursery block, a yield happens if there are unfinished child tasks.

The nice corollary is that you can consider any code between "async_" calls to be atomic with respect to other concurrent tasks.

Specifically, start_soon() does not have a context switch.  It's only queuing the launch of the concurrent task for the next time the caller yields control.