/* * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
publicclass Basic { staticfinal ContinuationScope FOO = new ContinuationScope() {};
@Test publicvoid test1() { // Basic freeze and thaw final AtomicInteger res = new AtomicInteger(0);
Continuation cont = new Continuation(FOO, ()-> { double r = 0; for (int k = 1; k < 20; k++) { int x = 3;
String s = "abc";
r += foo(k);
}
res.set((int)r);
});
int i = 0; while (!cont.isDone()) {
cont.run();
System.gc();
staticdouble fooThrow(int a) { long x = 8;
String s = "yyy";
String r = barThrow(a + 1); return Integer.parseInt(r)+1;
}
static String barThrow(long b) { double x = 9.99;
String s = "zzz";
Continuation.yield(FOO);
long r = b+1;
if (true) thrownew LoomException("Loom exception!"); return"" + r;
}
@Test publicvoid testException1() { // Freeze and thaw with exceptions
Continuation cont = new Continuation(FOO, ()-> { double r = 0; for (int k = 1; k < 20; k++) { int x = 3;
String s = "abc";
r += fooThrow(k);
}
});
cont.run(); try {
cont.run();
fail("Exception not thrown.");
} catch (LoomException e) {
assertEquals(e.getMessage(), "Loom exception!"); // e.printStackTrace();
StackTraceElement[] stes = e.getStackTrace();
assertEquals(stes[0].getMethodName(), "barThrow"); int index = -1; for (int i=0; i<stes.length; i++) { if (stes[i].getClassName().equals(Continuation.class.getName()) && stes[i].getMethodName().equals("enter")) {
index = i; break;
}
}
assertTrue(index >= 0);
StackTraceElement last = stes[index];
}
}
@Test publicvoid testManyArgs() { // Methods with stack-passed arguments final AtomicInteger res = new AtomicInteger(0);
Continuation cont = new Continuation(FOO, ()-> {
res.set((int)manyArgsDriver());
});
int i = 0; while (!cont.isDone()) {
cont.run();
System.gc();
}
assertEquals(res.get(), 247);
}
staticdouble manyArgsDriver() { double r = 0; for (int k = 1; k < 20; k++) { int x = 3;
String s = "abc";
staticdouble noSyncFoo(int a) { long x = 7; synchronized(FOO) {
x += FOO.getClass().getName().contains("FOO") ? 1 : 0;
}
String s = "yyy";
String r = bar2(a + 1); return Integer.parseInt(r)+1;
}
@Test publicvoid testPinnedCriticalSection() { // pinning due to critical section final AtomicReference<Continuation.Pinned> res = new AtomicReference<>();
staticdouble nativeFoo(int a) { try { int x = 8;
String s = "yyy"; return nativeBar(x);
} catch (Exception e) { thrownew AssertionError(e);
}
}
staticint nativeBaz(int b) { double x = 9.99;
String s = "zzz"; assert Continuation.isPinned(FOO); boolean res = Continuation.yield(FOO); assert res == false;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.