lua-users home
lua-l archive

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


I think someone actually posted a detailed and possibly even working
implementation, but here is a summary of how to allow yielding back to the
scheduler from within nested coroutines.

Essentially you need to introduce a magic value that means "yield back to
the scheduler" and then introduce a replacement for resume that does
essentially the following:

    function newresume( co, ... )
        local success, result = oldresume( co, ... )
        while true do
            if not success or result ~= kYieldToScheduler then
                return success, result
             end
             success, result = oldresume( co, yield( kYieldToScheduler ) )
        end
    end

A full implementation would deal with multiple return values from oldresume
but that starts requiring various inner functions.

The scheduler would continue to use oldresume.

Any calls to newresume will now potentially result in calls to yield from
that coroutine so one either needs to exercise care about where newresume is
called or one needs to use Mike Pall's RVM patch.

Mark