This commit is contained in:
Alexey Andreev 2015-03-11 19:02:32 +04:00
parent 04342d15e7
commit 7e23498c5b
5 changed files with 25 additions and 13 deletions

View File

@ -286,7 +286,7 @@ public class TObject {
@Override @Override
public void onTimer() { public void onTimer() {
if (!expired()) { if (!expired()) {
Platform.postpone(this); run();
} }
} }

View File

@ -36,6 +36,7 @@ public class TThread extends TObject implements TRunnable {
private long id; private long id;
private int priority = 0; private int priority = 0;
private long timeSliceStart; private long timeSliceStart;
private int yieldCount;
private TString name; private TString name;
TRunnable target; TRunnable target;
@ -101,6 +102,10 @@ public class TThread extends TObject implements TRunnable {
} }
public static void yield() { public static void yield() {
if (++currentThread.yieldCount < 30) {
return;
}
currentThread.yieldCount = 0;
if (currentThread.timeSliceStart + 100 < System.currentTimeMillis()) { if (currentThread.timeSliceStart + 100 < System.currentTimeMillis()) {
switchContext(currentThread); switchContext(currentThread);
} }
@ -146,12 +151,13 @@ public class TThread extends TObject implements TRunnable {
private static void sleep(long millis, final AsyncCallback<Void> callback) { private static void sleep(long millis, final AsyncCallback<Void> callback) {
final TThread current = currentThread(); final TThread current = currentThread();
window.setTimeout(new TimerHandler() { int intMillis = millis < Integer.MAX_VALUE ? (int)millis : Integer.MAX_VALUE;
@Override public void onTimer() { Platform.schedule(new PlatformRunnable() {
@Override public void run() {
setCurrentThread(current); setCurrentThread(current);
callback.complete(null); callback.complete(null);
} }
}, millis); }, intMillis);
} }
public final void setPriority(int newPriority){ public final void setPriority(int newPriority){

View File

@ -264,6 +264,7 @@ public class Decompiler {
generator.indexer = indexer; generator.indexer = indexer;
parentNode = codeTree.getRoot(); parentNode = codeTree.getRoot();
currentNode = parentNode.getFirstChild(); currentNode = parentNode.getFirstChild();
boolean saved = !async;
for (int i = 0; i < this.graph.size(); ++i) { for (int i = 0; i < this.graph.size(); ++i) {
Block block = stack.peek(); Block block = stack.peek();
while (block.end == i) { while (block.end == i) {
@ -292,7 +293,6 @@ public class Decompiler {
if (head != -1 && loopSuccessors[head] == next) { if (head != -1 && loopSuccessors[head] == next) {
next = head; next = head;
} }
boolean saved = !async;
if (node >= 0) { if (node >= 0) {
generator.currentBlock = program.basicBlockAt(node); generator.currentBlock = program.basicBlockAt(node);
int tmp = indexer.nodeAt(next); int tmp = indexer.nodeAt(next);

View File

@ -699,7 +699,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("function $save()").ws().append("{").indent().softNewLine(); writer.append("function $save()").ws().append("{").indent().softNewLine();
writer.append("$rt_nativeThread()"); writer.append("$rt_nativeThread()");
for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { int firstToSave = 0;
if (methodNode.getModifiers().contains(NodeModifier.STATIC)) {
firstToSave = 1;
}
for (int i = firstToSave; i < variableCount; ++i) {
writer.append(".push(").append(variableName(i)).append(")"); writer.append(".push(").append(variableName(i)).append(")");
} }
writer.append(".push($ptr);"); writer.append(".push($ptr);");
@ -710,7 +714,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine(); writer.append("if").ws().append("($rt_resuming())").ws().append("{").indent().softNewLine();
writer.append("var $T").ws().append('=').ws().append("$rt_nativeThread();").softNewLine(); writer.append("var $T").ws().append('=').ws().append("$rt_nativeThread();").softNewLine();
writer.append("$ptr").ws().append('=').ws().append("$T.pop();"); writer.append("$ptr").ws().append('=').ws().append("$T.pop();");
for (int i = variableCount - 1; i > ref.parameterCount(); --i) { for (int i = variableCount - 1; i >= firstToSave; --i) {
writer.append(variableName(i)).ws().append('=').ws().append("$T.pop();"); writer.append(variableName(i)).ws().append('=').ws().append("$T.pop();");
} }
writer.softNewLine(); writer.softNewLine();
@ -1990,9 +1994,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.appendMethodBody(monitorEnterRef).append("("); writer.appendMethodBody(monitorEnterRef).append("(");
statement.getObjectRef().acceptVisitor(this); statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();
writer.append("if").ws().append("($rt_suspending())").ws().append("{").indent().softNewLine();
writer.append("return $save();").softNewLine();
writer.outdent().append("}").softNewLine();
} else { } else {
MethodReference monitorEnterRef = new MethodReference( MethodReference monitorEnterRef = new MethodReference(
Object.class, "monitorEnterSync", Object.class, void.class); Object.class, "monitorEnterSync", Object.class, void.class);

View File

@ -479,11 +479,16 @@ TeaVMThread.prototype.resume = function() {
} }
TeaVMThread.prototype.run = function() { TeaVMThread.prototype.run = function() {
$rt_currentNativeThread = this; $rt_currentNativeThread = this;
this.runner(); try {
$rt_currentNativeThread = null; this.runner();
} finally {
$rt_currentNativeThread = null;
}
if (this.suspendCallback !== null) { if (this.suspendCallback !== null) {
var self = this; var self = this;
this.suspendCallback(function() { var callback = this.suspendCallback;
this.suspendCallback = null;
callback(function() {
self.resume(); self.resume();
}); });
} }