Search in sources :

Example 1 with VolatileCallSite

use of java.lang.invoke.VolatileCallSite in project openj9 by eclipse.

the class JSR292_MultiThreadedTests method testVolatility.

/**
 * The test creates a VolatileCallSite, uses 1 separate worker thread to change the target of a volatile call site and
 * validates that the update is visible to the 'main' thread.
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testVolatility() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));
    VolatileCallSite volatileCS = new VolatileCallSite(mh);
    String s = (String) volatileCS.dynamicInvoker().invokeExact();
    AssertJUnit.assertEquals("1", s);
    Thread workerThread = new WorkerThread_CS(volatileCS);
    workerThread.start();
    boolean updatePerformed = false;
    /* Sleep so that the worker thread has an opportunity to run */
    for (int i = 0; i < 4; i++) {
        Thread.sleep(1000);
        s = (String) volatileCS.dynamicInvoker().invokeExact();
        if (s.equals("2")) {
            updatePerformed = true;
            break;
        }
    }
    if (!updatePerformed) {
        System.out.println("WARNING: testVolatility: VolatileCallSite not updated after waiting 4 seconds");
        System.out.println("WARNING: testVolatility: joining worker thread");
        workerThread.join();
        s = (String) volatileCS.dynamicInvoker().invokeExact();
        if (s.equals("2")) {
            updatePerformed = true;
        } else {
            System.out.println("ERROR: testVolatility: VolatileCallSite update made by worker thread not visible in main thread");
        }
    }
    AssertJUnit.assertTrue(updatePerformed);
}
Also used : VolatileCallSite(java.lang.invoke.VolatileCallSite) SwitchPoint(java.lang.invoke.SwitchPoint) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 2 with VolatileCallSite

use of java.lang.invoke.VolatileCallSite in project openj9 by eclipse.

the class JSR292_MultiThreadedTests method testVolatility_WithoutJoin.

/**
 * Tests visibility of a VolatileCallSite update made in main thread in a separate worker thread
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testVolatility_WithoutJoin() throws Throwable {
    MethodHandle K_false = MethodHandles.constant(boolean.class, false);
    MethodHandle K_true = MethodHandles.constant(boolean.class, true);
    // Run the following code twice so that we provide hints to the JIT to compile the target method.
    for (int i = 0; i < 2; i++) {
        VolatileCallSite volatileCS = new VolatileCallSite(K_false);
        WorkerThread2_CS workerThread = new WorkerThread2_CS(volatileCS);
        workerThread.start();
        while (!workerThread.isStarted) {
            Thread.sleep(10);
        }
        volatileCS.setTarget(K_true);
        // The test fails if worker thread does not see the target update within 4 seconds
        synchronized (workerThread) {
            if (!workerThread.volatileUpdateSeen()) {
                workerThread.wait(30000);
            }
        }
        if (!workerThread.volatileUpdateSeen()) {
            Assert.fail("ERROR: testVolatility_WithoutJoin: volatile update made by main thread not visible in worker thread");
        }
    }
}
Also used : VolatileCallSite(java.lang.invoke.VolatileCallSite) SwitchPoint(java.lang.invoke.SwitchPoint) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 3 with VolatileCallSite

use of java.lang.invoke.VolatileCallSite in project openj9 by eclipse.

the class VolatileCallSiteTest method testBasic_VolatileCallSite.

/**
 * Basic sanity test for VolatileCallSite
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testBasic_VolatileCallSite() throws Throwable {
    MethodType mt = MethodType.methodType(double.class, String.class);
    VolatileCallSite vcs = new VolatileCallSite(mt);
    MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble", vcs.type());
    vcs.setTarget(mh);
    double d = (double) vcs.dynamicInvoker().invokeExact("1.1");
    AssertJUnit.assertEquals(1.1, d);
}
Also used : VolatileCallSite(java.lang.invoke.VolatileCallSite) MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 4 with VolatileCallSite

use of java.lang.invoke.VolatileCallSite in project openj9 by eclipse.

the class VolatileCallSiteTest method testIllegalState_getTarget_VolatileCallSite.

/**
 * Test for validating default MethodHandle behavior of VolatileCallSite
 * using VolatileCallSite.getTarget().invoke()
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testIllegalState_getTarget_VolatileCallSite() throws Throwable {
    MethodType mt = MethodType.methodType(void.class);
    VolatileCallSite vcs = new VolatileCallSite(mt);
    boolean iseHit = false;
    try {
        vcs.getTarget().invoke();
    } catch (IllegalStateException e) {
        iseHit = true;
    }
    AssertJUnit.assertTrue(iseHit);
}
Also used : VolatileCallSite(java.lang.invoke.VolatileCallSite) MethodType(java.lang.invoke.MethodType) Test(org.testng.annotations.Test)

Example 5 with VolatileCallSite

use of java.lang.invoke.VolatileCallSite in project openj9 by eclipse.

the class VolatileCallSiteTest method testBasicNegative_VolatileCallSite.

/**
 * Basic negative test for VolatileCallSite
 * @throws Throwable
 */
@Test(groups = { "level.extended" })
public void testBasicNegative_VolatileCallSite() throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "sin", MethodType.methodType(double.class, double.class));
    VolatileCallSite vcs = new VolatileCallSite(mh);
    boolean wmtThrown = false;
    try {
        String s = (String) vcs.dynamicInvoker().invokeExact(0.0);
    } catch (WrongMethodTypeException e) {
        wmtThrown = true;
    }
    AssertJUnit.assertTrue(wmtThrown);
}
Also used : VolatileCallSite(java.lang.invoke.VolatileCallSite) WrongMethodTypeException(java.lang.invoke.WrongMethodTypeException) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

VolatileCallSite (java.lang.invoke.VolatileCallSite)7 Test (org.testng.annotations.Test)7 MethodHandle (java.lang.invoke.MethodHandle)5 MethodType (java.lang.invoke.MethodType)4 SwitchPoint (java.lang.invoke.SwitchPoint)2 WrongMethodTypeException (java.lang.invoke.WrongMethodTypeException)1