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();
}
};
}
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;
}
}
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;
}
Aggregations