lua-users home
lua-l archive

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

No comments, so maybe I'm not alone in not quite understanding what
the situation is.

On Fri, Nov 28, 2008 at 1:02 AM, Patrick Donnelly
<> 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? 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?

I kindof assume there's a bunch of lua threads, you know each one is
waiting io readiness,
you have a main IO loop that uses select and only one OS thread, and
when a fd for a particular lua thread is ready you call lua_resume()
on that thread with some args?

>  My
> question is: what can we safely do while using that thread inside the
> callback function.

Depends on the relationship between the callback and the lua VM, which
isn't clear.

> If a memory error (or any error) is encountered
> while putting the items on the stack for lua_resume, what should
> happen? Further, is it safe to push a function and make a protected
> call on the yielded thread?

Yes. But... if the only error you can have is memory, its not so
likely that can happen, and
the VM maybe unusable if nmap has run out of memory. Generally, calls
to the basic lua
functions (pushstring, etc.) are assumed not to fail because the only
errors are due to not
being called correctly (hard, and a bug in the caller), or no memory
(hard on a modern box), and if they do error outside of a protected
env, the process will exited (though, you can actually set your own
handler for this condition).

lua_resume() effectively does a pcall inside, and you can see whether
the resumed function errored by its return values.