/** * Create a new ConcurrentV8. A ConcurrentV8 allows multiple * threads to work with the same V8 engine by releasing * the locks between calls. */ public ConcurrentV8() { v8 = V8.createV8Runtime(); v8.getLocker().release(); }
/** * Runs an {@link V8Runnable} on the V8 thread. * * <b>Note: </b> This method executes synchronously, not asynchronously; * it will not return until the passed {@link V8Runnable} is done * executing. The method is also synchronized, so it will block until it * gets a chance to run. * * @param runnable {@link V8Runnable} to run. */ public synchronized void run(final V8Runnable runnable) { try { v8.getLocker().acquire(); runnable.run(v8); } finally { if ((v8 != null) && (v8.getLocker() != null) && v8.getLocker().hasLock()) { v8.getLocker().release(); } } }
/** * Create a new ConcurrentV8. A ConcurrentV8 allows multiple * threads to work with the same V8 engine by releasing * the locks between calls. */ public ConcurrentV8() { v8 = V8.createV8Runtime(); v8.getLocker().release(); }
@Override public Object invoke(final V8Object receiver, final V8Array parameters) { v8TempRuntime = V8.createV8Runtime(); v8TempRuntime.getLocker().release(); throw new RuntimeException(); } }, "foo");
@Test public void testCannotUseReleasedLocker() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); try { v8Locker.checkThread(); } catch (Error e) { assertTrue(e.getMessage().startsWith("Invalid V8 thread access")); return; } fail("Expected exception"); }
@Override public Object invoke(final V8Object receiver, final V8Array parameters) { v8.getLocker().release(); v8.getLocker().acquire(); return null; } }, "foo");
@Override public void run() { v8.getLocker().acquire(); v8.getLocker().release(); } });
@Test public void testBinarySemaphore() throws InterruptedException { v8.getLocker().acquire(); // Lock has been acquired twice v8.getLocker().release(); // Lock should be released, second acquire shouldn't count Thread t = new Thread(new Runnable() { @Override public void run() { v8.getLocker().acquire(); v8.getLocker().release(); } }); t.start(); t.join(); v8.getLocker().acquire(); }
/** * Runs an {@link V8Runnable} on the V8 thread. * * <b>Note: </b> This method executes synchronously, not asynchronously; * it will not return until the passed {@link V8Runnable} is done * executing. The method is also synchronized, so it will block until it * gets a chance to run. * * @param runnable {@link V8Runnable} to run. */ public synchronized void run(final V8Runnable runnable) { try { v8.getLocker().acquire(); runnable.run(v8); } finally { if ((v8 != null) && (v8.getLocker() != null) && v8.getLocker().hasLock()) { v8.getLocker().release(); } } }
@Override public void run() { v8.getLocker().acquire(); v8.add("foo", "baz"); v8.getLocker().release(); } });
@Test public void testAcquireLocker() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); v8Locker.acquire(); v8Locker.checkThread(); }
@Test public void testGetThreadNullAfterRelease() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); assertNull(v8Locker.getThread()); }
@Test public void testReleaseAfterV8Released() { V8Locker v8Locker = new V8Locker(v8); v8.close(); v8Locker.release(); v8 = V8.createV8Runtime(); // Create a new runtime so the teardown doesn't fail }
@Test public void testDoesNotHasLock() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); assertFalse(v8Locker.hasLock()); }
@Test public void testMultiThreadAccess() throws InterruptedException { v8.add("foo", "bar"); v8.getLocker().release(); Thread t = new Thread(new Runnable() { @Override public void run() { v8.getLocker().acquire(); v8.add("foo", "baz"); v8.getLocker().release(); } }); t.start(); t.join(); v8.getLocker().acquire(); assertEquals("baz", v8.getString("foo")); }
@Test public void testTryAcquireLocker_True() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); boolean result = v8Locker.tryAcquire(); assertTrue(result); v8Locker.checkThread(); }
@Override public V8 create() throws Exception { V8 v8 = V8.createV8Runtime(); // Read passed javascripts paths to each V8 runtime instantiated for (String script : scripts) { v8.executeVoidScript(script); } // Release the runtime locker from the current thread v8.getLocker().release(); return v8; }