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
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.");
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 // The coordination between Threads::release_stable_list() and
2131 // Threads::smr_delete() uses the smr_delete_lock in order to
2132 // reduce the traffic on the Threads_lock.
2133 static Monitor* _smr_delete_lock;
2134 // The '_cnt', '_max' and '_times" fields are enabled via
2135 // -XX:+EnableThreadSMRStatistics (see thread.cpp for a
2136 // description about each field):
2137 static uint _smr_delete_lock_wait_cnt;
2138 static uint _smr_delete_lock_wait_max;
2139 // The smr_delete_notify flag is used for proper double-check
2140 // locking in order to reduce the traffic on the smr_delete_lock.
2141 static volatile uint _smr_delete_notify;
2142 static volatile uint _smr_deleted_thread_cnt;
2143 static volatile uint _smr_deleted_thread_time_max;
2144 static volatile uint _smr_deleted_thread_times;
2145 static ThreadsList* volatile _smr_java_thread_list;
2146 static uint64_t _smr_java_thread_list_alloc_cnt;
2147 static uint64_t _smr_java_thread_list_free_cnt;
2148 static uint _smr_java_thread_list_max;
2149 static uint _smr_nested_thread_list_max;
2150 static volatile uint _smr_tlh_cnt;
2151 static volatile uint _smr_tlh_time_max;
2152 static volatile uint _smr_tlh_times;
2153 static ThreadsList* _smr_to_delete_list;
2154 static uint _smr_to_delete_list_cnt;
2155 static uint _smr_to_delete_list_max;
2156
2157 static JavaThread* _thread_list;
2158 static int _number_of_threads;
2159 static int _number_of_non_daemon_threads;
2160 static int _return_code;
2161 static int _thread_claim_parity;
2162 #ifdef ASSERT
2163 static bool _vm_complete;
2164 #endif
2165
2166 static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2167 static void initialize_jsr292_core_classes(TRAPS);
2168
2169 static ThreadsList *acquire_stable_list_fast_path(Thread *self);
2170 static ThreadsList *acquire_stable_list_nested_path(Thread *self);
2171 static void add_smr_deleted_thread_times(uint add_value);
2172 static void clear_smr_delete_notify();
2173 static ThreadsList* get_smr_java_thread_list();
2174 static void inc_smr_deleted_thread_cnt();
2175 static void release_stable_list_fast_path(Thread *self);
2176 static void release_stable_list_nested_path(Thread *self);
2177 static void release_stable_list_wake_up(char *log_str);
2178 static void set_smr_delete_notify();
2179 static Monitor* smr_delete_lock() { return _smr_delete_lock; }
2180 static bool smr_delete_notify();
2181 static void smr_free_list(ThreadsList* threads);
2182 static void update_smr_deleted_thread_time_max(uint new_value);
2183 static ThreadsList* xchg_smr_java_thread_list(ThreadsList* new_list);
2184
2185 public:
2186 // Thread management
2187 // force_daemon is a concession to JNI, where we may need to add a
2188 // thread to the thread list before allocating its thread object
2189 static void add(JavaThread* p, bool force_daemon = false);
2190 static void remove(JavaThread* p);
2191 static void threads_do(ThreadClosure* tc);
2192 static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2193
2194 // SMR support:
2195 static ThreadsList *acquire_stable_list(Thread *self, bool is_ThreadsListSetter);
2196 static void release_stable_list(Thread *self);
2197 static bool is_a_protected_JavaThread(JavaThread *thread);
2198 static bool is_a_protected_JavaThread_with_lock(JavaThread *thread) {
2199 MutexLockerEx ml(Threads_lock->owned_by_self() ? NULL : Threads_lock);
2200 return is_a_protected_JavaThread(thread);
2201 }
2202 static void smr_delete(JavaThread *thread);
2203 static void inc_smr_tlh_cnt();
2204 static void update_smr_tlh_time_max(uint new_value);
2205 static void add_smr_tlh_times(uint add_value);
2206
2207 // Initializes the vm and creates the vm thread
2208 static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2209 static void convert_vm_init_libraries_to_agents();
2210 static void create_vm_init_libraries();
2211 static void create_vm_init_agents();
2212 static void shutdown_vm_agents();
2213 static bool destroy_vm();
2214 // Supported VM versions via JNI
2215 // Includes JNI_VERSION_1_1
2216 static jboolean is_supported_jni_version_including_1_1(jint version);
2217 // Does not include JNI_VERSION_1_1
2218 static jboolean is_supported_jni_version(jint version);
2219
2220 // The "thread claim parity" provides a way for threads to be claimed
2221 // by parallel worker tasks.
2222 //
2223 // Each thread contains a a "parity" field. A task will claim the
2224 // thread only if its parity field is the same as the global parity,
2225 // which is updated by calling change_thread_claim_parity().
2226 //
2247 // Apply "f->do_oop" to roots in all threads that
2248 // are part of compiled frames
2249 static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2250
2251 static void convert_hcode_pointers();
2252 static void restore_hcode_pointers();
2253
2254 // Sweeper
2255 static void nmethods_do(CodeBlobClosure* cf);
2256
2257 // RedefineClasses support
2258 static void metadata_do(void f(Metadata*));
2259 static void metadata_handles_do(void f(Metadata*));
2260
2261 #ifdef ASSERT
2262 static bool is_vm_complete() { return _vm_complete; }
2263 #endif
2264
2265 // Verification
2266 static void verify();
2267 static void log_smr_statistics();
2268 static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);
2269 static void print_smr_info_on(outputStream* st);
2270 static void print_smr_info_elements_on(outputStream* st, ThreadsList* t_list);
2271 static void print(bool print_stacks, bool internal_format) {
2272 // this function is only used by debug.cpp
2273 print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2274 }
2275 static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2276 static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2277 int buflen, bool* found_current);
2278 static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2279
2280 // Get Java threads that are waiting to enter a monitor.
2281 static GrowableArray<JavaThread*>* get_pending_threads(ThreadsList * t_list,
2282 int count, address monitor);
2283
2284 // Get owning Java thread from the monitor's owner field.
2285 static JavaThread *owning_thread_from_monitor_owner(ThreadsList * t_list,
2286 address owner);
2287
2288 // Number of threads on the active threads list
2289 static int number_of_threads() { return _number_of_threads; }
2290 // Number of non-daemon threads on the active threads list
|
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 ThreadsSMRSupport;
62 class NestedThreadsList;
63
64 class JvmtiThreadState;
65 class JvmtiGetLoadedClassesClosure;
66 class ThreadStatistics;
67 class ConcurrentLocksDump;
68 class ParkEvent;
69 class Parker;
70
71 class ciEnv;
72 class CompileThread;
73 class CompileLog;
74 class CompileTask;
75 class CompileQueue;
76 class CompilerCounters;
77 class vframeArray;
78
79 class DeoptResourceMark;
80 class jvmtiDeferredLocalVariableSet;
81
87 template <class T, MEMFLAGS F> class ChunkedList;
88 typedef ChunkedList<Metadata*, mtInternal> MetadataOnStackBuffer;
89
90 DEBUG_ONLY(class ResourceMark;)
91
92 class WorkerThread;
93
94 // Class hierarchy
95 // - Thread
96 // - NamedThread
97 // - VMThread
98 // - ConcurrentGCThread
99 // - WorkerThread
100 // - GangWorker
101 // - GCTaskThread
102 // - JavaThread
103 // - various subclasses eg CompilerThread, ServiceThread
104 // - WatcherThread
105
106 class Thread: public ThreadShadow {
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
125 // JavaThread lifecycle support:
126 friend class ScanHazardPtrGatherProtectedThreadsClosure; // for cmpxchg_threads_hazard_ptr(), get_threads_hazard_ptr(), is_hazard_ptr_tagged() access
127 friend class ScanHazardPtrGatherThreadsListClosure; // for get_nested_threads_hazard_ptr(), get_threads_hazard_ptr(), untag_hazard_ptr() access
128 friend class ScanHazardPtrPrintMatchingThreadsClosure; // for get_threads_hazard_ptr(), is_hazard_ptr_tagged() access
129 friend class ThreadsListSetter; // for get_threads_hazard_ptr() access
130 friend class ThreadsSMRSupport; // for get_threads_hazard_ptr() access
131
132 ThreadsList* volatile _threads_hazard_ptr;
133 ThreadsList* cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value);
134 ThreadsList* get_threads_hazard_ptr();
135 void set_threads_hazard_ptr(ThreadsList* new_list);
136 static bool is_hazard_ptr_tagged(ThreadsList* list) {
137 return (intptr_t(list) & intptr_t(1)) == intptr_t(1);
138 }
139 static ThreadsList* tag_hazard_ptr(ThreadsList* list) {
140 return (ThreadsList*)(intptr_t(list) | intptr_t(1));
141 }
142 static ThreadsList* untag_hazard_ptr(ThreadsList* list) {
143 return (ThreadsList*)(intptr_t(list) & ~intptr_t(1));
144 }
145 NestedThreadsList* _nested_threads_hazard_ptr;
146 NestedThreadsList* get_nested_threads_hazard_ptr() {
147 return _nested_threads_hazard_ptr;
148 }
149 void set_nested_threads_hazard_ptr(NestedThreadsList* value) {
150 assert(Threads_lock->owned_by_self(),
151 "must own Threads_lock for _nested_threads_hazard_ptr to be valid.");
2111 IdealGraphPrinter *_ideal_graph_printer;
2112 public:
2113 IdealGraphPrinter *ideal_graph_printer() { return _ideal_graph_printer; }
2114 void set_ideal_graph_printer(IdealGraphPrinter *n) { _ideal_graph_printer = n; }
2115 #endif
2116
2117 // Get/set the thread's current task
2118 CompileTask* task() { return _task; }
2119 void set_task(CompileTask* task) { _task = task; }
2120 };
2121
2122 inline CompilerThread* CompilerThread::current() {
2123 return JavaThread::current()->as_CompilerThread();
2124 }
2125
2126 // The active thread queue. It also keeps track of the current used
2127 // thread priorities.
2128 class Threads: AllStatic {
2129 friend class VMStructs;
2130 private:
2131 static JavaThread* _thread_list;
2132 static int _number_of_threads;
2133 static int _number_of_non_daemon_threads;
2134 static int _return_code;
2135 static int _thread_claim_parity;
2136 #ifdef ASSERT
2137 static bool _vm_complete;
2138 #endif
2139
2140 static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
2141 static void initialize_jsr292_core_classes(TRAPS);
2142
2143 public:
2144 // Thread management
2145 // force_daemon is a concession to JNI, where we may need to add a
2146 // thread to the thread list before allocating its thread object
2147 static void add(JavaThread* p, bool force_daemon = false);
2148 static void remove(JavaThread* p);
2149 static void threads_do(ThreadClosure* tc);
2150 static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
2151
2152 // Initializes the vm and creates the vm thread
2153 static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
2154 static void convert_vm_init_libraries_to_agents();
2155 static void create_vm_init_libraries();
2156 static void create_vm_init_agents();
2157 static void shutdown_vm_agents();
2158 static bool destroy_vm();
2159 // Supported VM versions via JNI
2160 // Includes JNI_VERSION_1_1
2161 static jboolean is_supported_jni_version_including_1_1(jint version);
2162 // Does not include JNI_VERSION_1_1
2163 static jboolean is_supported_jni_version(jint version);
2164
2165 // The "thread claim parity" provides a way for threads to be claimed
2166 // by parallel worker tasks.
2167 //
2168 // Each thread contains a a "parity" field. A task will claim the
2169 // thread only if its parity field is the same as the global parity,
2170 // which is updated by calling change_thread_claim_parity().
2171 //
2192 // Apply "f->do_oop" to roots in all threads that
2193 // are part of compiled frames
2194 static void compiled_frame_oops_do(OopClosure* f, CodeBlobClosure* cf);
2195
2196 static void convert_hcode_pointers();
2197 static void restore_hcode_pointers();
2198
2199 // Sweeper
2200 static void nmethods_do(CodeBlobClosure* cf);
2201
2202 // RedefineClasses support
2203 static void metadata_do(void f(Metadata*));
2204 static void metadata_handles_do(void f(Metadata*));
2205
2206 #ifdef ASSERT
2207 static bool is_vm_complete() { return _vm_complete; }
2208 #endif
2209
2210 // Verification
2211 static void verify();
2212 static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks);
2213 static void print(bool print_stacks, bool internal_format) {
2214 // this function is only used by debug.cpp
2215 print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */);
2216 }
2217 static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
2218 static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
2219 int buflen, bool* found_current);
2220 static void print_threads_compiling(outputStream* st, char* buf, int buflen);
2221
2222 // Get Java threads that are waiting to enter a monitor.
2223 static GrowableArray<JavaThread*>* get_pending_threads(ThreadsList * t_list,
2224 int count, address monitor);
2225
2226 // Get owning Java thread from the monitor's owner field.
2227 static JavaThread *owning_thread_from_monitor_owner(ThreadsList * t_list,
2228 address owner);
2229
2230 // Number of threads on the active threads list
2231 static int number_of_threads() { return _number_of_threads; }
2232 // Number of non-daemon threads on the active threads list
|