Search in sources :

Example 6 with IRegistrationHandle

use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.

the class DevelopmentThreadNameDecorator method decorate.

@Override
public IUndecorator decorate() throws Exception {
    final ThreadInfo threadInfo = ThreadInfo.CURRENT.get();
    final IFuture<?> future = IFuture.CURRENT.get();
    // Update the thread name.
    threadInfo.updateThreadName(future.getJobInput().getThreadName(), buildExecutionInfo(future));
    // Decorate the thread name upon job state change.
    final IRegistrationHandle listenerRegistration = future.addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.RUNNING, JobState.WAITING_FOR_PERMIT, JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter(), new IJobListener() {

        @Override
        public void changed(final JobEvent event) {
            threadInfo.updateThreadName(future.getJobInput().getThreadName(), buildExecutionInfo(future));
        }
    });
    return new IUndecorator() {

        @Override
        public void undecorate() {
            // Restore to the original thread name.
            listenerRegistration.dispose();
            threadInfo.reset();
        }
    };
}
Also used : ThreadInfo(org.eclipse.scout.rt.platform.job.internal.NamedThreadFactory.ThreadInfo) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener)

Example 7 with IRegistrationHandle

use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.

the class BlockingCondition method blockJobThread.

/**
 * Blocks a thread associated with a job.
 */
protected void blockJobThread(final JobFutureTask<?> futureTask, final long timeout, final TimeUnit unit, final boolean awaitInterruptibly, final String... executionHints) {
    if (!m_blocking) {
        return;
    }
    IRegistrationHandle waitForHints = IRegistrationHandle.NULL_HANDLE;
    RuntimeException exceptionWhileWaiting = null;
    Error errorWhileWaiting = null;
    m_lock.lock();
    try {
        if (!m_blocking) {
            // double-checked locking
            return;
        }
        // Associate the future with execution hints.
        waitForHints = registerWaitForHints(futureTask, executionHints);
        // Change job state.
        futureTask.changeState(new JobEventData().withState(JobState.WAITING_FOR_BLOCKING_CONDITION).withFuture(futureTask).withBlockingCondition(this));
        // Release the permit if being a semaphore aware task, but only if currently being a permit owner.
        futureTask.releasePermit();
        try {
            awaitUntilSignaledOrTimeout(timeout, unit, awaitInterruptibly);
        } catch (final RuntimeException e) {
            exceptionWhileWaiting = e;
        } catch (final Error e) {
            // NOSONAR
            errorWhileWaiting = e;
        }
    } finally {
        m_lock.unlock();
    }
    // re-acquire the permit (must be outside the lock)
    acquirePermitUninterruptibly(futureTask);
    // prepare to continue execution
    // if released via 'setBlocking(false)', the 'wait-for' hints are already disposed, but not if interrupted or timed out.
    waitForHints.dispose();
    futureTask.changeState(JobState.RUNNING);
    if (errorWhileWaiting != null) {
        // NOSONAR
        throw errorWhileWaiting;
    }
    if (exceptionWhileWaiting != null) {
        throw exceptionWhileWaiting;
    }
}
Also used : ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData)

Example 8 with IRegistrationHandle

use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.

the class BlockingCondition method registerWaitForHints.

/**
 * Registers the given 'wait-for' execution hints with the Future, and returns its registration handle.
 */
protected IRegistrationHandle registerWaitForHints(final IFuture<?> future, final String... executionHints) {
    if (executionHints.length == 0) {
        return IRegistrationHandle.NULL_HANDLE;
    }
    // Register the execution hints, and remember the hints registered. That are hints not already associated with the future.
    final Set<String> associatedExecutionHints = new HashSet<>(executionHints.length);
    for (final String executionHint : executionHints) {
        if (future.addExecutionHint(executionHint)) {
            associatedExecutionHints.add(executionHint);
        }
    }
    // Return the registration handle to unregister the registered execution hints.
    final AtomicBoolean disposed = new AtomicBoolean(false);
    final IRegistrationHandle registrationHandle = new IRegistrationHandle() {

        @Override
        public void dispose() {
            if (!disposed.compareAndSet(false, true)) {
                // already disposed, e.g. due to timeout or interruption
                return;
            }
            for (final String associatedExecutionHint : associatedExecutionHints) {
                future.removeExecutionHint(associatedExecutionHint);
            }
        }
    };
    m_waitForHints.add(registrationHandle);
    return registrationHandle;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) HashSet(java.util.HashSet)

Aggregations

IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)8 HashSet (java.util.HashSet)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)2 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 TypeParameterBeanRegistry (org.eclipse.scout.rt.platform.TypeParameterBeanRegistry)1 RunContext (org.eclipse.scout.rt.platform.context.RunContext)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IFilter (org.eclipse.scout.rt.platform.filter.IFilter)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 JobInput (org.eclipse.scout.rt.platform.job.JobInput)1 ThreadInfo (org.eclipse.scout.rt.platform.job.internal.NamedThreadFactory.ThreadInfo)1 JobEventData (org.eclipse.scout.rt.platform.job.listener.JobEventData)1 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)1