lua-users home
lua-l archive

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

Is it possible to freeze the execution of a lua program, save its state
and, some time later, restore the saved state and resume execution from
where it stopped?


I spent alot of time researching topic this a few weeks back and have
implemented a solution that allows multiple lua programs to run
concurrently while being able to freeze, restart or terminate them at
any point.

The best bang for the buck solution I've found so far is to spawn each
lua invocation in it's own separate thread, and then have a manager
class suspend and wake each lua thread as needed.  That allows you to
neatly avoid many issues with thread syncronization. So far it has
seems to work well and is fairly efficient. I'm using it as the
scripting language for a game title.

What you end up doing is turning a take a bunch of fully multi-tasking
threads and make  them co-operativly multi-task =).

So the manager works like this.

 LuaManager -->
         Runs LuaProgram1 till lua program call waits or time X
         Runs LuaProgram2 ....
         Runs LuaProgram3 ....
         Runs LuaProgram4 ....

The keys is to make each lua program call a "wait" function in the
manager class to get itself suspended after it's done it's update loop.
 Either that or only give the luaProgram a specific timeslice that its
allowed to use.   But that could lead to problems if the luaProgram is
calling C functions that are order dependent.  It's better to make the
luaProgram call wait when it's at a safe point to be suspended.

It's fairly straight forward and doesn't involving messing around with
the LUA src.