< prev index next >

src/share/vm/runtime/thread.hpp

Print this page




  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 //   - WatcherThread
 101 
 102 class Thread: public ThreadShadow {
 103   friend class VMStructs;
 104  private:






 105   // Exception handling
 106   // (Note: _pending_exception and friends are in ThreadShadow)
 107   //oop       _pending_exception;                // pending exception for current thread
 108   // const char* _exception_file;                   // file information for exception (debugging only)
 109   // int         _exception_line;                   // line information for exception (debugging only)
 110  protected:
 111   // Support for forcing alignment of thread objects for biased locking
 112   void*       _real_malloc_address;
 113  public:
 114   void* operator new(size_t size) throw() { return allocate(size, true); }
 115   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
 116     return allocate(size, false); }
 117   void  operator delete(void* p);
 118 
 119  protected:
 120   static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
 121  private:
 122 
 123   // ***************************************************************
 124   // Suspend and resume support


 243  private:
 244 
 245   // debug support for checking if code does allow safepoints or not
 246   // GC points in the VM can happen because of allocation, invoking a VM operation, or blocking on
 247   // mutex, or blocking on an object synchronizer (Java locking).
 248   // If !allow_safepoint(), then an assertion failure will happen in any of the above cases
 249   // If !allow_allocation(), then an assertion failure will happen during allocation
 250   // (Hence, !allow_safepoint() => !allow_allocation()).
 251   //
 252   // The two classes No_Safepoint_Verifier and No_Allocation_Verifier are used to set these counters.
 253   //
 254   NOT_PRODUCT(int _allow_safepoint_count;)      // If 0, thread allow a safepoint to happen
 255   debug_only(int _allow_allocation_count;)     // If 0, the thread is allowed to allocate oops.
 256 
 257   // Used by SkipGCALot class.
 258   NOT_PRODUCT(bool _skip_gcalot;)               // Should we elide gc-a-lot?
 259 
 260   friend class No_Alloc_Verifier;
 261   friend class No_Safepoint_Verifier;
 262   friend class Pause_No_Safepoint_Verifier;
 263   friend class ThreadLocalStorage;
 264   friend class GC_locker;
 265 
 266   ThreadLocalAllocBuffer _tlab;                 // Thread-local eden
 267   jlong _allocated_bytes;                       // Cumulative number of bytes allocated on
 268                                                 // the Java heap
 269 
 270   TRACE_DATA _trace_data;                       // Thread-local data for tracing
 271 
 272   ThreadExt _ext;
 273 
 274   int   _vm_operation_started_count;            // VM_Operation support
 275   int   _vm_operation_completed_count;          // VM_Operation support
 276 
 277   ObjectMonitor* _current_pending_monitor;      // ObjectMonitor this thread
 278                                                 // is waiting to lock
 279   bool _current_pending_monitor_is_from_java;   // locking is from Java code
 280 
 281   // ObjectMonitor on which this thread called Object.wait()
 282   ObjectMonitor* _current_waiting_monitor;
 283 


 290   int omInUseCount;                             // length of omInUseList
 291 
 292 #ifdef ASSERT
 293  private:
 294   bool _visited_for_critical_count;
 295 
 296  public:
 297   void set_visited_for_critical_count(bool z) { _visited_for_critical_count = z; }
 298   bool was_visited_for_critical_count() const   { return _visited_for_critical_count; }
 299 #endif
 300 
 301  public:
 302   enum {
 303     is_definitely_current_thread = true
 304   };
 305 
 306   // Constructor
 307   Thread();
 308   virtual ~Thread();
 309 
 310   // initializtion
 311   void initialize_thread_local_storage();


 312 

 313   // thread entry point
 314   virtual void run();
 315 
 316   // Testers
 317   virtual bool is_VM_thread()       const            { return false; }
 318   virtual bool is_Java_thread()     const            { return false; }
 319   virtual bool is_Compiler_thread() const            { return false; }
 320   virtual bool is_Code_cache_sweeper_thread() const  { return false; }
 321   virtual bool is_hidden_from_external_view() const  { return false; }
 322   virtual bool is_jvmti_agent_thread() const         { return false; }
 323   // True iff the thread can perform GC operations at a safepoint.
 324   // Generally will be true only of VM thread and parallel GC WorkGang
 325   // threads.
 326   virtual bool is_GC_task_thread() const             { return false; }
 327   virtual bool is_Watcher_thread() const             { return false; }
 328   virtual bool is_ConcurrentGC_thread() const        { return false; }
 329   virtual bool is_Named_thread() const               { return false; }
 330   virtual bool is_Worker_thread() const              { return false; }
 331 
 332   // Can this thread make Java upcalls
 333   virtual bool can_call_java() const                 { return false; }
 334 
 335   // Casts
 336   virtual WorkerThread* as_Worker_thread() const     { return NULL; }
 337 
 338   virtual char* name() const { return (char*)"Unknown thread"; }
 339 
 340   // Returns the current thread
 341   static inline Thread* current();
 342   // ... without having to include thread.inline.hpp.
 343   static Thread* current_noinline();



 344 
 345   // Common thread operations
 346   static void set_priority(Thread* thread, ThreadPriority priority);
 347   static ThreadPriority get_priority(const Thread* const thread);
 348   static void start(Thread* thread);
 349   static void interrupt(Thread* thr);
 350   static bool is_interrupted(Thread* thr, bool clear_interrupted);
 351 
 352   void set_native_thread_name(const char *name) {
 353     assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
 354     os::set_native_thread_name(name);
 355   }
 356 
 357   ObjectMonitor** omInUseList_addr()             { return (ObjectMonitor **)&omInUseList; }
 358   Monitor* SR_lock() const                       { return _SR_lock; }
 359 
 360   bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
 361 
 362   inline void set_suspend_flag(SuspendFlags f);
 363   inline void clear_suspend_flag(SuspendFlags f);


 632   jint _hashStateW;                           // Marsaglia Shift-XOR thread-local RNG
 633   jint _hashStateX;                           // thread-specific hashCode generator state
 634   jint _hashStateY;
 635   jint _hashStateZ;
 636   void * _schedctl;
 637 
 638 
 639   volatile jint rng[4];                      // RNG for spin loop
 640 
 641   // Low-level leaf-lock primitives used to implement synchronization
 642   // and native monitor-mutex infrastructure.
 643   // Not for general synchronization use.
 644   static void SpinAcquire(volatile int * Lock, const char * Name);
 645   static void SpinRelease(volatile int * Lock);
 646   static void muxAcquire(volatile intptr_t * Lock, const char * Name);
 647   static void muxAcquireW(volatile intptr_t * Lock, ParkEvent * ev);
 648   static void muxRelease(volatile intptr_t * Lock);
 649 };
 650 
 651 // Inline implementation of Thread::current()
 652 // Thread::current is "hot" it's called > 128K times in the 1st 500 msecs of
 653 // startup.
 654 // ThreadLocalStorage::thread is warm -- it's called > 16K times in the same
 655 // period.   This is inlined in thread_<os_family>.inline.hpp.
 656 
 657 inline Thread* Thread::current() {
 658 #ifdef ASSERT
 659   // This function is very high traffic. Define PARANOID to enable expensive
 660   // asserts.
 661 #ifdef PARANOID
 662   // Signal handler should call ThreadLocalStorage::get_thread_slow()
 663   Thread* t = ThreadLocalStorage::get_thread_slow();
 664   assert(t != NULL && !t->is_inside_signal_handler(),
 665          "Don't use Thread::current() inside signal handler");
 666 #endif

 667 #endif
 668   Thread* thread = ThreadLocalStorage::thread();
 669   assert(thread != NULL, "just checking");
 670   return thread;

 671 }
 672 
 673 // Name support for threads.  non-JavaThread subclasses with multiple
 674 // uniquely named instances should derive from this.
 675 class NamedThread: public Thread {
 676   friend class VMStructs;
 677   enum {
 678     max_name_len = 64
 679   };
 680  private:
 681   char* _name;
 682   // log JavaThread being processed by oops_do
 683   JavaThread* _processed_thread;
 684   uint _gc_id; // The current GC id when a thread takes part in GC
 685 
 686  public:
 687   NamedThread();
 688   ~NamedThread();
 689   // May only be called once per thread.
 690   void set_name(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);


1825  public:
1826   GrowableArray<MonitorInfo*>* cached_monitor_info() { return _cached_monitor_info; }
1827   void set_cached_monitor_info(GrowableArray<MonitorInfo*>* info) { _cached_monitor_info = info; }
1828 
1829   // clearing/querying jni attach status
1830   bool is_attaching_via_jni() const { return _jni_attach_state == _attaching_via_jni; }
1831   bool has_attached_via_jni() const { return is_attaching_via_jni() || _jni_attach_state == _attached_via_jni; }
1832   inline void set_done_attaching_via_jni();
1833  private:
1834   // This field is used to determine if a thread has claimed
1835   // a par_id: it is UINT_MAX if the thread has not claimed a par_id;
1836   // otherwise its value is the par_id that has been claimed.
1837   uint _claimed_par_id;
1838  public:
1839   uint get_claimed_par_id() { return _claimed_par_id; }
1840   void set_claimed_par_id(uint id) { _claimed_par_id = id; }
1841 };
1842 
1843 // Inline implementation of JavaThread::current
1844 inline JavaThread* JavaThread::current() {
1845   Thread* thread = ThreadLocalStorage::thread();
1846   assert(thread != NULL && thread->is_Java_thread(), "just checking");
1847   return (JavaThread*)thread;
1848 }
1849 
1850 inline CompilerThread* JavaThread::as_CompilerThread() {
1851   assert(is_Compiler_thread(), "just checking");
1852   return (CompilerThread*)this;
1853 }
1854 
1855 // Dedicated thread to sweep the code cache
1856 class CodeCacheSweeperThread : public JavaThread {
1857   nmethod*       _scanned_nmethod; // nmethod being scanned by the sweeper
1858  public:
1859   CodeCacheSweeperThread();
1860   // Track the nmethod currently being scanned by the sweeper
1861   void set_scanned_nmethod(nmethod* nm) {
1862     assert(_scanned_nmethod == NULL || nm == NULL, "should reset to NULL before writing a new value");
1863     _scanned_nmethod = nm;
1864   }
1865 
1866   // Hide sweeper thread from external view.




  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 //   - WatcherThread
 101 
 102 class Thread: public ThreadShadow {
 103   friend class VMStructs;
 104  private:
 105 
 106 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 107   // Current thread is maintained as a thread-local variable
 108   static THREAD_LOCAL_DECL Thread* _thr_current;
 109 #endif
 110 
 111   // Exception handling
 112   // (Note: _pending_exception and friends are in ThreadShadow)
 113   //oop       _pending_exception;                // pending exception for current thread
 114   // const char* _exception_file;                   // file information for exception (debugging only)
 115   // int         _exception_line;                   // line information for exception (debugging only)
 116  protected:
 117   // Support for forcing alignment of thread objects for biased locking
 118   void*       _real_malloc_address;
 119  public:
 120   void* operator new(size_t size) throw() { return allocate(size, true); }
 121   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
 122     return allocate(size, false); }
 123   void  operator delete(void* p);
 124 
 125  protected:
 126   static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
 127  private:
 128 
 129   // ***************************************************************
 130   // Suspend and resume support


 249  private:
 250 
 251   // debug support for checking if code does allow safepoints or not
 252   // GC points in the VM can happen because of allocation, invoking a VM operation, or blocking on
 253   // mutex, or blocking on an object synchronizer (Java locking).
 254   // If !allow_safepoint(), then an assertion failure will happen in any of the above cases
 255   // If !allow_allocation(), then an assertion failure will happen during allocation
 256   // (Hence, !allow_safepoint() => !allow_allocation()).
 257   //
 258   // The two classes No_Safepoint_Verifier and No_Allocation_Verifier are used to set these counters.
 259   //
 260   NOT_PRODUCT(int _allow_safepoint_count;)      // If 0, thread allow a safepoint to happen
 261   debug_only(int _allow_allocation_count;)     // If 0, the thread is allowed to allocate oops.
 262 
 263   // Used by SkipGCALot class.
 264   NOT_PRODUCT(bool _skip_gcalot;)               // Should we elide gc-a-lot?
 265 
 266   friend class No_Alloc_Verifier;
 267   friend class No_Safepoint_Verifier;
 268   friend class Pause_No_Safepoint_Verifier;

 269   friend class GC_locker;
 270 
 271   ThreadLocalAllocBuffer _tlab;                 // Thread-local eden
 272   jlong _allocated_bytes;                       // Cumulative number of bytes allocated on
 273                                                 // the Java heap
 274 
 275   TRACE_DATA _trace_data;                       // Thread-local data for tracing
 276 
 277   ThreadExt _ext;
 278 
 279   int   _vm_operation_started_count;            // VM_Operation support
 280   int   _vm_operation_completed_count;          // VM_Operation support
 281 
 282   ObjectMonitor* _current_pending_monitor;      // ObjectMonitor this thread
 283                                                 // is waiting to lock
 284   bool _current_pending_monitor_is_from_java;   // locking is from Java code
 285 
 286   // ObjectMonitor on which this thread called Object.wait()
 287   ObjectMonitor* _current_waiting_monitor;
 288 


 295   int omInUseCount;                             // length of omInUseList
 296 
 297 #ifdef ASSERT
 298  private:
 299   bool _visited_for_critical_count;
 300 
 301  public:
 302   void set_visited_for_critical_count(bool z) { _visited_for_critical_count = z; }
 303   bool was_visited_for_critical_count() const   { return _visited_for_critical_count; }
 304 #endif
 305 
 306  public:
 307   enum {
 308     is_definitely_current_thread = true
 309   };
 310 
 311   // Constructor
 312   Thread();
 313   virtual ~Thread();
 314 
 315   // Manage Thread::current()
 316   void initialize_thread_current();
 317   private:
 318   void clear_thread_current(); // needed for detaching JNI threads
 319 
 320   public:
 321   // thread entry point
 322   virtual void run();
 323 
 324   // Testers
 325   virtual bool is_VM_thread()       const            { return false; }
 326   virtual bool is_Java_thread()     const            { return false; }
 327   virtual bool is_Compiler_thread() const            { return false; }
 328   virtual bool is_Code_cache_sweeper_thread() const  { return false; }
 329   virtual bool is_hidden_from_external_view() const  { return false; }
 330   virtual bool is_jvmti_agent_thread() const         { return false; }
 331   // True iff the thread can perform GC operations at a safepoint.
 332   // Generally will be true only of VM thread and parallel GC WorkGang
 333   // threads.
 334   virtual bool is_GC_task_thread() const             { return false; }
 335   virtual bool is_Watcher_thread() const             { return false; }
 336   virtual bool is_ConcurrentGC_thread() const        { return false; }
 337   virtual bool is_Named_thread() const               { return false; }
 338   virtual bool is_Worker_thread() const              { return false; }
 339 
 340   // Can this thread make Java upcalls
 341   virtual bool can_call_java() const                 { return false; }
 342 
 343   // Casts
 344   virtual WorkerThread* as_Worker_thread() const     { return NULL; }
 345 
 346   virtual char* name() const { return (char*)"Unknown 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   static void set_priority(Thread* thread, ThreadPriority priority);
 358   static ThreadPriority get_priority(const Thread* const thread);
 359   static void start(Thread* thread);
 360   static void interrupt(Thread* thr);
 361   static bool is_interrupted(Thread* thr, bool clear_interrupted);
 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   ObjectMonitor** omInUseList_addr()             { return (ObjectMonitor **)&omInUseList; }
 369   Monitor* SR_lock() const                       { return _SR_lock; }
 370 
 371   bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
 372 
 373   inline void set_suspend_flag(SuspendFlags f);
 374   inline void clear_suspend_flag(SuspendFlags f);


 643   jint _hashStateW;                           // Marsaglia Shift-XOR thread-local RNG
 644   jint _hashStateX;                           // thread-specific hashCode generator state
 645   jint _hashStateY;
 646   jint _hashStateZ;
 647   void * _schedctl;
 648 
 649 
 650   volatile jint rng[4];                      // RNG for spin loop
 651 
 652   // Low-level leaf-lock primitives used to implement synchronization
 653   // and native monitor-mutex infrastructure.
 654   // Not for general synchronization use.
 655   static void SpinAcquire(volatile int * Lock, const char * Name);
 656   static void SpinRelease(volatile int * Lock);
 657   static void muxAcquire(volatile intptr_t * Lock, const char * Name);
 658   static void muxAcquireW(volatile intptr_t * Lock, ParkEvent * ev);
 659   static void muxRelease(volatile intptr_t * Lock);
 660 };
 661 
 662 // Inline implementation of Thread::current()





 663 inline Thread* Thread::current() {
 664   Thread* current = current_or_null();
 665   assert(current != NULL, "Thread::current() called on detached thread");
 666   return current;
 667 }
 668 
 669 inline Thread* Thread::current_or_null() {
 670 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 671   return _thr_current;
 672 #else
 673   return ThreadLocalStorage::thread();
 674 #endif
 675 }
 676 
 677 inline Thread* Thread::current_or_null_safe() {
 678   return ThreadLocalStorage::thread();
 679 }
 680 
 681 // Name support for threads.  non-JavaThread subclasses with multiple
 682 // uniquely named instances should derive from this.
 683 class NamedThread: public Thread {
 684   friend class VMStructs;
 685   enum {
 686     max_name_len = 64
 687   };
 688  private:
 689   char* _name;
 690   // log JavaThread being processed by oops_do
 691   JavaThread* _processed_thread;
 692   uint _gc_id; // The current GC id when a thread takes part in GC
 693 
 694  public:
 695   NamedThread();
 696   ~NamedThread();
 697   // May only be called once per thread.
 698   void set_name(const char* format, ...)  ATTRIBUTE_PRINTF(2, 3);


1833  public:
1834   GrowableArray<MonitorInfo*>* cached_monitor_info() { return _cached_monitor_info; }
1835   void set_cached_monitor_info(GrowableArray<MonitorInfo*>* info) { _cached_monitor_info = info; }
1836 
1837   // clearing/querying jni attach status
1838   bool is_attaching_via_jni() const { return _jni_attach_state == _attaching_via_jni; }
1839   bool has_attached_via_jni() const { return is_attaching_via_jni() || _jni_attach_state == _attached_via_jni; }
1840   inline void set_done_attaching_via_jni();
1841  private:
1842   // This field is used to determine if a thread has claimed
1843   // a par_id: it is UINT_MAX if the thread has not claimed a par_id;
1844   // otherwise its value is the par_id that has been claimed.
1845   uint _claimed_par_id;
1846  public:
1847   uint get_claimed_par_id() { return _claimed_par_id; }
1848   void set_claimed_par_id(uint id) { _claimed_par_id = id; }
1849 };
1850 
1851 // Inline implementation of JavaThread::current
1852 inline JavaThread* JavaThread::current() {
1853   Thread* thread = Thread::current();
1854   assert(thread->is_Java_thread(), "just checking");
1855   return (JavaThread*)thread;
1856 }
1857 
1858 inline CompilerThread* JavaThread::as_CompilerThread() {
1859   assert(is_Compiler_thread(), "just checking");
1860   return (CompilerThread*)this;
1861 }
1862 
1863 // Dedicated thread to sweep the code cache
1864 class CodeCacheSweeperThread : public JavaThread {
1865   nmethod*       _scanned_nmethod; // nmethod being scanned by the sweeper
1866  public:
1867   CodeCacheSweeperThread();
1868   // Track the nmethod currently being scanned by the sweeper
1869   void set_scanned_nmethod(nmethod* nm) {
1870     assert(_scanned_nmethod == NULL || nm == NULL, "should reset to NULL before writing a new value");
1871     _scanned_nmethod = nm;
1872   }
1873 
1874   // Hide sweeper thread from external view.


< prev index next >