Add back code for single-threaded coroutines.
This commit is contained in:
@@ -28,6 +28,8 @@ package org.luaj.vm;
|
|||||||
*/
|
*/
|
||||||
public class LThread extends LValue implements Runnable {
|
public class LThread extends LValue implements Runnable {
|
||||||
|
|
||||||
|
private static final boolean USE_JAVA_THREADS = true;
|
||||||
|
|
||||||
private static final int STATUS_SUSPENDED = 0;
|
private static final int STATUS_SUSPENDED = 0;
|
||||||
private static final int STATUS_RUNNING = 1;
|
private static final int STATUS_RUNNING = 1;
|
||||||
private static final int STATUS_NORMAL = 2;
|
private static final int STATUS_NORMAL = 2;
|
||||||
@@ -41,11 +43,9 @@ public class LThread extends LValue implements Runnable {
|
|||||||
private int status = STATUS_SUSPENDED;
|
private int status = STATUS_SUSPENDED;
|
||||||
|
|
||||||
LuaState threadVm;
|
LuaState threadVm;
|
||||||
Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
static LThread running;
|
static LThread running;
|
||||||
|
|
||||||
|
|
||||||
public LThread(LFunction c, LTable env) {
|
public LThread(LFunction c, LTable env) {
|
||||||
threadVm = new LuaState(env);
|
threadVm = new LuaState(env);
|
||||||
threadVm.pushlvalue(c);
|
threadVm.pushlvalue(c);
|
||||||
@@ -59,7 +59,7 @@ public class LThread extends LValue implements Runnable {
|
|||||||
return "thread: "+hashCode();
|
return "thread: "+hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the environment if a thread, or closure, and return 1, otherwise return 0 */
|
// Set the environment if a thread, or closure, and return 1, otherwise return 0
|
||||||
public boolean luaSetEnv(LTable t) {
|
public boolean luaSetEnv(LTable t) {
|
||||||
threadVm._G = t;
|
threadVm._G = t;
|
||||||
return true;
|
return true;
|
||||||
@@ -89,23 +89,25 @@ public class LThread extends LValue implements Runnable {
|
|||||||
if ( status != STATUS_RUNNING )
|
if ( status != STATUS_RUNNING )
|
||||||
threadVm.error(this+" not running");
|
threadVm.error(this+" not running");
|
||||||
status = STATUS_SUSPENDED;
|
status = STATUS_SUSPENDED;
|
||||||
this.notify();
|
if ( USE_JAVA_THREADS ) {
|
||||||
try {
|
this.notify();
|
||||||
this.wait();
|
try {
|
||||||
status = STATUS_RUNNING;
|
this.wait();
|
||||||
} catch ( InterruptedException e ) {
|
status = STATUS_RUNNING;
|
||||||
status = STATUS_DEAD;
|
} catch ( InterruptedException e ) {
|
||||||
threadVm.error(this+" "+e);
|
status = STATUS_DEAD;
|
||||||
|
threadVm.error(this+" "+e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This needs to leave any values returned by yield in the coroutine
|
// This needs to leave any values returned by yield in the coroutine
|
||||||
* on the calling vm stack
|
// on the calling vm stack
|
||||||
* @param vm
|
// @param vm
|
||||||
* @param nargs
|
// @param nargs
|
||||||
*/
|
//
|
||||||
public void resumeFrom(LuaState vm, int nargs) {
|
public void resumeFrom(LuaState vm, int nargs) {
|
||||||
|
|
||||||
synchronized ( this ) {
|
synchronized ( this ) {
|
||||||
@@ -126,19 +128,30 @@ public class LThread extends LValue implements Runnable {
|
|||||||
status = STATUS_RUNNING;
|
status = STATUS_RUNNING;
|
||||||
|
|
||||||
// copy args in
|
// copy args in
|
||||||
if ( thread == null ) {
|
if (threadVm.cc < 0) {
|
||||||
vm.xmove(threadVm, nargs);
|
vm.xmove(threadVm, nargs);
|
||||||
threadVm.prepStackCall();
|
threadVm.prepStackCall();
|
||||||
thread = new Thread(this);
|
|
||||||
thread.start();
|
|
||||||
} else {
|
} else {
|
||||||
threadVm.resettop();
|
threadVm.resettop();
|
||||||
vm.xmove(threadVm, nargs);
|
vm.xmove(threadVm, nargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// run this vm until it yields
|
// execute in the other thread
|
||||||
this.notify();
|
if ( USE_JAVA_THREADS ) {
|
||||||
this.wait();
|
// start the thread
|
||||||
|
if ( thread == null ) {
|
||||||
|
thread = new Thread(this);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// run this vm until it yields
|
||||||
|
this.notify();
|
||||||
|
this.wait();
|
||||||
|
} else {
|
||||||
|
// run this vm until it yields
|
||||||
|
while (threadVm.cc >= 0 && status == STATUS_RUNNING)
|
||||||
|
threadVm.exec();
|
||||||
|
}
|
||||||
|
|
||||||
// copy return values from yielding stack state
|
// copy return values from yielding stack state
|
||||||
vm.resettop();
|
vm.resettop();
|
||||||
@@ -156,7 +169,9 @@ public class LThread extends LValue implements Runnable {
|
|||||||
vm.resettop();
|
vm.resettop();
|
||||||
vm.pushboolean(false);
|
vm.pushboolean(false);
|
||||||
vm.pushstring("thread: "+t);
|
vm.pushstring("thread: "+t);
|
||||||
this.notify();
|
if ( USE_JAVA_THREADS ) {
|
||||||
|
this.notify();
|
||||||
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// previous thread is now running again
|
// previous thread is now running again
|
||||||
@@ -165,5 +180,4 @@ public class LThread extends LValue implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user