Skip to content
Permalink
Browse files
Defer finalization of Lua objects until someone in the Lua thread is …
…creating a new object.

This way a long-running Lua thread won't cause the garbage collector to time out blocking on synchronized(L).
  • Loading branch information
Tim Mensch committed Feb 8, 2013
1 parent b2c5d3f commit a364aee
Showing 1 changed file with 29 additions and 4 deletions.
@@ -27,6 +27,7 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.StringTokenizer;
import java.util.Vector;

/**
* This class represents a Lua object of any type. A LuaObject is constructed by a {@link LuaState} object using one of
@@ -51,11 +52,24 @@
* @author Rizzato
* @author Thiago Ponte
*/
@SuppressWarnings("rawtypes")
public class LuaObject
{
protected Integer ref;

protected LuaState L;
static int REGISTRYINDEX = LuaState.LUA_REGISTRYINDEX.intValue();

protected static Object m_deleteListLock = new Object();
class RegisterPair {
public RegisterPair(int a_, int b_)
{
a=a_;
b=b_;
}
int a;
int b;
};
protected static Vector<RegisterPair> m_toDelete = new Vector<RegisterPair>();

/**
* Creates a reference to an object in the variable globalName
@@ -71,6 +85,18 @@ protected LuaObject(LuaState L, String globalName)
L.getGlobal(globalName);
registerValue(-1);
L.pop(1);

synchronized (m_deleteListLock)
{
if (L.getCPtrPeer() != 0)
{
for (RegisterPair l : m_toDelete)
{
L.LunRef(l.a,l.b);
}
m_toDelete.clear();
}
}
}
}

@@ -209,10 +235,9 @@ protected void finalize()
{
try
{
synchronized (L)
synchronized (m_deleteListLock)
{
if (L.getCPtrPeer() != 0)
L.LunRef(REGISTRYINDEX, ref.intValue());
m_toDelete.add(new RegisterPair(LuaState.LUA_REGISTRYINDEX, ref));
}
}
catch (Exception e)

0 comments on commit a364aee

Please sign in to comment.