Class LuaThread
- All Implemented Interfaces:
KryoSerializable
LuaValue
that implements
a lua coroutine thread using Java Threads.
A LuaThread is typically created in response to a scripted call to
coroutine.create()
The threads must be initialized with the globals, so that
the global environment may be passed along according to rules of lua.
This is done via the constructor arguments LuaThread(Globals)
or
LuaThread(Globals, LuaValue)
.
see to it that this Globals
are initialized properly.
The behavior of coroutine threads matches closely the behavior of C coroutine library. However, because of the use of Java threads to manage call state, it is possible to yield from anywhere in luaj.
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
OrphanedThread
which is an Error
.
Applications should not catch OrphanedThread
, because it can break
the thread safety of luaj. The value controlling the polling interval
is thread_orphan_check_interval
and may be set by the user.
There are two main ways to abandon a coroutine. The first is to call
yield()
from lua, or equivalently Globals.yield(Varargs)
,
and arrange to have it never resumed possibly by values passed to yield.
The second is to throw OrphanedThread
, which should put the thread
in a dead state. In either case all references to the thread must be
dropped, and the garbage collector must run for the thread to be
garbage collected.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.prineside.luaj.LuaValue
LuaValue.NillableSerializer, LuaValue.None
Nested classes/interfaces inherited from class com.prineside.luaj.Varargs
Varargs.ArrayVarargs
-
Field Summary
FieldsModifier and TypeFieldDescriptionThread-local used by DebugLib to store debugging state.static int
The current number of coroutines.Error message handler for this thread, if any.static final int
static LuaValue
Shared metatable for lua threads.static final int
static final int
static final String[]
static final int
static final int
static final int
static long
Polling interval, in milliseconds, which each thread uses while waiting to return from a yielded state to check if the lua threads is no longer referenced and therefore should be garbage collected.Fields inherited from class com.prineside.luaj.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILLABLE_SERIALIZER, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the metatable for thisLuaValue
boolean
boolean
isthread()
Check ifthis
is athread
Check that optional argument is a thread and return asLuaThread
void
int
type()
Get the enumeration value for the type of this value.typename()
Get the String name of the type of this value.void
Methods inherited from class com.prineside.luaj.LuaValue
add, add, add, and, arg, arg1, argerror, assert_, buffer, call, call, call, call, call, cBool, cDouble, cFloat, checkboolean, checkclosure, checkdouble, checkfunction, checkglobals, checkint, checkjstring, checklong, checknotnil, checknumber, checknumber, checkstring, checktable, checkuserdata, checkuserdata, cInt, cNcDouble, cNcFloat, cNcInt, cObject, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, cRegObject, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, cVarargs, div, div, div, divInto, eq, eq_b, eqmtcall, equals, error, get, get, get, gt, gt, gt, gt_b, gt_b, gt_b, gteq, gteq, gteq, gteq_b, gteq_b, gteq_b, inext, initupvalue1, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isfunction, isint, isinttype, islong, isnil, isnumber, isRegObject, isstring, istable, isuserdata, isuserdata, isvalidkey, len, length, listOf, listOf, load, lt, lt, lt, lt_b, lt_b, lt_b, lteq, lteq, lteq, lteq_b, lteq_b, lteq_b, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq, neq_b, next, not, onInvoke, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, opttable, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawlen, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, strcmp, strcmp, strongvalue, strvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf
Methods inherited from class com.prineside.luaj.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, dealias, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
-
Field Details
-
s_metatable
Shared metatable for lua threads. -
coroutine_count
public static int coroutine_countThe current number of coroutines. Should not be set. -
thread_orphan_check_interval
public static long thread_orphan_check_intervalPolling interval, in milliseconds, which each thread uses while waiting to return from a yielded state to check if the lua threads is no longer referenced and therefore should be garbage collected. A short polling interval for many threads will consume server resources. Orphaned threads cannot be detected and collected unless garbage collection is run. This can be changed by Java startup code if desired. -
STATUS_INITIAL
public static final int STATUS_INITIAL- See Also:
-
STATUS_SUSPENDED
public static final int STATUS_SUSPENDED- See Also:
-
STATUS_RUNNING
public static final int STATUS_RUNNING- See Also:
-
STATUS_NORMAL
public static final int STATUS_NORMAL- See Also:
-
STATUS_DEAD
public static final int STATUS_DEAD- See Also:
-
STATUS_NAMES
-
state
-
MAX_CALLSTACK
public static final int MAX_CALLSTACK- See Also:
-
callstack
Thread-local used by DebugLib to store debugging state. This is an opaque value that should not be modified by applications. -
globals
-
errorfunc
Error message handler for this thread, if any.
-
-
Constructor Details
-
LuaThread
Private constructor for main thread only -
LuaThread
Create a LuaThread around a function and environment- Parameters:
func
- The function to execute
-
-
Method Details
-
write
- Specified by:
write
in interfaceKryoSerializable
-
read
- Specified by:
read
in interfaceKryoSerializable
-
type
public int type()Description copied from class:LuaValue
Get the enumeration value for the type of this value.- Specified by:
type
in classLuaValue
- Returns:
- value for this type, one of
LuaValue.TNIL
,LuaValue.TBOOLEAN
,LuaValue.TNUMBER
,LuaValue.TSTRING
,LuaValue.TTABLE
,LuaValue.TFUNCTION
,LuaValue.TUSERDATA
,LuaValue.TTHREAD
- See Also:
-
typename
Description copied from class:LuaValue
Get the String name of the type of this value.- Specified by:
typename
in classLuaValue
- Returns:
- name from type name list
LuaValue.TYPE_NAMES
corresponding to the type of this value: "nil", "boolean", "number", "string", "table", "function", "userdata", "thread" - See Also:
-
isthread
public boolean isthread()Description copied from class:LuaValue
Check ifthis
is athread
-
optthread
Description copied from class:LuaValue
Check that optional argument is a thread and return asLuaThread
-
checkthread
Description copied from class:LuaValue
- Overrides:
checkthread
in classLuaValue
- Returns:
this
if it is aLuaThread
- See Also:
-
getmetatable
Description copied from class:LuaValue
Get the metatable for thisLuaValue
For
LuaTable
andLuaUserdata
instances, the metatable returned is this instance metatable. For all other types, the class metatable value will be returned.- Overrides:
getmetatable
in classLuaValue
- Returns:
- metatable, or null if it there is none
-
getStatus
-
isMainThread
public boolean isMainThread() -
resume
-