lua-users home
lua-l archive

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


Hi Sam,

On Sat, Nov 29, 2008 at 6:33 PM, Sam Roberts <vieuxtech@gmail.com> wrote:
> On Fri, Nov 28, 2008 at 1:02 AM, Patrick Donnelly
> <batrick.donnelly@gmail.com> wrote:
>> I'm working on a scripting engine (Nmap's NSE) that yields a thread
>> while waiting on network I/O. The engine will hold the thread until a
>> callback from the network I/O library, while another thread is
>> executing, places the yielded thread back into the running queue while
>> leaving the necessary arguments on the stack for lua_resume.
>
> How many OS threads are you using?

Just one, Nmap is single-threaded.

> You say you get a callback while a thread
> is executing... but if the netio library is running a select/poll type
> thing, with a single
> OS thread, then that means the lua VM isn't actually executing a lua thread, at
> this moment, unless the netio select loop was called from within lua?
> Or there are multiple
> OS threads?

When a Lua thread makes a call to the socket library (e.g. connecting,
sending data, reading data,  etc.), the C function will register a
callback function in the underlying nsock library which will be called
when the connect/sending of data/etc. is complete, then yield the
thread so other threads may do work; the callback is only run when
another Lua thread makes a call to the socket library (the socket
library will check if any other sockets are ready and will call the
sockets' callback function if so) or if the engine checks the nsock
library directly when all threads are waiting. Inside this callback
function I am able to push the actual return values from the C socket
function (e.g. socket:connect) and then place the thread inside the
engine's running queue to be resumed later.

My principle concern is whether it is considered legal to have a
thread yield and then make a protected call using that thread before
resuming it. We are allowed to push arguments in order to resume the
thread but are we allowed to make function calls using that thread
before doing so? My current implementation shows this is possible, but
I question whether it is considered legal or safe. Essentially:

testme:
if (lua_resume(thread, lua_gettop(thread)) == LUA_YIELD)
{
  lua_getfield(thread, LUA_REGISTRYINDEX, "some_Lua_function");
  lua_pushthread(thread);
  if (lua_pcall(thread, 1, 0, 0) != 0)  /* <---- Is this legal?? */
    fatal("...");
  /* push other arguments to the thread */
  goto testme;
}

Thanks,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant