diff --git a/README.html b/README.html index 67c794c6..a67a8596 100644 --- a/README.html +++ b/README.html @@ -469,6 +469,18 @@ Time is a represented as number of milliseconds since the epoch, and most time and date formatting, locales, and other features are not implemented. +
+Luaj uses WeakReferences and the OrphanedThread error to ensure that coroutines that are no longer referenced +are properly garbage collected. For thread safety, OrphanedThread should not be caught by Java code. +See LuaThread +and OrphanedThread +javadoc for details. +
-The Java ME platform does not include this library, and it cannot be made to work because of the lack of a reflection API in Java SE. +The Java ME platform does not include this library, and it cannot be made to work because of the lack of a reflection API in Java ME.
The lua connand line tool includes luajava. @@ -734,6 +746,7 @@ and LuaForge:
+ * Each Java thread wakes up at regular intervals and checks a weak reference + * to determine if it can ever be resumed. If not, it throws + * {@link OrphanedThread} which is an {@link java.lang.Error}. + * Applications should not catch {@link OrphanedThread}, because it can break + * the thread safety of luaj. * * @see LuaValue * @see JsePlatform @@ -64,7 +64,10 @@ public class LuaThread extends LuaValue { public static LuaValue s_metatable; public static int coroutine_count = 0; - + + /** Interval at which to check for lua threads that are no longer referenced. + * This can be changed by Java startup code if desired. + */ static long thread_orphan_check_interval = 30000; private static final int STATUS_INITIAL = 0; diff --git a/src/core/org/luaj/vm2/OrphanedThread.java b/src/core/org/luaj/vm2/OrphanedThread.java index 08840d04..668217be 100644 --- a/src/core/org/luaj/vm2/OrphanedThread.java +++ b/src/core/org/luaj/vm2/OrphanedThread.java @@ -21,19 +21,20 @@ ******************************************************************************/ package org.luaj.vm2; - -/** - * Error sublcass that indicates a lua thread that is no longer referenced has been detected. - * - * The java thread in which this is thrown should correspond to a LuaThread being used as a - * coroutine that could not possibly be resumed again because there are no more references - * to the LuaThread with which it is associated. Rather than locking up resources forever, - * this error is thrown, and should fall through all the way to the thread's run() method. - * - * Java code mixed with the luaj vm should not catch this error because it may occur when - * the coroutine is not running, so any processing done during error handling could break - * the thread-safety of the application because other lua processing could be going on in - * a different thread. +/** + * {@link java.lang.Error} sublcass that indicates a lua thread that is no + * longer referenced has been detected. + *
+ * The java thread in which this is thrown should correspond to a + * {@link LuaThread} being used as a coroutine that could not possibly be + * resumed again because there are no more references to the LuaThread with + * which it is associated. Rather than locking up resources forever, this error + * is thrown, and should fall through all the way to the thread's {@link Thread.run}() method. + *
+ * Java code mixed with the luaj vm should not catch this error because it may
+ * occur when the coroutine is not running, so any processing done during error
+ * handling could break the thread-safety of the application because other lua
+ * processing could be going on in a different thread.
*/
public class OrphanedThread extends Error {
diff --git a/src/jse/org/luaj/vm2/lib/jse/JavaClass.java b/src/jse/org/luaj/vm2/lib/jse/JavaClass.java
index 32e12fbb..aabbb2ee 100644
--- a/src/jse/org/luaj/vm2/lib/jse/JavaClass.java
+++ b/src/jse/org/luaj/vm2/lib/jse/JavaClass.java
@@ -76,9 +76,17 @@ class JavaClass extends JavaInstance implements CoerceJavaToLua.Coercion {
if ( fields == null ) {
Map m = new HashMap();
Field[] f = ((Class)m_instance).getFields();
- for ( int i=0; i