< prev index next >

src/hotspot/share/runtime/thread.hpp

Print this page
rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed
rev 47865 : dholmes CR: Fix indents, trailing spaces and various typos. Add descriptions for the '_cnt', '_max' and '_times" fields, add impl notes to document the type choices.
rev 47866 : robinw CR: Fix some inefficient code, update some comments, fix some indents, and add some 'const' specifiers.


  40 #include "runtime/park.hpp"
  41 #include "runtime/safepoint.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "runtime/threadLocalStorage.hpp"
  44 #include "runtime/thread_ext.hpp"
  45 #include "runtime/unhandledOops.hpp"
  46 #include "trace/traceBackend.hpp"
  47 #include "trace/traceMacros.hpp"
  48 #include "utilities/align.hpp"
  49 #include "utilities/exceptions.hpp"
  50 #include "utilities/macros.hpp"
  51 #if INCLUDE_ALL_GCS
  52 #include "gc/g1/dirtyCardQueue.hpp"
  53 #include "gc/g1/satbMarkQueue.hpp"
  54 #endif // INCLUDE_ALL_GCS
  55 #ifdef ZERO
  56 # include "stack_zero.hpp"
  57 #endif
  58 
  59 class ThreadSafepointState;


  60 
  61 class JvmtiThreadState;
  62 class JvmtiGetLoadedClassesClosure;
  63 class ThreadStatistics;
  64 class ConcurrentLocksDump;
  65 class ParkEvent;
  66 class Parker;
  67 
  68 class ciEnv;
  69 class CompileThread;
  70 class CompileLog;
  71 class CompileTask;
  72 class CompileQueue;
  73 class CompilerCounters;
  74 class vframeArray;
  75 
  76 class DeoptResourceMark;
  77 class jvmtiDeferredLocalVariableSet;
  78 
  79 class GCTaskQueue;


  84 template <class T, MEMFLAGS F> class ChunkedList;
  85 typedef ChunkedList<Metadata*, mtInternal> MetadataOnStackBuffer;
  86 
  87 DEBUG_ONLY(class ResourceMark;)
  88 
  89 class WorkerThread;
  90 
  91 // Class hierarchy
  92 // - Thread
  93 //   - NamedThread
  94 //     - VMThread
  95 //     - ConcurrentGCThread
  96 //     - WorkerThread
  97 //       - GangWorker
  98 //       - GCTaskThread
  99 //   - JavaThread
 100 //     - various subclasses eg CompilerThread, ServiceThread
 101 //   - WatcherThread
 102 
 103 class Thread: public ThreadShadow {

 104   friend class VMStructs;
 105   friend class JVMCIVMStructs;
 106  private:
 107 
 108 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 109   // Current thread is maintained as a thread-local variable
 110   static THREAD_LOCAL_DECL Thread* _thr_current;
 111 #endif
 112 
 113   // Exception handling
 114   // (Note: _pending_exception and friends are in ThreadShadow)
 115   //oop       _pending_exception;                // pending exception for current thread
 116   // const char* _exception_file;                   // file information for exception (debugging only)
 117   // int         _exception_line;                   // line information for exception (debugging only)
 118  protected:
 119   // Support for forcing alignment of thread objects for biased locking
 120   void*       _real_malloc_address;









































 121  public:
 122   void* operator new(size_t size) throw() { return allocate(size, true); }
 123   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
 124     return allocate(size, false); }
 125   void  operator delete(void* p);
 126 
 127  protected:
 128   static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
 129  private:
 130 
 131   // ***************************************************************
 132   // Suspend and resume support
 133   // ***************************************************************
 134   //
 135   // VM suspend/resume no longer exists - it was once used for various
 136   // things including safepoints but was deprecated and finally removed
 137   // in Java 7. Because VM suspension was considered "internal" Java-level
 138   // suspension was considered "external", and this legacy naming scheme
 139   // remains.
 140   //


 342   virtual bool is_Named_thread() const               { return false; }
 343   virtual bool is_Worker_thread() const              { return false; }
 344 
 345   // Can this thread make Java upcalls
 346   virtual bool can_call_java() const                 { return false; }
 347 
 348   // Casts
 349   virtual WorkerThread* as_Worker_thread() const     { return NULL; }
 350 
 351   virtual char* name() const { return (char*)"Unknown thread"; }
 352 
 353   // Returns the current thread (ASSERTS if NULL)
 354   static inline Thread* current();
 355   // Returns the current thread, or NULL if not attached
 356   static inline Thread* current_or_null();
 357   // Returns the current thread, or NULL if not attached, and is
 358   // safe for use from signal-handlers
 359   static inline Thread* current_or_null_safe();
 360 
 361   // Common thread operations



 362   static void set_priority(Thread* thread, ThreadPriority priority);
 363   static ThreadPriority get_priority(const Thread* const thread);
 364   static void start(Thread* thread);
 365   static void interrupt(Thread* thr);
 366   static bool is_interrupted(Thread* thr, bool clear_interrupted);
 367 
 368   void set_native_thread_name(const char *name) {
 369     assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
 370     os::set_native_thread_name(name);
 371   }
 372 
 373   ObjectMonitor** omInUseList_addr()             { return (ObjectMonitor **)&omInUseList; }
 374   Monitor* SR_lock() const                       { return _SR_lock; }
 375 
 376   bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
 377 
 378   inline void set_suspend_flag(SuspendFlags f);
 379   inline void clear_suspend_flag(SuspendFlags f);
 380 
 381   inline void set_has_async_exception();


 559   address stack_base() const           { assert(_stack_base != NULL,"Sanity check"); return _stack_base; }
 560   void    set_stack_base(address base) { _stack_base = base; }
 561   size_t  stack_size() const           { return _stack_size; }
 562   void    set_stack_size(size_t size)  { _stack_size = size; }
 563   address stack_end()  const           { return stack_base() - stack_size(); }
 564   void    record_stack_base_and_size();
 565 
 566   bool    on_local_stack(address adr) const {
 567     // QQQ this has knowledge of direction, ought to be a stack method
 568     return (_stack_base >= adr && adr >= stack_end());
 569   }
 570 
 571   uintptr_t self_raw_id()                    { return _self_raw_id; }
 572   void      set_self_raw_id(uintptr_t value) { _self_raw_id = value; }
 573 
 574   int     lgrp_id() const        { return _lgrp_id; }
 575   void    set_lgrp_id(int value) { _lgrp_id = value; }
 576 
 577   // Printing
 578   virtual void print_on(outputStream* st) const;

 579   void print() const { print_on(tty); }
 580   virtual void print_on_error(outputStream* st, char* buf, int buflen) const;
 581   void print_value_on(outputStream* st) const;
 582 
 583   // Debug-only code
 584 #ifdef ASSERT
 585  private:
 586   // Deadlock detection support for Mutex locks. List of locks own by thread.
 587   Monitor* _owned_locks;
 588   // Mutex::set_owner_implementation is the only place where _owned_locks is modified,
 589   // thus the friendship
 590   friend class Mutex;
 591   friend class Monitor;
 592 
 593  public:
 594   void print_owned_locks_on(outputStream* st) const;
 595   void print_owned_locks() const                 { print_owned_locks_on(tty);    }
 596   Monitor* owned_locks() const                   { return _owned_locks;          }
 597   bool owns_locks() const                        { return owned_locks() != NULL; }
 598   bool owns_locks_but_compiled_lock() const;


 781   static void start();
 782   static void stop();
 783   // Only allow start once the VM is sufficiently initialized
 784   // Otherwise the first task to enroll will trigger the start
 785   static void make_startable();
 786  private:
 787   int sleep() const;
 788 };
 789 
 790 
 791 class CompilerThread;
 792 
 793 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
 794 
 795 class JavaThread: public Thread {
 796   friend class VMStructs;
 797   friend class JVMCIVMStructs;
 798   friend class WhiteBox;
 799  private:
 800   JavaThread*    _next;                          // The next thread in the Threads list

 801   oop            _threadObj;                     // The Java level thread object
 802 
 803 #ifdef ASSERT
 804  private:
 805   int _java_call_counter;
 806 
 807  public:
 808   int  java_call_counter()                       { return _java_call_counter; }
 809   void inc_java_call_counter()                   { _java_call_counter++; }
 810   void dec_java_call_counter() {
 811     assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
 812     _java_call_counter--;
 813   }
 814  private:  // restore original namespace restriction
 815 #endif  // ifdef ASSERT
 816 
 817 #ifndef PRODUCT
 818  public:
 819   enum {
 820     jump_ring_buffer_size = 16


1108   intptr_t* last_Java_sp() const                 { return _anchor.last_Java_sp(); }
1109 
1110   // last_Java_pc
1111 
1112   address last_Java_pc(void)                     { return _anchor.last_Java_pc(); }
1113 
1114   // Safepoint support
1115 #if !(defined(PPC64) || defined(AARCH64))
1116   JavaThreadState thread_state() const           { return _thread_state; }
1117   void set_thread_state(JavaThreadState s)       { _thread_state = s;    }
1118 #else
1119   // Use membars when accessing volatile _thread_state. See
1120   // Threads::create_vm() for size checks.
1121   inline JavaThreadState thread_state() const;
1122   inline void set_thread_state(JavaThreadState s);
1123 #endif
1124   ThreadSafepointState *safepoint_state() const  { return _safepoint_state; }
1125   void set_safepoint_state(ThreadSafepointState *state) { _safepoint_state = state; }
1126   bool is_at_poll_safepoint()                    { return _safepoint_state->is_at_poll_safepoint(); }
1127 





1128   // thread has called JavaThread::exit() or is terminated
1129   bool is_exiting()                              { return _terminated == _thread_exiting || is_terminated(); }
1130   // thread is terminated (no longer on the threads list); we compare
1131   // against the two non-terminated values so that a freed JavaThread
1132   // will also be considered terminated.
1133   bool is_terminated()                           { return _terminated != _not_terminated && _terminated != _thread_exiting; }
1134   void set_terminated(TerminatedTypes t)         { _terminated = t; }



1135   // special for Threads::remove() which is static:
1136   void set_terminated_value()                    { _terminated = _thread_terminated; }
1137   void block_if_vm_exited();
1138 
1139   bool doing_unsafe_access()                     { return _doing_unsafe_access; }
1140   void set_doing_unsafe_access(bool val)         { _doing_unsafe_access = val; }
1141 
1142   bool do_not_unlock_if_synchronized()             { return _do_not_unlock_if_synchronized; }
1143   void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1144 
1145   inline void set_polling_page(void* poll_value);
1146   inline volatile void* get_polling_page();
1147 
1148  private:
1149   // Support for thread handshake operations
1150   HandshakeState _handshake;
1151  public:
1152   void set_handshake_operation(HandshakeOperation* op) {
1153     _handshake.set_operation(this, op);
1154   }
1155 
1156   bool has_handshake() const {


1203   static void check_special_condition_for_native_trans_and_transition(JavaThread *thread);
1204 
1205   bool is_ext_suspend_completed(bool called_by_wait, int delay, uint32_t *bits);
1206   bool is_ext_suspend_completed_with_lock(uint32_t *bits) {
1207     MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1208     // Warning: is_ext_suspend_completed() may temporarily drop the
1209     // SR_lock to allow the thread to reach a stable thread state if
1210     // it is currently in a transient thread state.
1211     return is_ext_suspend_completed(false /* !called_by_wait */,
1212                                     SuspendRetryDelay, bits);
1213   }
1214 
1215   // We cannot allow wait_for_ext_suspend_completion() to run forever or
1216   // we could hang. SuspendRetryCount and SuspendRetryDelay are normally
1217   // passed as the count and delay parameters. Experiments with specific
1218   // calls to wait_for_ext_suspend_completion() can be done by passing
1219   // other values in the code. Experiments with all calls can be done
1220   // via the appropriate -XX options.
1221   bool wait_for_ext_suspend_completion(int count, int delay, uint32_t *bits);
1222 



1223   inline void set_external_suspend();
1224   inline void clear_external_suspend();
1225 
1226   inline void set_deopt_suspend();
1227   inline void clear_deopt_suspend();
1228   bool is_deopt_suspend()         { return (_suspend_flags & _deopt_suspend) != 0; }
1229 
1230   bool is_external_suspend() const {
1231     return (_suspend_flags & _external_suspend) != 0;
1232   }
1233   // Whenever a thread transitions from native to vm/java it must suspend
1234   // if external|deopt suspend is present.
1235   bool is_suspend_after_native() const {
1236     return (_suspend_flags & (_external_suspend | _deopt_suspend)) != 0;
1237   }
1238 
1239   // external suspend request is completed
1240   bool is_ext_suspended() const {
1241     return (_suspend_flags & _ext_suspended) != 0;
1242   }


2049   IdealGraphPrinter *_ideal_graph_printer;
2050  public:
2051   IdealGraphPrinter *ideal_graph_printer()           { return _ideal_graph_printer; }
2052   void set_ideal_graph_printer(IdealGraphPrinter *n) { _ideal_graph_printer = n; }
2053 #endif
2054 
2055   // Get/set the thread's current task
2056   CompileTask* task()                      { return _task; }
2057   void         set_task(CompileTask* task) { _task = task; }
2058 };
2059 
2060 inline CompilerThread* CompilerThread::current() {
2061   return JavaThread::current()->as_CompilerThread();
2062 }
2063 
2064 // The active thread queue. It also keeps track of the current used
2065 // thread priorities.
2066 class Threads: AllStatic {
2067   friend class VMStructs;
2068  private:







































2069   static JavaThread* _thread_list;
2070   static int         _number_of_threads;
2071   static int         _number_of_non_daemon_threads;
2072   static int         _return_code;
2073   static int         _thread_claim_parity;
2074 #ifdef ASSERT
2075   static bool        _vm_complete;
2076 #endif
2077 
2078   static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2079   static void initialize_jsr292_core_classes(TRAPS);



2080  public:
2081   // Thread management
2082   // force_daemon is a concession to JNI, where we may need to add a
2083   // thread to the thread list before allocating its thread object
2084   static void add(JavaThread* p, bool force_daemon = false);
2085   static void remove(JavaThread* p);
2086   static bool includes(JavaThread* p);
2087   static JavaThread* first()                     { return _thread_list; }
2088   static void threads_do(ThreadClosure* tc);
2089   static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2090 
































2091   // Initializes the vm and creates the vm thread
2092   static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2093   static void convert_vm_init_libraries_to_agents();
2094   static void create_vm_init_libraries();
2095   static void create_vm_init_agents();
2096   static void shutdown_vm_agents();
2097   static bool destroy_vm();
2098   // Supported VM versions via JNI
2099   // Includes JNI_VERSION_1_1
2100   static jboolean is_supported_jni_version_including_1_1(jint version);
2101   // Does not include JNI_VERSION_1_1
2102   static jboolean is_supported_jni_version(jint version);
2103 
2104   // The "thread claim parity" provides a way for threads to be claimed
2105   // by parallel worker tasks.
2106   //
2107   // Each thread contains a a "parity" field. A task will claim the
2108   // thread only if its parity field is the same as the global parity,
2109   // which is updated by calling change_thread_claim_parity().
2110   //


2131   // Apply "f->do_oop" to roots in all threads that
2132   // are part of compiled frames
2133   static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2134 
2135   static void convert_hcode_pointers();
2136   static void restore_hcode_pointers();
2137 
2138   // Sweeper
2139   static void nmethods_do(CodeBlobClosure* cf);
2140 
2141   // RedefineClasses support
2142   static void metadata_do(void f(Metadata*));
2143   static void metadata_handles_do(void f(Metadata*));
2144 
2145 #ifdef ASSERT
2146   static bool is_vm_complete() { return _vm_complete; }
2147 #endif
2148 
2149   // Verification
2150   static void verify();

2151   static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);


2152   static void print(bool print_stacks, bool internal_format) {
2153     // this function is only used by debug.cpp
2154     print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2155   }
2156   static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2157   static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2158                              int buflen, bool* found_current);
2159   static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2160 
2161   // Get Java threads that are waiting to enter a monitor. If doLock
2162   // is true, then Threads_lock is grabbed as needed. Otherwise, the
2163   // VM needs to be at a safepoint.
2164   static GrowableArray<JavaThread*>* get_pending_threads(int count,
2165                                                          address monitor, bool doLock);
2166 
2167   // Get owning Java thread from the monitor's owner field. If doLock
2168   // is true, then Threads_lock is grabbed as needed. Otherwise, the
2169   // VM needs to be at a safepoint.
2170   static JavaThread *owning_thread_from_monitor_owner(address owner,
2171                                                       bool doLock);
2172 
2173   // Number of threads on the active threads list
2174   static int number_of_threads()                 { return _number_of_threads; }
2175   // Number of non-daemon threads on the active threads list
2176   static int number_of_non_daemon_threads()      { return _number_of_non_daemon_threads; }
2177 
2178   // Deoptimizes all frames tied to marked nmethods
2179   static void deoptimized_wrt_marked_nmethods();
2180 
2181   static JavaThread* find_java_thread_from_java_tid(jlong java_tid);
2182 
2183 };
2184 
2185 
2186 // Thread iterator
2187 class ThreadClosure: public StackObj {
2188  public:
2189   virtual void do_thread(Thread* thread) = 0;
2190 };
2191 
2192 class SignalHandlerMark: public StackObj {
2193  private:
2194   Thread* _thread;
2195  public:
2196   SignalHandlerMark(Thread* t) {
2197     _thread = t;
2198     if (_thread) _thread->enter_signal_handler();
2199   }
2200   ~SignalHandlerMark() {
2201     if (_thread) _thread->leave_signal_handler();
2202     _thread = NULL;


  40 #include "runtime/park.hpp"
  41 #include "runtime/safepoint.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "runtime/threadLocalStorage.hpp"
  44 #include "runtime/thread_ext.hpp"
  45 #include "runtime/unhandledOops.hpp"
  46 #include "trace/traceBackend.hpp"
  47 #include "trace/traceMacros.hpp"
  48 #include "utilities/align.hpp"
  49 #include "utilities/exceptions.hpp"
  50 #include "utilities/macros.hpp"
  51 #if INCLUDE_ALL_GCS
  52 #include "gc/g1/dirtyCardQueue.hpp"
  53 #include "gc/g1/satbMarkQueue.hpp"
  54 #endif // INCLUDE_ALL_GCS
  55 #ifdef ZERO
  56 # include "stack_zero.hpp"
  57 #endif
  58 
  59 class ThreadSafepointState;
  60 class ThreadsList;
  61 class NestedThreadsList;
  62 
  63 class JvmtiThreadState;
  64 class JvmtiGetLoadedClassesClosure;
  65 class ThreadStatistics;
  66 class ConcurrentLocksDump;
  67 class ParkEvent;
  68 class Parker;
  69 
  70 class ciEnv;
  71 class CompileThread;
  72 class CompileLog;
  73 class CompileTask;
  74 class CompileQueue;
  75 class CompilerCounters;
  76 class vframeArray;
  77 
  78 class DeoptResourceMark;
  79 class jvmtiDeferredLocalVariableSet;
  80 
  81 class GCTaskQueue;


  86 template <class T, MEMFLAGS F> class ChunkedList;
  87 typedef ChunkedList<Metadata*, mtInternal> MetadataOnStackBuffer;
  88 
  89 DEBUG_ONLY(class ResourceMark;)
  90 
  91 class WorkerThread;
  92 
  93 // Class hierarchy
  94 // - Thread
  95 //   - NamedThread
  96 //     - VMThread
  97 //     - ConcurrentGCThread
  98 //     - WorkerThread
  99 //       - GangWorker
 100 //       - GCTaskThread
 101 //   - JavaThread
 102 //     - various subclasses eg CompilerThread, ServiceThread
 103 //   - WatcherThread
 104 
 105 class Thread: public ThreadShadow {
 106   friend class Threads;
 107   friend class VMStructs;
 108   friend class JVMCIVMStructs;
 109  private:
 110 
 111 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 112   // Current thread is maintained as a thread-local variable
 113   static THREAD_LOCAL_DECL Thread* _thr_current;
 114 #endif
 115 
 116   // Exception handling
 117   // (Note: _pending_exception and friends are in ThreadShadow)
 118   //oop       _pending_exception;                // pending exception for current thread
 119   // const char* _exception_file;                   // file information for exception (debugging only)
 120   // int         _exception_line;                   // line information for exception (debugging only)
 121  protected:
 122   // Support for forcing alignment of thread objects for biased locking
 123   void*       _real_malloc_address;
 124   // JavaThread lifecycle support:
 125   friend class ScanHazardPtrGatherProtectedThreadsClosure;
 126   friend class ScanHazardPtrGatherThreadsListClosure;
 127   friend class ScanHazardPtrPrintMatchingThreadsClosure;
 128   friend class ThreadsListHandle;
 129   friend class ThreadsListSetter;
 130   ThreadsList* volatile _threads_hazard_ptr;
 131   ThreadsList*          cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value);
 132   ThreadsList*          get_threads_hazard_ptr();
 133   void                  set_threads_hazard_ptr(ThreadsList* new_list);
 134   static bool           is_hazard_ptr_tagged(ThreadsList* list) {
 135     return (intptr_t(list) & intptr_t(1)) == intptr_t(1);
 136   }
 137   static ThreadsList*   tag_hazard_ptr(ThreadsList* list) {
 138     return (ThreadsList*)(intptr_t(list) | intptr_t(1));
 139   }
 140   static ThreadsList*   untag_hazard_ptr(ThreadsList* list) {
 141     return (ThreadsList*)(intptr_t(list) & ~intptr_t(1));
 142   }
 143   NestedThreadsList* _nested_threads_hazard_ptr;
 144   NestedThreadsList* get_nested_threads_hazard_ptr() {
 145     return _nested_threads_hazard_ptr;
 146   }
 147   void set_nested_threads_hazard_ptr(NestedThreadsList* value) {
 148     assert(Threads_lock->owned_by_self(),
 149            "must own Threads_lock for _nested_threads_hazard_ptr to be valid.");
 150     _nested_threads_hazard_ptr = value;
 151   }
 152   // This field is enabled via -XX:+EnableThreadSMRStatistics:
 153   uint _nested_threads_hazard_ptr_cnt;
 154   void dec_nested_threads_hazard_ptr_cnt() {
 155     assert(_nested_threads_hazard_ptr_cnt != 0, "mismatched {dec,inc}_nested_threads_hazard_ptr_cnt()");
 156     _nested_threads_hazard_ptr_cnt--;
 157   }
 158   void inc_nested_threads_hazard_ptr_cnt() {
 159     _nested_threads_hazard_ptr_cnt++;
 160   }
 161   uint nested_threads_hazard_ptr_cnt() {
 162     return _nested_threads_hazard_ptr_cnt;
 163   }
 164 
 165  public:
 166   void* operator new(size_t size) throw() { return allocate(size, true); }
 167   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
 168     return allocate(size, false); }
 169   void  operator delete(void* p);
 170 
 171  protected:
 172   static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
 173  private:
 174 
 175   // ***************************************************************
 176   // Suspend and resume support
 177   // ***************************************************************
 178   //
 179   // VM suspend/resume no longer exists - it was once used for various
 180   // things including safepoints but was deprecated and finally removed
 181   // in Java 7. Because VM suspension was considered "internal" Java-level
 182   // suspension was considered "external", and this legacy naming scheme
 183   // remains.
 184   //


 386   virtual bool is_Named_thread() const               { return false; }
 387   virtual bool is_Worker_thread() const              { return false; }
 388 
 389   // Can this thread make Java upcalls
 390   virtual bool can_call_java() const                 { return false; }
 391 
 392   // Casts
 393   virtual WorkerThread* as_Worker_thread() const     { return NULL; }
 394 
 395   virtual char* name() const { return (char*)"Unknown thread"; }
 396 
 397   // Returns the current thread (ASSERTS if NULL)
 398   static inline Thread* current();
 399   // Returns the current thread, or NULL if not attached
 400   static inline Thread* current_or_null();
 401   // Returns the current thread, or NULL if not attached, and is
 402   // safe for use from signal-handlers
 403   static inline Thread* current_or_null_safe();
 404 
 405   // Common thread operations
 406 #ifdef ASSERT
 407   static void check_for_dangling_thread_pointer(Thread *thread);
 408 #endif
 409   static void set_priority(Thread* thread, ThreadPriority priority);
 410   static ThreadPriority get_priority(const Thread* const thread);
 411   static void start(Thread* thread);
 412   static void interrupt(Thread* thr);
 413   static bool is_interrupted(Thread* thr, bool clear_interrupted);
 414 
 415   void set_native_thread_name(const char *name) {
 416     assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
 417     os::set_native_thread_name(name);
 418   }
 419 
 420   ObjectMonitor** omInUseList_addr()             { return (ObjectMonitor **)&omInUseList; }
 421   Monitor* SR_lock() const                       { return _SR_lock; }
 422 
 423   bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
 424 
 425   inline void set_suspend_flag(SuspendFlags f);
 426   inline void clear_suspend_flag(SuspendFlags f);
 427 
 428   inline void set_has_async_exception();


 606   address stack_base() const           { assert(_stack_base != NULL,"Sanity check"); return _stack_base; }
 607   void    set_stack_base(address base) { _stack_base = base; }
 608   size_t  stack_size() const           { return _stack_size; }
 609   void    set_stack_size(size_t size)  { _stack_size = size; }
 610   address stack_end()  const           { return stack_base() - stack_size(); }
 611   void    record_stack_base_and_size();
 612 
 613   bool    on_local_stack(address adr) const {
 614     // QQQ this has knowledge of direction, ought to be a stack method
 615     return (_stack_base >= adr && adr >= stack_end());
 616   }
 617 
 618   uintptr_t self_raw_id()                    { return _self_raw_id; }
 619   void      set_self_raw_id(uintptr_t value) { _self_raw_id = value; }
 620 
 621   int     lgrp_id() const        { return _lgrp_id; }
 622   void    set_lgrp_id(int value) { _lgrp_id = value; }
 623 
 624   // Printing
 625   virtual void print_on(outputStream* st) const;
 626   virtual void print_nested_threads_hazard_ptrs_on(outputStream* st) const;
 627   void print() const { print_on(tty); }
 628   virtual void print_on_error(outputStream* st, char* buf, int buflen) const;
 629   void print_value_on(outputStream* st) const;
 630 
 631   // Debug-only code
 632 #ifdef ASSERT
 633  private:
 634   // Deadlock detection support for Mutex locks. List of locks own by thread.
 635   Monitor* _owned_locks;
 636   // Mutex::set_owner_implementation is the only place where _owned_locks is modified,
 637   // thus the friendship
 638   friend class Mutex;
 639   friend class Monitor;
 640 
 641  public:
 642   void print_owned_locks_on(outputStream* st) const;
 643   void print_owned_locks() const                 { print_owned_locks_on(tty);    }
 644   Monitor* owned_locks() const                   { return _owned_locks;          }
 645   bool owns_locks() const                        { return owned_locks() != NULL; }
 646   bool owns_locks_but_compiled_lock() const;


 829   static void start();
 830   static void stop();
 831   // Only allow start once the VM is sufficiently initialized
 832   // Otherwise the first task to enroll will trigger the start
 833   static void make_startable();
 834  private:
 835   int sleep() const;
 836 };
 837 
 838 
 839 class CompilerThread;
 840 
 841 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
 842 
 843 class JavaThread: public Thread {
 844   friend class VMStructs;
 845   friend class JVMCIVMStructs;
 846   friend class WhiteBox;
 847  private:
 848   JavaThread*    _next;                          // The next thread in the Threads list
 849   bool           _on_thread_list;                // Is set when this JavaThread is added to the Threads list
 850   oop            _threadObj;                     // The Java level thread object
 851 
 852 #ifdef ASSERT
 853  private:
 854   int _java_call_counter;
 855 
 856  public:
 857   int  java_call_counter()                       { return _java_call_counter; }
 858   void inc_java_call_counter()                   { _java_call_counter++; }
 859   void dec_java_call_counter() {
 860     assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
 861     _java_call_counter--;
 862   }
 863  private:  // restore original namespace restriction
 864 #endif  // ifdef ASSERT
 865 
 866 #ifndef PRODUCT
 867  public:
 868   enum {
 869     jump_ring_buffer_size = 16


1157   intptr_t* last_Java_sp() const                 { return _anchor.last_Java_sp(); }
1158 
1159   // last_Java_pc
1160 
1161   address last_Java_pc(void)                     { return _anchor.last_Java_pc(); }
1162 
1163   // Safepoint support
1164 #if !(defined(PPC64) || defined(AARCH64))
1165   JavaThreadState thread_state() const           { return _thread_state; }
1166   void set_thread_state(JavaThreadState s)       { _thread_state = s;    }
1167 #else
1168   // Use membars when accessing volatile _thread_state. See
1169   // Threads::create_vm() for size checks.
1170   inline JavaThreadState thread_state() const;
1171   inline void set_thread_state(JavaThreadState s);
1172 #endif
1173   ThreadSafepointState *safepoint_state() const  { return _safepoint_state; }
1174   void set_safepoint_state(ThreadSafepointState *state) { _safepoint_state = state; }
1175   bool is_at_poll_safepoint()                    { return _safepoint_state->is_at_poll_safepoint(); }
1176 
1177   // JavaThread termination and lifecycle support:
1178   void smr_delete();
1179   bool on_thread_list() const { return _on_thread_list; }
1180   void set_on_thread_list() { _on_thread_list = true; }
1181 
1182   // thread has called JavaThread::exit() or is terminated
1183   bool is_exiting() const;
1184   // thread is terminated (no longer on the threads list); we compare
1185   // against the two non-terminated values so that a freed JavaThread
1186   // will also be considered terminated.
1187   bool check_is_terminated(TerminatedTypes l_terminated) const {
1188     return l_terminated != _not_terminated && l_terminated != _thread_exiting;
1189   }
1190   bool is_terminated() const;
1191   void set_terminated(TerminatedTypes t);
1192   // special for Threads::remove() which is static:
1193   void set_terminated_value();
1194   void block_if_vm_exited();
1195 
1196   bool doing_unsafe_access()                     { return _doing_unsafe_access; }
1197   void set_doing_unsafe_access(bool val)         { _doing_unsafe_access = val; }
1198 
1199   bool do_not_unlock_if_synchronized()             { return _do_not_unlock_if_synchronized; }
1200   void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1201 
1202   inline void set_polling_page(void* poll_value);
1203   inline volatile void* get_polling_page();
1204 
1205  private:
1206   // Support for thread handshake operations
1207   HandshakeState _handshake;
1208  public:
1209   void set_handshake_operation(HandshakeOperation* op) {
1210     _handshake.set_operation(this, op);
1211   }
1212 
1213   bool has_handshake() const {


1260   static void check_special_condition_for_native_trans_and_transition(JavaThread *thread);
1261 
1262   bool is_ext_suspend_completed(bool called_by_wait, int delay, uint32_t *bits);
1263   bool is_ext_suspend_completed_with_lock(uint32_t *bits) {
1264     MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1265     // Warning: is_ext_suspend_completed() may temporarily drop the
1266     // SR_lock to allow the thread to reach a stable thread state if
1267     // it is currently in a transient thread state.
1268     return is_ext_suspend_completed(false /* !called_by_wait */,
1269                                     SuspendRetryDelay, bits);
1270   }
1271 
1272   // We cannot allow wait_for_ext_suspend_completion() to run forever or
1273   // we could hang. SuspendRetryCount and SuspendRetryDelay are normally
1274   // passed as the count and delay parameters. Experiments with specific
1275   // calls to wait_for_ext_suspend_completion() can be done by passing
1276   // other values in the code. Experiments with all calls can be done
1277   // via the appropriate -XX options.
1278   bool wait_for_ext_suspend_completion(int count, int delay, uint32_t *bits);
1279 
1280   // test for suspend - most (all?) of these should go away
1281   bool is_thread_fully_suspended(bool wait_for_suspend, uint32_t *bits);
1282 
1283   inline void set_external_suspend();
1284   inline void clear_external_suspend();
1285 
1286   inline void set_deopt_suspend();
1287   inline void clear_deopt_suspend();
1288   bool is_deopt_suspend()         { return (_suspend_flags & _deopt_suspend) != 0; }
1289 
1290   bool is_external_suspend() const {
1291     return (_suspend_flags & _external_suspend) != 0;
1292   }
1293   // Whenever a thread transitions from native to vm/java it must suspend
1294   // if external|deopt suspend is present.
1295   bool is_suspend_after_native() const {
1296     return (_suspend_flags & (_external_suspend | _deopt_suspend)) != 0;
1297   }
1298 
1299   // external suspend request is completed
1300   bool is_ext_suspended() const {
1301     return (_suspend_flags & _ext_suspended) != 0;
1302   }


2109   IdealGraphPrinter *_ideal_graph_printer;
2110  public:
2111   IdealGraphPrinter *ideal_graph_printer()           { return _ideal_graph_printer; }
2112   void set_ideal_graph_printer(IdealGraphPrinter *n) { _ideal_graph_printer = n; }
2113 #endif
2114 
2115   // Get/set the thread's current task
2116   CompileTask* task()                      { return _task; }
2117   void         set_task(CompileTask* task) { _task = task; }
2118 };
2119 
2120 inline CompilerThread* CompilerThread::current() {
2121   return JavaThread::current()->as_CompilerThread();
2122 }
2123 
2124 // The active thread queue. It also keeps track of the current used
2125 // thread priorities.
2126 class Threads: AllStatic {
2127   friend class VMStructs;
2128  private:
2129   // Safe Memory Reclamation (SMR) support:
2130   static Monitor*              _smr_delete_lock;
2131   // The '_cnt', '_max' and '_times" fields are enabled via
2132   // -XX:+EnableThreadSMRStatistics:
2133                                        // # of parallel threads in _smr_delete_lock->wait().
2134   static uint                  _smr_delete_lock_wait_cnt;
2135                                        // Max # of parallel threads in _smr_delete_lock->wait().
2136   static uint                  _smr_delete_lock_wait_max;
2137                                        // Flag to indicate when an _smr_delete_lock->notify() is needed.
2138   static volatile uint         _smr_delete_notify;
2139                                        // # of threads deleted over VM lifetime.
2140   static volatile uint         _smr_deleted_thread_cnt;
2141                                        // Max time in millis to delete a thread.
2142   static volatile uint         _smr_deleted_thread_time_max;
2143                                        // Cumulative time in millis to delete threads.
2144   static volatile uint         _smr_deleted_thread_times;
2145   static ThreadsList* volatile _smr_java_thread_list;
2146   static ThreadsList*          get_smr_java_thread_list();
2147   static ThreadsList*          xchg_smr_java_thread_list(ThreadsList* new_list);
2148                                        // # of ThreadsLists allocated over VM lifetime.
2149   static uint64_t              _smr_java_thread_list_alloc_cnt;
2150                                        // # of ThreadsLists freed over VM lifetime.
2151   static uint64_t              _smr_java_thread_list_free_cnt;
2152                                        // Max size ThreadsList allocated.
2153   static uint                  _smr_java_thread_list_max;
2154                                        // Max # of nested ThreadsLists for a thread.
2155   static uint                  _smr_nested_thread_list_max;
2156                                        // # of ThreadsListHandles deleted over VM lifetime.
2157   static volatile uint         _smr_tlh_cnt;
2158                                        // Max time in millis to delete a ThreadsListHandle.
2159   static volatile uint         _smr_tlh_time_max;
2160                                        // Cumulative time in millis to delete ThreadsListHandles.
2161   static volatile uint         _smr_tlh_times;
2162   static ThreadsList*          _smr_to_delete_list;
2163                                        // # of parallel ThreadsLists on the to-delete list.
2164   static uint                  _smr_to_delete_list_cnt;
2165                                        // Max # of parallel ThreadsLists on the to-delete list.
2166   static uint                  _smr_to_delete_list_max;
2167 
2168   static JavaThread*           _thread_list;
2169   static int                   _number_of_threads;
2170   static int                   _number_of_non_daemon_threads;
2171   static int                   _return_code;
2172   static int                   _thread_claim_parity;
2173 #ifdef ASSERT
2174   static bool                  _vm_complete;
2175 #endif
2176 
2177   static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2178   static void initialize_jsr292_core_classes(TRAPS);
2179 
2180   static void smr_free_list(ThreadsList* threads);
2181 
2182  public:
2183   // Thread management
2184   // force_daemon is a concession to JNI, where we may need to add a
2185   // thread to the thread list before allocating its thread object
2186   static void add(JavaThread* p, bool force_daemon = false);
2187   static void remove(JavaThread* p);


2188   static void threads_do(ThreadClosure* tc);
2189   static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2190 
2191   // SMR support:
2192   template <class T>
2193   static void threads_do_smr(T *tc, Thread *self);
2194   static ThreadsList *acquire_stable_list(Thread *self, bool is_ThreadsListSetter);
2195   static ThreadsList *acquire_stable_list_fast_path(Thread *self);
2196   static ThreadsList *acquire_stable_list_nested_path(Thread *self);
2197   static void release_stable_list(Thread *self);
2198   static void release_stable_list_fast_path(Thread *self);
2199   static void release_stable_list_nested_path(Thread *self);
2200   static void release_stable_list_wake_up(char *log_str);
2201   static bool is_a_protected_JavaThread(JavaThread *thread);
2202   static bool is_a_protected_JavaThread_with_lock(JavaThread *thread) {
2203     MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
2204     return is_a_protected_JavaThread(thread);
2205   }
2206   static void smr_delete(JavaThread *thread);
2207   // The coordination between Threads::release_stable_list() and
2208   // Threads::smr_delete() uses the smr_delete_lock in order to
2209   // reduce the traffic on the Threads_lock.
2210   static Monitor* smr_delete_lock() { return _smr_delete_lock; }
2211   // The smr_delete_notify flag is used for proper double-check
2212   // locking in order to reduce the traffic on the smr_delete_lock.
2213   static bool smr_delete_notify();
2214   static void set_smr_delete_notify();
2215   static void clear_smr_delete_notify();
2216   static void inc_smr_deleted_thread_cnt();
2217   static void update_smr_deleted_thread_time_max(uint new_value);
2218   static void add_smr_deleted_thread_times(uint add_value);
2219   static void inc_smr_tlh_cnt();
2220   static void update_smr_tlh_time_max(uint new_value);
2221   static void add_smr_tlh_times(uint add_value);
2222 
2223   // Initializes the vm and creates the vm thread
2224   static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2225   static void convert_vm_init_libraries_to_agents();
2226   static void create_vm_init_libraries();
2227   static void create_vm_init_agents();
2228   static void shutdown_vm_agents();
2229   static bool destroy_vm();
2230   // Supported VM versions via JNI
2231   // Includes JNI_VERSION_1_1
2232   static jboolean is_supported_jni_version_including_1_1(jint version);
2233   // Does not include JNI_VERSION_1_1
2234   static jboolean is_supported_jni_version(jint version);
2235 
2236   // The "thread claim parity" provides a way for threads to be claimed
2237   // by parallel worker tasks.
2238   //
2239   // Each thread contains a a "parity" field. A task will claim the
2240   // thread only if its parity field is the same as the global parity,
2241   // which is updated by calling change_thread_claim_parity().
2242   //


2263   // Apply "f->do_oop" to roots in all threads that
2264   // are part of compiled frames
2265   static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2266 
2267   static void convert_hcode_pointers();
2268   static void restore_hcode_pointers();
2269 
2270   // Sweeper
2271   static void nmethods_do(CodeBlobClosure* cf);
2272 
2273   // RedefineClasses support
2274   static void metadata_do(void f(Metadata*));
2275   static void metadata_handles_do(void f(Metadata*));
2276 
2277 #ifdef ASSERT
2278   static bool is_vm_complete() { return _vm_complete; }
2279 #endif
2280 
2281   // Verification
2282   static void verify();
2283   static void log_smr_statistics();
2284   static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);
2285   static void print_smr_info_on(outputStream* st);
2286   static void print_smr_info_elements_on(outputStream* st, ThreadsList* t_list);
2287   static void print(bool print_stacks, bool internal_format) {
2288     // this function is only used by debug.cpp
2289     print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2290   }
2291   static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2292   static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2293                              int buflen, bool* found_current);
2294   static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2295 
2296   // Get Java threads that are waiting to enter a monitor.
2297   static GrowableArray<JavaThread*>* get_pending_threads(ThreadsList * t_list,
2298                                                          int count, address monitor);


2299 
2300   // Get owning Java thread from the monitor's owner field.
2301   static JavaThread *owning_thread_from_monitor_owner(ThreadsList * t_list,
2302                                                       address owner);


2303 
2304   // Number of threads on the active threads list
2305   static int number_of_threads()                 { return _number_of_threads; }
2306   // Number of non-daemon threads on the active threads list
2307   static int number_of_non_daemon_threads()      { return _number_of_non_daemon_threads; }
2308 
2309   // Deoptimizes all frames tied to marked nmethods
2310   static void deoptimized_wrt_marked_nmethods();



2311 };
2312 
2313 
2314 // Thread iterator
2315 class ThreadClosure: public StackObj {
2316  public:
2317   virtual void do_thread(Thread* thread) = 0;
2318 };
2319 
2320 class SignalHandlerMark: public StackObj {
2321  private:
2322   Thread* _thread;
2323  public:
2324   SignalHandlerMark(Thread* t) {
2325     _thread = t;
2326     if (_thread) _thread->enter_signal_handler();
2327   }
2328   ~SignalHandlerMark() {
2329     if (_thread) _thread->leave_signal_handler();
2330     _thread = NULL;
< prev index next >