1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #ifndef SHARE_RUNTIME_THREAD_HPP 27 #define SHARE_RUNTIME_THREAD_HPP 28 29 #include "jni.h" 30 #include "gc/shared/gcThreadLocalData.hpp" 31 #include "gc/shared/threadLocalAllocBuffer.hpp" 32 #include "memory/allocation.hpp" 33 #include "runtime/atomic.hpp" 34 #include "runtime/globals.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/threadHeapSampler.hpp" 37 #include "runtime/threadLocalStorage.hpp" 38 #include "runtime/threadStatisticalInfo.hpp" 39 #include "runtime/unhandledOops.hpp" 40 #include "utilities/globalDefinitions.hpp" 41 #include "utilities/macros.hpp" 42 #if INCLUDE_JFR 43 #include "jfr/support/jfrThreadExtension.hpp" 44 #endif 45 46 class CompilerThread; 47 class HandleArea; 48 class HandleMark; 49 class JvmtiRawMonitor; 50 class NMethodClosure; 51 class Metadata; 52 class OopClosure; 53 class OSThread; 54 class ParkEvent; 55 class ResourceArea; 56 class SafeThreadsListPtr; 57 class ThreadClosure; 58 class ThreadsList; 59 class ThreadsSMRSupport; 60 class VMErrorCallback; 61 62 63 class PerfTraceTime; 64 65 DEBUG_ONLY(class ResourceMark;) 66 67 class WorkerThread; 68 69 class JavaThread; 70 71 // Class hierarchy 72 // - Thread 73 // - JavaThread 74 // - various subclasses eg CompilerThread, ServiceThread 75 // - NonJavaThread 76 // - NamedThread 77 // - VMThread 78 // - ConcurrentGCThread 79 // - WorkerThread 80 // - WatcherThread 81 // - JfrThreadSampler 82 // - LogAsyncWriter 83 // 84 // All Thread subclasses must be either JavaThread or NonJavaThread. 85 // This means !t->is_Java_thread() iff t is a NonJavaThread, or t is 86 // a partially constructed/destroyed Thread. 87 88 // Thread execution sequence and actions: 89 // All threads: 90 // - thread_native_entry // per-OS native entry point 91 // - stack initialization 92 // - other OS-level initialization (signal masks etc) 93 // - handshake with creating thread (if not started suspended) 94 // - this->call_run() // common shared entry point 95 // - shared common initialization 96 // - this->pre_run() // virtual per-thread-type initialization 97 // - this->run() // virtual per-thread-type "main" logic 98 // - shared common tear-down 99 // - this->post_run() // virtual per-thread-type tear-down 100 // - // 'this' no longer referenceable 101 // - OS-level tear-down (minimal) 102 // - final logging 103 // 104 // For JavaThread: 105 // - this->run() // virtual but not normally overridden 106 // - this->thread_main_inner() // extra call level to ensure correct stack calculations 107 // - this->entry_point() // set differently for each kind of JavaThread 108 109 class Thread: public ThreadShadow { 110 friend class VMError; 111 friend class VMErrorCallbackMark; 112 friend class VMStructs; 113 friend class JVMCIVMStructs; 114 private: 115 116 #ifndef USE_LIBRARY_BASED_TLS_ONLY 117 // Current thread is maintained as a thread-local variable 118 static THREAD_LOCAL Thread* _thr_current; 119 #endif 120 121 // On AArch64, the high order 32 bits are used by a "patching epoch" number 122 // which reflects if this thread has executed the required fences, after 123 // an nmethod gets disarmed. The low order 32 bits denote the disarmed value. 124 uint64_t _nmethod_disarmed_guard_value; 125 126 public: 127 void set_nmethod_disarmed_guard_value(int value) { 128 _nmethod_disarmed_guard_value = (uint64_t)(uint32_t)value; 129 } 130 131 static ByteSize nmethod_disarmed_guard_value_offset() { 132 ByteSize offset = byte_offset_of(Thread, _nmethod_disarmed_guard_value); 133 // At least on x86_64, nmethod entry barrier encodes disarmed value offset 134 // in instruction as disp8 immed 135 assert(in_bytes(offset) < 128, "Offset >= 128"); 136 return offset; 137 } 138 139 private: 140 // Thread local data area available to the GC. The internal 141 // structure and contents of this data area is GC-specific. 142 // Only GC and GC barrier code should access this data area. 143 GCThreadLocalData _gc_data; 144 145 public: 146 static ByteSize gc_data_offset() { 147 return byte_offset_of(Thread, _gc_data); 148 } 149 150 template <typename T> T* gc_data() { 151 STATIC_ASSERT(sizeof(T) <= sizeof(_gc_data)); 152 return reinterpret_cast<T*>(&_gc_data); 153 } 154 155 // Exception handling 156 // (Note: _pending_exception and friends are in ThreadShadow) 157 //oop _pending_exception; // pending exception for current thread 158 // const char* _exception_file; // file information for exception (debugging only) 159 // int _exception_line; // line information for exception (debugging only) 160 protected: 161 162 DEBUG_ONLY(static Thread* _starting_thread;) 163 164 // JavaThread lifecycle support: 165 friend class 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 166 friend class ScanHazardPtrGatherProtectedThreadsClosure; // for cmpxchg_threads_hazard_ptr(), get_threads_hazard_ptr(), is_hazard_ptr_tagged() access 167 friend class ScanHazardPtrGatherThreadsListClosure; // for get_threads_hazard_ptr(), untag_hazard_ptr() access 168 friend class ScanHazardPtrPrintMatchingThreadsClosure; // for get_threads_hazard_ptr(), is_hazard_ptr_tagged() access 169 friend class ThreadsSMRSupport; // for _nested_threads_hazard_ptr_cnt, _threads_hazard_ptr, _threads_list_ptr access 170 friend class ThreadsListHandleTest; // for _nested_threads_hazard_ptr_cnt, _threads_hazard_ptr, _threads_list_ptr access 171 friend class ValidateHazardPtrsClosure; // for get_threads_hazard_ptr(), untag_hazard_ptr() access 172 173 ThreadsList* volatile _threads_hazard_ptr; 174 SafeThreadsListPtr* _threads_list_ptr; 175 ThreadsList* cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value); 176 ThreadsList* get_threads_hazard_ptr() const; 177 void set_threads_hazard_ptr(ThreadsList* new_list); 178 static bool is_hazard_ptr_tagged(ThreadsList* list) { 179 return (intptr_t(list) & intptr_t(1)) == intptr_t(1); 180 } 181 static ThreadsList* tag_hazard_ptr(ThreadsList* list) { 182 return (ThreadsList*)(intptr_t(list) | intptr_t(1)); 183 } 184 static ThreadsList* untag_hazard_ptr(ThreadsList* list) { 185 return (ThreadsList*)(intptr_t(list) & ~intptr_t(1)); 186 } 187 // This field is enabled via -XX:+EnableThreadSMRStatistics: 188 uint _nested_threads_hazard_ptr_cnt; 189 void dec_nested_threads_hazard_ptr_cnt() { 190 assert(_nested_threads_hazard_ptr_cnt != 0, "mismatched {dec,inc}_nested_threads_hazard_ptr_cnt()"); 191 _nested_threads_hazard_ptr_cnt--; 192 } 193 void inc_nested_threads_hazard_ptr_cnt() { 194 _nested_threads_hazard_ptr_cnt++; 195 } 196 uint nested_threads_hazard_ptr_cnt() { 197 return _nested_threads_hazard_ptr_cnt; 198 } 199 200 public: 201 // Is the target JavaThread protected by the calling Thread or by some other 202 // mechanism? 203 static bool is_JavaThread_protected(const JavaThread* target); 204 // Is the target JavaThread protected by a ThreadsListHandle (TLH) associated 205 // with the calling Thread? 206 static bool is_JavaThread_protected_by_TLH(const JavaThread* target); 207 208 private: 209 DEBUG_ONLY(bool _suspendible_thread;) 210 DEBUG_ONLY(bool _indirectly_suspendible_thread;) 211 DEBUG_ONLY(bool _indirectly_safepoint_thread;) 212 213 public: 214 #ifdef ASSERT 215 void set_suspendible_thread() { _suspendible_thread = true; } 216 void clear_suspendible_thread() { _suspendible_thread = false; } 217 bool is_suspendible_thread() { return _suspendible_thread; } 218 219 void set_indirectly_suspendible_thread() { _indirectly_suspendible_thread = true; } 220 void clear_indirectly_suspendible_thread() { _indirectly_suspendible_thread = false; } 221 bool is_indirectly_suspendible_thread() { return _indirectly_suspendible_thread; } 222 223 void set_indirectly_safepoint_thread() { _indirectly_safepoint_thread = true; } 224 void clear_indirectly_safepoint_thread() { _indirectly_safepoint_thread = false; } 225 bool is_indirectly_safepoint_thread() { return _indirectly_safepoint_thread; } 226 #endif 227 228 private: 229 // Point to the last handle mark 230 HandleMark* _last_handle_mark; 231 232 // Claim value for parallel iteration over threads. 233 uintx _threads_do_token; 234 235 // Support for GlobalCounter 236 private: 237 volatile uintx _rcu_counter; 238 public: 239 volatile uintx* get_rcu_counter() { 240 return &_rcu_counter; 241 } 242 243 public: 244 void set_last_handle_mark(HandleMark* mark) { _last_handle_mark = mark; } 245 HandleMark* last_handle_mark() const { return _last_handle_mark; } 246 247 private: 248 // Used by SkipGCALot class. 249 NOT_PRODUCT(bool _skip_gcalot;) // Should we elide gc-a-lot? 250 251 friend class GCLocker; 252 253 private: 254 ThreadLocalAllocBuffer _tlab; // Thread-local eden 255 jlong _allocated_bytes; // Cumulative number of bytes allocated on 256 // the Java heap 257 ThreadHeapSampler _heap_sampler; // For use when sampling the memory. 258 259 ThreadStatisticalInfo _statistical_info; // Statistics about the thread 260 261 JFR_ONLY(DEFINE_THREAD_LOCAL_FIELD_JFR;) // Thread-local data for jfr 262 263 JvmtiRawMonitor* _current_pending_raw_monitor; // JvmtiRawMonitor this thread 264 // is waiting to lock 265 public: 266 // Constructor 267 Thread(MemTag mem_tag = mtThread); 268 virtual ~Thread() = 0; // Thread is abstract. 269 270 // Manage Thread::current() 271 void initialize_thread_current(); 272 static void clear_thread_current(); // TLS cleanup needed before threads terminate 273 274 protected: 275 // To be implemented by children. 276 virtual void run() = 0; 277 virtual void pre_run() = 0; 278 virtual void post_run() = 0; // Note: Thread must not be deleted prior to calling this! 279 280 #ifdef ASSERT 281 enum RunState { 282 PRE_CALL_RUN, 283 CALL_RUN, 284 PRE_RUN, 285 RUN, 286 POST_RUN 287 // POST_CALL_RUN - can't define this one as 'this' may be deleted when we want to set it 288 }; 289 RunState _run_state; // for lifecycle checks 290 #endif 291 292 293 public: 294 // invokes <ChildThreadClass>::run(), with common preparations and cleanups. 295 void call_run(); 296 297 // Testers 298 virtual bool is_VM_thread() const { return false; } 299 virtual bool is_Java_thread() const { return false; } 300 virtual bool is_Compiler_thread() const { return false; } 301 virtual bool is_service_thread() const { return false; } 302 virtual bool is_hidden_from_external_view() const { return false; } 303 virtual bool is_jvmti_agent_thread() const { return false; } 304 virtual bool is_Watcher_thread() const { return false; } 305 virtual bool is_ConcurrentGC_thread() const { return false; } 306 virtual bool is_Named_thread() const { return false; } 307 virtual bool is_Worker_thread() const { return false; } 308 virtual bool is_JfrSampler_thread() const { return false; } 309 virtual bool is_AttachListener_thread() const { return false; } 310 virtual bool is_monitor_deflation_thread() const { return false; } 311 312 // Convenience cast functions 313 CompilerThread* as_Compiler_thread() const { 314 assert(is_Compiler_thread(), "Must be compiler thread"); 315 return (CompilerThread*)this; 316 } 317 318 // Can this thread make Java upcalls 319 virtual bool can_call_java() const { return false; } 320 321 // Is this a JavaThread that is on the VM's current ThreadsList? 322 // If so it must participate in the safepoint protocol. 323 virtual bool is_active_Java_thread() const { return false; } 324 325 // All threads are given names. For singleton subclasses we can 326 // just hard-wire the known name of the instance. JavaThreads and 327 // NamedThreads support multiple named instances, and dynamic 328 // changing of the name of an instance. 329 virtual const char* name() const { return "Unknown thread"; } 330 331 // A thread's type name is also made available for debugging 332 // and logging. 333 virtual const char* type_name() const { return "Thread"; } 334 335 // Returns the current thread (ASSERTS if null) 336 static inline Thread* current(); 337 // Returns the current thread, or null if not attached 338 static inline Thread* current_or_null(); 339 // Returns the current thread, or null if not attached, and is 340 // safe for use from signal-handlers 341 static inline Thread* current_or_null_safe(); 342 343 // Common thread operations 344 #ifdef ASSERT 345 static void check_for_dangling_thread_pointer(Thread *thread); 346 #endif 347 static void set_priority(Thread* thread, ThreadPriority priority); 348 static void start(Thread* thread); 349 350 void set_native_thread_name(const char *name) { 351 assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread"); 352 os::set_native_thread_name(name); 353 } 354 355 // Support for Unhandled Oop detection 356 // Add the field for both, fastdebug and debug, builds to keep 357 // Thread's fields layout the same. 358 // Note: CHECK_UNHANDLED_OOPS is defined only for fastdebug build. 359 #ifdef CHECK_UNHANDLED_OOPS 360 private: 361 UnhandledOops* _unhandled_oops; 362 #elif defined(ASSERT) 363 private: 364 void* _unhandled_oops; 365 #endif 366 #ifdef CHECK_UNHANDLED_OOPS 367 public: 368 UnhandledOops* unhandled_oops() { return _unhandled_oops; } 369 // Mark oop safe for gc. It may be stack allocated but won't move. 370 void allow_unhandled_oop(oop *op) { 371 if (CheckUnhandledOops) unhandled_oops()->allow_unhandled_oop(op); 372 } 373 // Clear oops at safepoint so crashes point to unhandled oop violator 374 void clear_unhandled_oops() { 375 if (CheckUnhandledOops) unhandled_oops()->clear_unhandled_oops(); 376 } 377 #endif // CHECK_UNHANDLED_OOPS 378 379 public: 380 #ifndef PRODUCT 381 bool skip_gcalot() { return _skip_gcalot; } 382 void set_skip_gcalot(bool v) { _skip_gcalot = v; } 383 #endif 384 385 // Resource area 386 ResourceArea* resource_area() const { return _resource_area; } 387 void set_resource_area(ResourceArea* area) { _resource_area = area; } 388 389 OSThread* osthread() const { return _osthread; } 390 void set_osthread(OSThread* thread) { _osthread = thread; } 391 392 // Internal handle support 393 HandleArea* handle_area() const { return _handle_area; } 394 void set_handle_area(HandleArea* area) { _handle_area = area; } 395 396 GrowableArray<Metadata*>* metadata_handles() const { return _metadata_handles; } 397 void set_metadata_handles(GrowableArray<Metadata*>* handles){ _metadata_handles = handles; } 398 399 // Thread-Local Allocation Buffer (TLAB) support 400 ThreadLocalAllocBuffer& tlab() { return _tlab; } 401 void initialize_tlab(); 402 403 jlong allocated_bytes() { return _allocated_bytes; } 404 void set_allocated_bytes(jlong value) { _allocated_bytes = value; } 405 void incr_allocated_bytes(jlong size) { _allocated_bytes += size; } 406 inline jlong cooked_allocated_bytes(); 407 408 ThreadHeapSampler& heap_sampler() { return _heap_sampler; } 409 410 ThreadStatisticalInfo& statistical_info() { return _statistical_info; } 411 412 JFR_ONLY(DEFINE_THREAD_LOCAL_ACCESSOR_JFR;) 413 414 // For tracking the Jvmti raw monitor the thread is pending on. 415 JvmtiRawMonitor* current_pending_raw_monitor() { 416 return _current_pending_raw_monitor; 417 } 418 void set_current_pending_raw_monitor(JvmtiRawMonitor* monitor) { 419 _current_pending_raw_monitor = monitor; 420 } 421 422 // GC support 423 // Apply "f->do_oop" to all root oops in "this". 424 // Used by JavaThread::oops_do. 425 // Apply "cf->do_nmethod" (if !nullptr) to all nmethods active in frames 426 virtual void oops_do_no_frames(OopClosure* f, NMethodClosure* cf); 427 virtual void oops_do_frames(OopClosure* f, NMethodClosure* cf) {} 428 void oops_do(OopClosure* f, NMethodClosure* cf); 429 430 // Handles the parallel case for claim_threads_do. 431 private: 432 bool claim_par_threads_do(uintx claim_token); 433 public: 434 // Requires that "claim_token" is that of the current iteration. 435 // If "is_par" is false, sets the token of "this" to 436 // "claim_token", and returns "true". If "is_par" is true, 437 // uses an atomic instruction to set the current thread's token to 438 // "claim_token", if it is not already. Returns "true" iff the 439 // calling thread does the update, this indicates that the calling thread 440 // has claimed the thread in the current iteration. 441 bool claim_threads_do(bool is_par, uintx claim_token) { 442 if (!is_par) { 443 _threads_do_token = claim_token; 444 return true; 445 } else { 446 return claim_par_threads_do(claim_token); 447 } 448 } 449 450 uintx threads_do_token() const { return _threads_do_token; } 451 452 // jvmtiRedefineClasses support 453 void metadata_handles_do(void f(Metadata*)); 454 455 private: 456 // Check if address is within the given range of this thread's 457 // stack: stack_base() > adr >/>= limit 458 // The check is inclusive of limit if passed true, else exclusive. 459 bool is_in_stack_range(address adr, address limit, bool inclusive) const { 460 assert(stack_base() > limit && limit >= stack_end(), "limit is outside of stack"); 461 return stack_base() > adr && (inclusive ? adr >= limit : adr > limit); 462 } 463 464 public: 465 // Check if address is within the given range of this thread's 466 // stack: stack_base() > adr >= limit 467 bool is_in_stack_range_incl(address adr, address limit) const { 468 return is_in_stack_range(adr, limit, true); 469 } 470 471 // Check if address is within the given range of this thread's 472 // stack: stack_base() > adr > limit 473 bool is_in_stack_range_excl(address adr, address limit) const { 474 return is_in_stack_range(adr, limit, false); 475 } 476 477 // Check if address is in the stack mapped to this thread. Used mainly in 478 // error reporting (so has to include guard zone) and frame printing. 479 // Expects _stack_base to be initialized - checked with assert. 480 bool is_in_full_stack_checked(address adr) const { 481 return is_in_stack_range_incl(adr, stack_end()); 482 } 483 484 // Like is_in_full_stack_checked but without the assertions as this 485 // may be called in a thread before _stack_base is initialized. 486 bool is_in_full_stack(address adr) const { 487 address stack_end = _stack_base - _stack_size; 488 return _stack_base > adr && adr >= stack_end; 489 } 490 491 // Check if address is in the live stack of this thread (not just for locks). 492 // Warning: can only be called by the current thread on itself. 493 bool is_in_live_stack(address adr) const { 494 assert(Thread::current() == this, "is_in_live_stack can only be called from current thread"); 495 return is_in_stack_range_incl(adr, os::current_stack_pointer()); 496 } 497 498 // Sets this thread as starting thread. Returns failure if thread 499 // creation fails due to lack of memory, too many threads etc. 500 bool set_as_starting_thread(); 501 502 protected: 503 // OS data associated with the thread 504 OSThread* _osthread; // Platform-specific thread information 505 506 // Thread local resource area for temporary allocation within the VM 507 ResourceArea* _resource_area; 508 509 DEBUG_ONLY(ResourceMark* _current_resource_mark;) 510 511 // Thread local handle area for allocation of handles within the VM 512 HandleArea* _handle_area; 513 GrowableArray<Metadata*>* _metadata_handles; 514 515 // Support for stack overflow handling, get_thread, etc. 516 address _stack_base; 517 size_t _stack_size; 518 int _lgrp_id; 519 520 public: 521 // Stack overflow support 522 address stack_base() const DEBUG_ONLY(;) NOT_DEBUG({ return _stack_base; }) 523 void set_stack_base(address base) { _stack_base = base; } 524 size_t stack_size() const { return _stack_size; } 525 void set_stack_size(size_t size) { _stack_size = size; } 526 address stack_end() const { return stack_base() - stack_size(); } 527 void record_stack_base_and_size(); 528 void register_thread_stack_with_NMT(); 529 void unregister_thread_stack_with_NMT(); 530 531 int lgrp_id() const { return _lgrp_id; } 532 void set_lgrp_id(int value) { _lgrp_id = value; } 533 534 // Printing 535 void print_on(outputStream* st, bool print_extended_info) const; 536 virtual void print_on(outputStream* st) const { print_on(st, false); } 537 void print() const; 538 virtual void print_on_error(outputStream* st, char* buf, int buflen) const; 539 // Basic, non-virtual, printing support that is simple and always safe. 540 void print_value_on(outputStream* st) const; 541 542 // Debug-only code 543 #ifdef ASSERT 544 private: 545 // Deadlock detection support for Mutex locks. List of locks own by thread. 546 Mutex* _owned_locks; 547 // Mutex::set_owner_implementation is the only place where _owned_locks is modified, 548 // thus the friendship 549 friend class Mutex; 550 friend class Monitor; 551 552 public: 553 void print_owned_locks_on(outputStream* st) const; 554 void print_owned_locks() const { print_owned_locks_on(tty); } 555 Mutex* owned_locks() const { return _owned_locks; } 556 bool owns_locks() const { return owned_locks() != nullptr; } 557 558 // Deadlock detection 559 ResourceMark* current_resource_mark() { return _current_resource_mark; } 560 void set_current_resource_mark(ResourceMark* rm) { _current_resource_mark = rm; } 561 #endif // ASSERT 562 563 private: 564 volatile int _jvmti_env_iteration_count; 565 566 public: 567 void entering_jvmti_env_iteration() { ++_jvmti_env_iteration_count; } 568 void leaving_jvmti_env_iteration() { --_jvmti_env_iteration_count; } 569 bool is_inside_jvmti_env_iteration() { return _jvmti_env_iteration_count > 0; } 570 571 // Code generation 572 static ByteSize exception_file_offset() { return byte_offset_of(Thread, _exception_file); } 573 static ByteSize exception_line_offset() { return byte_offset_of(Thread, _exception_line); } 574 575 static ByteSize stack_base_offset() { return byte_offset_of(Thread, _stack_base); } 576 static ByteSize stack_size_offset() { return byte_offset_of(Thread, _stack_size); } 577 578 static ByteSize tlab_start_offset() { return byte_offset_of(Thread, _tlab) + ThreadLocalAllocBuffer::start_offset(); } 579 static ByteSize tlab_end_offset() { return byte_offset_of(Thread, _tlab) + ThreadLocalAllocBuffer::end_offset(); } 580 static ByteSize tlab_top_offset() { return byte_offset_of(Thread, _tlab) + ThreadLocalAllocBuffer::top_offset(); } 581 static ByteSize tlab_pf_top_offset() { return byte_offset_of(Thread, _tlab) + ThreadLocalAllocBuffer::pf_top_offset(); } 582 583 JFR_ONLY(DEFINE_THREAD_LOCAL_OFFSET_JFR;) 584 585 public: 586 ParkEvent * volatile _ParkEvent; // for Object monitors, JVMTI raw monitors, 587 // and ObjectSynchronizer::read_stable_mark 588 589 // Termination indicator used by the signal handler. 590 // _ParkEvent is just a convenient field we can null out after setting the JavaThread termination state 591 // (which can't itself be read from the signal handler if a signal hits during the Thread destructor). 592 bool has_terminated() { return Atomic::load(&_ParkEvent) == nullptr; }; 593 594 jint _hashStateW; // Marsaglia Shift-XOR thread-local RNG 595 jint _hashStateX; // thread-specific hashCode generator state 596 jint _hashStateY; 597 jint _hashStateZ; 598 599 // Low-level leaf-lock primitives used to implement synchronization. 600 // Not for general synchronization use. 601 static void SpinAcquire(volatile int * Lock, const char * Name); 602 static void SpinRelease(volatile int * Lock); 603 604 #if defined(__APPLE__) && defined(AARCH64) 605 private: 606 DEBUG_ONLY(bool _wx_init); 607 WXMode _wx_state; 608 public: 609 void init_wx(); 610 WXMode enable_wx(WXMode new_state); 611 612 void assert_wx_state(WXMode expected) { 613 assert(_wx_state == expected, "wrong state"); 614 } 615 #endif // __APPLE__ && AARCH64 616 617 private: 618 bool _in_asgct = false; 619 public: 620 bool in_asgct() const { return _in_asgct; } 621 void set_in_asgct(bool value) { _in_asgct = value; } 622 static bool current_in_asgct() { 623 Thread *cur = Thread::current_or_null_safe(); 624 return cur != nullptr && cur->in_asgct(); 625 } 626 627 private: 628 VMErrorCallback* _vm_error_callbacks; 629 630 bool _profile_vm_locks; 631 bool _profile_vm_calls; 632 bool _profile_vm_ops; 633 bool _profile_rt_calls; 634 bool _profile_upcalls; 635 636 jlong _all_bc_counter_value; 637 jlong _clinit_bc_counter_value; 638 639 PerfTraceTime* _current_rt_call_timer; 640 public: 641 bool profile_vm_locks() const { return _profile_vm_locks; } 642 void set_profile_vm_locks(bool v) { _profile_vm_locks = v; } 643 644 bool profile_vm_calls() const { return _profile_vm_calls; } 645 void set_profile_vm_calls(bool v) { _profile_vm_calls = v; } 646 647 bool profile_vm_ops() const { return _profile_vm_ops; } 648 void set_profile_vm_ops(bool v) { _profile_vm_ops = v; } 649 650 bool profile_rt_calls() const { return _profile_rt_calls; } 651 void set_profile_rt_calls(bool v) { _profile_rt_calls = v; } 652 653 bool profile_upcalls() const { return _profile_upcalls; } 654 void set_profile_upcalls(bool v) { _profile_upcalls = v; } 655 656 PerfTraceTime* current_rt_call_timer() const { return _current_rt_call_timer; } 657 void set_current_rt_call_timer(PerfTraceTime* c) { _current_rt_call_timer = c; } 658 bool has_current_rt_call_timer() const { return _current_rt_call_timer != nullptr; } 659 660 bool do_profile_rt_call() const { 661 return ProfileRuntimeCalls && profile_rt_calls() && !has_current_rt_call_timer(); 662 } 663 664 jlong bc_counter_value() const { return _all_bc_counter_value; } 665 666 jlong clinit_bc_counter_value() const { return _clinit_bc_counter_value; } 667 668 void inc_clinit_bc_counter_value(jlong l) { _clinit_bc_counter_value += l; } 669 670 static ByteSize bc_counter_offset() { return byte_offset_of(Thread, _all_bc_counter_value); } 671 }; 672 673 class ProfileVMCallContext : StackObj { 674 private: 675 Thread* _thread; 676 bool _enabled; 677 PerfTraceTime* _timer; 678 679 static int _perf_nested_runtime_calls_count; 680 681 static const char* name(PerfTraceTime* t); 682 static void notify_nested_rt_call(PerfTraceTime* current, PerfTraceTime* inner_timer); 683 public: 684 inline ProfileVMCallContext(Thread* current, PerfTraceTime* timer, bool is_on) 685 : _thread(current), _enabled(is_on), _timer(timer) { 686 if (_enabled) { 687 assert(timer != nullptr, ""); 688 assert(_thread->current_rt_call_timer() == nullptr, "%s", name(_thread->current_rt_call_timer())); 689 _thread->set_current_rt_call_timer(timer); 690 } else if (current->profile_rt_calls()) { 691 notify_nested_rt_call(current->current_rt_call_timer(), timer); 692 } 693 } 694 695 inline ~ProfileVMCallContext() { 696 if (_enabled) { 697 assert(_timer == _thread->current_rt_call_timer(), 698 "%s vs %s", name(_timer), name(_thread->current_rt_call_timer())); 699 _thread->set_current_rt_call_timer(nullptr); 700 } 701 } 702 703 static int nested_runtime_calls_count() { return _perf_nested_runtime_calls_count; }; 704 }; 705 706 class PauseRuntimeCallProfiling : public StackObj { 707 protected: 708 Thread* _thread; 709 bool _enabled; 710 PerfTraceTime* _timer; 711 712 public: 713 inline PauseRuntimeCallProfiling(Thread* current, bool is_on) 714 : _thread(current), _enabled(is_on), _timer(nullptr) { 715 if (_enabled) { 716 _timer = _thread->current_rt_call_timer(); 717 _thread->set_current_rt_call_timer(nullptr); 718 } 719 } 720 721 inline ~PauseRuntimeCallProfiling () { 722 if (_enabled) { 723 guarantee(_thread->current_rt_call_timer() == nullptr, ""); 724 _thread->set_current_rt_call_timer(_timer); // restore 725 } 726 } 727 }; 728 729 class ThreadInAsgct { 730 private: 731 Thread* _thread; 732 bool _saved_in_asgct; 733 public: 734 ThreadInAsgct(Thread* thread) : _thread(thread) { 735 assert(thread != nullptr, "invariant"); 736 // Allow AsyncGetCallTrace to be reentrant - save the previous state. 737 _saved_in_asgct = thread->in_asgct(); 738 thread->set_in_asgct(true); 739 } 740 ~ThreadInAsgct() { 741 assert(_thread->in_asgct(), "invariant"); 742 _thread->set_in_asgct(_saved_in_asgct); 743 } 744 }; 745 746 // Inline implementation of Thread::current() 747 inline Thread* Thread::current() { 748 Thread* current = current_or_null(); 749 assert(current != nullptr, "Thread::current() called on detached thread"); 750 return current; 751 } 752 753 inline Thread* Thread::current_or_null() { 754 #ifndef USE_LIBRARY_BASED_TLS_ONLY 755 return _thr_current; 756 #else 757 if (ThreadLocalStorage::is_initialized()) { 758 return ThreadLocalStorage::thread(); 759 } 760 return nullptr; 761 #endif 762 } 763 764 inline Thread* Thread::current_or_null_safe() { 765 if (ThreadLocalStorage::is_initialized()) { 766 return ThreadLocalStorage::thread(); 767 } 768 return nullptr; 769 } 770 771 #endif // SHARE_RUNTIME_THREAD_HPP