Mark Hamburg | 1 Jan 2004 17:38
Picon
Favicon

GC causes unexpected reentrancy

Some background: I'm working on a multi-threaded (as in system threads)
application in which multiple threads can all access the same Lua universe
through multiple lua_States. They end up serializing at the Lua API because
of the way lua_lock works, but that's not important.

Now, consider the case where I've got userdata that needs to store Lua
references of some sort. In my case, it's that I've got reference counts on
userdata items for non-Lua references and any items with non-zero reference
counts have to be entered into a Lua table so that they don't get garbage
collected. I believe, however, that the same problems would arise if the
userdata items held Lua refs.

Because the application is multi-threaded, when I update the Lua data
structures (userdata retention table or a ref table), I need to bracket
those operations with mutex locks.

So, now consider the following path....

* Retain an object (or release it)
    * Locks the retention table mutex
        * Calls to Lua to modify the table
            * Lua invokes the GC
                * The GC invokes a userdata GC method
                    * Userdata GC method releases an object
                        * Attempt to lock the retention table mutex

Deadlock.

For my particular usage, I might be able to resolve my problem by using a
recursive mutex for the retention table, but that gets a bit delicate. For
(Continue reading)

D Burgess | 1 Jan 2004 21:02

Exceptions

Would anyone like to share the code for replacing 
setjmp/longjmp with C++ exceptions or Win32 structured
exception handling?

David B

Kevin Baca | 1 Jan 2004 21:30

RE: Exceptions

Here's one example:

http://www.fensende.com/~mcuddy/ltn/luapp.html

Although it's lua 4.x, it shouldn't be much trouble to modify it for lua
5.x.

-Kevin

> -----Original Message-----
> From: lua-bounces <at> bazar2.conectiva.com.br 
> [mailto:lua-bounces <at> bazar2.conectiva.com.br] On Behalf Of D Burgess
> Sent: Friday, January 02, 2004 12:00 AM
> To: Lua list
> Subject: Exceptions
> 
> 
> Would anyone like to share the code for replacing 
> setjmp/longjmp with C++ exceptions or Win32 structured 
> exception handling?
> 
> David B
> 
> 
> 

Kevin Baca | 1 Jan 2004 21:37

RE: GC causes unexpected reentrancy

How about turning GC on and off as needed?

Requires source modification.  See the luaC_checkGC macro.

-Kevin

> 
> Some background: I'm working on a multi-threaded (as in 
> system threads) application in which multiple threads can all 
> access the same Lua universe through multiple lua_States. 
> They end up serializing at the Lua API because of the way 
> lua_lock works, but that's not important.
> 
> Now, consider the case where I've got userdata that needs to 
> store Lua references of some sort. In my case, it's that I've 
> got reference counts on userdata items for non-Lua references 
> and any items with non-zero reference counts have to be 
> entered into a Lua table so that they don't get garbage 
> collected. I believe, however, that the same problems would 
> arise if the userdata items held Lua refs.
> 
> Because the application is multi-threaded, when I update the 
> Lua data structures (userdata retention table or a ref 
> table), I need to bracket those operations with mutex locks.
> 
> So, now consider the following path....
> 
> * Retain an object (or release it)
>     * Locks the retention table mutex
>         * Calls to Lua to modify the table
(Continue reading)

D Burgess | 1 Jan 2004 22:50

RE: Exceptions

I note that lhf in
http://lua-users.org/lists/lua-l/2003-06/msg00164.html

lhf says: The next version will contain macros that make this even 
easier.

Any chance of a preview of this code?
DB
+++++++++++++++++++++++++++++++++
>Here's one example:
>
>http://www.fensende.com/~mcuddy/ltn/luapp.html
>
>Although it's lua 4.x, it shouldn't be much trouble to modify it for lua
>5.x.
>
>-Kevin
>
>> -----Original Message-----
>> From: lua-bounces <at> bazar2.conectiva.com.br 
>> [mailto:lua-bounces <at> bazar2.conectiva.com.br] On Behalf Of D Burgess
>> Sent: Friday, January 02, 2004 12:00 AM
>> To: Lua list
>> Subject: Exceptions
>> 
>> 
>> Would anyone like to share the code for replacing 
>> setjmp/longjmp with C++ exceptions or Win32 structured 
>> exception handling?
>> 
(Continue reading)

