lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Kevin Martin
> Sent: zaterdag 30 maart 2013 9:02
> To: Lua mailing list
> Subject: Re: Lua and preemptive scheduling for coroutines
> 
> I considered something similar to this a while ago, but it doesn't work if
> the coroutine has a pcall inside (or if there is a c function on the stack
> that has called lua_pcall) as the error will be caught there rather than
> killing the routine.

I created a small module, see https://github.com/Tieske/corowatch 

It works around pcall. It keeps a register of coroutines and once an error has been thrown it updates the debughook to execute on every line/instruction. Everytime the hook gets called, and an error was already thrown but the coroutine is still alive, it throws it again. So that would be immediately after the pcall that caught the initial one.

Haven't tried a c-side pcall, but following the same logic, the c code should handle the error, and either return an error itself, which kills the coroutine, or if not, the next statement will be caught by the hook and the error will be thrown again.

Please give it a try and let me know what you think.

Thijs.

PS;
- if a coroutine that is being watched creates a new coroutine, the new one inherits the settings from the one that created it
- Gaspar mentioned yielding, but that only works if you know what the scheduler expects, so that is scheduler specific. As an alternative I added a warning callback, which should get a timeout smaller than the kill-timeout. So one could add the scheduler specific yield in there.
- it modifies the global coroutine table (has to, to make it work)