lua-users home
lua-l archive

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


Hi All,

I've implemented a scheduler that reuses coroutines. Pretty standard stuff: spawn a new coroutine, yield, sleeping for some time, stop, wait for events.

Stop causes the current coroutine to abort and return itself to the pool of available coroutines, but I need to implement something to kill any arbitrary coroutine.

Stop works by throwing a nil error, which is just caught by a protected call. I'm assuming this doesn't work since lua_error doesn't return:

// Note: coro->m_Thread is *not* the currently running thread
lua_pushnil( coro->m_Thread );
lua_error( coro->m_Thread );

I'm thinking of this as a workaround:

static int kill( lua_State* L )
{
  Coro* coro = (Coro*)lua_touserdata( L, 1 );
  coro->m_KillMe = 1;
  return 0;
}

and then put a check in all other coroutine functions so as soon as the coroutine yields again it gets killed (i.e. a deferred stop.) Although this works I'd rather avoid having the coroutine run just to be killed when it yields as it can take a while if the coroutine is sleeping for a long period of time, leaving me with a useless "zoombie" coroutine that could be doing useful work.

Is there a way to throw an error in another Lua thread?

Thanks,

Andre