lua-users home
lua-l archive

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




On Sat, Feb 14, 2009 at 6:48 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
Hi, list!

Lua 5.1.4

I need to delay my script execution until I get an event from a host
program. To do this I wrap my script into a coroutine, and pass a code
to resume it into a callback. Engine stores callback in Lua registry
and calls it when ready.

A pseudocode:

     local coro -- A forward declaration
     coro = coroutine.create(function()
       ENGINE.call_me_later(function() assert(coroutine.resume(coro)) end)
       coroutine.yield()
     end)
     assert(coroutine.resume(coro))

When I run it, coroutine.resume() inside the callback fails with
message "cannot resume running coroutine". Coroutine status is
"running" and coroutine.running() points to the coroutine as well.

It seems like ENGINE.call_me_later() is executing the function that it is being passed before returning, so it gets run in the context of the coro coroutine itself, which obviously cannot be resumed because it is not suspended. I freely admit I may be misunderstanding something here though.

-Duncan