lua-users home
lua-l archive

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


On 03/31/2014 02:03 PM, Dominique Torette wrote:

Hi,

 

I’m trying to build a kind of pre-emptive scheduler around Lua VM 5.2.

The idea is to execute different lua_State in “parallel’.

To give the illusion of parallelism, the idea is to run a thread for a given number of Lua VM instructions and then run the next thread in a list for this number of instructions and so on…

From the LUA regular API, when starting the execution of a thread, it is not possible to control the number of Lua VM instructions to execute.

But from the LUA debug API, it is possible to define a ‘hook’ to execute every COUNT of instructions.

The first idea was to install such hook and perform the round-robin scheduling from the hook. The lua_State of active thread seems to be saved before running such hook.

But I didn’t found any means to overwrite the thread to be resumed from the hook.

Then the idea was to ‘yield’ from the hook, back to the round robin scheduler.

This seems to work in the simplest cases. But I’ve noticed some strange “intereferences” when thread use coroutines on its own.

 

The Lua resume API has a ‘from’ parameter: int lua_resume (lua_State *L, lua_State *from, int nargs);

But from the description, I didn’t catch the how this param has to be used.

 

I’ve written a small “C” main program to illustrate the concept and the “interferences”…

In the program here below, the first iteration of the loop works as expected.

Then the L[0] and its coroutine are resumed, but the hook is reached faster than for the first iteration and the L[1] is rescheduled.

After L[0] will be never rescheduled, only L[1].

 

Thanks in advance, Dominique Torette.


Hi
Maybe you are interested in my talk I gave at the last Lua workshop in Toulouse:
http://www.lua.org/wshop13.html#Jericke

It includes some details how I used debug hooks to make Lua preemtive.
--
Thomas