Search in sources :

Example 1 with IPermitAcquiredCallback

use of org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback in project scout.rt by eclipse.

the class JobManager method competeForPermitAndExecute.

/**
 * Competes for an execution permit (if semaphore aware) and executes the runnable via {@link ExecutorService}.
 */
protected void competeForPermitAndExecute(final JobFutureTask<?> futureTask, final IRejectableRunnable futureRunner) {
    final ExecutionSemaphore executionSemaphore = futureTask.getExecutionSemaphore();
    if (executionSemaphore == null) {
        m_executor.execute(futureRunner);
    } else {
        futureTask.changeState(JobState.WAITING_FOR_PERMIT);
        executionSemaphore.compete(futureTask, QueuePosition.TAIL, new IPermitAcquiredCallback() {

            @Override
            public void onPermitAcquired() {
                m_executor.execute(futureRunner);
            }
        });
    }
}
Also used : IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback)

Example 2 with IPermitAcquiredCallback

use of org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback in project scout.rt by eclipse.

the class MutualExclusionTest method testAcquisition5.

/**
 * Task1 acquires the mutex. Then, task2 tries to acquire the mutex and blocks until acquired. Because task2 cannot
 * acquire the mutex (hold by task1), it is cancelled hard (interrupted=true). That causes task2 no longer to wait for
 * the mutex, meaning that when task1 releases the mutex, task2 does not become the mutex owner.
 */
@Test(timeout = 5_000)
public void testAcquisition5() throws java.lang.InterruptedException {
    final ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
    assertEquals(0, mutex.getCompetitorCount());
    // Make task1 to acquire the mutex
    IPermitAcquiredCallback callbackTask1 = mock(IPermitAcquiredCallback.class);
    assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, callbackTask1));
    verify(callbackTask1, times(1)).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(1, mutex.getCompetitorCount());
    // Task2 tries to acquire the mutex, and blocks until acquired
    final BlockingCountDownLatch interruptedLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                mutex.acquire(m_task2, QueuePosition.TAIL);
            } catch (ThreadInterruptedError e) {
                interruptedLatch.countDown();
            }
        }
    }, Jobs.newInput().withExceptionHandling(null, false));
    JobTestUtil.waitForPermitCompetitors(mutex, 2);
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(2, mutex.getCompetitorCount());
    future.cancel(true);
    assertTrue(interruptedLatch.await());
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(2, mutex.getCompetitorCount());
    mutex.release(m_task1);
    assertFalse(mutex.isPermitOwner(m_task2));
    assertEquals(0, mutex.getCompetitorCount());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 3 with IPermitAcquiredCallback

use of org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback in project scout.rt by eclipse.

the class MutualExclusionTest method testAcquisition3.

/**
 * Task1 acquires the mutex. Then, task2 tries to acquire the mutex (without blocking). Afterwards, task3 tries to
 * acquire the mutex (without blocking), and puts itself in front of the queue. Once task1 releases the mutex, it is
 * passed to the task3, and then to task2.
 */
@Test(timeout = 1000)
public void testAcquisition3() {
    ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
    assertEquals(0, mutex.getCompetitorCount());
    // Make task1 to acquire the mutex
    IPermitAcquiredCallback callbackTask1 = mock(IPermitAcquiredCallback.class);
    assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, callbackTask1));
    verify(callbackTask1, times(1)).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(1, mutex.getCompetitorCount());
    // Task2 tries to acquire the mutex (without blocking)
    IPermitAcquiredCallback callbackTask2 = mock(IPermitAcquiredCallback.class);
    assertFalse(mutex.compete(m_task2, QueuePosition.TAIL, callbackTask2));
    verify(callbackTask2, never()).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(2, mutex.getCompetitorCount());
    // Task3 tries to acquire the mutex (without blocking)
    IPermitAcquiredCallback callbackTask3 = mock(IPermitAcquiredCallback.class);
    assertFalse(mutex.compete(m_task3, QueuePosition.HEAD, callbackTask3));
    verify(callbackTask3, never()).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(3, mutex.getCompetitorCount());
    mutex.release(m_task1);
    verify(callbackTask3, times(1)).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task3));
    assertEquals(2, mutex.getCompetitorCount());
    mutex.release(m_task3);
    verify(callbackTask2, times(1)).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task2));
    assertEquals(1, mutex.getCompetitorCount());
    mutex.release(m_task2);
    assertFalse(mutex.isPermitOwner(m_task2));
    assertEquals(0, mutex.getCompetitorCount());
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback) Test(org.junit.Test)

Example 4 with IPermitAcquiredCallback

use of org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback in project scout.rt by eclipse.

the class MutualExclusionTest method testAcquisition4.

/**
 * Task1 acquires the mutex. Then, task2 tries to acquire the mutex and blocks until acquired. Once task1 releases the
 * mutex, task2 does become the mutex owner.
 */
@Test(timeout = 5000)
public void testAcquisition4() {
    final ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
    assertEquals(0, mutex.getCompetitorCount());
    // Make task1 to acquire the mutex
    IPermitAcquiredCallback callbackTask1 = mock(IPermitAcquiredCallback.class);
    assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, callbackTask1));
    verify(callbackTask1, times(1)).onPermitAcquired();
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(1, mutex.getCompetitorCount());
    // Task2 tries to acquire the mutex, and blocks until acquired
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            mutex.acquire(m_task2, QueuePosition.TAIL);
        }
    }, Jobs.newInput());
    try {
        future.awaitDone(1, TimeUnit.SECONDS);
        fail("timeout expected");
    } catch (TimedOutError e) {
    // NOOP
    }
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(2, mutex.getCompetitorCount());
    mutex.release(m_task1);
    assertTrue(mutex.isPermitOwner(m_task2));
    assertEquals(1, mutex.getCompetitorCount());
    mutex.release(m_task2);
    assertFalse(mutex.isPermitOwner(m_task2));
    assertEquals(0, mutex.getCompetitorCount());
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 5 with IPermitAcquiredCallback

use of org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback in project scout.rt by eclipse.

the class MutualExclusionTest method testAcquisition2.

/**
 * Tests blocking mutex acquisition.
 */
@Test(timeout = 1000)
public void testAcquisition2() {
    ExecutionSemaphore mutex = (ExecutionSemaphore) m_task1.getExecutionSemaphore();
    assertEquals(0, mutex.getCompetitorCount());
    // Make task1 to acquire the mutex.
    final AtomicReference<Thread> thread = new AtomicReference<>();
    assertTrue(mutex.compete(m_task1, QueuePosition.HEAD, new IPermitAcquiredCallback() {

        @Override
        public void onPermitAcquired() {
            thread.set(Thread.currentThread());
        }
    }));
    assertSame(Thread.currentThread(), thread.get());
    assertTrue(mutex.isPermitOwner(m_task1));
    assertEquals(1, mutex.getCompetitorCount());
    // Wrong mutex release.
    try {
        mutex.release(m_task2);
        fail();
    } catch (AssertionException e) {
        assertTrue(mutex.isPermitOwner(m_task1));
        assertEquals(1, mutex.getCompetitorCount());
    }
    // Task1 releases the mutex.
    mutex.release(m_task1);
    assertFalse(mutex.isPermitOwner(m_task1));
    assertEquals(0, mutex.getCompetitorCount());
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IPermitAcquiredCallback(org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

IPermitAcquiredCallback (org.eclipse.scout.rt.platform.job.internal.ExecutionSemaphore.IPermitAcquiredCallback)5 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)4 Test (org.junit.Test)4 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)1 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)1