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 // - 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 // ***************************************************************
238 // If !allow_safepoint(), then an assertion failure will happen in any of the above cases
239 // If !allow_allocation(), then an assertion failure will happen during allocation
240 // (Hence, !allow_safepoint() => !allow_allocation()).
241 //
242 // The two classes No_Safepoint_Verifier and No_Allocation_Verifier are used to set these counters.
243 //
244 NOT_PRODUCT(int _allow_safepoint_count;) // If 0, thread allow a safepoint to happen
245 debug_only (int _allow_allocation_count;) // If 0, the thread is allowed to allocate oops.
246
247 // Used by SkipGCALot class.
248 NOT_PRODUCT(bool _skip_gcalot;) // Should we elide gc-a-lot?
249
250 friend class No_Alloc_Verifier;
251 friend class No_Safepoint_Verifier;
252 friend class Pause_No_Safepoint_Verifier;
253 friend class ThreadLocalStorage;
254 friend class GC_locker;
255
256 ThreadLocalAllocBuffer _tlab; // Thread-local eden
257 ThreadLocalAllocBuffer _gclab; // Thread-local allocation buffer for GC (e.g. evacuation)
258 jlong _allocated_bytes; // Cumulative number of bytes allocated on
259 // the Java heap
260 jlong _allocated_bytes_gclab; // Cumulative number of bytes allocated on
261 // the Java heap, in GCLABs
262
263 // Thread-local buffer used by MetadataOnStackMark.
264 MetadataOnStackBuffer* _metadata_on_stack_buffer;
265
266 TRACE_DATA _trace_data; // Thread-local data for tracing
267
268 ThreadExt _ext;
269
270 int _vm_operation_started_count; // VM_Operation support
271 int _vm_operation_completed_count; // VM_Operation support
272
273 char _oom_during_evac;
274
275 ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread
276 // is waiting to lock
277 bool _current_pending_monitor_is_from_java; // locking is from Java code
429 void set_osthread(OSThread* thread) { _osthread = thread; }
430
431 // JNI handle support
432 JNIHandleBlock* active_handles() const { return _active_handles; }
433 void set_active_handles(JNIHandleBlock* block) { _active_handles = block; }
434 JNIHandleBlock* free_handle_block() const { return _free_handle_block; }
435 void set_free_handle_block(JNIHandleBlock* block) { _free_handle_block = block; }
436
437 // Internal handle support
438 HandleArea* handle_area() const { return _handle_area; }
439 void set_handle_area(HandleArea* area) { _handle_area = area; }
440
441 GrowableArray<Metadata*>* metadata_handles() const { return _metadata_handles; }
442 void set_metadata_handles(GrowableArray<Metadata*>* handles){ _metadata_handles = handles; }
443
444 // Thread-Local Allocation Buffer (TLAB) support
445 ThreadLocalAllocBuffer& tlab() { return _tlab; }
446 void initialize_tlab() {
447 if (UseTLAB) {
448 tlab().initialize(false);
449 gclab().initialize(true);
450 }
451 }
452
453 // Thread-Local GC Allocation Buffer (GCLAB) support
454 ThreadLocalAllocBuffer& gclab() { return _gclab; }
455
456 jlong allocated_bytes() { return _allocated_bytes; }
457 void set_allocated_bytes(jlong value) { _allocated_bytes = value; }
458 void incr_allocated_bytes(jlong size) { _allocated_bytes += size; }
459 inline jlong cooked_allocated_bytes();
460
461 jlong allocated_bytes_gclab() { return _allocated_bytes_gclab; }
462 void set_allocated_bytes_gclab(jlong value) { _allocated_bytes_gclab = value; }
463 void incr_allocated_bytes_gclab(jlong size) { _allocated_bytes_gclab += size; }
464
465 TRACE_DATA* trace_data() { return &_trace_data; }
466
467 const ThreadExt& ext() const { return _ext; }
468 ThreadExt& ext() { return _ext; }
469
470 // VM operation support
471 int vm_operation_ticket() { return ++_vm_operation_started_count; }
472 int vm_operation_completed_count() { return _vm_operation_completed_count; }
473 void increment_vm_operation_completed_count() { _vm_operation_completed_count++; }
474
971 // We use intptr_t instead of address so debugger doesn't try and display strings
972 intptr_t _target;
973 intptr_t _instruction;
974 const char* _file;
975 int _line;
976 } _jmp_ring[ jump_ring_buffer_size ];
977 #endif /* PRODUCT */
978
979 #if INCLUDE_ALL_GCS
980 // Support for G1 barriers
981
982 ObjPtrQueue _satb_mark_queue; // Thread-local log for SATB barrier.
983 // Set of all such queues.
984 static SATBMarkQueueSet _satb_mark_queue_set;
985
986 DirtyCardQueue _dirty_card_queue; // Thread-local log for dirty cards.
987 // Set of all such queues.
988 static DirtyCardQueueSet _dirty_card_queue_set;
989
990 void flush_barrier_queues();
991
992 // Support for Shenandoah barriers
993 static char _gc_state_global;
994 char _gc_state;
995
996 #endif // INCLUDE_ALL_GCS
997
998 friend class VMThread;
999 friend class ThreadWaitTransition;
1000 friend class VM_Exit;
1001
1002 void initialize(); // Initialized the instance variables
1003
1004 public:
1005 // Constructor
1006 JavaThread(bool is_attaching_via_jni = false); // for main thread and JNI attached threads
1007 JavaThread(ThreadFunction entry_point, size_t stack_size = 0);
1008 ~JavaThread();
1009
1010 #ifdef ASSERT
1011 // verify this JavaThread hasn't be published in the Threads::list yet
1012 void verify_not_published();
1013 #endif
1014
1015 //JNI functiontable getter/setter for JVMTI jni function table interception API.
1701 #if INCLUDE_ALL_GCS
1702 // SATB marking queue support
1703 ObjPtrQueue& satb_mark_queue() { return _satb_mark_queue; }
1704 static SATBMarkQueueSet& satb_mark_queue_set() {
1705 return _satb_mark_queue_set;
1706 }
1707
1708 // Dirty card queue support
1709 DirtyCardQueue& dirty_card_queue() { return _dirty_card_queue; }
1710 static DirtyCardQueueSet& dirty_card_queue_set() {
1711 return _dirty_card_queue_set;
1712 }
1713
1714 inline char gc_state() const;
1715
1716 private:
1717 void set_gc_state(char in_prog);
1718
1719 public:
1720 static void set_gc_state_all_threads(char in_prog);
1721
1722 #endif // INCLUDE_ALL_GCS
1723
1724 // This method initializes the SATB and dirty card queues before a
1725 // JavaThread is added to the Java thread list. Right now, we don't
1726 // have to do anything to the dirty card queue (it should have been
1727 // activated when the thread was created), but we have to activate
1728 // the SATB queue if the thread is created while a marking cycle is
1729 // in progress. The activation / de-activation of the SATB queues at
1730 // the beginning / end of a marking cycle is done during safepoints
1731 // so we have to make sure this method is called outside one to be
1732 // able to safely read the active field of the SATB queue set. Right
1733 // now, it is called just before the thread is added to the Java
1734 // thread list in the Threads::add() method. That method is holding
1735 // the Threads_lock which ensures we are outside a safepoint. We
1736 // cannot do the obvious and set the active field of the SATB queue
1737 // when the thread is created given that, in some cases, safepoints
1738 // might happen between the JavaThread constructor being called and the
1739 // thread being added to the Java thread list (an example of this is
1740 // when the structure for the DestroyJavaVM thread is created).
1741 #if INCLUDE_ALL_GCS
|
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 // - WatcherThread
101
102 class Thread: public ThreadShadow {
103 friend class VMStructs;
104
105 #if INCLUDE_ALL_GCS
106 protected:
107 // Support for Shenandoah barriers. This is only accessible from JavaThread,
108 // but we really want to keep this field at lower Thread offset (below first
109 // 128 bytes), because that makes barrier fastpaths optimally encoded.
110 char _gc_state;
111 static char _gc_state_global;
112 #endif
113
114 private:
115 // Exception handling
116 // (Note: _pending_exception and friends are in ThreadShadow)
117 //oop _pending_exception; // pending exception for current thread
118 // const char* _exception_file; // file information for exception (debugging only)
119 // int _exception_line; // line information for exception (debugging only)
120 protected:
121 // Support for forcing alignment of thread objects for biased locking
122 void* _real_malloc_address;
123 public:
124 void* operator new(size_t size) throw() { return allocate(size, true); }
125 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
126 return allocate(size, false); }
127 void operator delete(void* p);
128
129 protected:
130 static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
131 private:
132
133 // ***************************************************************
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 ThreadLocalAllocBuffer _gclab; // Thread-local allocation buffer for GC (e.g. evacuation)
268 uint _worker_id; // Worker ID
269 bool _force_satb_flush; // Force SATB flush
270
271 jlong _allocated_bytes; // Cumulative number of bytes allocated on
272 // the Java heap
273 jlong _allocated_bytes_gclab; // Cumulative number of bytes allocated on
274 // the Java heap, in GCLABs
275
276 // Thread-local buffer used by MetadataOnStackMark.
277 MetadataOnStackBuffer* _metadata_on_stack_buffer;
278
279 TRACE_DATA _trace_data; // Thread-local data for tracing
280
281 ThreadExt _ext;
282
283 int _vm_operation_started_count; // VM_Operation support
284 int _vm_operation_completed_count; // VM_Operation support
285
286 char _oom_during_evac;
287
288 ObjectMonitor* _current_pending_monitor; // ObjectMonitor this thread
289 // is waiting to lock
290 bool _current_pending_monitor_is_from_java; // locking is from Java code
442 void set_osthread(OSThread* thread) { _osthread = thread; }
443
444 // JNI handle support
445 JNIHandleBlock* active_handles() const { return _active_handles; }
446 void set_active_handles(JNIHandleBlock* block) { _active_handles = block; }
447 JNIHandleBlock* free_handle_block() const { return _free_handle_block; }
448 void set_free_handle_block(JNIHandleBlock* block) { _free_handle_block = block; }
449
450 // Internal handle support
451 HandleArea* handle_area() const { return _handle_area; }
452 void set_handle_area(HandleArea* area) { _handle_area = area; }
453
454 GrowableArray<Metadata*>* metadata_handles() const { return _metadata_handles; }
455 void set_metadata_handles(GrowableArray<Metadata*>* handles){ _metadata_handles = handles; }
456
457 // Thread-Local Allocation Buffer (TLAB) support
458 ThreadLocalAllocBuffer& tlab() { return _tlab; }
459 void initialize_tlab() {
460 if (UseTLAB) {
461 tlab().initialize(false);
462 if (UseShenandoahGC && (is_Java_thread() || is_Worker_thread())) {
463 gclab().initialize(true);
464 }
465 }
466 }
467
468 // Thread-Local GC Allocation Buffer (GCLAB) support
469 ThreadLocalAllocBuffer& gclab() {
470 assert (UseShenandoahGC, "Only for Shenandoah");
471 assert (!_gclab.is_initialized() || (is_Java_thread() || is_Worker_thread()),
472 "Only Java and GC worker threads are allowed to get GCLABs");
473 return _gclab;
474 }
475
476 void set_worker_id(uint id) { _worker_id = id; }
477 uint worker_id() { return _worker_id; }
478
479 void set_force_satb_flush(bool value) { _force_satb_flush = value; }
480 bool is_force_satb_flush() { return _force_satb_flush; }
481
482 jlong allocated_bytes() { return _allocated_bytes; }
483 void set_allocated_bytes(jlong value) { _allocated_bytes = value; }
484 void incr_allocated_bytes(jlong size) { _allocated_bytes += size; }
485 inline jlong cooked_allocated_bytes();
486
487 jlong allocated_bytes_gclab() { return _allocated_bytes_gclab; }
488 void set_allocated_bytes_gclab(jlong value) { _allocated_bytes_gclab = value; }
489 void incr_allocated_bytes_gclab(jlong size) { _allocated_bytes_gclab += size; }
490
491 TRACE_DATA* trace_data() { return &_trace_data; }
492
493 const ThreadExt& ext() const { return _ext; }
494 ThreadExt& ext() { return _ext; }
495
496 // VM operation support
497 int vm_operation_ticket() { return ++_vm_operation_started_count; }
498 int vm_operation_completed_count() { return _vm_operation_completed_count; }
499 void increment_vm_operation_completed_count() { _vm_operation_completed_count++; }
500
997 // We use intptr_t instead of address so debugger doesn't try and display strings
998 intptr_t _target;
999 intptr_t _instruction;
1000 const char* _file;
1001 int _line;
1002 } _jmp_ring[ jump_ring_buffer_size ];
1003 #endif /* PRODUCT */
1004
1005 #if INCLUDE_ALL_GCS
1006 // Support for G1 barriers
1007
1008 ObjPtrQueue _satb_mark_queue; // Thread-local log for SATB barrier.
1009 // Set of all such queues.
1010 static SATBMarkQueueSet _satb_mark_queue_set;
1011
1012 DirtyCardQueue _dirty_card_queue; // Thread-local log for dirty cards.
1013 // Set of all such queues.
1014 static DirtyCardQueueSet _dirty_card_queue_set;
1015
1016 void flush_barrier_queues();
1017 #endif // INCLUDE_ALL_GCS
1018
1019 friend class VMThread;
1020 friend class ThreadWaitTransition;
1021 friend class VM_Exit;
1022
1023 void initialize(); // Initialized the instance variables
1024
1025 public:
1026 // Constructor
1027 JavaThread(bool is_attaching_via_jni = false); // for main thread and JNI attached threads
1028 JavaThread(ThreadFunction entry_point, size_t stack_size = 0);
1029 ~JavaThread();
1030
1031 #ifdef ASSERT
1032 // verify this JavaThread hasn't be published in the Threads::list yet
1033 void verify_not_published();
1034 #endif
1035
1036 //JNI functiontable getter/setter for JVMTI jni function table interception API.
1722 #if INCLUDE_ALL_GCS
1723 // SATB marking queue support
1724 ObjPtrQueue& satb_mark_queue() { return _satb_mark_queue; }
1725 static SATBMarkQueueSet& satb_mark_queue_set() {
1726 return _satb_mark_queue_set;
1727 }
1728
1729 // Dirty card queue support
1730 DirtyCardQueue& dirty_card_queue() { return _dirty_card_queue; }
1731 static DirtyCardQueueSet& dirty_card_queue_set() {
1732 return _dirty_card_queue_set;
1733 }
1734
1735 inline char gc_state() const;
1736
1737 private:
1738 void set_gc_state(char in_prog);
1739
1740 public:
1741 static void set_gc_state_all_threads(char in_prog);
1742 static void set_force_satb_flush_all_threads(bool value);
1743 #endif // INCLUDE_ALL_GCS
1744
1745 // This method initializes the SATB and dirty card queues before a
1746 // JavaThread is added to the Java thread list. Right now, we don't
1747 // have to do anything to the dirty card queue (it should have been
1748 // activated when the thread was created), but we have to activate
1749 // the SATB queue if the thread is created while a marking cycle is
1750 // in progress. The activation / de-activation of the SATB queues at
1751 // the beginning / end of a marking cycle is done during safepoints
1752 // so we have to make sure this method is called outside one to be
1753 // able to safely read the active field of the SATB queue set. Right
1754 // now, it is called just before the thread is added to the Java
1755 // thread list in the Threads::add() method. That method is holding
1756 // the Threads_lock which ensures we are outside a safepoint. We
1757 // cannot do the obvious and set the active field of the SATB queue
1758 // when the thread is created given that, in some cases, safepoints
1759 // might happen between the JavaThread constructor being called and the
1760 // thread being added to the Java thread list (an example of this is
1761 // when the structure for the DestroyJavaVM thread is created).
1762 #if INCLUDE_ALL_GCS
|