Search in sources :

Example 1 with InnocuousThread

use of jdk.internal.misc.InnocuousThread in project Bytecoder by mirkosertic.

the class Invoker method invokeUnchecked.

/**
 * Invoke handler without checking the thread identity or number of handlers
 * on the thread stack.
 */
static <V, A> void invokeUnchecked(CompletionHandler<V, ? super A> handler, A attachment, V value, Throwable exc) {
    if (exc == null) {
        handler.completed(value, attachment);
    } else {
        handler.failed(exc, attachment);
    }
    // clear interrupt
    Thread.interrupted();
    // clear thread locals when in default thread pool
    if (System.getSecurityManager() != null) {
        Thread me = Thread.currentThread();
        if (me instanceof InnocuousThread) {
            GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
            ((InnocuousThread) me).eraseThreadLocals();
            if (thisGroupAndInvokeCount != null) {
                myGroupAndInvokeCount.set(thisGroupAndInvokeCount);
            }
        }
    }
}
Also used : InnocuousThread(jdk.internal.misc.InnocuousThread) InnocuousThread(jdk.internal.misc.InnocuousThread)

Example 2 with InnocuousThread

use of jdk.internal.misc.InnocuousThread in project Bytecoder by mirkosertic.

the class CleanerImpl method run.

/**
 * Process queued Cleanables as long as the cleanable lists are not empty.
 * A Cleanable is in one of the lists for each Object and for the Cleaner
 * itself.
 * Terminates when the Cleaner is no longer reachable and
 * has been cleaned and there are no more Cleanable instances
 * for which the object is reachable.
 * <p>
 * If the thread is a ManagedLocalsThread, the threadlocals
 * are erased before each cleanup
 */
@Override
public void run() {
    Thread t = Thread.currentThread();
    InnocuousThread mlThread = (t instanceof InnocuousThread) ? (InnocuousThread) t : null;
    while (!phantomCleanableList.isListEmpty() || !weakCleanableList.isListEmpty() || !softCleanableList.isListEmpty()) {
        if (mlThread != null) {
            // Clear the thread locals
            mlThread.eraseThreadLocals();
        }
        try {
            // Wait for a Ref, with a timeout to avoid getting hung
            // due to a race with clear/clean
            Cleanable ref = (Cleanable) queue.remove(60 * 1000L);
            if (ref != null) {
                ref.clean();
            }
        } catch (Throwable e) {
        // ignore exceptions from the cleanup action
        // (including interruption of cleanup thread)
        }
    }
}
Also used : InnocuousThread(jdk.internal.misc.InnocuousThread) Cleanable(java.lang.ref.Cleaner.Cleanable) InnocuousThread(jdk.internal.misc.InnocuousThread)

Aggregations

InnocuousThread (jdk.internal.misc.InnocuousThread)2 Cleanable (java.lang.ref.Cleaner.Cleanable)1