Changed native functions to use new AsyncCallback approach.

This commit is contained in:
Steve Hannah 2015-02-09 10:54:06 -08:00
parent 9465d24723
commit a1df42878c
2 changed files with 81 additions and 43 deletions

View File

@ -15,19 +15,18 @@
*/
package org.teavm.classlib.java.lang;
<<<<<<< HEAD
import org.teavm.dom.browser.Window;
import org.teavm.javascript.spi.Async;
import org.teavm.dependency.PluggableDependency;
import org.teavm.javascript.ni.GeneratedBy;
import org.teavm.javascript.ni.InjectedBy;
import org.teavm.javascript.ni.Rename;
import org.teavm.javascript.ni.Superclass;
import org.teavm.runtime.Async;
=======
import org.teavm.javascript.spi.Rename;
import org.teavm.javascript.spi.Superclass;
import org.teavm.jso.JS;
import org.teavm.jso.JSArray;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
import org.teavm.platform.Platform;
>>>>>>> dd25ae4759716d735fe6f93a54c8bfab2e7fc7bf
import org.teavm.platform.async.AsyncCallback;
/**
*
@ -39,6 +38,13 @@ public class TObject {
private TThread owner;
private TObject monitorLock;
private int monitorCount=0;
private JSArray<NotifyListener> notifyListeners;
private final Window window = (Window)JS.getGlobal();
@JSFunctor
private static interface NotifyListener extends JSObject{
void handleNotify();
}
static void monitorEnter(TObject o){
if ( o.monitorLock == null ){
@ -59,13 +65,11 @@ public class TObject {
static void monitorExit(TObject o){
o.monitorCount--;
if ( o.monitorCount == 0 ){
if ( o.monitorLock != null ){
if ( o.monitorCount == 0 && o.monitorLock != null){
o.owner = null;
o.monitorLock.notifyAll();
}
}
}
static boolean holdsLock(TObject o){
return o.owner == TThread.currentThread();
@ -114,14 +118,27 @@ public class TObject {
return Platform.clone(this);
}
@GeneratedBy(ObjectNativeGenerator.class)
@Rename("notify")
public native final void notify0();
public final void notify0(){
if (notifyListeners != null && notifyListeners.getLength() > 0){
notifyListeners.shift().handleNotify();
}
}
@GeneratedBy(ObjectNativeGenerator.class)
@Rename("notifyAll")
public native final void notifyAll0();
public final void notifyAll0(){
if (notifyListeners != null){
JSArray<NotifyListener> listeners = window.newArray();
while (notifyListeners.getLength() > 0 ){
listeners.push(notifyListeners.shift());
}
while ( listeners.getLength() > 0 ){
listeners.shift().handleNotify();
}
}
}
@Rename("wait")
@ -134,15 +151,37 @@ public class TObject {
}
@Async
@GeneratedBy(ObjectNativeGenerator.class)
@Rename("wait")
public native final void wait0(long timeout, int nanos) throws TInterruptedException;
@Rename("wait")
public final void wait0(long timeout, int nanos, final AsyncCallback<Void> callback){
if ( notifyListeners == null ){
notifyListeners = window.newArray();
}
final TThread currentThread = TThread.currentThread();
notifyListeners.push(new NotifyListener(){
@Override
public void handleNotify() {
TThread.setCurrentThread(currentThread);
try {
callback.complete(null);
} finally {
TThread.setCurrentThread(TThread.getMainThread());
}
}
});
}
@Rename("wait")
public final void wait0() throws TInterruptedException {
try {
wait(0l);
} catch (InterruptedException ex) {
} catch ( InterruptedException ex){
throw new TInterruptedException();
}
}

View File

@ -15,33 +15,26 @@
*/
package org.teavm.classlib.java.lang;
<<<<<<< HEAD
import org.teavm.dependency.PluggableDependency;
import org.teavm.javascript.ni.GeneratedBy;
import org.teavm.runtime.Async;
=======
import org.teavm.dom.browser.TimerHandler;
import org.teavm.dom.browser.Window;
import org.teavm.javascript.spi.Async;
import org.teavm.jso.JS;
import org.teavm.platform.async.AsyncCallback;
>>>>>>> dd25ae4759716d735fe6f93a54c8bfab2e7fc7bf
/**
*
* @author Alexey Andreev
*/
public class TThread extends TObject implements TRunnable {
<<<<<<< HEAD
private static Window window = (Window)JS.getGlobal();
private static TThread mainThread = new TThread(TString.wrap("main"));
private static TThread currentThread = mainThread;
private static long nextId = 1;
private static int activeCount = 1;
private long id;
=======
private static Window window = (Window)JS.getGlobal();
private static TThread currentThread = new TThread(TString.wrap("main"));
>>>>>>> dd25ae4759716d735fe6f93a54c8bfab2e7fc7bf
private TString name;
private TRunnable target;
@ -63,9 +56,13 @@ public class TThread extends TObject implements TRunnable {
id=nextId++;
}
@PluggableDependency(ThreadNativeGenerator.class)
@GeneratedBy(ThreadNativeGenerator.class)
public native void start();
public void start(){
window.setTimeout(new TimerHandler() {
@Override public void onTimer() {
launch(TThread.this);
}
}, 0);
}
private static void launch(TThread thread) {
try {
@ -80,10 +77,10 @@ public class TThread extends TObject implements TRunnable {
}
private static void setCurrentThread(TThread thread){
static void setCurrentThread(TThread thread){
currentThread = thread;
}
private static TThread getMainThread(){
static TThread getMainThread(){
return mainThread;
}
@ -106,9 +103,12 @@ public class TThread extends TObject implements TRunnable {
public static native void yield();
private static void yield(final AsyncCallback<Void> callback) {
final TThread current = currentThread();
window.setTimeout(new TimerHandler() {
@Override public void onTimer() {
setCurrentThread(current);
callback.complete(null);
setCurrentThread(mainThread);
}
}, 0);
}
@ -136,21 +136,20 @@ public class TThread extends TObject implements TRunnable {
return TObject.holdsLock(obj);
}
@Async
<<<<<<< HEAD
@GeneratedBy(ThreadNativeGenerator.class)
private static native void sleep(double millis) throws TInterruptedException;
=======
public static native void sleep(long millis) throws TInterruptedException;
private static void sleep(long millis, final AsyncCallback<Void> callback) {
final TThread current = currentThread();
window.setTimeout(new TimerHandler() {
@Override public void onTimer() {
setCurrentThread(current);
callback.complete(null);
setCurrentThread(mainThread);
}
}, millis);
}
>>>>>>> dd25ae4759716d735fe6f93a54c8bfab2e7fc7bf
}