use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.
the class JSR292_MultiThreadedTests method testSyncAll.
/**
* The test creates a MutableCallSite object, uses 1 separate Worker thread to change the target of the MutableCallSite,
* makes a call to MutableCallSite.syncAll() and validates that the change to the target by the worker thread is visible to the "main" thread.
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void testSyncAll() throws Throwable {
MethodHandle mh = null;
MutableCallSite mutableCS = null;
mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));
mutableCS = new MutableCallSite(mh);
String s = (String) mutableCS.dynamicInvoker().invokeExact();
AssertJUnit.assertEquals("1", s);
Thread workerThread = new WorkerThread_CS(mutableCS);
workerThread.start();
/* Sleep so that the worker thread has an opportunity to run */
for (int i = 0; i < 4; i++) {
Thread.sleep(1000);
if (workerThread.isAlive() == false) {
break;
}
}
/*Call to MutableCallSite.syncAll(). We expect update made by worker thread to be visible to the "main" thread now*/
MutableCallSite.syncAll(new MutableCallSite[] { mutableCS });
s = (String) mutableCS.dynamicInvoker().invokeExact();
boolean updatePerformed = false;
if (!s.equals("2")) {
System.out.println("WARNING: testSyncAll: MutableCallSite update not visible after 4 seconds");
System.out.println("WARNING: testSyncAll: joining worker thread");
workerThread.join();
/*Call to MutableCallSite.syncAll(). We expect update made by worker thread to be visible to the "main" thread now*/
MutableCallSite.syncAll(new MutableCallSite[] { mutableCS });
s = (String) mutableCS.dynamicInvoker().invokeExact();
if (s.equals("2")) {
updatePerformed = true;
} else {
System.out.println("ERROR: testSyncAll: MutableCallSite update made by worker thread not visible in main thread after syncAll");
}
} else {
updatePerformed = true;
}
AssertJUnit.assertTrue(updatePerformed);
}
use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.
the class MutableCallSiteTest method testIllegalState_dynamicInvoker_MutableCallSite.
/**
* Test for validating default MethodHandle behavior of MutableCallSite
* using MutableCallSite.dynamicInvoker().invoke()
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void testIllegalState_dynamicInvoker_MutableCallSite() throws Throwable {
MethodType mt = MethodType.methodType(void.class);
MutableCallSite mcs = new MutableCallSite(mt);
boolean iseHit = false;
try {
mcs.dynamicInvoker().invoke();
} catch (IllegalStateException e) {
iseHit = true;
}
AssertJUnit.assertTrue(iseHit);
}
use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.
the class MutableCallSiteTest method testBasicNegative_MutableCallSite.
/**
* Basic negative test for MutableCallSite
* @throws Throwable
*/
@Test(groups = { "level.extended" })
public void testBasicNegative_MutableCallSite() throws Throwable {
MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "pow", MethodType.methodType(double.class, double.class, double.class));
MutableCallSite mcs = new MutableCallSite(mh);
boolean wmtThrown = false;
try {
String s = (String) mcs.dynamicInvoker().invokeExact(2, 3);
} catch (WrongMethodTypeException e) {
wmtThrown = true;
}
AssertJUnit.assertTrue(wmtThrown);
}
use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.
the class BootstrapMethods method mcsBootstrap.
public static CallSite mcsBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
Lookup lookup = lookup();
MethodHandle add = lookup.findStatic(BootstrapMethods.class, "add", methodType(int.class, int.class, int.class));
MutableCallSite mcs = new MutableCallSite(add);
return new MutableCallSite(mcs.dynamicInvoker());
}
use of java.lang.invoke.MutableCallSite in project openj9 by eclipse.
the class BootstrapMethods method catchBootstrap.
public static CallSite catchBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
Lookup lookup = lookup();
MethodHandle add = lookup.findStatic(BootstrapMethods.class, "add", methodType(int.class, int.class, int.class));
MethodHandle catchHandle = catchException(add, RuntimeException.class, throwException(add.type().returnType(), RuntimeException.class));
return new MutableCallSite(catchHandle);
}
Aggregations