/** * 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(); } } }
protected V8(final String globalAlias) { super(null); released = false; v8RuntimePtr = _createIsolate(globalAlias); locker = new V8Locker(this); checkThread(); objectHandle = _getGlobalObject(v8RuntimePtr); }
/** * Removes a handler from the list of breakpoint handlers. * If the handler is not present in the list, the list is unchanged. * * @param handler The handler to remove. */ public void removeBreakHandler(final BreakHandler handler) { runtime.getLocker().checkThread(); breakHandlers.remove(handler); }
@Test public void testAcquireLocker() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); v8Locker.acquire(); v8Locker.checkThread(); }
@Test public void testTryAcquireLocker_True() { V8Locker v8Locker = new V8Locker(v8); v8Locker.release(); boolean result = v8Locker.tryAcquire(); assertTrue(result); v8Locker.checkThread(); }
@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 void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null; } } } }
V8Locker(final V8 runtime) { this.runtime = runtime; acquire(); }
@Test public void testAcquireOnCreation() { V8Locker v8Locker = new V8Locker(v8); v8Locker.checkThread(); }
/** * 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(); }
} finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null;
V8Locker(final V8 runtime) { this.runtime = runtime; acquire(); }