lua_state and threading

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_state and threading

Dan Marks
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?

Daniel Marks
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: lua_state and threading

Luiz Henrique de Figueiredo
>From [hidden email] Sat Apr 18 01:22:37 1998

>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.

good!

>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?

a Cclosure is like a Lua closure, except that it's a C function :-)
in 3.1, we have introduced the anonymous functions and the concept of upvalues,
which are a way to access the value of local variables in the code that defines
the function (see manual for example).
A closure is then made of code plus upvalues.
in a Cclosure, the code is  C function, that's all.
the upvalues are the ones pushed before the the call to lua_pushCclosure.

one typical application of Cclosures is to export C functions to Lua with
some fixed arguments. 
for example, you might want to export fputs to Lua, but with a fixed FILE*.
so, you just push this FILE* as an upvalue.
then you get a function in Lua that only needs the string.
the C function itself receives 2 args, but the C closure will provide one
automatically.

hope this helps.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua_state and threading

Steve Dekorte-2
Here's an example of the read() error I've been getting.
After the end of the file, read() return spurious results and
eventually causes a memory access exception.
(On OpenStep 4.2 Mach, at least)

This only happens with the latest version of Lua.
Earlier versions worked ok.

---

-- create a small /tmp/test file before running
function testLuaRead()
    local result = 1
    local count = 0

    readfrom("/tmp/test")

    while ( result ~= nil and count < 100 ) do
       count = count + 1
        result = read("{%s*}%S%S*")
        NSLog("result = "..tostring(result))
    end

end

testLuaRead()

Reply | Threaded
Open this post in threaded view
|

Idea for Lua web site

Steve Dekorte-2
In reply to this post by Luiz Henrique de Figueiredo
It would be cool to have a page on the Lua web site that listed some of the
people and projects using Lua.

Steve

Reply | Threaded
Open this post in threaded view
|

Re: Idea for Lua web site

Luiz Henrique de Figueiredo
>From [hidden email] Sun May  3 22:19:41 1998
>
>It would be cool to have a page on the Lua web site that listed some of the
>people and projects using Lua.

I agree. Anyone that wants their projects listed, please send me the info
(a small paragraph describing the project, and any related URLs) and I'll
set up a page with those.
Thanks.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Idea for Lua web site

Steve Able
It was suggested that projects using LUA be listed.

GT Software, Inc. has developed an IBM CICS Based HTTP/FTP Server that
includes a 3270 Web Browser. The Product "Novation" (TM) includes LUA as a
HTTP server  script language. 

url = http://www.gtsoftware.com/

Cheers,

Steven Able

GT Software, Inc.
Voice: (770)475-9526
Fax: (770)664-7184
[hidden email]

I am captivated more by dreams of the future than by the history of the
past. 



Reply | Threaded
Open this post in threaded view
|

Re: Idea for Lua web site

Steve Dekorte-2
In reply to this post by Luiz Henrique de Figueiredo
>I agree. Anyone that wants their projects listed, please send me the info
>(a small paragraph describing the project, and any related URLs) and I'll
>set up a page with those.

I'm working independently on a graphical Lua development environment &
debugger for OpenStep(on NT and Rhapsody).

Steve