39 #include "runtime/park.hpp"
40 #include "runtime/safepoint.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "runtime/threadLocalStorage.hpp"
43 #include "runtime/thread_ext.hpp"
44 #include "runtime/unhandledOops.hpp"
45 #include "trace/traceBackend.hpp"
46 #include "trace/traceMacros.hpp"
47 #include "utilities/align.hpp"
48 #include "utilities/exceptions.hpp"
49 #include "utilities/macros.hpp"
50 #if INCLUDE_ALL_GCS
51 #include "gc/g1/dirtyCardQueue.hpp"
52 #include "gc/g1/satbMarkQueue.hpp"
53 #endif // INCLUDE_ALL_GCS
54 #ifdef ZERO
55 # include "stack_zero.hpp"
56 #endif
57
58 class ThreadSafepointState;
59
60 class JvmtiThreadState;
61 class JvmtiGetLoadedClassesClosure;
62 class ThreadStatistics;
63 class ConcurrentLocksDump;
64 class ParkEvent;
65 class Parker;
66
67 class ciEnv;
68 class CompileThread;
69 class CompileLog;
70 class CompileTask;
71 class CompileQueue;
72 class CompilerCounters;
73 class vframeArray;
74
75 class DeoptResourceMark;
76 class jvmtiDeferredLocalVariableSet;
77
78 class GCTaskQueue;
83 template <class T, MEMFLAGS F> class ChunkedList;
84 typedef ChunkedList<Metadata*, mtInternal> MetadataOnStackBuffer;
85
86 DEBUG_ONLY(class ResourceMark;)
87
88 class WorkerThread;
89
90 // Class hierarchy
91 // - Thread
92 // - NamedThread
93 // - VMThread
94 // - ConcurrentGCThread
95 // - WorkerThread
96 // - GangWorker
97 // - GCTaskThread
98 // - JavaThread
99 // - various subclasses eg CompilerThread, ServiceThread
100 // - WatcherThread
101
102 class Thread: public ThreadShadow {
103 friend class VMStructs;
104 friend class JVMCIVMStructs;
105 private:
106
107 #ifndef USE_LIBRARY_BASED_TLS_ONLY
108 // Current thread is maintained as a thread-local variable
109 static THREAD_LOCAL_DECL Thread* _thr_current;
110 #endif
111
112 // Exception handling
113 // (Note: _pending_exception and friends are in ThreadShadow)
114 //oop _pending_exception; // pending exception for current thread
115 // const char* _exception_file; // file information for exception (debugging only)
116 // int _exception_line; // line information for exception (debugging only)
117 protected:
118 // Support for forcing alignment of thread objects for biased locking
119 void* _real_malloc_address;
120 public:
121 void* operator new(size_t size) throw() { return allocate(size, true); }
122 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
123 return allocate(size, false); }
124 void operator delete(void* p);
125
126 protected:
127 static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
128 private:
129
130 // ***************************************************************
131 // Suspend and resume support
132 // ***************************************************************
133 //
134 // VM suspend/resume no longer exists - it was once used for various
135 // things including safepoints but was deprecated and finally removed
136 // in Java 7. Because VM suspension was considered "internal" Java-level
137 // suspension was considered "external", and this legacy naming scheme
138 // remains.
139 //
339 virtual bool is_Named_thread() const { return false; }
340 virtual bool is_Worker_thread() const { return false; }
341
342 // Can this thread make Java upcalls
343 virtual bool can_call_java() const { return false; }
344
345 // Casts
346 virtual WorkerThread* as_Worker_thread() const { return NULL; }
347
348 virtual char* name() const { return (char*)"Unknown thread"; }
349
350 // Returns the current thread (ASSERTS if NULL)
351 static inline Thread* current();
352 // Returns the current thread, or NULL if not attached
353 static inline Thread* current_or_null();
354 // Returns the current thread, or NULL if not attached, and is
355 // safe for use from signal-handlers
356 static inline Thread* current_or_null_safe();
357
358 // Common thread operations
359 static void set_priority(Thread* thread, ThreadPriority priority);
360 static ThreadPriority get_priority(const Thread* const thread);
361 static void start(Thread* thread);
362 static void interrupt(Thread* thr);
363 static bool is_interrupted(Thread* thr, bool clear_interrupted);
364
365 void set_native_thread_name(const char *name) {
366 assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
367 os::set_native_thread_name(name);
368 }
369
370 ObjectMonitor** omInUseList_addr() { return (ObjectMonitor **)&omInUseList; }
371 Monitor* SR_lock() const { return _SR_lock; }
372
373 bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
374
375 inline void set_suspend_flag(SuspendFlags f);
376 inline void clear_suspend_flag(SuspendFlags f);
377
378 inline void set_has_async_exception();
554 address stack_base() const { assert(_stack_base != NULL,"Sanity check"); return _stack_base; }
555 void set_stack_base(address base) { _stack_base = base; }
556 size_t stack_size() const { return _stack_size; }
557 void set_stack_size(size_t size) { _stack_size = size; }
558 address stack_end() const { return stack_base() - stack_size(); }
559 void record_stack_base_and_size();
560
561 bool on_local_stack(address adr) const {
562 // QQQ this has knowledge of direction, ought to be a stack method
563 return (_stack_base >= adr && adr >= stack_end());
564 }
565
566 uintptr_t self_raw_id() { return _self_raw_id; }
567 void set_self_raw_id(uintptr_t value) { _self_raw_id = value; }
568
569 int lgrp_id() const { return _lgrp_id; }
570 void set_lgrp_id(int value) { _lgrp_id = value; }
571
572 // Printing
573 virtual void print_on(outputStream* st) const;
574 void print() const { print_on(tty); }
575 virtual void print_on_error(outputStream* st, char* buf, int buflen) const;
576 void print_value_on(outputStream* st) const;
577
578 // Debug-only code
579 #ifdef ASSERT
580 private:
581 // Deadlock detection support for Mutex locks. List of locks own by thread.
582 Monitor* _owned_locks;
583 // Mutex::set_owner_implementation is the only place where _owned_locks is modified,
584 // thus the friendship
585 friend class Mutex;
586 friend class Monitor;
587
588 public:
589 void print_owned_locks_on(outputStream* st) const;
590 void print_owned_locks() const { print_owned_locks_on(tty); }
591 Monitor* owned_locks() const { return _owned_locks; }
592 bool owns_locks() const { return owned_locks() != NULL; }
593 bool owns_locks_but_compiled_lock() const;
774 static void start();
775 static void stop();
776 // Only allow start once the VM is sufficiently initialized
777 // Otherwise the first task to enroll will trigger the start
778 static void make_startable();
779 private:
780 int sleep() const;
781 };
782
783
784 class CompilerThread;
785
786 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
787
788 class JavaThread: public Thread {
789 friend class VMStructs;
790 friend class JVMCIVMStructs;
791 friend class WhiteBox;
792 private:
793 JavaThread* _next; // The next thread in the Threads list
794 oop _threadObj; // The Java level thread object
795
796 #ifdef ASSERT
797 private:
798 int _java_call_counter;
799
800 public:
801 int java_call_counter() { return _java_call_counter; }
802 void inc_java_call_counter() { _java_call_counter++; }
803 void dec_java_call_counter() {
804 assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
805 _java_call_counter--;
806 }
807 private: // restore original namespace restriction
808 #endif // ifdef ASSERT
809
810 #ifndef PRODUCT
811 public:
812 enum {
813 jump_ring_buffer_size = 16
1101 intptr_t* last_Java_sp() const { return _anchor.last_Java_sp(); }
1102
1103 // last_Java_pc
1104
1105 address last_Java_pc(void) { return _anchor.last_Java_pc(); }
1106
1107 // Safepoint support
1108 #if !(defined(PPC64) || defined(AARCH64))
1109 JavaThreadState thread_state() const { return _thread_state; }
1110 void set_thread_state(JavaThreadState s) { _thread_state = s; }
1111 #else
1112 // Use membars when accessing volatile _thread_state. See
1113 // Threads::create_vm() for size checks.
1114 inline JavaThreadState thread_state() const;
1115 inline void set_thread_state(JavaThreadState s);
1116 #endif
1117 ThreadSafepointState *safepoint_state() const { return _safepoint_state; }
1118 void set_safepoint_state(ThreadSafepointState *state) { _safepoint_state = state; }
1119 bool is_at_poll_safepoint() { return _safepoint_state->is_at_poll_safepoint(); }
1120
1121 // thread has called JavaThread::exit() or is terminated
1122 bool is_exiting() { return _terminated == _thread_exiting || is_terminated(); }
1123 // thread is terminated (no longer on the threads list); we compare
1124 // against the two non-terminated values so that a freed JavaThread
1125 // will also be considered terminated.
1126 bool is_terminated() { return _terminated != _not_terminated && _terminated != _thread_exiting; }
1127 void set_terminated(TerminatedTypes t) { _terminated = t; }
1128 // special for Threads::remove() which is static:
1129 void set_terminated_value() { _terminated = _thread_terminated; }
1130 void block_if_vm_exited();
1131
1132 bool doing_unsafe_access() { return _doing_unsafe_access; }
1133 void set_doing_unsafe_access(bool val) { _doing_unsafe_access = val; }
1134
1135 bool do_not_unlock_if_synchronized() { return _do_not_unlock_if_synchronized; }
1136 void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1137
1138 // Suspend/resume support for JavaThread
1139 private:
1140 inline void set_ext_suspended();
1141 inline void clear_ext_suspended();
1142
1143 public:
1144 void java_suspend();
1145 void java_resume();
1146 int java_suspend_self();
1147
1148 void check_and_wait_while_suspended() {
1149 assert(JavaThread::current() == this, "sanity check");
1169 static void check_special_condition_for_native_trans_and_transition(JavaThread *thread);
1170
1171 bool is_ext_suspend_completed(bool called_by_wait, int delay, uint32_t *bits);
1172 bool is_ext_suspend_completed_with_lock(uint32_t *bits) {
1173 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1174 // Warning: is_ext_suspend_completed() may temporarily drop the
1175 // SR_lock to allow the thread to reach a stable thread state if
1176 // it is currently in a transient thread state.
1177 return is_ext_suspend_completed(false /* !called_by_wait */,
1178 SuspendRetryDelay, bits);
1179 }
1180
1181 // We cannot allow wait_for_ext_suspend_completion() to run forever or
1182 // we could hang. SuspendRetryCount and SuspendRetryDelay are normally
1183 // passed as the count and delay parameters. Experiments with specific
1184 // calls to wait_for_ext_suspend_completion() can be done by passing
1185 // other values in the code. Experiments with all calls can be done
1186 // via the appropriate -XX options.
1187 bool wait_for_ext_suspend_completion(int count, int delay, uint32_t *bits);
1188
1189 inline void set_external_suspend();
1190 inline void clear_external_suspend();
1191
1192 inline void set_deopt_suspend();
1193 inline void clear_deopt_suspend();
1194 bool is_deopt_suspend() { return (_suspend_flags & _deopt_suspend) != 0; }
1195
1196 bool is_external_suspend() const {
1197 return (_suspend_flags & _external_suspend) != 0;
1198 }
1199 // Whenever a thread transitions from native to vm/java it must suspend
1200 // if external|deopt suspend is present.
1201 bool is_suspend_after_native() const {
1202 return (_suspend_flags & (_external_suspend | _deopt_suspend)) != 0;
1203 }
1204
1205 // external suspend request is completed
1206 bool is_ext_suspended() const {
1207 return (_suspend_flags & _ext_suspended) != 0;
1208 }
2015 IdealGraphPrinter *_ideal_graph_printer;
2016 public:
2017 IdealGraphPrinter *ideal_graph_printer() { return _ideal_graph_printer; }
2018 void set_ideal_graph_printer(IdealGraphPrinter *n) { _ideal_graph_printer = n; }
2019 #endif
2020
2021 // Get/set the thread's current task
2022 CompileTask* task() { return _task; }
2023 void set_task(CompileTask* task) { _task = task; }
2024 };
2025
2026 inline CompilerThread* CompilerThread::current() {
2027 return JavaThread::current()->as_CompilerThread();
2028 }
2029
2030 // The active thread queue. It also keeps track of the current used
2031 // thread priorities.
2032 class Threads: AllStatic {
2033 friend class VMStructs;
2034 private:
2035 static JavaThread* _thread_list;
2036 static int _number_of_threads;
2037 static int _number_of_non_daemon_threads;
2038 static int _return_code;
2039 static int _thread_claim_parity;
2040 #ifdef ASSERT
2041 static bool _vm_complete;
2042 #endif
2043
2044 static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2045 static void initialize_jsr292_core_classes(TRAPS);
2046 public:
2047 // Thread management
2048 // force_daemon is a concession to JNI, where we may need to add a
2049 // thread to the thread list before allocating its thread object
2050 static void add(JavaThread* p, bool force_daemon = false);
2051 static void remove(JavaThread* p);
2052 static bool includes(JavaThread* p);
2053 static JavaThread* first() { return _thread_list; }
2054 static void threads_do(ThreadClosure* tc);
2055 static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2056
2057 // Initializes the vm and creates the vm thread
2058 static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2059 static void convert_vm_init_libraries_to_agents();
2060 static void create_vm_init_libraries();
2061 static void create_vm_init_agents();
2062 static void shutdown_vm_agents();
2063 static bool destroy_vm();
2064 // Supported VM versions via JNI
2065 // Includes JNI_VERSION_1_1
2066 static jboolean is_supported_jni_version_including_1_1(jint version);
2067 // Does not include JNI_VERSION_1_1
2068 static jboolean is_supported_jni_version(jint version);
2069
2070 // The "thread claim parity" provides a way for threads to be claimed
2071 // by parallel worker tasks.
2072 //
2073 // Each thread contains a a "parity" field. A task will claim the
2074 // thread only if its parity field is the same as the global parity,
2075 // which is updated by calling change_thread_claim_parity().
2076 //
2097 // Apply "f->do_oop" to roots in all threads that
2098 // are part of compiled frames
2099 static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2100
2101 static void convert_hcode_pointers();
2102 static void restore_hcode_pointers();
2103
2104 // Sweeper
2105 static void nmethods_do(CodeBlobClosure* cf);
2106
2107 // RedefineClasses support
2108 static void metadata_do(void f(Metadata*));
2109 static void metadata_handles_do(void f(Metadata*));
2110
2111 #ifdef ASSERT
2112 static bool is_vm_complete() { return _vm_complete; }
2113 #endif
2114
2115 // Verification
2116 static void verify();
2117 static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);
2118 static void print(bool print_stacks, bool internal_format) {
2119 // this function is only used by debug.cpp
2120 print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2121 }
2122 static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2123 static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2124 int buflen, bool* found_current);
2125 static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2126
2127 // Get Java threads that are waiting to enter a monitor. If doLock
2128 // is true, then Threads_lock is grabbed as needed. Otherwise, the
2129 // VM needs to be at a safepoint.
2130 static GrowableArray<JavaThread*>* get_pending_threads(int count,
2131 address monitor, bool doLock);
2132
2133 // Get owning Java thread from the monitor's owner field. If doLock
2134 // is true, then Threads_lock is grabbed as needed. Otherwise, the
2135 // VM needs to be at a safepoint.
2136 static JavaThread *owning_thread_from_monitor_owner(address owner,
2137 bool doLock);
2138
2139 // Number of threads on the active threads list
2140 static int number_of_threads() { return _number_of_threads; }
2141 // Number of non-daemon threads on the active threads list
2142 static int number_of_non_daemon_threads() { return _number_of_non_daemon_threads; }
2143
2144 // Deoptimizes all frames tied to marked nmethods
2145 static void deoptimized_wrt_marked_nmethods();
2146
2147 static JavaThread* find_java_thread_from_java_tid(jlong java_tid);
2148
2149 };
2150
2151
2152 // Thread iterator
2153 class ThreadClosure: public StackObj {
2154 public:
2155 virtual void do_thread(Thread* thread) = 0;
2156 };
2157
2158 class SignalHandlerMark: public StackObj {
2159 private:
2160 Thread* _thread;
2161 public:
2162 SignalHandlerMark(Thread* t) {
2163 _thread = t;
2164 if (_thread) _thread->enter_signal_handler();
2165 }
2166 ~SignalHandlerMark() {
2167 if (_thread) _thread->leave_signal_handler();
2168 _thread = NULL;
|
39 #include "runtime/park.hpp"
40 #include "runtime/safepoint.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "runtime/threadLocalStorage.hpp"
43 #include "runtime/thread_ext.hpp"
44 #include "runtime/unhandledOops.hpp"
45 #include "trace/traceBackend.hpp"
46 #include "trace/traceMacros.hpp"
47 #include "utilities/align.hpp"
48 #include "utilities/exceptions.hpp"
49 #include "utilities/macros.hpp"
50 #if INCLUDE_ALL_GCS
51 #include "gc/g1/dirtyCardQueue.hpp"
52 #include "gc/g1/satbMarkQueue.hpp"
53 #endif // INCLUDE_ALL_GCS
54 #ifdef ZERO
55 # include "stack_zero.hpp"
56 #endif
57
58 class ThreadSafepointState;
59 class ThreadsList;
60 class NestedThreadsList;
61
62 class JvmtiThreadState;
63 class JvmtiGetLoadedClassesClosure;
64 class ThreadStatistics;
65 class ConcurrentLocksDump;
66 class ParkEvent;
67 class Parker;
68
69 class ciEnv;
70 class CompileThread;
71 class CompileLog;
72 class CompileTask;
73 class CompileQueue;
74 class CompilerCounters;
75 class vframeArray;
76
77 class DeoptResourceMark;
78 class jvmtiDeferredLocalVariableSet;
79
80 class GCTaskQueue;
85 template <class T, MEMFLAGS F> class ChunkedList;
86 typedef ChunkedList<Metadata*, mtInternal> MetadataOnStackBuffer;
87
88 DEBUG_ONLY(class ResourceMark;)
89
90 class WorkerThread;
91
92 // Class hierarchy
93 // - Thread
94 // - NamedThread
95 // - VMThread
96 // - ConcurrentGCThread
97 // - WorkerThread
98 // - GangWorker
99 // - GCTaskThread
100 // - JavaThread
101 // - various subclasses eg CompilerThread, ServiceThread
102 // - WatcherThread
103
104 class Thread: public ThreadShadow {
105 friend class Threads;
106 friend class VMStructs;
107 friend class JVMCIVMStructs;
108 private:
109
110 #ifndef USE_LIBRARY_BASED_TLS_ONLY
111 // Current thread is maintained as a thread-local variable
112 static THREAD_LOCAL_DECL Thread* _thr_current;
113 #endif
114
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 // JavaThread lifecycle support:
124 friend class ScanHazardPtrGatherProtectedThreadsClosure;
125 friend class ScanHazardPtrGatherThreadsListClosure;
126 friend class ScanHazardPtrPrintMatchingThreadsClosure;
127 friend class ThreadsListHandle;
128 friend class ThreadsListSetter;
129 ThreadsList* volatile _threads_hazard_ptr;
130 ThreadsList* cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value);
131 ThreadsList* get_threads_hazard_ptr();
132 void set_threads_hazard_ptr(ThreadsList* new_list);
133 static bool is_hazard_ptr_tagged(ThreadsList* list) {
134 return (intptr_t(list) & intptr_t(1)) == intptr_t(1);
135 }
136 static ThreadsList* tag_hazard_ptr(ThreadsList* list) {
137 return (ThreadsList*)(intptr_t(list) | intptr_t(1));
138 }
139 static ThreadsList* untag_hazard_ptr(ThreadsList* list) {
140 return (ThreadsList*)(intptr_t(list) & ~intptr_t(1));
141 }
142 NestedThreadsList* _nested_threads_hazard_ptr;
143 NestedThreadsList* get_nested_threads_hazard_ptr() {
144 return _nested_threads_hazard_ptr;
145 }
146 void set_nested_threads_hazard_ptr(NestedThreadsList* value) {
147 assert(Threads_lock->owned_by_self(),
148 "must own Threads_lock for _nested_threads_hazard_ptr to be valid.");
149 _nested_threads_hazard_ptr = value;
150 }
151 // This field is enabled via -XX:+EnableThreadSMRStatistics:
152 uint _nested_threads_hazard_ptr_cnt;
153 void dec_nested_threads_hazard_ptr_cnt() {
154 assert(_nested_threads_hazard_ptr_cnt != 0, "mismatched {dec,inc}_nested_threads_hazard_ptr_cnt()");
155 _nested_threads_hazard_ptr_cnt--;
156 }
157 void inc_nested_threads_hazard_ptr_cnt() {
158 _nested_threads_hazard_ptr_cnt++;
159 }
160 uint nested_threads_hazard_ptr_cnt() {
161 return _nested_threads_hazard_ptr_cnt;
162 }
163
164 public:
165 void* operator new(size_t size) throw() { return allocate(size, true); }
166 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
167 return allocate(size, false); }
168 void operator delete(void* p);
169
170 protected:
171 static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
172 private:
173
174 // ***************************************************************
175 // Suspend and resume support
176 // ***************************************************************
177 //
178 // VM suspend/resume no longer exists - it was once used for various
179 // things including safepoints but was deprecated and finally removed
180 // in Java 7. Because VM suspension was considered "internal" Java-level
181 // suspension was considered "external", and this legacy naming scheme
182 // remains.
183 //
383 virtual bool is_Named_thread() const { return false; }
384 virtual bool is_Worker_thread() const { return false; }
385
386 // Can this thread make Java upcalls
387 virtual bool can_call_java() const { return false; }
388
389 // Casts
390 virtual WorkerThread* as_Worker_thread() const { return NULL; }
391
392 virtual char* name() const { return (char*)"Unknown thread"; }
393
394 // Returns the current thread (ASSERTS if NULL)
395 static inline Thread* current();
396 // Returns the current thread, or NULL if not attached
397 static inline Thread* current_or_null();
398 // Returns the current thread, or NULL if not attached, and is
399 // safe for use from signal-handlers
400 static inline Thread* current_or_null_safe();
401
402 // Common thread operations
403 #ifdef ASSERT
404 static void check_for_dangling_thread_pointer(Thread *thread);
405 #endif
406 static void set_priority(Thread* thread, ThreadPriority priority);
407 static ThreadPriority get_priority(const Thread* const thread);
408 static void start(Thread* thread);
409 static void interrupt(Thread* thr);
410 static bool is_interrupted(Thread* thr, bool clear_interrupted);
411
412 void set_native_thread_name(const char *name) {
413 assert(Thread::current() == this, "set_native_thread_name can only be called on the current thread");
414 os::set_native_thread_name(name);
415 }
416
417 ObjectMonitor** omInUseList_addr() { return (ObjectMonitor **)&omInUseList; }
418 Monitor* SR_lock() const { return _SR_lock; }
419
420 bool has_async_exception() const { return (_suspend_flags & _has_async_exception) != 0; }
421
422 inline void set_suspend_flag(SuspendFlags f);
423 inline void clear_suspend_flag(SuspendFlags f);
424
425 inline void set_has_async_exception();
601 address stack_base() const { assert(_stack_base != NULL,"Sanity check"); return _stack_base; }
602 void set_stack_base(address base) { _stack_base = base; }
603 size_t stack_size() const { return _stack_size; }
604 void set_stack_size(size_t size) { _stack_size = size; }
605 address stack_end() const { return stack_base() - stack_size(); }
606 void record_stack_base_and_size();
607
608 bool on_local_stack(address adr) const {
609 // QQQ this has knowledge of direction, ought to be a stack method
610 return (_stack_base >= adr && adr >= stack_end());
611 }
612
613 uintptr_t self_raw_id() { return _self_raw_id; }
614 void set_self_raw_id(uintptr_t value) { _self_raw_id = value; }
615
616 int lgrp_id() const { return _lgrp_id; }
617 void set_lgrp_id(int value) { _lgrp_id = value; }
618
619 // Printing
620 virtual void print_on(outputStream* st) const;
621 virtual void print_nested_threads_hazard_ptrs_on(outputStream* st) const;
622 void print() const { print_on(tty); }
623 virtual void print_on_error(outputStream* st, char* buf, int buflen) const;
624 void print_value_on(outputStream* st) const;
625
626 // Debug-only code
627 #ifdef ASSERT
628 private:
629 // Deadlock detection support for Mutex locks. List of locks own by thread.
630 Monitor* _owned_locks;
631 // Mutex::set_owner_implementation is the only place where _owned_locks is modified,
632 // thus the friendship
633 friend class Mutex;
634 friend class Monitor;
635
636 public:
637 void print_owned_locks_on(outputStream* st) const;
638 void print_owned_locks() const { print_owned_locks_on(tty); }
639 Monitor* owned_locks() const { return _owned_locks; }
640 bool owns_locks() const { return owned_locks() != NULL; }
641 bool owns_locks_but_compiled_lock() const;
822 static void start();
823 static void stop();
824 // Only allow start once the VM is sufficiently initialized
825 // Otherwise the first task to enroll will trigger the start
826 static void make_startable();
827 private:
828 int sleep() const;
829 };
830
831
832 class CompilerThread;
833
834 typedef void (*ThreadFunction)(JavaThread*, TRAPS);
835
836 class JavaThread: public Thread {
837 friend class VMStructs;
838 friend class JVMCIVMStructs;
839 friend class WhiteBox;
840 private:
841 JavaThread* _next; // The next thread in the Threads list
842 bool _on_thread_list; // Is set when this JavaThread is added to the Threads list
843 oop _threadObj; // The Java level thread object
844
845 #ifdef ASSERT
846 private:
847 int _java_call_counter;
848
849 public:
850 int java_call_counter() { return _java_call_counter; }
851 void inc_java_call_counter() { _java_call_counter++; }
852 void dec_java_call_counter() {
853 assert(_java_call_counter > 0, "Invalid nesting of JavaCallWrapper");
854 _java_call_counter--;
855 }
856 private: // restore original namespace restriction
857 #endif // ifdef ASSERT
858
859 #ifndef PRODUCT
860 public:
861 enum {
862 jump_ring_buffer_size = 16
1150 intptr_t* last_Java_sp() const { return _anchor.last_Java_sp(); }
1151
1152 // last_Java_pc
1153
1154 address last_Java_pc(void) { return _anchor.last_Java_pc(); }
1155
1156 // Safepoint support
1157 #if !(defined(PPC64) || defined(AARCH64))
1158 JavaThreadState thread_state() const { return _thread_state; }
1159 void set_thread_state(JavaThreadState s) { _thread_state = s; }
1160 #else
1161 // Use membars when accessing volatile _thread_state. See
1162 // Threads::create_vm() for size checks.
1163 inline JavaThreadState thread_state() const;
1164 inline void set_thread_state(JavaThreadState s);
1165 #endif
1166 ThreadSafepointState *safepoint_state() const { return _safepoint_state; }
1167 void set_safepoint_state(ThreadSafepointState *state) { _safepoint_state = state; }
1168 bool is_at_poll_safepoint() { return _safepoint_state->is_at_poll_safepoint(); }
1169
1170 // JavaThread termination and lifecycle support:
1171 void smr_delete();
1172 bool on_thread_list() { return _on_thread_list; }
1173 void set_on_thread_list() { _on_thread_list = true; }
1174
1175 // thread has called JavaThread::exit() or is terminated
1176 bool is_exiting() const;
1177 // thread is terminated (no longer on the threads list); we compare
1178 // against the two non-terminated values so that a freed JavaThread
1179 // will also be considered terminated.
1180 bool check_is_terminated(TerminatedTypes l_terminated) const {
1181 return l_terminated != _not_terminated && l_terminated != _thread_exiting;
1182 }
1183 bool is_terminated();
1184 void set_terminated(TerminatedTypes t);
1185 // special for Threads::remove() which is static:
1186 void set_terminated_value();
1187 void block_if_vm_exited();
1188
1189 bool doing_unsafe_access() { return _doing_unsafe_access; }
1190 void set_doing_unsafe_access(bool val) { _doing_unsafe_access = val; }
1191
1192 bool do_not_unlock_if_synchronized() { return _do_not_unlock_if_synchronized; }
1193 void set_do_not_unlock_if_synchronized(bool val) { _do_not_unlock_if_synchronized = val; }
1194
1195 // Suspend/resume support for JavaThread
1196 private:
1197 inline void set_ext_suspended();
1198 inline void clear_ext_suspended();
1199
1200 public:
1201 void java_suspend();
1202 void java_resume();
1203 int java_suspend_self();
1204
1205 void check_and_wait_while_suspended() {
1206 assert(JavaThread::current() == this, "sanity check");
1226 static void check_special_condition_for_native_trans_and_transition(JavaThread *thread);
1227
1228 bool is_ext_suspend_completed(bool called_by_wait, int delay, uint32_t *bits);
1229 bool is_ext_suspend_completed_with_lock(uint32_t *bits) {
1230 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
1231 // Warning: is_ext_suspend_completed() may temporarily drop the
1232 // SR_lock to allow the thread to reach a stable thread state if
1233 // it is currently in a transient thread state.
1234 return is_ext_suspend_completed(false /* !called_by_wait */,
1235 SuspendRetryDelay, bits);
1236 }
1237
1238 // We cannot allow wait_for_ext_suspend_completion() to run forever or
1239 // we could hang. SuspendRetryCount and SuspendRetryDelay are normally
1240 // passed as the count and delay parameters. Experiments with specific
1241 // calls to wait_for_ext_suspend_completion() can be done by passing
1242 // other values in the code. Experiments with all calls can be done
1243 // via the appropriate -XX options.
1244 bool wait_for_ext_suspend_completion(int count, int delay, uint32_t *bits);
1245
1246 // test for suspend - most (all?) of these should go away
1247 bool is_thread_fully_suspended(bool wait_for_suspend, uint32_t *bits);
1248
1249 inline void set_external_suspend();
1250 inline void clear_external_suspend();
1251
1252 inline void set_deopt_suspend();
1253 inline void clear_deopt_suspend();
1254 bool is_deopt_suspend() { return (_suspend_flags & _deopt_suspend) != 0; }
1255
1256 bool is_external_suspend() const {
1257 return (_suspend_flags & _external_suspend) != 0;
1258 }
1259 // Whenever a thread transitions from native to vm/java it must suspend
1260 // if external|deopt suspend is present.
1261 bool is_suspend_after_native() const {
1262 return (_suspend_flags & (_external_suspend | _deopt_suspend)) != 0;
1263 }
1264
1265 // external suspend request is completed
1266 bool is_ext_suspended() const {
1267 return (_suspend_flags & _ext_suspended) != 0;
1268 }
2075 IdealGraphPrinter *_ideal_graph_printer;
2076 public:
2077 IdealGraphPrinter *ideal_graph_printer() { return _ideal_graph_printer; }
2078 void set_ideal_graph_printer(IdealGraphPrinter *n) { _ideal_graph_printer = n; }
2079 #endif
2080
2081 // Get/set the thread's current task
2082 CompileTask* task() { return _task; }
2083 void set_task(CompileTask* task) { _task = task; }
2084 };
2085
2086 inline CompilerThread* CompilerThread::current() {
2087 return JavaThread::current()->as_CompilerThread();
2088 }
2089
2090 // The active thread queue. It also keeps track of the current used
2091 // thread priorities.
2092 class Threads: AllStatic {
2093 friend class VMStructs;
2094 private:
2095 // Safe Memory Reclamation (SMR) support:
2096 static Monitor* _smr_delete_lock;
2097 // The '_cnt', '_max' and '_times" fields are enabled via
2098 // -XX:+EnableThreadSMRStatistics:
2099 static uint _smr_delete_lock_wait_cnt;
2100 static uint _smr_delete_lock_wait_max;
2101 static volatile jint _smr_delete_notify;
2102 static volatile jint _smr_deleted_thread_cnt;
2103 static volatile jint _smr_deleted_thread_time_max;
2104 static volatile jint _smr_deleted_thread_times;
2105 static ThreadsList* volatile _smr_java_thread_list;
2106 static ThreadsList* get_smr_java_thread_list();
2107 static ThreadsList* xchg_smr_java_thread_list(ThreadsList* new_list);
2108 static long _smr_java_thread_list_alloc_cnt;
2109 static long _smr_java_thread_list_free_cnt;
2110 static uint _smr_java_thread_list_max;
2111 static uint _smr_nested_thread_list_max;
2112 static volatile jint _smr_tlh_cnt;
2113 static volatile jint _smr_tlh_time_max;
2114 static volatile jint _smr_tlh_times;
2115 static ThreadsList* _smr_to_delete_list;
2116 static uint _smr_to_delete_list_cnt;
2117 static uint _smr_to_delete_list_max;
2118
2119 static JavaThread* _thread_list;
2120 static int _number_of_threads;
2121 static int _number_of_non_daemon_threads;
2122 static int _return_code;
2123 static int _thread_claim_parity;
2124 #ifdef ASSERT
2125 static bool _vm_complete;
2126 #endif
2127
2128 static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2129 static void initialize_jsr292_core_classes(TRAPS);
2130
2131 static void smr_free_list(ThreadsList* threads);
2132
2133 public:
2134 // Thread management
2135 // force_daemon is a concession to JNI, where we may need to add a
2136 // thread to the thread list before allocating its thread object
2137 static void add(JavaThread* p, bool force_daemon = false);
2138 static void remove(JavaThread* p);
2139 static void threads_do(ThreadClosure* tc);
2140 static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2141
2142 // SMR support:
2143 template <class T>
2144 static void threads_do_smr(T *tc, Thread *self);
2145 static ThreadsList *acquire_stable_list(Thread *self, bool is_ThreadsListSetter);
2146 static ThreadsList *acquire_stable_list_fast_path(Thread *self);
2147 static ThreadsList *acquire_stable_list_nested_path(Thread *self);
2148 static void release_stable_list(Thread *self);
2149 static void release_stable_list_fast_path(Thread *self);
2150 static void release_stable_list_nested_path(Thread *self);
2151 static void release_stable_list_wake_up(char *log_str);
2152 static bool is_a_protected_JavaThread(JavaThread *thread);
2153 static bool is_a_protected_JavaThread_with_lock(JavaThread *thread) {
2154 MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
2155 return is_a_protected_JavaThread(thread);
2156 }
2157 static void smr_delete(JavaThread *thread);
2158 // The coordination between Threads::release_stable_list() and
2159 // Threads::smr_delete() uses the smr_delete_lock in order to
2160 // reduce the traffic on the Threads_lock.
2161 static Monitor* smr_delete_lock() { return _smr_delete_lock; }
2162 // The smr_delete_notify flag is used for proper double-check
2163 // locking in order to reduce the traffic on the smr_delete_lock.
2164 static bool smr_delete_notify();
2165 static void set_smr_delete_notify();
2166 static void clear_smr_delete_notify();
2167 static void inc_smr_deleted_thread_cnt();
2168 static void update_smr_deleted_thread_time_max(jint new_value);
2169 static void add_smr_deleted_thread_times(jint add_value);
2170 static void inc_smr_tlh_cnt();
2171 static void update_smr_tlh_time_max(jint new_value);
2172 static void add_smr_tlh_times(jint add_value);
2173
2174 // Initializes the vm and creates the vm thread
2175 static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2176 static void convert_vm_init_libraries_to_agents();
2177 static void create_vm_init_libraries();
2178 static void create_vm_init_agents();
2179 static void shutdown_vm_agents();
2180 static bool destroy_vm();
2181 // Supported VM versions via JNI
2182 // Includes JNI_VERSION_1_1
2183 static jboolean is_supported_jni_version_including_1_1(jint version);
2184 // Does not include JNI_VERSION_1_1
2185 static jboolean is_supported_jni_version(jint version);
2186
2187 // The "thread claim parity" provides a way for threads to be claimed
2188 // by parallel worker tasks.
2189 //
2190 // Each thread contains a a "parity" field. A task will claim the
2191 // thread only if its parity field is the same as the global parity,
2192 // which is updated by calling change_thread_claim_parity().
2193 //
2214 // Apply "f->do_oop" to roots in all threads that
2215 // are part of compiled frames
2216 static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2217
2218 static void convert_hcode_pointers();
2219 static void restore_hcode_pointers();
2220
2221 // Sweeper
2222 static void nmethods_do(CodeBlobClosure* cf);
2223
2224 // RedefineClasses support
2225 static void metadata_do(void f(Metadata*));
2226 static void metadata_handles_do(void f(Metadata*));
2227
2228 #ifdef ASSERT
2229 static bool is_vm_complete() { return _vm_complete; }
2230 #endif
2231
2232 // Verification
2233 static void verify();
2234 static void log_smr_statistics();
2235 static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);
2236 static void print_smr_info_on(outputStream* st);
2237 static void print_smr_info_elements_on(outputStream* st, ThreadsList* t_list);
2238 static void print(bool print_stacks, bool internal_format) {
2239 // this function is only used by debug.cpp
2240 print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2241 }
2242 static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2243 static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2244 int buflen, bool* found_current);
2245 static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2246
2247 // Get Java threads that are waiting to enter a monitor.
2248 static GrowableArray<JavaThread*>* get_pending_threads(ThreadsList * t_list,
2249 int count, address monitor);
2250
2251 // Get owning Java thread from the monitor's owner field.
2252 static JavaThread *owning_thread_from_monitor_owner(ThreadsList * t_list,
2253 address owner);
2254
2255 // Number of threads on the active threads list
2256 static int number_of_threads() { return _number_of_threads; }
2257 // Number of non-daemon threads on the active threads list
2258 static int number_of_non_daemon_threads() { return _number_of_non_daemon_threads; }
2259
2260 // Deoptimizes all frames tied to marked nmethods
2261 static void deoptimized_wrt_marked_nmethods();
2262 };
2263
2264
2265 // Thread iterator
2266 class ThreadClosure: public StackObj {
2267 public:
2268 virtual void do_thread(Thread* thread) = 0;
2269 };
2270
2271 class SignalHandlerMark: public StackObj {
2272 private:
2273 Thread* _thread;
2274 public:
2275 SignalHandlerMark(Thread* t) {
2276 _thread = t;
2277 if (_thread) _thread->enter_signal_handler();
2278 }
2279 ~SignalHandlerMark() {
2280 if (_thread) _thread->leave_signal_handler();
2281 _thread = NULL;
|