Add back code for single-threaded coroutines.

This commit is contained in:
James Roseborough
2008-07-24 14:52:45 +00:00
parent 28f349a6fa
commit 00b14efa99

View File

@@ -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,6 +89,7 @@ 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;
if ( USE_JAVA_THREADS ) {
this.notify(); this.notify();
try { try {
this.wait(); this.wait();
@@ -97,15 +98,16 @@ public class LThread extends LValue implements Runnable {
status = STATUS_DEAD; status = STATUS_DEAD;
threadVm.error(this+" "+e); 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);
} }
// execute in the other thread
if ( USE_JAVA_THREADS ) {
// start the thread
if ( thread == null ) {
thread = new Thread(this);
thread.start();
}
// run this vm until it yields // run this vm until it yields
this.notify(); this.notify();
this.wait(); 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);
if ( USE_JAVA_THREADS ) {
this.notify(); 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 {
} }
} }
} }