[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Threads vs Coroutines and Wait()
- From: Mark Hamburg <mhamburg@...>
- Date: Tue, 08 Mar 2005 08:18:33 -0800
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
success, result = oldresume( co, yield( kYieldToScheduler ) )
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.