diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index fdfc1d6d8..96a957fd8 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -17,64 +17,57 @@ package org.teavm.classlib.java.lang; import org.teavm.dom.browser.Window; import org.teavm.javascript.spi.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; import org.teavm.platform.async.AsyncCallback; - /** * * @author Alexey Andreev */ @Superclass("") public class TObject { - + private static final Window window = (Window)JS.getGlobal(); private TThread owner; private TObject monitorLock; - private int monitorCount=0; + private int monitorCount; private JSArray notifyListeners; - private final Window window = (Window)JS.getGlobal(); - - @JSFunctor - private static interface NotifyListener extends JSObject{ + + interface NotifyListener extends JSObject { void handleNotify(); } - + static void monitorEnter(TObject o){ - if ( o.monitorLock == null ){ + if (o.monitorLock == null ){ o.monitorLock = new TObject(); } - while (o.owner != null && o.owner != TThread.currentThread() ){ + while (o.owner != null && o.owner != TThread.currentThread()) { try { o.monitorLock.wait(); } catch (InterruptedException ex) { - + } } o.owner = TThread.currentThread(); o.monitorCount++; - } - + static void monitorExit(TObject o){ - o.monitorCount--; - if ( o.monitorCount == 0 && 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(); } - + @Rename("fakeInit") public TObject() { } @@ -126,44 +119,40 @@ public class TObject { } } - + @Rename("notifyAll") public final void notifyAll0(){ if (notifyListeners != null){ JSArray listeners = window.newArray(); - while (notifyListeners.getLength() > 0 ){ + while (notifyListeners.getLength() > 0) { listeners.push(notifyListeners.shift()); } - while ( listeners.getLength() > 0 ){ + while (listeners.getLength() > 0) { listeners.shift().handleNotify(); } } - } - - + @Rename("wait") public final void wait0(long timeout) throws TInterruptedException{ try { wait(timeout, 0); - } catch ( InterruptedException ex){ + } catch (InterruptedException ex) { throw new TInterruptedException(); } } - + @Async @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 callback){ - if ( notifyListeners == null ){ - notifyListeners = window.newArray(); + public final void wait0(long timeout, int nanos, final AsyncCallback callback) { + if (notifyListeners == null) { + notifyListeners = window.newArray(); } final TThread currentThread = TThread.currentThread(); - notifyListeners.push(new NotifyListener(){ - + notifyListeners.push(new NotifyListener() { @Override public void handleNotify() { TThread.setCurrentThread(currentThread); @@ -172,12 +161,10 @@ public class TObject { } finally { TThread.setCurrentThread(TThread.getMainThread()); } - } - }); } - + @Rename("wait") public final void wait0() throws TInterruptedException { try { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java index 1c5bacaa7..578444e65 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java @@ -27,7 +27,6 @@ import org.teavm.platform.async.AsyncCallback; * @author Alexey Andreev */ public class TThread extends TObject implements TRunnable { - private static Window window = (Window)JS.getGlobal(); private static TThread mainThread = new TThread(TString.wrap("main")); private static TThread currentThread = mainThread; @@ -73,10 +72,8 @@ public class TThread extends TObject implements TRunnable { activeCount--; setCurrentThread(mainThread); } - - } - + static void setCurrentThread(TThread thread){ currentThread = thread; } @@ -136,13 +133,11 @@ public class TThread extends TObject implements TRunnable { return TObject.holdsLock(obj); } - @Async public static native void sleep(long millis) throws TInterruptedException; private static void sleep(long millis, final AsyncCallback callback) { final TThread current = currentThread(); - window.setTimeout(new TimerHandler() { @Override public void onTimer() { setCurrentThread(current);