Search in sources :

Example 1 with State

use of com.oracle.truffle.espresso.threads.State in project graal by oracle.

the class VM method JVM_MonitorWait.

@VmImpl(isJni = true)
@TruffleBoundary
@SuppressFBWarnings(value = { "IMSE" }, justification = "Not dubious, .wait is just forwarded from the guest.")
@SuppressWarnings("try")
public void JVM_MonitorWait(@JavaType(Object.class) StaticObject self, long timeout, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    EspressoContext context = getContext();
    StaticObject currentThread = context.getCurrentThread();
    State state = timeout > 0 ? State.TIMED_WAITING : State.WAITING;
    try (Transition transition = Transition.transition(context, state)) {
        if (context.EnableManagement) {
            // Locks bookkeeping.
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, self);
            Target_java_lang_Thread.incrementThreadCounter(currentThread, meta.HIDDEN_THREAD_WAITED_COUNT);
        }
        final boolean report = context.shouldReportVMEvents();
        if (report) {
            context.reportMonitorWait(self, timeout);
        }
        boolean timedOut = !InterpreterToVM.monitorWait(self.getLock(getContext()), timeout);
        if (report) {
            context.reportMonitorWaited(self, timedOut);
        }
    } catch (GuestInterruptedException e) {
        profiler.profile(0);
        if (getThreadAccess().isInterrupted(currentThread, true)) {
            throw meta.throwExceptionWithMessage(meta.java_lang_InterruptedException, e.getMessage());
        }
        getThreadAccess().fullSafePoint(currentThread);
    } catch (IllegalMonitorStateException e) {
        profiler.profile(1);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalMonitorStateException, e.getMessage());
    } catch (IllegalArgumentException e) {
        profiler.profile(2);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, e.getMessage());
    } finally {
        if (context.EnableManagement) {
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, null);
        }
    }
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) GuestInterruptedException(com.oracle.truffle.espresso.blocking.GuestInterruptedException) State(com.oracle.truffle.espresso.threads.State) Transition(com.oracle.truffle.espresso.threads.Transition) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) SuppressFBWarnings(com.oracle.truffle.espresso.impl.SuppressFBWarnings)

Example 2 with State

use of com.oracle.truffle.espresso.threads.State in project graal by oracle.

the class Target_sun_misc_Unsafe method park.

/**
 * Block current thread, returning when a balancing <tt>unpark</tt> occurs, or a balancing
 * <tt>unpark</tt> has already occurred, or the thread is interrupted, or, if not absolute and
 * time is not zero, the given time nanoseconds have elapsed, or if absolute, the given deadline
 * in milliseconds since Epoch has passed, or spuriously (i.e., returning for no "reason").
 * Note: This operation is in the Unsafe class only because <tt>unpark</tt> is, so it would be
 * strange to place it elsewhere.
 */
@TruffleBoundary
@Substitution(hasReceiver = true)
@SuppressWarnings("try")
public static void park(@JavaType(Unsafe.class) StaticObject self, boolean isAbsolute, long time, @Inject Meta meta) {
    if (time < 0 || (isAbsolute && time == 0)) {
        // don't wait at all
        return;
    }
    EspressoContext context = meta.getContext();
    StaticObject thread = context.getCurrentThread();
    if (meta.getThreadAccess().isInterrupted(thread, false)) {
        return;
    }
    Unsafe unsafe = UnsafeAccess.getIfAllowed(meta);
    Thread hostThread = Thread.currentThread();
    Object blocker = LockSupport.getBlocker(hostThread);
    State state = time > 0 ? State.TIMED_WAITING : State.WAITING;
    try (Transition transition = Transition.transition(context, state)) {
        Field parkBlocker = meta.java_lang_Thread.lookupDeclaredField(Symbol.Name.parkBlocker, Type.java_lang_Object);
        StaticObject guestBlocker = parkBlocker.getObject(thread);
        // LockSupport.park(/* guest blocker */);
        if (!StaticObject.isNull(guestBlocker)) {
            unsafe.putObject(hostThread, PARK_BLOCKER_OFFSET, guestBlocker);
        }
        parkBoundary(self, isAbsolute, time, meta);
    }
    unsafe.putObject(hostThread, PARK_BLOCKER_OFFSET, blocker);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) State(com.oracle.truffle.espresso.threads.State) Transition(com.oracle.truffle.espresso.threads.Transition) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Unsafe(sun.misc.Unsafe) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 EspressoContext (com.oracle.truffle.espresso.runtime.EspressoContext)2 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)2 State (com.oracle.truffle.espresso.threads.State)2 Transition (com.oracle.truffle.espresso.threads.Transition)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 GuestInterruptedException (com.oracle.truffle.espresso.blocking.GuestInterruptedException)1 Field (com.oracle.truffle.espresso.impl.Field)1 SuppressFBWarnings (com.oracle.truffle.espresso.impl.SuppressFBWarnings)1 Unsafe (sun.misc.Unsafe)1