[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: lua_state and threading
- From: Dan Marks <dmarks@...>
- Date: Fri, 17 Apr 1998 23:19:09 -0500 (CDT)
Since lhf asked (I think) about any results using the new lua_state feature
of 3.1 alpha I thought I would report some results.
We (Ron Stack and I) are currently working on a Win32 program called 3DImager
which is now more like a Labview like program. It is for controlling our
optics experiments, automating taking pictures and doing image processing.
We use lua code to run our experiments, linking in extra libraries
to do image processing.
Anyways, I incorporated 3.1 alpha into 3DImager. We wanted to make
multiple scripts able to run simultaneously. To do this, we used a hack
of Visual C++ called Thread-local variables. Each time a new thread of
created, a new instance of the thread local variables are created with it.
To make lua multithreaded, we made the lua_state pointer a Thread-local
variable. Each running lua script has its own output window. To make
it so that a lua script could be aborted by pushing a button on the user
interface, we hacked the VM loop to check a variable called
lua_stop and lua_error() if it is set to one.
One method of allowing multiple threads to run separate lua instances
is to use Thread-local variables, either by declaring lua_state to
be a Thread-local variable, or by redefining the "L" macro to use a
threading API function which retrieves the thread local data
(which is a pointer to the current lua instance). One disadvantage of
this is that a thread can not access more than its lua instance.
However, it is simple and requires few system specific changes to be
made to lua. Otherwise we could use a solution like Sun's green threads
for Java, or POSIX threads.
Anyways, we hope to allow hardware device code itself to be written in
lua, so simple devices that are controlled through GPIB or the serial
port will be easy to adapt to.
One thing I did not get with 3.1 alpha is what the luaCclosure function
is for. In the example it is used for the iolib library to assign tag
functions. How is this different than lua_pushcfunction?