/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "code/codeCache.hpp" #include "code/nmethod.hpp" #include "code/nmethodBarrier.hpp" #include "gc/shared/barrierSet.hpp" #include "logging/log.hpp" #include "memory/resourceArea.hpp" #include "runtime/atomic.hpp" #include "runtime/orderAccess.hpp" #include "runtime/thread.inline.hpp" #include "utilities/debug.hpp" #include "utilities/spinYield.hpp" bool nmethodBarrierState::is_safe() const { return Thread::current()->in_nmethod_entry_barrier(); } bool nmethodBarrierState::enter() { while (true) { CounterUnion old; old._value = Atomic::load(&_state); CounterUnion next; next._value = old._value; int disarmed_value = nmethodBarrier::disarmed_value(); assert(disarmed_value != 0, "sanity"); if (old._counters._epoch != disarmed_value) { next._counters._epoch = disarmed_value; next._counters._lock = 1; } else if (old._counters._lock == 0) { return false; } else { ++next._counters._lock; } assert(next._counters._lock > 0, "check"); assert(next._counters._epoch == nmethodBarrier::disarmed_value(), "sanity"); if (Atomic::cmpxchg(next._value, &_state, old._value) == old._value) { Thread::current()->set_is_in_nmethod_entry_barrier(true); return true; } } } void nmethodBarrierState::leave() { while (true) { CounterUnion old; old._value = Atomic::load(&_state); CounterUnion next; next._value = old._value; --next._counters._lock; assert(old._counters._epoch == nmethodBarrier::disarmed_value(), "sanity"); assert(old._counters._lock > 0, "check"); assert(next._counters._lock >= 0, "check"); if (Atomic::cmpxchg(next._value, &_state, old._value) == old._value) { Thread::current()->set_is_in_nmethod_entry_barrier(false); return; } } } void nmethodBarrierState::wait() { SpinYield yield; CounterUnion old; for (;;) { old._value = Atomic::load(&_state); if (old._counters._lock == 0) { return; } yield.wait(); } } nmethodBarrier::nmethodBarrier(nmethod* nm) : _is_deoptimized(false), _nm(nm), _return_address_ptr(NULL) { } nmethodBarrier::nmethodBarrier(nmethod* nm, address* return_address_ptr) : _is_deoptimized(false), _nm(nm), _return_address_ptr(return_address_ptr) { } int nmethodBarrier::enter(address* return_address_ptr) { BarrierSet* bs = BarrierSet::barrier_set(); address return_address = *return_address_ptr; CodeBlob* cb = CodeCache::find_blob(return_address); assert(cb != NULL, "invariant"); assert(bs->needs_nmethod_entry_barrier(), "how did we get here?"); nmethod* nm = cb->as_nmethod(); nmethodBarrier nmbarrier(nm, return_address_ptr); return bs->on_nmethod_entry_barrier(nm, &nmbarrier) ? 0 : 1; } void nmethodBarrier::disarm() { _nm->disarm_barrier(); // multiple threads can enter the barrier at the same time while armed // one can finish and decide that the barrier can be disarmed, in the meantime the other // have made the same decision continued on and deoptimized on something else. // in that case the assert here might fail. assert(!_is_deoptimized, "can only disarm methods that didn't deoptimize!"); } int nmethodBarrier::disarmed_value() { BarrierSet* bs = BarrierSet::barrier_set(); int value = bs->nmethod_entry_barrier_state(); #ifdef ASSERT Thread* thread = Thread::current(); if (thread->is_Java_thread()) { int* state_addr = reinterpret_cast(reinterpret_cast(thread) + in_bytes(bs->nmethod_entry_barrier_state_thread_offset())); assert(*state_addr == value, "global and local values out of sync"); } #endif return value; }