[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: helper threads layer
- From: Javier Guerra <javier@...>
- Date: Thu, 9 Mar 2006 12:15:40 -0500
hi everybody
finally, i think i have it working! it's a library of helper threads, meant to
make it easy to write nonblocking libraries in C.
to be a bit more specific: in C, you split funcions in three:
static int xxx_prepare (lua_State *L, void **udata)
static int xxx_work (void *udata)
static int xxx_finish (lua_State *L, void *udata)
and register it with a macro add_helperfunc (L, &ops) (where ops is a struct
with those three C functions). after that, you get a Lua function in the
stack.
from Lua, you call that function, it calls the xxx_prepare() funcion with any
given parameters and returns a 'task'.
before that, you would have created a couple (or more) FIFO queues with
helper.newqueue(), and one or more threads with helper.newthread(). each
thread has one input and one output queue (several threads can share a
queue).
when you add the task to the queue (with queue:addtask (task)), a thread will
pick it up, and execute xxx_work() with the same udata built by the
xxx_prepare(). after xxx_work() returns, the thread pushes the task in the
output queue.
back in Lua, you can do a queue:wait(), it blocks until there's a task in the
queue (put there by a thread). then you call helper.finish(task) to execute
xxx_finish(). xxx_finish() should dispose any memory used by the task (in
udata), and return any value to the Lua caller.
now, we could write non-blocking versions of most IO libraries, and build Lua
wrappers that use this primitives to get a coroutine scheduler.
i'll post it later in LuaForge; any thoughts about the architecture?
--
Javier
Attachment:
pgphtYGVj8wz0.pgp
Description: PGP signature