/* * 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. *
*/
oop JavaThread::threadObj() const { // Ideally we would verify the current thread is oop_safe when this is called, but as we can // be called from a signal handler we would have to use Thread::current_or_null_safe(). That // has overhead and also interacts poorly with GetLastError on Windows due to the use of TLS. // Instead callers must verify oop safe access. return _threadObj.resolve();
}
void JavaThread::set_scopedValueCache(oop p) { if (_scopedValueCache.ptr_raw() != NULL) { // i.e. if the OopHandle has been allocated
_scopedValueCache.replace(p);
} else {
assert(p == NULL, "not yet initialized");
}
}
void JavaThread::clear_scopedValueBindings() {
set_scopedValueCache(NULL);
oop vthread_oop = vthread(); // vthread may be null here if we get a VM error during startup, // before the java.lang.Thread instance has been created. if (vthread_oop != NULL) {
java_lang_Thread::clear_scopedValueBindings(vthread_oop);
}
}
void JavaThread::allocate_threadObj(Handle thread_group, constchar* thread_name, bool daemon, TRAPS) {
assert(thread_group.not_null(), "thread group should be specified");
assert(threadObj() == NULL, "should only create Java thread object once");
// We are called from jni_AttachCurrentThread/jni_AttachCurrentThreadAsDaemon. // We cannot use JavaCalls::construct_new_instance because the java.lang.Thread // constructor calls Thread.current(), which must be set here.
java_lang_Thread::set_thread(thread_oop(), this);
set_threadOopHandles(thread_oop());
JavaValue result(T_VOID); if (thread_name != NULL) {
Handle name = java_lang_String::create_from_str(thread_name, CHECK); // Thread gets assigned specified name and null target
JavaCalls::call_special(&result,
thread_oop,
ik,
vmSymbols::object_initializer_name(),
vmSymbols::threadgroup_string_void_signature(),
thread_group,
name,
THREAD);
} else { // Thread gets assigned name "Thread-nnn" and null target // (java.lang.Thread doesn't have a constructor taking only a ThreadGroup argument)
JavaCalls::call_special(&result,
thread_oop,
ik,
vmSymbols::object_initializer_name(),
vmSymbols::threadgroup_runnable_void_signature(),
thread_group,
Handle(),
THREAD);
}
os::set_priority(this, NormPriority);
if (daemon) {
java_lang_Thread::set_daemon(thread_oop());
}
}
#ifdef ASSERT // Checks safepoint allowed and clears unhandled oops at potential safepoints. void JavaThread::check_possible_safepoint() { if (_no_safepoint_count > 0) {
print_owned_locks();
assert(false, "Possible safepoint reached by thread that does not allow it");
} #ifdef CHECK_UNHANDLED_OOPS // Clear unhandled oops in JavaThreads so we get a crash right away.
clear_unhandled_oops(); #endif// CHECK_UNHANDLED_OOPS
// Macos/aarch64 should be in the right state for safepoint (e.g. // deoptimization needs WXWrite). Crashes caused by the wrong state rarely // happens in practice, making such issues hard to find and reproduce. #ifdefined(__APPLE__) && defined(AARCH64) if (AssertWXAtThreadSync) {
assert_wx_state(WXWrite);
} #endif
}
void JavaThread::check_for_valid_safepoint_state() { // Check NoSafepointVerifier, which is implied by locks taken that can be // shared with the VM thread. This makes sure that no locks with allow_vm_block // are held.
check_possible_safepoint();
if (thread_state() != _thread_in_vm) {
fatal("LEAF method calling lock?");
}
if (GCALotAtAllSafepoints) { // We could enter a safepoint here and thus have a gc
InterfaceSupport::check_gc_alot();
}
} #endif// ASSERT
void JavaThread::interrupt() { // All callers should have 'this' thread protected by a // ThreadsListHandle so that it cannot terminate and deallocate // itself.
debug_only(check_for_dangling_thread_pointer(this);)
// For Windows _interrupt_event
WINDOWS_ONLY(osthread()->set_interrupted(true);)
// For Thread.sleep
_SleepEvent->unpark();
// For JSR166 LockSupport.park
parker()->unpark();
// For ObjectMonitor and JvmtiRawMonitor
_ParkEvent->unpark();
}
if (_threadObj.peek() == NULL) { // If there is no j.l.Thread then it is impossible to have // been interrupted. We can find NULL during VM initialization // or when a JNI thread is still in the process of attaching. // In such cases this must be the current thread.
assert(this == Thread::current(), "invariant"); returnfalse;
}
// NOTE that since there is no "lock" around the interrupt and // is_interrupted operations, there is the possibility that the // interrupted flag will be "false" but that the // low-level events will be in the signaled state. This is // intentional. The effect of this is that Object.wait() and // LockSupport.park() will appear to have a spurious wakeup, which // is allowed and not harmful, and the possibility is so rare that // it is not worth the added complexity to add yet another lock. // For the sleep event an explicit reset is performed on entry // to JavaThread::sleep, so there is no early return. It has also been // recommended not to put the interrupted flag into the "event" // structure because it hides the issue. // Also, because there is no lock, we must only clear the interrupt // state if we are going to report that we were interrupted; otherwise // an interrupt that happens just after we read the field would be lost. if (interrupted && clear_interrupted) {
assert(this == Thread::current(), "only the current thread can clear");
java_lang_Thread::set_interrupted(threadObj(), false);
WINDOWS_ONLY(osthread()->set_interrupted(false);)
}
return interrupted;
}
void JavaThread::block_if_vm_exited() { if (_terminated == _vm_exited) { // _vm_exited is set at safepoint, and Threads_lock is never released // so we will block here forever. // Here we can be doing a jump from a safe state to an unsafe state without // proper transition, but it happens after the final safepoint has begun so // this jump won't cause any safepoint problems.
set_thread_state(_thread_in_vm);
Threads_lock->lock();
ShouldNotReachHere();
}
}
JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) : JavaThread() {
_jni_attach_state = _not_attaching_via_jni;
set_entry_point(entry_point); // Create the native thread itself. // %note runtime_23
os::ThreadType thr_type = os::java_thread;
thr_type = entry_point == &CompilerThread::thread_entry ? os::compiler_thread :
os::java_thread;
os::create_thread(this, thr_type, stack_sz); // The _osthread may be NULL here because we ran out of memory (too many threads active). // We need to throw and OutOfMemoryError - however we cannot do this here because the caller // may hold a lock and all locks must be unlocked before throwing the exception (throwing // the exception consists of creating the exception object & initializing it, initialization // will leave the VM via a JavaCall and then all locks must be unlocked). // // The thread is still suspended when we reach here. Thread must be explicit started // by creator! Furthermore, the thread must also explicitly be added to the Threads list // by calling Threads:add. The reason why this is not done here, is because the thread // object must be fully initialized (take a look at JVM_Start)
}
JavaThread::~JavaThread() {
// Enqueue OopHandles for release by the service thread.
add_oop_handles_for_release();
// Return the sleep event to the free list
ParkEvent::Release(_SleepEvent);
_SleepEvent = NULL;
// Free any remaining previous UnrollBlock
vframeArray* old_array = vframe_array_last();
JvmtiDeferredUpdates* updates = deferred_updates(); if (updates != NULL) { // This can only happen if thread is destroyed before deoptimization occurs.
assert(updates->count() > 0, "Updates holder not deleted"); // free deferred updates. delete updates;
set_deferred_updates(NULL);
}
// All Java related clean up happens in exit
ThreadSafepointState::destroy(this); if (_thread_stat != NULL) delete _thread_stat;
// First JavaThread specific code executed by a new Java thread. void JavaThread::pre_run() { // empty - see comments in run()
}
// The main routine called by a new Java thread. This isn't overridden // by subclasses, instead different subclasses define a different "entry_point" // which defines the actual logic for that kind of thread. void JavaThread::run() { // initialize thread-local alloc buffer related fields
initialize_tlab();
_stack_overflow_state.create_stack_guard_pages();
cache_global_variables();
// Thread is now sufficiently initialized to be handled by the safepoint code as being // in the VM. Change thread state from _thread_new to _thread_in_vm
assert(this->thread_state() == _thread_new, "wrong thread state");
set_thread_state(_thread_in_vm);
// Before a thread is on the threads list it is always safe, so after leaving the // _thread_new we should emit a instruction barrier. The distance to modified code // from here is probably far enough, but this is consistent and safe.
OrderAccess::cross_modify_fence();
// This operation might block. We call that after all safepoint checks for a new thread has // been completed.
set_active_handles(JNIHandleBlock::allocate_block());
if (JvmtiExport::should_post_thread_life()) {
JvmtiExport::post_thread_start(this);
}
// We call another function to do the rest so we are sure that the stack addresses used // from there will be lower than the stack base just computed.
thread_main_inner();
}
// Execute thread entry point unless this thread has a pending exception. // Note: Due to JVMTI StopThread we can have pending exceptions already! if (!this->has_pending_exception()) {
{
ResourceMark rm(this);
this->set_native_thread_name(this->name());
}
HandleMark hm(this);
this->entry_point()(this, this);
}
DTRACE_THREAD_PROBE(stop, this);
// Cleanup is handled in post_run()
}
// Shared teardown for all JavaThreads void JavaThread::post_run() {
this->exit(false);
this->unregister_thread_stack_with_NMT(); // Defer deletion to here to ensure 'this' is still referenceable in call_run // for any shared tear-down.
this->smr_delete();
}
staticvoid ensure_join(JavaThread* thread) { // We do not need to grab the Threads_lock, since we are operating on ourself.
Handle threadObj(thread, thread->threadObj());
assert(threadObj.not_null(), "java thread object must exist");
ObjectLocker lock(threadObj, thread); // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
java_lang_Thread::set_thread_status(threadObj(), JavaThreadStatus::TERMINATED); // Clear the native thread instance - this makes isAlive return false and allows the join() // to complete once we've done the notify_all below
java_lang_Thread::set_thread(threadObj(), NULL);
lock.notify_all(thread); // Ignore pending exception, since we are exiting anyway
thread->clear_pending_exception();
}
// For any new cleanup additions, please check to see if they need to be applied to // cleanup_failed_attach_current_thread as well. void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
assert(this == JavaThread::current(), "thread consistency check");
assert(!is_exiting(), "should not be exiting or terminated already");
if (log_is_enabled(Debug, os, thread, timer)) {
_timer_exit_phase1.start();
}
HandleMark hm(this);
Handle uncaught_exception(this, this->pending_exception());
this->clear_pending_exception();
Handle threadObj(this, this->threadObj());
assert(threadObj.not_null(), "Java thread object should be created");
if (!destroy_vm) { if (uncaught_exception.not_null()) {
EXCEPTION_MARK; // Call method Thread.dispatchUncaughtException().
Klass* thread_klass = vmClasses::Thread_klass();
JavaValue result(T_VOID);
JavaCalls::call_virtual(&result,
threadObj, thread_klass,
vmSymbols::dispatchUncaughtException_name(),
vmSymbols::throwable_void_signature(),
uncaught_exception,
THREAD); if (HAS_PENDING_EXCEPTION) {
ResourceMark rm(this);
jio_fprintf(defaultStream::error_stream(), "\nException: %s thrown from the UncaughtExceptionHandler" " in thread \"%s\"\n",
pending_exception()->klass()->external_name(),
name());
CLEAR_PENDING_EXCEPTION;
}
}
if (!is_Compiler_thread()) { // We have finished executing user-defined Java code and now have to do the // implementation specific clean-up by calling Thread.exit(). We prevent any // asynchronous exceptions from being delivered while in Thread.exit() // to ensure the clean-up is not corrupted.
NoAsyncExceptionDeliveryMark _no_async(this);
// notify JVMTI if (JvmtiExport::should_post_thread_life()) {
JvmtiExport::post_thread_end(this);
}
} else { // before_exit() has already posted JVMTI THREAD_END events
}
// Cleanup any pending async exception now since we cannot access oops after // BarrierSet::barrier_set()->on_thread_detach() has been executed. if (has_async_exception_condition()) {
handshake_state()->clean_async_exception_operation();
}
// The careful dance between thread suspension and exit is handled here. // Since we are in thread_in_vm state and suspension is done with handshakes, // we can just put in the exiting state and it will be correctly handled. // Also, no more async exceptions will be added to the queue after this point.
set_terminated(_thread_exiting);
ThreadService::current_thread_exiting(this, is_daemon(threadObj()));
if (log_is_enabled(Debug, os, thread, timer)) {
_timer_exit_phase1.stop();
_timer_exit_phase2.start();
}
// Capture daemon status before the thread is marked as terminated. bool daemon = is_daemon(threadObj());
// Notify waiters on thread object. This has to be done after exit() is called // on the thread (if the thread is the last thread in a daemon ThreadGroup the // group should have the destroyed bit set before waiters are notified).
ensure_join(this);
assert(!this->has_pending_exception(), "ensure_join should have cleared");
if (log_is_enabled(Debug, os, thread, timer)) {
_timer_exit_phase2.stop();
_timer_exit_phase3.start();
} // 6282335 JNI DetachCurrentThread spec states that all Java monitors // held by this thread must be released. The spec does not distinguish // between JNI-acquired and regular Java monitors. We can only see // regular Java monitors here if monitor enter-exit matching is broken. // // ensure_join() ignores IllegalThreadStateExceptions, and so does // ObjectSynchronizer::release_monitors_owned_by_thread(). if (exit_type == jni_detach) { // Sanity check even though JNI DetachCurrentThread() would have // returned JNI_ERR if there was a Java frame. JavaThread exit // should be done executing Java code by the time we get here.
assert(!this->has_last_Java_frame(), "should not have a Java frame when detaching or exiting");
ObjectSynchronizer::release_monitors_owned_by_thread(this);
assert(!this->has_pending_exception(), "release_monitors should have cleared");
}
// Since above code may not release JNI monitors and if someone forgot to do an // JNI monitorexit, held count should be equal jni count. // Consider scan all object monitor for this owner if JNI count > 0 (at least on detach).
assert(this->held_monitor_count() == this->jni_monitor_count(), "held monitor count should be equal to jni: " INT64_FORMAT " != " INT64_FORMAT,
(int64_t)this->held_monitor_count(), (int64_t)this->jni_monitor_count()); if (CheckJNICalls && this->jni_monitor_count() > 0) { // We would like a fatal here, but due to we never checked this before there // is a lot of tests which breaks, even with an error log.
log_debug(jni)("JavaThread %s (tid: " UINTX_FORMAT ") with Objects still locked by JNI MonitorEnter.",
exit_type == JavaThread::normal_exit ? "exiting" : "detaching", os::current_thread_id());
}
// These things needs to be done while we are still a Java Thread. Make sure that thread // is in a consistent state, in case GC happens
JFR_ONLY(Jfr::on_thread_exit(this);)
// These have to be removed while this is still a valid thread.
_stack_overflow_state.remove_stack_guard_pages();
if (UseTLAB) {
tlab().retire();
}
if (JvmtiEnv::environments_might_exist()) {
JvmtiExport::cleanup_thread(this);
}
// We need to cache the thread name for logging purposes below as once // we have called on_thread_detach this thread must not access any oops. char* thread_name = NULL; if (log_is_enabled(Debug, os, thread, timer)) {
ResourceMark rm(this);
thread_name = os::strdup(name());
}
if (log_is_enabled(Debug, os, thread, timer)) {
_timer_exit_phase3.stop();
_timer_exit_phase4.start();
}
#if INCLUDE_JVMCI if (JVMCICounterSize > 0) { if (jvmci_counters_include(this)) { for (int i = 0; i < JVMCICounterSize; i++) {
_jvmci_old_thread_counters[i] += _jvmci_counters[i];
}
}
} #endif// INCLUDE_JVMCI
// Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread. // We call BarrierSet::barrier_set()->on_thread_detach() here so no touching of oops after this point.
Threads::remove(this, daemon);
// Asynchronous exceptions support // void JavaThread::handle_async_exception(oop java_throwable) {
assert(java_throwable != NULL, "should have an _async_exception to throw");
assert(!is_at_poll_safepoint(), "should have never called this method");
if (has_last_Java_frame()) {
frame f = last_frame(); if (f.is_runtime_frame()) { // If the topmost frame is a runtime stub, then we are calling into // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..) // must deoptimize the caller before continuing, as the compiled exception // handler table may not be valid.
RegisterMap reg_map(this,
RegisterMap::UpdateMap::skip,
RegisterMap::ProcessFrames::include,
RegisterMap::WalkContinuation::skip);
frame compiled_frame = f.sender(®_map); if (!StressCompiledExceptionHandlers && compiled_frame.can_be_deoptimized()) {
Deoptimization::deoptimize(this, compiled_frame);
}
}
}
// We cannot call Exceptions::_throw(...) here because we cannot block
set_pending_exception(java_throwable, __FILE__, __LINE__);
clear_scopedValueBindings();
LogTarget(Info, exceptions) lt; if (lt.is_enabled()) {
ResourceMark rm;
LogStream ls(lt);
ls.print("Async. exception installed at runtime exit (" INTPTR_FORMAT ")", p2i(this)); if (has_last_Java_frame()) {
frame f = last_frame();
ls.print(" (pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " )", p2i(f.pc()), p2i(f.sp()));
}
ls.print_cr(" of type: %s", java_throwable->klass()->external_name());
}
}
void JavaThread::install_async_exception(AsyncExceptionHandshake* aeh) { // Do not throw asynchronous exceptions against the compiler thread // or if the thread is already exiting. if (!can_call_java() || is_exiting()) { delete aeh; return;
}
ResourceMark rm; if (log_is_enabled(Info, exceptions)) {
log_info(exceptions)("Pending Async. exception installed of type: %s",
InstanceKlass::cast(exception->klass())->external_name());
} // for AbortVMOnException flag
Exceptions::debug_check_abort(exception->klass()->external_name());
// Interrupt thread so it will wake up from a potential wait()/sleep()/park()
java_lang_Thread::set_interrupted(threadObj(), true);
this->interrupt();
}
class InstallAsyncExceptionHandshake : public HandshakeClosure {
AsyncExceptionHandshake* _aeh; public:
InstallAsyncExceptionHandshake(AsyncExceptionHandshake* aeh) :
HandshakeClosure("InstallAsyncException"), _aeh(aeh) {}
~InstallAsyncExceptionHandshake() { // If InstallAsyncExceptionHandshake was never executed we need to clean up _aeh. delete _aeh;
} void do_thread(Thread* thr) {
JavaThread* target = JavaThread::cast(thr);
target->install_async_exception(_aeh);
_aeh = nullptr;
}
};
// External suspension mechanism. // // Guarantees on return (for a valid target thread): // - Target thread will not execute any new bytecode. // - Target thread will not enter any new monitors. // bool JavaThread::java_suspend() { #if INCLUDE_JVMTI // Suspending a JavaThread in VTMS transition or disabling VTMS transitions can cause deadlocks.
assert(!is_in_VTMS_transition(), "no suspend allowed in VTMS transition");
assert(!is_VTMS_transition_disabler(), "no suspend allowed for VTMS transition disablers"); #endif
guarantee(Thread::is_JavaThread_protected(/* target */ this), "target JavaThread is not protected in calling context."); return this->handshake_state()->suspend();
}
// Wait for another thread to perform object reallocation and relocking on behalf of // this thread. The current thread is required to change to _thread_blocked in order // to be seen to be safepoint/handshake safe whilst suspended and only after becoming // handshake safe, the other thread can complete the handshake used to synchronize // with this thread and then perform the reallocation and relocking. // See EscapeBarrier::sync_and_suspend_*()
bool spin_wait = os::is_MP(); do {
ThreadBlockInVM tbivm(this, true/* allow_suspend */); // Wait for object deoptimization if requested. if (spin_wait) { // A single deoptimization is typically very short. Microbenchmarks // showed 5% better performance when spinning. const uint spin_limit = 10 * SpinYield::default_spin_limit;
SpinYield spin(spin_limit); for (uint i = 0; is_obj_deopt_suspend() && i < spin_limit; i++) {
spin.wait();
} // Spin just once
spin_wait = false;
} else {
MonitorLocker ml(this, EscapeBarrier_lock, Monitor::_no_safepoint_check_flag); if (is_obj_deopt_suspend()) {
ml.wait();
}
} // A handshake for obj. deoptimization suspend could have been processed so // we must check after processing.
} while (is_obj_deopt_suspend());
}
#ifdef ASSERT // Verify the JavaThread has not yet been published in the Threads::list, and // hence doesn't need protection from concurrent access at this stage. void JavaThread::verify_not_published() { // Cannot create a ThreadsListHandle here and check !tlh.includes(this) // since an unpublished JavaThread doesn't participate in the // Thread-SMR protocol for keeping a ThreadsList alive.
assert(!on_thread_list(), "JavaThread shouldn't have been published yet!");
} #endif
// Slow path when the native==>Java barriers detect a safepoint/handshake is // pending, when _suspend_flags is non-zero or when we need to process a stack // watermark. Also check for pending async exceptions (except unsafe access error). // Note only the native==>Java barriers can call this function when thread state // is _thread_in_native_trans. void JavaThread::check_special_condition_for_native_trans(JavaThread *thread) {
assert(thread->thread_state() == _thread_in_native_trans, "wrong state");
assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "Unwalkable stack in native->Java transition");
thread->set_thread_state(_thread_in_vm);
// Enable WXWrite: called directly from interpreter native wrapper.
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
// After returning from native, it could be that the stack frames are not // yet safe to use. We catch such situations in the subsequent stack watermark // barrier, which will trap unsafe stack frames.
StackWatermarkSet::before_unwind(thread);
}
#ifndef PRODUCT // Deoptimization // Function for testing deoptimization void JavaThread::deoptimize() {
StackFrameStream fst(this, false/* update */, true /* process_frames */); bool deopt = false; // Dump stack only if a deopt actually happens. bool only_at = strlen(DeoptimizeOnlyAt) > 0; // Iterate over all frames in the thread and deoptimize for (; !fst.is_done(); fst.next()) { if (fst.current()->can_be_deoptimized()) {
if (only_at) { // Deoptimize only at particular bcis. DeoptimizeOnlyAt // consists of comma or carriage return separated numbers so // search for the current bci in that string.
address pc = fst.current()->pc();
nmethod* nm = (nmethod*) fst.current()->cb();
ScopeDesc* sd = nm->scope_desc_at(pc); char buffer[8];
jio_snprintf(buffer, sizeof(buffer), "%d", sd->bci());
size_t len = strlen(buffer); constchar * found = strstr(DeoptimizeOnlyAt, buffer); while (found != NULL) { if ((found[len] == ',' || found[len] == '\n' || found[len] == '\0') &&
(found == DeoptimizeOnlyAt || found[-1] == ',' || found[-1] == '\n')) { // Check that the bci found is bracketed by terminators. break;
}
found = strstr(found + 1, buffer);
} if (!found) { continue;
}
}
if (DebugDeoptimization && !deopt) {
deopt = true; // One-time only print before deopt
tty->print_cr("[BEFORE Deoptimization]");
trace_frames();
trace_stack();
}
Deoptimization::deoptimize(this, *fst.current());
}
}
if (DebugDeoptimization && deopt) {
tty->print_cr("[AFTER Deoptimization]");
trace_frames();
}
}
// Make zombies void JavaThread::make_zombies() { for (StackFrameStream fst(this, true/* update */, true /* process_frames */); !fst.is_done(); fst.next()) { if (fst.current()->can_be_deoptimized()) { // it is a Java nmethod
nmethod* nm = CodeCache::find_nmethod(fst.current()->pc());
nm->make_not_entrant();
}
}
} #endif// PRODUCT
void JavaThread::deoptimize_marked_methods() { if (!has_last_Java_frame()) return;
StackFrameStream fst(this, false/* update */, true /* process_frames */); for (; !fst.is_done(); fst.next()) { if (fst.current()->should_be_deoptimized()) {
Deoptimization::deoptimize(this, *fst.current());
}
}
}
// Push on a new block of JNI handles. void JavaThread::push_jni_handle_block() { // Allocate a new block for JNI handles. // Inlined code from jni_PushLocalFrame()
JNIHandleBlock* old_handles = active_handles();
JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(this);
assert(old_handles != NULL && new_handles != NULL, "should not be NULL");
new_handles->set_pop_frame_link(old_handles); // make sure java handles get gc'd.
set_active_handles(new_handles);
}
// Pop off the current block of JNI handles. void JavaThread::pop_jni_handle_block() { // Release our JNI handle block
JNIHandleBlock* old_handles = active_handles();
JNIHandleBlock* new_handles = old_handles->pop_frame_link();
assert(new_handles != nullptr, "should never set active handles to null");
set_active_handles(new_handles);
old_handles->set_pop_frame_link(NULL);
JNIHandleBlock::release_block(old_handles, this);
}
void JavaThread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) { // Verify that the deferred card marks have been flushed.
assert(deferred_card_mark().is_empty(), "Should be empty during GC");
// Traverse the GCHandles
Thread::oops_do_no_frames(f, cf);
if (active_handles() != NULL) {
active_handles()->oops_do(f);
}
DEBUG_ONLY(verify_frame_info();)
if (has_last_Java_frame()) { // Traverse the monitor chunks for (MonitorChunk* chunk = monitor_chunks(); chunk != NULL; chunk = chunk->next()) {
chunk->oops_do(f);
}
}
assert(vframe_array_head() == NULL, "deopt in progress at a safepoint!"); // If we have deferred set_locals there might be oops waiting to be // written
GrowableArray<jvmtiDeferredLocalVariableSet*>* list = JvmtiDeferredUpdates::deferred_locals(this); if (list != NULL) { for (int i = 0; i < list->length(); i++) {
list->at(i)->oops_do(f);
}
}
// Traverse instance variables at the end since the GC may be moving things // around using this function
f->do_oop((oop*) &_vm_result);
f->do_oop((oop*) &_exception_oop); #if INCLUDE_JVMCI
f->do_oop((oop*) &_jvmci_reserved_oop0); #endif
if (jvmti_thread_state() != NULL) {
jvmti_thread_state()->oops_do(f, cf);
}
// The continuation oops are really on the stack. But there is typically at most // one of those per thread, so we handle them here in the oops_do_no_frames part // so that we don't have to sprinkle as many stack watermark checks where these // oops are used. We just need to make sure the thread has started processing.
ContinuationEntry* entry = _cont_entry; while (entry != nullptr) {
f->do_oop((oop*)entry->cont_addr());
f->do_oop((oop*)entry->chunk_addr());
entry = entry->parent();
}
}
void JavaThread::oops_do_frames(OopClosure* f, CodeBlobClosure* cf) { if (!has_last_Java_frame()) { return;
} // Finish any pending lazy GC activity for the frames
StackWatermarkSet::finish_processing(this, NULL /* context */, StackWatermarkKind::gc); // Traverse the execution stack for (StackFrameStream fst(this, true/* update */, false /* process_frames */); !fst.is_done(); fst.next()) {
fst.current()->oops_do(f, cf, fst.register_map());
}
}
#ifdef ASSERT void JavaThread::verify_states_for_handshake() { // This checks that the thread has a correct frame state during a handshake.
verify_frame_info();
} #endif
if (has_last_Java_frame()) { // Traverse the execution stack for (StackFrameStream fst(this, true/* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
fst.current()->nmethods_do(cf);
}
}
if (jvmti_thread_state() != NULL) {
jvmti_thread_state()->nmethods_do(cf);
}
}
void JavaThread::metadata_do(MetadataClosure* f) { if (has_last_Java_frame()) { // Traverse the execution stack to call f() on the methods in the stack for (StackFrameStream fst(this, true/* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
fst.current()->metadata_do(f);
}
} elseif (is_Compiler_thread()) { // need to walk ciMetadata in current compile tasks to keep alive.
CompilerThread* ct = (CompilerThread*)this; if (ct->env() != NULL) {
ct->env()->metadata_do(f);
}
CompileTask* task = ct->task(); if (task != NULL) {
task->metadata_do(f);
}
}
}
// Printing constchar* _get_thread_state_name(JavaThreadState _thread_state) { switch (_thread_state) { case _thread_uninitialized: return"_thread_uninitialized"; case _thread_new: return"_thread_new"; case _thread_new_trans: return"_thread_new_trans"; case _thread_in_native: return"_thread_in_native"; case _thread_in_native_trans: return"_thread_in_native_trans"; case _thread_in_vm: return"_thread_in_vm"; case _thread_in_vm_trans: return"_thread_in_vm_trans"; case _thread_in_Java: return"_thread_in_Java"; case _thread_in_Java_trans: return"_thread_in_Java_trans"; case _thread_blocked: return"_thread_blocked"; case _thread_blocked_trans: return"_thread_blocked_trans"; default: return"unknown thread state";
}
}
// Called by fatal error handler. The difference between this and // JavaThread::print() is that we can't grab lock or allocate memory. void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
st->print("%s \"%s\"", type_name(), get_thread_name_string(buf, buflen));
Thread* current = Thread::current_or_null_safe();
assert(current != nullptr, "cannot be called by a detached thread"); if (!current->is_Java_thread() || JavaThread::cast(current)->is_oop_safe()) { // Only access threadObj() if current thread is not a JavaThread // or if it is a JavaThread that can safely access oops.
oop thread_obj = threadObj(); if (thread_obj != nullptr) { if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
}
}
st->print(" [");
st->print("%s", _get_thread_state_name(_thread_state)); if (osthread()) {
st->print(", id=%d", osthread()->thread_id());
}
st->print(", stack(" PTR_FORMAT "," PTR_FORMAT ")",
p2i(stack_end()), p2i(stack_base()));
st->print("]");
void JavaThread::verify() { // Verify oops in the thread.
oops_do(&VerifyOopClosure::verify_oop, NULL);
// Verify the stack frames.
frames_do(frame_verify);
}
// CR 6300358 (sub-CR 2137150) // Most callers of this method assume that it can't return NULL but a // thread may not have a name whilst it is in the process of attaching to // the VM - see CR 6412693, and there are places where a JavaThread can be // seen prior to having its threadObj set (e.g., JNI attaching threads and // if vm exit occurs during initialization). These cases can all be accounted // for such that this method never returns NULL. constchar* JavaThread::name() const { if (Thread::is_JavaThread_protected(/* target */ this)) { // The target JavaThread is protected so get_thread_name_string() is safe: return get_thread_name_string();
}
// The target JavaThread is not protected so we return the default: return Thread::name();
}
// Returns a non-NULL representation of this thread's name, or a suitable // descriptive string if there is no set name. constchar* JavaThread::get_thread_name_string(char* buf, int buflen) const { constchar* name_str; #ifdef ASSERT
Thread* current = Thread::current_or_null_safe();
assert(current != nullptr, "cannot be called by a detached thread"); if (!current->is_Java_thread() || JavaThread::cast(current)->is_oop_safe()) { // Only access threadObj() if current thread is not a JavaThread // or if it is a JavaThread that can safely access oops. #endif
oop thread_obj = threadObj(); if (thread_obj != NULL) {
oop name = java_lang_Thread::name(thread_obj); if (name != NULL) { if (buf == NULL) {
name_str = java_lang_String::as_utf8_string(name);
} else {
name_str = java_lang_String::as_utf8_string(name, buf, buflen);
}
} elseif (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
name_str = "";
} else {
name_str = "";
}
} else {
name_str = Thread::name();
} #ifdef ASSERT
} else { // Current JavaThread has exited... if (current == this) { // ... and is asking about itself:
name_str = "";
} else { // ... and it can't safely determine this JavaThread's name so // use the default thread name.
name_str = Thread::name();
}
} #endif
assert(name_str != NULL, "unexpected NULL thread name"); return name_str;
}
// Helper to extract the name from the thread oop for logging. constchar* JavaThread::name_for(oop thread_obj) {
assert(thread_obj != NULL, "precondition");
oop name = java_lang_Thread::name(thread_obj); constchar* name_str; if (name != NULL) {
name_str = java_lang_String::as_utf8_string(name);
} else {
name_str = "";
} return name_str;
}
assert(Threads_lock->owner() == Thread::current(), "must have threads lock");
assert(NoPriority <= prio && prio <= MaxPriority, "sanity check"); // Link Java Thread object <-> C++ Thread
// Get the C++ thread object (an oop) from the JNI handle (a jthread) // and put it into a new Handle. The Handle "thread_oop" can then // be used to pass the C++ thread object to other methods.
// Set the Java level thread object (jthread) field of the // new thread (a JavaThread *) to C++ thread object using the // "thread_oop" handle.
// Set the thread field (a JavaThread *) of the // oop representing the java_lang_Thread to the new thread (a JavaThread *).
Handle thread_oop(Thread::current(),
JNIHandles::resolve_non_null(jni_thread));
assert(InstanceKlass::cast(thread_oop->klass())->is_linked(), "must be initialized");
set_threadOopHandles(thread_oop());
java_lang_Thread::set_thread(thread_oop(), this);
if (prio == NoPriority) {
prio = java_lang_Thread::priority(thread_oop());
assert(prio != NoPriority, "A valid priority should be present");
}
// Push the Java priority down to the native thread; needs Threads_lock
Thread::set_priority(this, prio);
// Add the new thread to the Threads list and set it in motion. // We must have threads lock in order to call Threads::add. // It is crucial that we do not block before the thread is // added to the Threads list for if a GC happens, then the java_thread oop // will not be visited by GC.
Threads::add(this);
}
oop JavaThread::current_park_blocker() { // Support for JSR-166 locks
oop thread_oop = threadObj(); if (thread_oop != NULL) { return java_lang_Thread::park_blocker(thread_oop);
} return NULL;
}
void JavaThread::print_stack_on(outputStream* st) { if (!has_last_Java_frame()) return;
RegisterMap reg_map(this,
RegisterMap::UpdateMap::include,
RegisterMap::ProcessFrames::include,
RegisterMap::WalkContinuation::skip);
vframe* start_vf = platform_thread_last_java_vframe(®_map); int count = 0; for (vframe* f = start_vf; f != NULL; f = f->sender()) { if (f->is_java_frame()) {
javaVFrame* jvf = javaVFrame::cast(f);
java_lang_Throwable::print_stack_element(st, jvf->method(), jvf->bci());
// Print out lock information if (JavaMonitorsInStackTrace) {
jvf->print_lock_info_on(st, count);
}
} else { // Ignore non-Java frames
}
// Bail-out case for too deep stacks if MaxJavaStackTraceDepth > 0
count++; if (MaxJavaStackTraceDepth > 0 && MaxJavaStackTraceDepth == count) return;
}
}
#if INCLUDE_JVMTI // Rebind JVMTI thread state from carrier to virtual or from virtual to carrier.
JvmtiThreadState* JavaThread::rebind_to_jvmti_thread_state_of(oop thread_oop) {
set_jvmti_vthread(thread_oop);
// unbind current JvmtiThreadState from JavaThread
JvmtiThreadState::unbind_from(jvmti_thread_state(), this);
// bind new JvmtiThreadState to JavaThread
JvmtiThreadState::bind_to(java_lang_Thread::jvmti_thread_state(thread_oop), this);
return jvmti_thread_state();
} #endif
// JVMTI PopFrame support void JavaThread::popframe_preserve_args(ByteSize size_in_bytes, void* start) {
assert(_popframe_preserved_args == NULL, "should not wipe out old PopFrame preserved arguments"); if (in_bytes(size_in_bytes) != 0) {
_popframe_preserved_args = NEW_C_HEAP_ARRAY(char, in_bytes(size_in_bytes), mtThread);
_popframe_preserved_args_size = in_bytes(size_in_bytes);
Copy::conjoint_jbytes(start, _popframe_preserved_args, _popframe_preserved_args_size);
}
}
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.