lua-users home
lua-l archive

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


On Dec 10, 2001 at 02:36 -0800, Steve Dekorte wrote:
> 
> This is great. Is there any way to execute n number of instructions 
> before going to sleep?

Not as it stands.  That would be fairly simple to add.

I didn't want to tackle that the first time out though, because it
wasn't on my personal wishlist.  Mainly because pre-emptive
multitasking is much trickier to debug (on the scripting side) than
cooperative multitasking, since it's less predictable and gets you
into all kinds of race conditions -- a scary situation for script
programmers!

But given those caveats, lua_sleep() makes it pretty easy to add.  As
simple as:

  if (L->oplimit && ++L->opcounter >= L->oplimit) {
    L->opcounter = 0;
    lua_sleep(L);
  }

at the very bottom of the luaV_execute() loop (or just before the
instruction fetch), plus some code to initialize and access opcounter
and oplimit.  Set oplimit to non-zero to enable it.

> Is there a hook that get's called when sleep is 
> called?

Nope.  I don't think it would be hard to add, but I haven't paid much
attention to how callbacks work in Lua.  What would you use it for?

> I'm interested in playing around with actor-based programming techniques 
> in Lua where every object has it's own "thread" of execution.  Real 
> threads would be much to heavy, but emulating threads by putting an 
> object to sleep and switching to the next active object might work.

Yeah, definitely.  I prefer the explicit cooperative approach because
of those race issues, but I don't see any reason the preemptive
approach wouldn't work, Lua-wise.

-- 
Thatcher Ulrich <tu@tulrich.com>
http://tulrich.com