Mark Hamburg | 2 Jan 2004 01:21
Picon
Favicon

Re: Exceptions

I modified the 5.0 distribution to allow luaD_throw and luaD_rawrunprotected
to be overriden. In my case it was to work with MacOS X's exception system
in Cocoa, but the principle is the same. You basically need to throw
exceptions of whatever type you are using in the luaD_throw equivalent --
most of the work is actually in translating between exception types -- and
implement an appropriate catching context in luaD_rawrunprotected.

I remember being favorably impressed at the time at how easy this was to do.

Summary:

    void my_throw( lua_State *L, int errcode ) {

        lua_unlock( L );
            // Unlock because we're about to go into non Lua VM C code

        // translate the error code and possibly the top of the stack into
        // an exception

        // raise/throw the exception

    }

    int my_rawrunprotected(
            lua_State *L,
            void (*f)( lua_State *L, void *ud ),
            void *ud ) {

        volatile int status = 0;

(Continue reading)

Mark Hamburg | 2 Jan 2004 01:29
Picon
Favicon

Re: GC causes unexpected reentrancy

Thanks for the suggestion. I could where I know I want to avoid reentrancy
issues just push the GC threshold up temporarily to avoid garbage collection
being triggered. That just takes identifying all such places. Executing the
gc metamethods on a separate thread is the more general solution but takes
actually changing the code.

Mark

on 1/1/04 12:37 PM, Kevin Baca at lualist <at> aidiastudios.com wrote:

> How about turning GC on and off as needed?
> 
> Requires source modification.  See the luaC_checkGC macro.

Evan Wies | 2 Jan 2004 01:59

RE: Fastest way of mapping a C++ object (or C struct) to Lua

I really like luabind (http://luabind.sourceforge.net).  It basically does
what tolua does, but uses template metaprogramming and some binding
functions, rather than parsing a "cleaned header" which generates code.  You
could do it yourself (as the other posts describe), but (for my purposes at
least) why bother if a compiler can make the same thing for you via
templates.

You would simply do this:

void register_objects( lua_State* L )
{
	using namespace luabind;
	module( L )
	[
		class<vector3>("vector3")
			// bind vector3 properties / methods for use in lua
		,
		class_<NPC>( "NPC" )
			.def( constructor<>() )
			.property( "health",   &NPC::Health )
			.property( "position", &NPC::position )
			.def( "Move",          &NPC::Move )
	];
}

Now you can simply use it in Lua:

npc = NPC();
npc.health = 1;
npc.position = vector3(0,0,0);
(Continue reading)

Dirk Zoller | 2 Jan 2004 12:28
Picon
Favicon

Minor printf() glitch.


Hello all,

I added the following lines to the beginning of <lua.h>:

#if !defined __GNUC__ && !defined __attribute__
# define __attribute__(X)
#endif

and changed lines from <lua.h> and <lauxlib.h> as follows:

LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...)
      __attribute__((format (printf, 2, 3)));

LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...)
      __attribute__((noreturn, format (printf, 2, 3)));

This enables type checking on all arguments passed through ... to a
printf-like format string. The result was one warning:

lstrlib.c:192: warning: unknown conversion type character `'' in format

Looks like this line should rather read:

         luaL_error(ms->L, "malformed pattern (ends with `%%')");

I have had no problems with this kind of __attribute__ definition in various
large multi-platform projects so far. It helps some and does not impose
portability problems.

(Continue reading)

Roberto Ierusalimschy | 2 Jan 2004 12:50
Picon
Picon

Re: Exceptions

> lhf says: The next version will contain macros that make this even 
> easier.
> 
> Any chance of a preview of this code?

We sent an overview to the list already. See

  http://lua-users.org/lists/lua-l/2003-12/msg00359.html

-- Roberto


Gmane