lua-users home
lua-l archive

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


> I wonder what exactly you're hoping to gain from making the yields implicit?

- Explicit yields are easy to forget at certain places
- Code may loop forever, especially a 3rd party untrusted code, which you may want to run by loading scripts
- If you want to implement something like multi-tenancy, where coroutines belong to different tenants and each tenant has a right for running a certain tenant-specific amount of time (e.g. tenant A can run at most 40% of overal time, and tenants B and C - only 30% each). With ability to interrupt coroutines implicitly it becomes much easier.
- My previous experience with Java, where only the thread itself may finish itself, shows that it is not always good to rely on the thread/coroutine to obey to the rules, when it comes to giving control to other threads/coroutines. If all of your code follows a strict discipline it works. But once you start using third-party libs or forget yielding at a few places, you get into problems. 

> It just means that now you have to deal with the synchronisation problems that stem from your code being potentially interrupted at any point while you don't 
> pick up the real parallelism from OS pre-emptive threading primitives.

As I mentioned, I have something like millions of actors in mind (e.g. one per player or on per session or one per social network user). You cannot map all of them to OS threading primitives anyway. Therefore, you need to re-invent one ;-) And actors do not need real parallelism most of the time. They are really similar to coroutines in this regard.

-Leo

Von: Josh Simmons <simmons.44@gmail.com>
An: Lua mailing list <lua-l@lists.lua.org>
Gesendet: 7:10 Freitag, 29.März 2013
Betreff: Re: Lua and preemptive scheduling for coroutines

I wonder what exactly you're hoping to gain from making the yields implicit?

It just means that now you have to deal with the synchronisation problems that stem from your code being potentially interrupted at any point while you don't pick up the real parallelism from OS pre-emptive threading primitives.


On Fri, Mar 29, 2013 at 5:02 PM, Leo Romanoff <romixlev@yahoo.com> wrote:




----- Ursprüngliche Message -----
> Von: Ross Bencina <rossb-lists@audiomulch.com>
> An: Lua mailing list <lua-l@lists.lua.org>
> CC:
> Gesendet: 4:58 Freitag, 29.März 2013
> Betreff: Re: Lua and preemptive scheduling for coroutines
>
> On 29/03/2013 2:35 PM, Tim Hill wrote:
>>  You're not going to be able to pull that one off. The fundamental
>>  structure of coroutines is different from threads, and nothing you do
>>  can really change that (aside from a VERY significant reworking of
>>  the Lua code base).
>
> I don't know about that, all he's talking about is conditionally
> injecting yeild() into the code at arbitrary points (such as the end of each
> statement)
>
> This could be done manually with a syntactic substitution. The question is
> whether it can be done using a debug hook or similar mechanism.


That's a correct guess and interpretation. I've done something similar for Java using a byte-code instrumentation during class-loading. Indeed, I was injecting a conditional yield() after each n instructions. Alternatively, it could be done at the beginning of each basic block in the control-flow graph of a function. I know that Groovy language, which is also Java based, does something very similar.

But I have no idea how something like byte-code rewriting can be done in Lua. And I have a feeling that debug hooks are a more light-weight approach. They don't need to really change the code by rewriting it. They just intercept execution after each instruction, line, call, return, etc, which seems to be a cleaner way.

Another important point: In case of instruction counting or time slices, I don't need to suspend exactly when the even happens, i.e. I don't need to suspend e.g. inside a C stack (or Lua stack) frames and so on. It is OK to suspend a bit later, once it is safe, i.e. when the execution is easy to suspend.

-Leo