/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, Azul Systems, Inc. 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. *
*/
// Many of the following fields are effectively final - immutable // Note that nascent threads can't use the Native Monitor-Mutex // construct until the _MutexEvent is initialized ... // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents // we might instead use a stack of ParkEvents that we could provision on-demand. // The stack would act as a cache to avoid calls to ParkEvent::Allocate() // and ::Release()
_ParkEvent = ParkEvent::Allocate(this);
#ifdef CHECK_UNHANDLED_OOPS if (CheckUnhandledOops) {
_unhandled_oops = new UnhandledOops(this);
} #endif// CHECK_UNHANDLED_OOPS
// Notify the barrier set that a thread is being created. The initial // thread is created before the barrier set is available. The call to // BarrierSet::on_thread_create() for this thread is therefore deferred // to BarrierSet::set_barrier_set().
BarrierSet* const barrier_set = BarrierSet::barrier_set(); if (barrier_set != NULL) {
barrier_set->on_thread_create(this);
} else { // Only the main thread should be created before the barrier set // and that happens just before Thread::current is set. No other thread // can attach as the VM is not created yet, so they can't execute this code. // If the main thread creates other threads before the barrier set that is an error.
assert(Thread::current_or_null() == NULL, "creating thread before barrier set");
}
void Thread::record_stack_base_and_size() { // Note: at this point, Thread object is not yet initialized. Do not rely on // any members being initialized. Do not rely on Thread::current() being set. // If possible, refrain from doing anything which may crash or assert since // quite probably those crash dumps will be useless.
set_stack_base(os::current_stack_base());
set_stack_size(os::current_stack_size());
// Set stack limits after thread is initialized. if (is_Java_thread()) {
JavaThread::cast(this)->stack_overflow_state()->initialize(stack_base(), stack_end());
}
}
// Note: at this point the thread object may already have deleted itself, // so from here on do not dereference *this*. Not all thread types currently // delete themselves when they terminate. But no thread should ever be deleted // asynchronously with respect to its termination - that is what _run_state can // be used to check.
assert(Thread::current_or_null() == NULL, "current thread still present");
}
Thread::~Thread() {
// Attached threads will remain in PRE_CALL_RUN, as will threads that don't actually // get started due to errors etc. Any active thread should at least reach post_run // before it is deleted (usually in post_run()).
assert(_run_state == PRE_CALL_RUN ||
_run_state == POST_RUN, "Active Thread deleted before post_run(): " "_run_state=%d", (int)_run_state);
// Notify the barrier set that a thread is being destroyed. Note that a barrier // set might not be available if we encountered errors during bootstrapping.
BarrierSet* const barrier_set = BarrierSet::barrier_set(); if (barrier_set != NULL) {
barrier_set->on_thread_destroy(this);
}
// deallocate data structures delete resource_area(); // since the handle marks are using the handle area, we have to deallocated the root // handle mark before deallocating the thread's handle area,
assert(last_handle_mark() != NULL, "check we have an element"); delete last_handle_mark();
assert(last_handle_mark() == NULL, "check we have reached the end");
ParkEvent::Release(_ParkEvent); // Set to NULL as a termination indicator for has_terminated().
Atomic::store(&_ParkEvent, (ParkEvent*)NULL);
delete handle_area(); delete metadata_handles();
// osthread() can be NULL, if creation of thread failed. if (osthread() != NULL) os::free_thread(osthread());
// Clear Thread::current if thread is deleting itself and it has not // already been done. This must be done before the memory is deallocated. // Needed to ensure JNI correctly detects non-attached threads. if (this == Thread::current_or_null()) {
Thread::clear_thread_current();
}
#ifdef ASSERT // A JavaThread is considered dangling if it not handshake-safe with respect to // the current thread, it is not on a ThreadsList, or not at safepoint. void Thread::check_for_dangling_thread_pointer(Thread *thread) {
assert(!thread->is_Java_thread() ||
JavaThread::cast(thread)->is_handshake_safe_for(Thread::current()) ||
!JavaThread::cast(thread)->on_thread_list() ||
SafepointSynchronize::is_at_safepoint() ||
ThreadsSMRSupport::is_a_protected_JavaThread_with_lock(JavaThread::cast(thread)), "possibility of dangling Thread pointer");
} #endif
// Is the target JavaThread protected by the calling Thread or by some other // mechanism? // bool Thread::is_JavaThread_protected(const JavaThread* target) {
Thread* current_thread = Thread::current();
// Do the simplest check first: if (SafepointSynchronize::is_at_safepoint()) { // The target is protected since JavaThreads cannot exit // while we're at a safepoint. returntrue;
}
// If the target hasn't been started yet then it is trivially // "protected". We assume the caller is the thread that will do // the starting. if (target->osthread() == NULL || target->osthread()->get_state() <= INITIALIZED) { returntrue;
}
// Now make the simple checks based on who the caller is: if (current_thread == target || Threads_lock->owner() == current_thread) { // Target JavaThread is self or calling thread owns the Threads_lock. // Second check is the same as Threads_lock->owner_is_self(), // but we already have the current thread so check directly. returntrue;
}
// Check the ThreadsLists associated with the calling thread (if any) // to see if one of them protects the target JavaThread: if (is_JavaThread_protected_by_TLH(target)) { returntrue;
}
// Use this debug code with -XX:+UseNewCode to diagnose locations that // are missing a ThreadsListHandle or other protection mechanism: // guarantee(!UseNewCode, "current_thread=" INTPTR_FORMAT " is not protecting target=" // INTPTR_FORMAT, p2i(current_thread), p2i(target));
// Note: Since 'target' isn't protected by a TLH, the call to // target->is_handshake_safe_for() may crash, but we have debug bits so // we'll be able to figure out what protection mechanism is missing.
assert(target->is_handshake_safe_for(current_thread), "JavaThread=" INTPTR_FORMAT " is not protected and not handshake safe.", p2i(target));
// The target JavaThread is not protected so it is not safe to query: returnfalse;
}
// Is the target JavaThread protected by a ThreadsListHandle (TLH) associated // with the calling Thread? // bool Thread::is_JavaThread_protected_by_TLH(const JavaThread* target) {
Thread* current_thread = Thread::current();
// Check the ThreadsLists associated with the calling thread (if any) // to see if one of them protects the target JavaThread: for (SafeThreadsListPtr* stlp = current_thread->_threads_list_ptr;
stlp != NULL; stlp = stlp->previous()) { if (stlp->list()->includes(target)) { // The target JavaThread is protected by this ThreadsList: returntrue;
}
}
// The target JavaThread is not protected by a TLH so it is not safe to query: returnfalse;
}
void Thread::set_priority(Thread* thread, ThreadPriority priority) {
debug_only(check_for_dangling_thread_pointer(thread);) // Can return an error!
(void)os::set_priority(thread, priority);
}
void Thread::start(Thread* thread) { // Start is different from resume in that its safety is guaranteed by context or // being called from a Java method synchronized on the Thread object. if (thread->is_Java_thread()) { // Initialize the thread state to RUNNABLE before starting this thread. // Can not set it after the thread started because we do not know the // exact thread state at that time. It could be in MONITOR_WAIT or // in SLEEPING or some other state.
java_lang_Thread::set_thread_status(JavaThread::cast(thread)->threadObj(),
JavaThreadStatus::RUNNABLE);
}
os::start_thread(thread);
}
// GC Support bool Thread::claim_par_threads_do(uintx claim_token) {
uintx token = _threads_do_token; if (token != claim_token) {
uintx res = Atomic::cmpxchg(&_threads_do_token, token, claim_token); if (res == token) { returntrue;
}
guarantee(res == claim_token, "invariant");
} returnfalse;
}
void Thread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) { // Do oop for ThreadShadow
f->do_oop((oop*)&_pending_exception);
handle_area()->oops_do(f);
}
// If the caller is a NamedThread, then remember, in the current scope, // the given JavaThread in its _processed_thread field. class RememberProcessedThread: public StackObj {
NamedThread* _cur_thr; public:
RememberProcessedThread(Thread* thread) {
Thread* self = Thread::current(); if (self->is_Named_thread()) {
_cur_thr = (NamedThread *)self;
assert(_cur_thr->processed_thread() == NULL, "nesting not supported");
_cur_thr->set_processed_thread(thread);
} else {
_cur_thr = NULL;
}
}
~RememberProcessedThread() { if (_cur_thr) {
assert(_cur_thr->processed_thread() != NULL, "nesting not supported");
_cur_thr->set_processed_thread(NULL);
}
}
};
void Thread::oops_do(OopClosure* f, CodeBlobClosure* cf) { // Record JavaThread to GC thread
RememberProcessedThread rpt(this);
oops_do_no_frames(f, cf);
oops_do_frames(f, cf);
}
void Thread::metadata_handles_do(void f(Metadata*)) { // Only walk the Handles in Thread. if (metadata_handles() != NULL) { for (int i = 0; i< metadata_handles()->length(); i++) {
f(metadata_handles()->at(i));
}
}
}
void Thread::print_on(outputStream* st, bool print_extended_info) const { // get_priority assumes osthread initialized if (osthread() != NULL) { int os_prio; if (os::get_native_priority(this, &os_prio) == OS_OK) {
st->print("os_prio=%d ", os_prio);
}
// Thread::print_on_error() is called by fatal error handler. Don't use // any lock or allocate memory. void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
#ifdef ASSERT void Thread::print_owned_locks_on(outputStream* st) const {
Mutex* cur = _owned_locks; if (cur == NULL) {
st->print(" (no locks) ");
} else {
st->print_cr(" Locks owned:"); while (cur) {
cur->print_on(st);
cur = cur->next();
}
}
} #endif// ASSERT
// We had to move these methods here, because vm threads get into ObjectSynchronizer::enter // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being // used for compilation in the future. If that change is made, the need for these methods // should be revisited, and they should be removed if possible.
bool Thread::set_as_starting_thread() {
assert(_starting_thread == NULL, "already initialized: " "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread)); // NOTE: this must be called inside the main thread.
DEBUG_ONLY(_starting_thread = this;) return os::create_main_thread(JavaThread::cast(this));
}
// Ad-hoc mutual exclusion primitives: SpinLock // // We employ SpinLocks _only for low-contention, fixed-length // short-duration critical sections where we're concerned // about native mutex_t or HotSpot Mutex:: latency. // // TODO-FIXME: ListLock should be of type SpinLock. // We should make this a 1st-class type, integrated into the lock // hierarchy as leaf-locks. Critically, the SpinLock structure // should have sufficient padding to avoid false-sharing and excessive // cache-coherency traffic.
// Slow-path : We've encountered contention -- Spin/Yield/Block strategy. int ctr = 0; int Yields = 0; for (;;) { while (*adr != 0) {
++ctr; if ((ctr & 0xFFF) == 0 || !os::is_MP()) { if (Yields > 5) {
os::naked_short_sleep(1);
} else {
os::naked_yield();
++Yields;
}
} else {
SpinPause();
}
} if (Atomic::cmpxchg(adr, 0, 1) == 0) return;
}
}
void Thread::SpinRelease(volatileint * adr) {
assert(*adr != 0, "invariant");
OrderAccess::fence(); // guarantee at least release consistency. // Roach-motel semantics. // It's safe if subsequent LDs and STs float "up" into the critical section, // but prior LDs and STs within the critical section can't be allowed // to reorder or float past the ST that releases the lock. // Loads and stores in the critical section - which appear in program // order before the store that releases the lock - must also appear // before the store that releases the lock in memory visibility order. // Conceptually we need a #loadstore|#storestore "release" MEMBAR before // the ST of 0 into the lock-word which releases the lock, so fence // more than covers this on all platforms.
*adr = 0;
}
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.