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