/* * 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. *
*/
class HandleArea; class HandleMark; class ICRefillVerifier; class JvmtiRawMonitor; class Metadata; class OSThread; class ParkEvent; class ResourceArea; class SafeThreadsListPtr; class ThreadClosure; class ThreadsList; class ThreadsSMRSupport;
class OopClosure; class CodeBlobClosure;
DEBUG_ONLY(class ResourceMark;)
class WorkerThread;
class JavaThread;
// Class hierarchy // - Thread // - JavaThread // - various subclasses eg CompilerThread, ServiceThread // - NonJavaThread // - NamedThread // - VMThread // - ConcurrentGCThread // - WorkerThread // - WatcherThread // - JfrThreadSampler // - LogAsyncWriter // // All Thread subclasses must be either JavaThread or NonJavaThread. // This means !t->is_Java_thread() iff t is a NonJavaThread, or t is // a partially constructed/destroyed Thread.
// Thread execution sequence and actions: // All threads: // - thread_native_entry // per-OS native entry point // - stack initialization // - other OS-level initialization (signal masks etc) // - handshake with creating thread (if not started suspended) // - this->call_run() // common shared entry point // - shared common initialization // - this->pre_run() // virtual per-thread-type initialization // - this->run() // virtual per-thread-type "main" logic // - shared common tear-down // - this->post_run() // virtual per-thread-type tear-down // - // 'this' no longer referenceable // - OS-level tear-down (minimal) // - final logging // // For JavaThread: // - this->run() // virtual but not normally overridden // - this->thread_main_inner() // extra call level to ensure correct stack calculations // - this->entry_point() // set differently for each kind of JavaThread
class Thread: public ThreadShadow { friendclass VMStructs; friendclass JVMCIVMStructs; private:
#ifndef USE_LIBRARY_BASED_TLS_ONLY // Current thread is maintained as a thread-local variable static THREAD_LOCAL Thread* _thr_current; #endif
// On AArch64, the high order 32 bits are used by a "patching epoch" number // which reflects if this thread has executed the required fences, after // an nmethod gets disarmed. The low order 32 bit denote the disarm value.
uint64_t _nmethod_disarm_value;
static ByteSize nmethod_disarmed_offset() {
ByteSize offset = byte_offset_of(Thread, _nmethod_disarm_value); // At least on x86_64, nmethod entry barrier encodes disarmed value offset // in instruction as disp8 immed
assert(in_bytes(offset) < 128, "Offset >= 128"); return offset;
}
private: // Thread local data area available to the GC. The internal // structure and contents of this data area is GC-specific. // Only GC and GC barrier code should access this data area.
GCThreadLocalData _gc_data;
// Exception handling // (Note: _pending_exception and friends are in ThreadShadow) //oop _pending_exception; // pending exception for current thread // const char* _exception_file; // file information for exception (debugging only) // int _exception_line; // line information for exception (debugging only) protected:
DEBUG_ONLY(static Thread* _starting_thread;)
// JavaThread lifecycle support: friendclass SafeThreadsListPtr; // for _threads_list_ptr, cmpxchg_threads_hazard_ptr(), {dec_,inc_,}nested_threads_hazard_ptr_cnt(), {g,s}et_threads_hazard_ptr(), inc_nested_handle_cnt(), tag_hazard_ptr() access friendclass ScanHazardPtrGatherProtectedThreadsClosure; // for cmpxchg_threads_hazard_ptr(), get_threads_hazard_ptr(), is_hazard_ptr_tagged() access friendclass ScanHazardPtrGatherThreadsListClosure; // for get_threads_hazard_ptr(), untag_hazard_ptr() access friendclass ScanHazardPtrPrintMatchingThreadsClosure; // for get_threads_hazard_ptr(), is_hazard_ptr_tagged() access friendclass ThreadsSMRSupport; // for _nested_threads_hazard_ptr_cnt, _threads_hazard_ptr, _threads_list_ptr access friendclass ThreadsListHandleTest; // for _nested_threads_hazard_ptr_cnt, _threads_hazard_ptr, _threads_list_ptr access friendclass ValidateHazardPtrsClosure; // for get_threads_hazard_ptr(), untag_hazard_ptr() access
public: // Is the target JavaThread protected by the calling Thread or by some other // mechanism? staticbool is_JavaThread_protected(const JavaThread* target); // Is the target JavaThread protected by a ThreadsListHandle (TLH) associated // with the calling Thread? staticbool is_JavaThread_protected_by_TLH(const JavaThread* target);
public: // Determines if a heap allocation failure will be retried // (e.g., by deoptimizing and re-executing in the interpreter). // In this case, the failed allocation must raise // Universe::out_of_memory_error_retry() and omit side effects // such as JVMTI events and handling -XX:+HeapDumpOnOutOfMemoryError // and -XX:OnOutOfMemoryError. virtualbool in_retryable_allocation() const { returnfalse; }
private: // Used by SkipGCALot class.
NOT_PRODUCT(bool _skip_gcalot;) // Should we elide gc-a-lot?
friendclass GCLocker;
private:
ThreadLocalAllocBuffer _tlab; // Thread-local eden
jlong _allocated_bytes; // Cumulative number of bytes allocated on // the Java heap
ThreadHeapSampler _heap_sampler; // For use when sampling the memory.
ThreadStatisticalInfo _statistical_info; // Statistics about the thread
JFR_ONLY(DEFINE_THREAD_LOCAL_FIELD_JFR;) // Thread-local data for jfr
JvmtiRawMonitor* _current_pending_raw_monitor; // JvmtiRawMonitor this thread // is waiting to lock public: // Constructor
Thread(); virtual ~Thread() = 0; // Thread is abstract.
protected: // To be implemented by children. virtualvoid run() = 0; virtualvoid pre_run() = 0; virtualvoid post_run() = 0; // Note: Thread must not be deleted prior to calling this!
#ifdef ASSERT enum RunState {
PRE_CALL_RUN,
CALL_RUN,
PRE_RUN,
RUN,
POST_RUN // POST_CALL_RUN - can't define this one as 'this' may be deleted when we want to set it
};
RunState _run_state; // for lifecycle checks #endif
public: // invokes <ChildThreadClass>::run(), with common preparations and cleanups. void call_run();
// Can this thread make Java upcalls virtualbool can_call_java() const { returnfalse; }
// Is this a JavaThread that is on the VM's current ThreadsList? // If so it must participate in the safepoint protocol. virtualbool is_active_Java_thread() const { returnfalse; }
// All threads are given names. For singleton subclasses we can // just hard-wire the known name of the instance. JavaThreads and // NamedThreads support multiple named instances, and dynamic // changing of the name of an instance. virtualconstchar* name() const { return"Unknown thread"; }
// A thread's type name is also made available for debugging // and logging. virtualconstchar* type_name() const { return"Thread"; }
// Returns the current thread (ASSERTS if NULL) staticinline Thread* current(); // Returns the current thread, or NULL if not attached staticinline Thread* current_or_null(); // Returns the current thread, or NULL if not attached, and is // safe for use from signal-handlers staticinline Thread* current_or_null_safe();
void set_native_thread_name(constchar *name) {
assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
os::set_native_thread_name(name);
}
// Support for Unhandled Oop detection // Add the field for both, fastdebug and debug, builds to keep // Thread's fields layout the same. // Note: CHECK_UNHANDLED_OOPS is defined only for fastdebug build. #ifdef CHECK_UNHANDLED_OOPS private:
UnhandledOops* _unhandled_oops; #elifdefined(ASSERT) private: void* _unhandled_oops; #endif #ifdef CHECK_UNHANDLED_OOPS public:
UnhandledOops* unhandled_oops() { return _unhandled_oops; } // Mark oop safe for gc. It may be stack allocated but won't move. void allow_unhandled_oop(oop *op) { if (CheckUnhandledOops) unhandled_oops()->allow_unhandled_oop(op);
} // Clear oops at safepoint so crashes point to unhandled oop violator void clear_unhandled_oops() { if (CheckUnhandledOops) unhandled_oops()->clear_unhandled_oops();
} #endif// CHECK_UNHANDLED_OOPS
// For tracking the Jvmti raw monitor the thread is pending on.
JvmtiRawMonitor* current_pending_raw_monitor() { return _current_pending_raw_monitor;
} void set_current_pending_raw_monitor(JvmtiRawMonitor* monitor) {
_current_pending_raw_monitor = monitor;
}
// GC support // Apply "f->do_oop" to all root oops in "this". // Used by JavaThread::oops_do. // Apply "cf->do_code_blob" (if !NULL) to all code blobs active in frames virtualvoid oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf); virtualvoid oops_do_frames(OopClosure* f, CodeBlobClosure* cf) {} void oops_do(OopClosure* f, CodeBlobClosure* cf);
// Handles the parallel case for claim_threads_do. private: bool claim_par_threads_do(uintx claim_token); public: // Requires that "claim_token" is that of the current iteration. // If "is_par" is false, sets the token of "this" to // "claim_token", and returns "true". If "is_par" is true, // uses an atomic instruction to set the current thread's token to // "claim_token", if it is not already. Returns "true" iff the // calling thread does the update, this indicates that the calling thread // has claimed the thread in the current iteration. bool claim_threads_do(bool is_par, uintx claim_token) { if (!is_par) {
_threads_do_token = claim_token; returntrue;
} else { return claim_par_threads_do(claim_token);
}
}
// jvmtiRedefineClasses support void metadata_handles_do(void f(Metadata*));
private: // Check if address is within the given range of this thread's // stack: stack_base() > adr >/>= limit // The check is inclusive of limit if passed true, else exclusive. bool is_in_stack_range(address adr, address limit, bool inclusive) const {
assert(stack_base() > limit && limit >= stack_end(), "limit is outside of stack"); return stack_base() > adr && (inclusive ? adr >= limit : adr > limit);
}
public: // Used by fast lock support virtualbool is_lock_owned(address adr) const;
// Check if address is within the given range of this thread's // stack: stack_base() > adr >= limit bool is_in_stack_range_incl(address adr, address limit) const { return is_in_stack_range(adr, limit, true);
}
// Check if address is within the given range of this thread's // stack: stack_base() > adr > limit bool is_in_stack_range_excl(address adr, address limit) const { return is_in_stack_range(adr, limit, false);
}
// Check if address is in the stack mapped to this thread. Used mainly in // error reporting (so has to include guard zone) and frame printing. // Expects _stack_base to be initialized - checked with assert. bool is_in_full_stack_checked(address adr) const { return is_in_stack_range_incl(adr, stack_end());
}
// Like is_in_full_stack_checked but without the assertions as this // may be called in a thread before _stack_base is initialized. bool is_in_full_stack(address adr) const {
address stack_end = _stack_base - _stack_size; return _stack_base > adr && adr >= stack_end;
}
// Check if address is in the live stack of this thread (not just for locks). // Warning: can only be called by the current thread on itself. bool is_in_live_stack(address adr) const {
assert(Thread::current() == this, "is_in_live_stack can only be called from current thread"); return is_in_stack_range_incl(adr, os::current_stack_pointer());
}
// Sets this thread as starting thread. Returns failure if thread // creation fails due to lack of memory, too many threads etc. bool set_as_starting_thread();
protected: // OS data associated with the thread
OSThread* _osthread; // Platform-specific thread information
// Thread local resource area for temporary allocation within the VM
ResourceArea* _resource_area;
DEBUG_ONLY(ResourceMark* _current_resource_mark;)
// Thread local handle area for allocation of handles within the VM
HandleArea* _handle_area;
GrowableArray<Metadata*>* _metadata_handles;
// Support for stack overflow handling, get_thread, etc.
address _stack_base;
size_t _stack_size; int _lgrp_id;
// Printing void print_on(outputStream* st, bool print_extended_info) const; virtualvoid print_on(outputStream* st) const { print_on(st, false); } void print() const; virtualvoid print_on_error(outputStream* st, char* buf, int buflen) const; // Basic, non-virtual, printing support that is simple and always safe. void print_value_on(outputStream* st) const;
// Debug-only code #ifdef ASSERT private: // Deadlock detection support for Mutex locks. List of locks own by thread.
Mutex* _owned_locks; // Mutex::set_owner_implementation is the only place where _owned_locks is modified, // thus the friendship friendclass Mutex; friendclass Monitor;
public:
ParkEvent * volatile _ParkEvent; // for Object monitors, JVMTI raw monitors, // and ObjectSynchronizer::read_stable_mark
// Termination indicator used by the signal handler. // _ParkEvent is just a convenient field we can NULL out after setting the JavaThread termination state // (which can't itself be read from the signal handler if a signal hits during the Thread destructor). bool has_terminated() { return Atomic::load(&_ParkEvent) == NULL; };
// Low-level leaf-lock primitives used to implement synchronization. // Not for general synchronization use. staticvoid SpinAcquire(volatileint * Lock, constchar * Name); staticvoid SpinRelease(volatileint * Lock);
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.