comparison src/share/vm/runtime/thread.cpp @ 6948:e522a00b91aa

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/ after NPG - C++ build works
author Doug Simon <doug.simon@oracle.com>
date Mon, 12 Nov 2012 23:14:12 +0100
parents 957c266d8bc5 c284cf4781f0
children 2cb439954abf
comparison
equal deleted inserted replaced
6711:ae13cc658b80 6948:e522a00b91aa
35 #include "interpreter/interpreter.hpp" 35 #include "interpreter/interpreter.hpp"
36 #include "interpreter/linkResolver.hpp" 36 #include "interpreter/linkResolver.hpp"
37 #include "interpreter/oopMapCache.hpp" 37 #include "interpreter/oopMapCache.hpp"
38 #include "jvmtifiles/jvmtiEnv.hpp" 38 #include "jvmtifiles/jvmtiEnv.hpp"
39 #include "memory/gcLocker.inline.hpp" 39 #include "memory/gcLocker.inline.hpp"
40 #include "memory/metaspaceShared.hpp"
40 #include "memory/oopFactory.hpp" 41 #include "memory/oopFactory.hpp"
41 #include "memory/universe.inline.hpp" 42 #include "memory/universe.inline.hpp"
42 #include "oops/instanceKlass.hpp" 43 #include "oops/instanceKlass.hpp"
43 #include "oops/objArrayOop.hpp" 44 #include "oops/objArrayOop.hpp"
44 #include "oops/oop.inline.hpp" 45 #include "oops/oop.inline.hpp"
177 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) { 178 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
178 if (UseBiasedLocking) { 179 if (UseBiasedLocking) {
179 const int alignment = markOopDesc::biased_lock_alignment; 180 const int alignment = markOopDesc::biased_lock_alignment;
180 size_t aligned_size = size + (alignment - sizeof(intptr_t)); 181 size_t aligned_size = size + (alignment - sizeof(intptr_t));
181 void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC) 182 void* real_malloc_addr = throw_excpt? AllocateHeap(aligned_size, flags, CURRENT_PC)
182 : os::malloc(aligned_size, flags, CURRENT_PC); 183 : AllocateHeap(aligned_size, flags, CURRENT_PC,
184 AllocFailStrategy::RETURN_NULL);
183 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment); 185 void* aligned_addr = (void*) align_size_up((intptr_t) real_malloc_addr, alignment);
184 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <= 186 assert(((uintptr_t) aligned_addr + (uintptr_t) size) <=
185 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size), 187 ((uintptr_t) real_malloc_addr + (uintptr_t) aligned_size),
186 "JavaThread alignment code overflowed allocated storage"); 188 "JavaThread alignment code overflowed allocated storage");
187 if (TraceBiasedLocking) { 189 if (TraceBiasedLocking) {
191 } 193 }
192 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr; 194 ((Thread*) aligned_addr)->_real_malloc_address = real_malloc_addr;
193 return aligned_addr; 195 return aligned_addr;
194 } else { 196 } else {
195 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC) 197 return throw_excpt? AllocateHeap(size, flags, CURRENT_PC)
196 : os::malloc(size, flags, CURRENT_PC); 198 : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
197 } 199 }
198 } 200 }
199 201
200 void Thread::operator delete(void* p) { 202 void Thread::operator delete(void* p) {
201 if (UseBiasedLocking) { 203 if (UseBiasedLocking) {
220 222
221 // allocated data structures 223 // allocated data structures
222 set_osthread(NULL); 224 set_osthread(NULL);
223 set_resource_area(new (mtThread)ResourceArea()); 225 set_resource_area(new (mtThread)ResourceArea());
224 set_handle_area(new (mtThread) HandleArea(NULL)); 226 set_handle_area(new (mtThread) HandleArea(NULL));
227 set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(300, true));
225 set_active_handles(NULL); 228 set_active_handles(NULL);
226 set_free_handle_block(NULL); 229 set_free_handle_block(NULL);
227 set_last_handle_mark(NULL); 230 set_last_handle_mark(NULL);
228 231
229 // This initial value ==> never claimed. 232 // This initial value ==> never claimed.
307 // method to complete because it may need to allocate memory to 310 // method to complete because it may need to allocate memory to
308 // store information for the new thread. 311 // store information for the new thread.
309 312
310 // initialize structure dependent on thread local storage 313 // initialize structure dependent on thread local storage
311 ThreadLocalStorage::set_thread(this); 314 ThreadLocalStorage::set_thread(this);
312
313 // set up any platform-specific state.
314 os::initialize_thread();
315 } 315 }
316 316
317 void Thread::record_stack_base_and_size() { 317 void Thread::record_stack_base_and_size() {
318 set_stack_base(os::current_stack_base()); 318 set_stack_base(os::current_stack_base());
319 set_stack_size(os::current_stack_size()); 319 set_stack_size(os::current_stack_size());
320 320 // CR 7190089: on Solaris, primordial thread's stack is adjusted
321 // in initialize_thread(). Without the adjustment, stack size is
322 // incorrect if stack is set to unlimited (ulimit -s unlimited).
323 // So far, only Solaris has real implementation of initialize_thread().
324 //
325 // set up any platform-specific state.
326 os::initialize_thread(this);
327
328 #if INCLUDE_NMT
321 // record thread's native stack, stack grows downward 329 // record thread's native stack, stack grows downward
322 address vm_base = _stack_base - _stack_size; 330 address stack_low_addr = stack_base() - stack_size();
323 MemTracker::record_virtual_memory_reserve(vm_base, _stack_size, 331 MemTracker::record_thread_stack(stack_low_addr, stack_size(), this,
324 CURRENT_PC, this); 332 CURRENT_PC);
325 MemTracker::record_virtual_memory_type(vm_base, mtThreadStack); 333 #endif // INCLUDE_NMT
326 } 334 }
327 335
328 336
329 Thread::~Thread() { 337 Thread::~Thread() {
330 // Reclaim the objectmonitors from the omFreeList of the moribund thread. 338 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
331 ObjectSynchronizer::omFlush (this) ; 339 ObjectSynchronizer::omFlush (this) ;
332 340
333 MemTracker::record_virtual_memory_release((_stack_base - _stack_size), 341 // stack_base can be NULL if the thread is never started or exited before
334 _stack_size, this); 342 // record_stack_base_and_size called. Although, we would like to ensure
343 // that all started threads do call record_stack_base_and_size(), there is
344 // not proper way to enforce that.
345 #if INCLUDE_NMT
346 if (_stack_base != NULL) {
347 address low_stack_addr = stack_base() - stack_size();
348 MemTracker::release_thread_stack(low_stack_addr, stack_size(), this);
349 #ifdef ASSERT
350 set_stack_base(NULL);
351 #endif
352 }
353 #endif // INCLUDE_NMT
335 354
336 // deallocate data structures 355 // deallocate data structures
337 delete resource_area(); 356 delete resource_area();
338 // since the handle marks are using the handle area, we have to deallocated the root 357 // since the handle marks are using the handle area, we have to deallocated the root
339 // handle mark before deallocating the thread's handle area, 358 // handle mark before deallocating the thread's handle area,
347 ParkEvent::Release (_SleepEvent) ; _SleepEvent = NULL ; 366 ParkEvent::Release (_SleepEvent) ; _SleepEvent = NULL ;
348 ParkEvent::Release (_MutexEvent) ; _MutexEvent = NULL ; 367 ParkEvent::Release (_MutexEvent) ; _MutexEvent = NULL ;
349 ParkEvent::Release (_MuxEvent) ; _MuxEvent = NULL ; 368 ParkEvent::Release (_MuxEvent) ; _MuxEvent = NULL ;
350 369
351 delete handle_area(); 370 delete handle_area();
371 delete metadata_handles();
352 372
353 // osthread() can be NULL, if creation of thread failed. 373 // osthread() can be NULL, if creation of thread failed.
354 if (osthread() != NULL) os::free_thread(osthread()); 374 if (osthread() != NULL) os::free_thread(osthread());
355 375
356 delete _SR_lock; 376 delete _SR_lock;
818 838
819 void Thread::nmethods_do(CodeBlobClosure* cf) { 839 void Thread::nmethods_do(CodeBlobClosure* cf) {
820 // no nmethods in a generic thread... 840 // no nmethods in a generic thread...
821 } 841 }
822 842
843 void Thread::metadata_do(void f(Metadata*)) {
844 if (metadata_handles() != NULL) {
845 for (int i = 0; i< metadata_handles()->length(); i++) {
846 f(metadata_handles()->at(i));
847 }
848 }
849 }
850
823 void Thread::print_on(outputStream* st) const { 851 void Thread::print_on(outputStream* st) const {
824 // get_priority assumes osthread initialized 852 // get_priority assumes osthread initialized
825 if (osthread() != NULL) { 853 if (osthread() != NULL) {
826 st->print("prio=%d tid=" INTPTR_FORMAT " ", get_priority(this), this); 854 int os_prio;
855 if (os::get_native_priority(this, &os_prio) == OS_OK) {
856 st->print("os_prio=%d ", os_prio);
857 }
858 st->print("tid=" INTPTR_FORMAT " ", this);
827 osthread()->print_on(st); 859 osthread()->print_on(st);
828 } 860 }
829 debug_only(if (WizardMode) print_owned_locks_on(st);) 861 debug_only(if (WizardMode) print_owned_locks_on(st);)
830 } 862 }
831 863
917 #endif 949 #endif
918 950
919 bool Thread::is_in_stack(address adr) const { 951 bool Thread::is_in_stack(address adr) const {
920 assert(Thread::current() == this, "is_in_stack can only be called from current thread"); 952 assert(Thread::current() == this, "is_in_stack can only be called from current thread");
921 address end = os::current_stack_pointer(); 953 address end = os::current_stack_pointer();
954 // Allow non Java threads to call this without stack_base
955 if (_stack_base == NULL) return true;
922 if (stack_base() >= adr && adr >= end) return true; 956 if (stack_base() >= adr && adr >= end) return true;
923 957
924 return false; 958 return false;
925 } 959 }
926 960
938 // NOTE: this must be called inside the main thread. 972 // NOTE: this must be called inside the main thread.
939 return os::create_main_thread((JavaThread*)this); 973 return os::create_main_thread((JavaThread*)this);
940 } 974 }
941 975
942 static void initialize_class(Symbol* class_name, TRAPS) { 976 static void initialize_class(Symbol* class_name, TRAPS) {
943 klassOop klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK); 977 Klass* klass = SystemDictionary::resolve_or_fail(class_name, true, CHECK);
944 instanceKlass::cast(klass)->initialize(CHECK); 978 InstanceKlass::cast(klass)->initialize(CHECK);
945 } 979 }
946 980
947 981
948 // Creates the initial ThreadGroup 982 // Creates the initial ThreadGroup
949 static Handle create_initial_thread_group(TRAPS) { 983 static Handle create_initial_thread_group(TRAPS) {
950 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH); 984 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ThreadGroup(), true, CHECK_NH);
951 instanceKlassHandle klass (THREAD, k); 985 instanceKlassHandle klass (THREAD, k);
952 986
953 Handle system_instance = klass->allocate_instance_handle(CHECK_NH); 987 Handle system_instance = klass->allocate_instance_handle(CHECK_NH);
954 { 988 {
955 JavaValue result(T_VOID); 989 JavaValue result(T_VOID);
978 return main_instance; 1012 return main_instance;
979 } 1013 }
980 1014
981 // Creates the initial Thread 1015 // Creates the initial Thread
982 static oop create_initial_thread(Handle thread_group, JavaThread* thread, TRAPS) { 1016 static oop create_initial_thread(Handle thread_group, JavaThread* thread, TRAPS) {
983 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL); 1017 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK_NULL);
984 instanceKlassHandle klass (THREAD, k); 1018 instanceKlassHandle klass (THREAD, k);
985 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL); 1019 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
986 1020
987 java_lang_Thread::set_thread(thread_oop(), thread); 1021 java_lang_Thread::set_thread(thread_oop(), thread);
988 java_lang_Thread::set_priority(thread_oop(), NormPriority); 1022 java_lang_Thread::set_priority(thread_oop(), NormPriority);
1000 CHECK_NULL); 1034 CHECK_NULL);
1001 return thread_oop(); 1035 return thread_oop();
1002 } 1036 }
1003 1037
1004 static void call_initializeSystemClass(TRAPS) { 1038 static void call_initializeSystemClass(TRAPS) {
1005 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK); 1039 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
1006 instanceKlassHandle klass (THREAD, k); 1040 instanceKlassHandle klass (THREAD, k);
1007 1041
1008 JavaValue result(T_VOID); 1042 JavaValue result(T_VOID);
1009 JavaCalls::call_static(&result, klass, vmSymbols::initializeSystemClass_name(), 1043 JavaCalls::call_static(&result, klass, vmSymbols::initializeSystemClass_name(),
1010 vmSymbols::void_method_signature(), CHECK); 1044 vmSymbols::void_method_signature(), CHECK);
1011 } 1045 }
1012 1046
1013 char java_runtime_name[128] = ""; 1047 char java_runtime_name[128] = "";
1048 char java_runtime_version[128] = "";
1014 1049
1015 // extract the JRE name from sun.misc.Version.java_runtime_name 1050 // extract the JRE name from sun.misc.Version.java_runtime_name
1016 static const char* get_java_runtime_name(TRAPS) { 1051 static const char* get_java_runtime_name(TRAPS) {
1017 klassOop k = SystemDictionary::find(vmSymbols::sun_misc_Version(), 1052 Klass* k = SystemDictionary::find(vmSymbols::sun_misc_Version(),
1018 Handle(), Handle(), CHECK_AND_CLEAR_NULL); 1053 Handle(), Handle(), CHECK_AND_CLEAR_NULL);
1019 fieldDescriptor fd; 1054 fieldDescriptor fd;
1020 bool found = k != NULL && 1055 bool found = k != NULL &&
1021 instanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_name_name(), 1056 InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_name_name(),
1022 vmSymbols::string_signature(), &fd); 1057 vmSymbols::string_signature(), &fd);
1023 if (found) { 1058 if (found) {
1024 oop name_oop = k->java_mirror()->obj_field(fd.offset()); 1059 oop name_oop = k->java_mirror()->obj_field(fd.offset());
1025 if (name_oop == NULL) 1060 if (name_oop == NULL)
1026 return NULL; 1061 return NULL;
1031 } else { 1066 } else {
1032 return NULL; 1067 return NULL;
1033 } 1068 }
1034 } 1069 }
1035 1070
1071 // extract the JRE version from sun.misc.Version.java_runtime_version
1072 static const char* get_java_runtime_version(TRAPS) {
1073 Klass* k = SystemDictionary::find(vmSymbols::sun_misc_Version(),
1074 Handle(), Handle(), CHECK_AND_CLEAR_NULL);
1075 fieldDescriptor fd;
1076 bool found = k != NULL &&
1077 InstanceKlass::cast(k)->find_local_field(vmSymbols::java_runtime_version_name(),
1078 vmSymbols::string_signature(), &fd);
1079 if (found) {
1080 oop name_oop = k->java_mirror()->obj_field(fd.offset());
1081 if (name_oop == NULL)
1082 return NULL;
1083 const char* name = java_lang_String::as_utf8_string(name_oop,
1084 java_runtime_version,
1085 sizeof(java_runtime_version));
1086 return name;
1087 } else {
1088 return NULL;
1089 }
1090 }
1091
1036 // General purpose hook into Java code, run once when the VM is initialized. 1092 // General purpose hook into Java code, run once when the VM is initialized.
1037 // The Java library method itself may be changed independently from the VM. 1093 // The Java library method itself may be changed independently from the VM.
1038 static void call_postVMInitHook(TRAPS) { 1094 static void call_postVMInitHook(TRAPS) {
1039 klassOop k = SystemDictionary::PostVMInitHook_klass(); 1095 Klass* k = SystemDictionary::PostVMInitHook_klass();
1040 instanceKlassHandle klass (THREAD, k); 1096 instanceKlassHandle klass (THREAD, k);
1041 if (klass.not_null()) { 1097 if (klass.not_null()) {
1042 JavaValue result(T_VOID); 1098 JavaValue result(T_VOID);
1043 JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(), 1099 JavaCalls::call_static(&result, klass, vmSymbols::run_method_name(),
1044 vmSymbols::void_method_signature(), 1100 vmSymbols::void_method_signature(),
1050 // the vm info string 1106 // the vm info string
1051 ResourceMark rm(THREAD); 1107 ResourceMark rm(THREAD);
1052 const char *vm_info = VM_Version::vm_info_string(); 1108 const char *vm_info = VM_Version::vm_info_string();
1053 1109
1054 // java.lang.System class 1110 // java.lang.System class
1055 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK); 1111 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
1056 instanceKlassHandle klass (THREAD, k); 1112 instanceKlassHandle klass (THREAD, k);
1057 1113
1058 // setProperty arguments 1114 // setProperty arguments
1059 Handle key_str = java_lang_String::create_from_str("java.vm.info", CHECK); 1115 Handle key_str = java_lang_String::create_from_str("java.vm.info", CHECK);
1060 Handle value_str = java_lang_String::create_from_str(vm_info, CHECK); 1116 Handle value_str = java_lang_String::create_from_str(vm_info, CHECK);
1075 1131
1076 void JavaThread::allocate_threadObj(Handle thread_group, char* thread_name, bool daemon, TRAPS) { 1132 void JavaThread::allocate_threadObj(Handle thread_group, char* thread_name, bool daemon, TRAPS) {
1077 assert(thread_group.not_null(), "thread group should be specified"); 1133 assert(thread_group.not_null(), "thread group should be specified");
1078 assert(threadObj() == NULL, "should only create Java thread object once"); 1134 assert(threadObj() == NULL, "should only create Java thread object once");
1079 1135
1080 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK); 1136 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
1081 instanceKlassHandle klass (THREAD, k); 1137 instanceKlassHandle klass (THREAD, k);
1082 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); 1138 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
1083 1139
1084 java_lang_Thread::set_thread(thread_oop(), this); 1140 java_lang_Thread::set_thread(thread_oop(), this);
1085 java_lang_Thread::set_priority(thread_oop(), NormPriority); 1141 java_lang_Thread::set_priority(thread_oop(), NormPriority);
1162 // The watcher thread exists to simulate timer interrupts. It should 1218 // The watcher thread exists to simulate timer interrupts. It should
1163 // be replaced by an abstraction over whatever native support for 1219 // be replaced by an abstraction over whatever native support for
1164 // timer interrupts exists on the platform. 1220 // timer interrupts exists on the platform.
1165 1221
1166 WatcherThread* WatcherThread::_watcher_thread = NULL; 1222 WatcherThread* WatcherThread::_watcher_thread = NULL;
1223 bool WatcherThread::_startable = false;
1167 volatile bool WatcherThread::_should_terminate = false; 1224 volatile bool WatcherThread::_should_terminate = false;
1168 1225
1169 WatcherThread::WatcherThread() : Thread() { 1226 WatcherThread::WatcherThread() : Thread() {
1170 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread"); 1227 assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1171 if (os::create_thread(this, os::watcher_thread)) { 1228 if (os::create_thread(this, os::watcher_thread)) {
1182 os::start_thread(this); 1239 os::start_thread(this);
1183 } 1240 }
1184 } 1241 }
1185 } 1242 }
1186 1243
1244 int WatcherThread::sleep() const {
1245 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1246
1247 // remaining will be zero if there are no tasks,
1248 // causing the WatcherThread to sleep until a task is
1249 // enrolled
1250 int remaining = PeriodicTask::time_to_wait();
1251 int time_slept = 0;
1252
1253 // we expect this to timeout - we only ever get unparked when
1254 // we should terminate or when a new task has been enrolled
1255 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1256
1257 jlong time_before_loop = os::javaTimeNanos();
1258
1259 for (;;) {
1260 bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, remaining);
1261 jlong now = os::javaTimeNanos();
1262
1263 if (remaining == 0) {
1264 // if we didn't have any tasks we could have waited for a long time
1265 // consider the time_slept zero and reset time_before_loop
1266 time_slept = 0;
1267 time_before_loop = now;
1268 } else {
1269 // need to recalulate since we might have new tasks in _tasks
1270 time_slept = (int) ((now - time_before_loop) / 1000000);
1271 }
1272
1273 // Change to task list or spurious wakeup of some kind
1274 if (timedout || _should_terminate) {
1275 break;
1276 }
1277
1278 remaining = PeriodicTask::time_to_wait();
1279 if (remaining == 0) {
1280 // Last task was just disenrolled so loop around and wait until
1281 // another task gets enrolled
1282 continue;
1283 }
1284
1285 remaining -= time_slept;
1286 if (remaining <= 0)
1287 break;
1288 }
1289
1290 return time_slept;
1291 }
1292
1187 void WatcherThread::run() { 1293 void WatcherThread::run() {
1188 assert(this == watcher_thread(), "just checking"); 1294 assert(this == watcher_thread(), "just checking");
1189 1295
1190 this->record_stack_base_and_size(); 1296 this->record_stack_base_and_size();
1191 this->initialize_thread_local_storage(); 1297 this->initialize_thread_local_storage();
1194 assert(watcher_thread() == Thread::current(), "thread consistency check"); 1300 assert(watcher_thread() == Thread::current(), "thread consistency check");
1195 assert(watcher_thread() == this, "thread consistency check"); 1301 assert(watcher_thread() == this, "thread consistency check");
1196 1302
1197 // Calculate how long it'll be until the next PeriodicTask work 1303 // Calculate how long it'll be until the next PeriodicTask work
1198 // should be done, and sleep that amount of time. 1304 // should be done, and sleep that amount of time.
1199 size_t time_to_wait = PeriodicTask::time_to_wait(); 1305 int time_waited = sleep();
1200
1201 // we expect this to timeout - we only ever get unparked when
1202 // we should terminate
1203 {
1204 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1205
1206 jlong prev_time = os::javaTimeNanos();
1207 for (;;) {
1208 int res= _SleepEvent->park(time_to_wait);
1209 if (res == OS_TIMEOUT || _should_terminate)
1210 break;
1211 // spurious wakeup of some kind
1212 jlong now = os::javaTimeNanos();
1213 time_to_wait -= (now - prev_time) / 1000000;
1214 if (time_to_wait <= 0)
1215 break;
1216 prev_time = now;
1217 }
1218 }
1219 1306
1220 if (is_error_reported()) { 1307 if (is_error_reported()) {
1221 // A fatal error has happened, the error handler(VMError::report_and_die) 1308 // A fatal error has happened, the error handler(VMError::report_and_die)
1222 // should abort JVM after creating an error log file. However in some 1309 // should abort JVM after creating an error log file. However in some
1223 // rare cases, the error handler itself might deadlock. Here we try to 1310 // rare cases, the error handler itself might deadlock. Here we try to
1243 // ShowMessageBoxOnError when it is ready to abort. 1330 // ShowMessageBoxOnError when it is ready to abort.
1244 os::sleep(this, 5 * 1000, false); 1331 os::sleep(this, 5 * 1000, false);
1245 } 1332 }
1246 } 1333 }
1247 1334
1248 PeriodicTask::real_time_tick(time_to_wait); 1335 PeriodicTask::real_time_tick(time_waited);
1249
1250 // If we have no more tasks left due to dynamic disenrollment,
1251 // shut down the thread since we don't currently support dynamic enrollment
1252 if (PeriodicTask::num_tasks() == 0) {
1253 _should_terminate = true;
1254 }
1255 } 1336 }
1256 1337
1257 // Signal that it is terminated 1338 // Signal that it is terminated
1258 { 1339 {
1259 MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag); 1340 MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1264 // Thread destructor usually does this.. 1345 // Thread destructor usually does this..
1265 ThreadLocalStorage::set_thread(NULL); 1346 ThreadLocalStorage::set_thread(NULL);
1266 } 1347 }
1267 1348
1268 void WatcherThread::start() { 1349 void WatcherThread::start() {
1269 if (watcher_thread() == NULL) { 1350 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1351
1352 if (watcher_thread() == NULL && _startable) {
1270 _should_terminate = false; 1353 _should_terminate = false;
1271 // Create the single instance of WatcherThread 1354 // Create the single instance of WatcherThread
1272 new WatcherThread(); 1355 new WatcherThread();
1273 } 1356 }
1274 } 1357 }
1275 1358
1359 void WatcherThread::make_startable() {
1360 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1361 _startable = true;
1362 }
1363
1276 void WatcherThread::stop() { 1364 void WatcherThread::stop() {
1365 {
1366 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1367 _should_terminate = true;
1368 OrderAccess::fence(); // ensure WatcherThread sees update in main loop
1369
1370 WatcherThread* watcher = watcher_thread();
1371 if (watcher != NULL)
1372 watcher->unpark();
1373 }
1374
1277 // it is ok to take late safepoints here, if needed 1375 // it is ok to take late safepoints here, if needed
1278 MutexLocker mu(Terminator_lock); 1376 MutexLocker mu(Terminator_lock);
1279 _should_terminate = true;
1280 OrderAccess::fence(); // ensure WatcherThread sees update in main loop
1281
1282 Thread* watcher = watcher_thread();
1283 if (watcher != NULL)
1284 watcher->_SleepEvent->unpark();
1285 1377
1286 while(watcher_thread() != NULL) { 1378 while(watcher_thread() != NULL) {
1287 // This wait should make safepoint checks, wait without a timeout, 1379 // This wait should make safepoint checks, wait without a timeout,
1288 // and wait as a suspend-equivalent condition. 1380 // and wait as a suspend-equivalent condition.
1289 // 1381 //
1295 // suspend-equivalent condition solves that timeout problem. 1387 // suspend-equivalent condition solves that timeout problem.
1296 // 1388 //
1297 Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0, 1389 Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0,
1298 Mutex::_as_suspend_equivalent_flag); 1390 Mutex::_as_suspend_equivalent_flag);
1299 } 1391 }
1392 }
1393
1394 void WatcherThread::unpark() {
1395 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() ? NULL : PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1396 PeriodicTask_lock->notify();
1300 } 1397 }
1301 1398
1302 void WatcherThread::print_on(outputStream* st) const { 1399 void WatcherThread::print_on(outputStream* st) const {
1303 st->print("\"%s\" ", name()); 1400 st->print("\"%s\" ", name());
1304 Thread::print_on(st); 1401 Thread::print_on(st);
1332 set_deopt_nmethod(NULL); 1429 set_deopt_nmethod(NULL);
1333 clear_must_deopt_id(); 1430 clear_must_deopt_id();
1334 set_monitor_chunks(NULL); 1431 set_monitor_chunks(NULL);
1335 set_next(NULL); 1432 set_next(NULL);
1336 set_thread_state(_thread_new); 1433 set_thread_state(_thread_new);
1434 #if INCLUDE_NMT
1337 set_recorder(NULL); 1435 set_recorder(NULL);
1436 #endif
1338 _terminated = _not_terminated; 1437 _terminated = _not_terminated;
1339 _privileged_stack_top = NULL; 1438 _privileged_stack_top = NULL;
1340 _array_for_gc = NULL; 1439 _array_for_gc = NULL;
1341 _suspend_equivalent = false; 1440 _suspend_equivalent = false;
1342 _in_deopt_handler = 0; 1441 _in_deopt_handler = 0;
1497 JavaThread::~JavaThread() { 1596 JavaThread::~JavaThread() {
1498 if (TraceThreadEvents) { 1597 if (TraceThreadEvents) {
1499 tty->print_cr("terminate thread %p", this); 1598 tty->print_cr("terminate thread %p", this);
1500 } 1599 }
1501 1600
1502 // Info NMT that this JavaThread is exiting, its memory 1601 // By now, this thread should already be invisible to safepoint,
1503 // recorder should be collected 1602 // and its per-thread recorder also collected.
1504 assert(!is_safepoint_visible(), "wrong state"); 1603 assert(!is_safepoint_visible(), "wrong state");
1505 MemTracker::thread_exiting(this); 1604 #if INCLUDE_NMT
1605 assert(get_recorder() == NULL, "Already collected");
1606 #endif // INCLUDE_NMT
1506 1607
1507 // JSR166 -- return the parker to the free list 1608 // JSR166 -- return the parker to the free list
1508 Parker::Release(_parker); 1609 Parker::Release(_parker);
1509 _parker = NULL ; 1610 _parker = NULL ;
1510 1611
1993 tty->print("Async. exception installed at runtime exit (" INTPTR_FORMAT ")", this); 2094 tty->print("Async. exception installed at runtime exit (" INTPTR_FORMAT ")", this);
1994 if (has_last_Java_frame() ) { 2095 if (has_last_Java_frame() ) {
1995 frame f = last_frame(); 2096 frame f = last_frame();
1996 tty->print(" (pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " )", f.pc(), f.sp()); 2097 tty->print(" (pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " )", f.pc(), f.sp());
1997 } 2098 }
1998 tty->print_cr(" of type: %s", instanceKlass::cast(_pending_async_exception->klass())->external_name()); 2099 tty->print_cr(" of type: %s", InstanceKlass::cast(_pending_async_exception->klass())->external_name());
1999 } 2100 }
2000 _pending_async_exception = NULL; 2101 _pending_async_exception = NULL;
2001 clear_has_async_exception(); 2102 clear_has_async_exception();
2002 } 2103 }
2003 } 2104 }
2116 // Set async. pending exception in thread. 2217 // Set async. pending exception in thread.
2117 set_pending_async_exception(java_throwable); 2218 set_pending_async_exception(java_throwable);
2118 2219
2119 if (TraceExceptions) { 2220 if (TraceExceptions) {
2120 ResourceMark rm; 2221 ResourceMark rm;
2121 tty->print_cr("Pending Async. exception installed of type: %s", instanceKlass::cast(_pending_async_exception->klass())->external_name()); 2222 tty->print_cr("Pending Async. exception installed of type: %s", InstanceKlass::cast(_pending_async_exception->klass())->external_name());
2122 } 2223 }
2123 // for AbortVMOnException flag 2224 // for AbortVMOnException flag
2124 NOT_PRODUCT(Exceptions::debug_check_abort(instanceKlass::cast(_pending_async_exception->klass())->external_name())); 2225 NOT_PRODUCT(Exceptions::debug_check_abort(InstanceKlass::cast(_pending_async_exception->klass())->external_name()));
2125 } 2226 }
2126 } 2227 }
2127 2228
2128 2229
2129 // Interrupt thread so it will wake up from a potential wait() 2230 // Interrupt thread so it will wake up from a potential wait()
2403 } 2504 }
2404 } 2505 }
2405 } 2506 }
2406 2507
2407 void JavaThread::remove_stack_guard_pages() { 2508 void JavaThread::remove_stack_guard_pages() {
2509 assert(Thread::current() == this, "from different thread");
2408 if (_stack_guard_state == stack_guard_unused) return; 2510 if (_stack_guard_state == stack_guard_unused) return;
2409 address low_addr = stack_base() - stack_size(); 2511 address low_addr = stack_base() - stack_size();
2410 size_t len = (StackYellowPages + StackRedPages) * os::vm_page_size(); 2512 size_t len = (StackYellowPages + StackRedPages) * os::vm_page_size();
2411 2513
2412 if (os::allocate_stack_guard_pages()) { 2514 if (os::allocate_stack_guard_pages()) {
2568 if (!has_last_Java_frame()) return; 2670 if (!has_last_Java_frame()) return;
2569 // BiasedLocking needs an updated RegisterMap for the revoke monitors pass 2671 // BiasedLocking needs an updated RegisterMap for the revoke monitors pass
2570 StackFrameStream fst(this, UseBiasedLocking); 2672 StackFrameStream fst(this, UseBiasedLocking);
2571 for(; !fst.is_done(); fst.next()) { 2673 for(; !fst.is_done(); fst.next()) {
2572 if (fst.current()->should_be_deoptimized()) { 2674 if (fst.current()->should_be_deoptimized()) {
2675 if (LogCompilation && xtty != NULL) {
2676 nmethod* nm = fst.current()->cb()->as_nmethod_or_null();
2677 xtty->elem("deoptimized thread='" UINTX_FORMAT "' compile_id='%d'",
2678 this->name(), nm != NULL ? nm->compile_id() : -1);
2679 }
2680
2573 Deoptimization::deoptimize(this, *fst.current(), fst.register_map(), Deoptimization::Reason_constraint); 2681 Deoptimization::deoptimize(this, *fst.current(), fst.register_map(), Deoptimization::Reason_constraint);
2574 } 2682 }
2575 } 2683 }
2576 } 2684 }
2577 2685
2669 2777
2670 // Traverse instance variables at the end since the GC may be moving things 2778 // Traverse instance variables at the end since the GC may be moving things
2671 // around using this function 2779 // around using this function
2672 f->do_oop((oop*) &_threadObj); 2780 f->do_oop((oop*) &_threadObj);
2673 f->do_oop((oop*) &_vm_result); 2781 f->do_oop((oop*) &_vm_result);
2674 f->do_oop((oop*) &_vm_result_2);
2675 #ifdef GRAAL 2782 #ifdef GRAAL
2676 f->do_oop((oop*) &_graal_deopt_info); 2783 f->do_oop((oop*) &_graal_deopt_info);
2677 #endif 2784 #endif
2678 f->do_oop((oop*) &_exception_oop); 2785 f->do_oop((oop*) &_exception_oop);
2679 f->do_oop((oop*) &_pending_async_exception); 2786 f->do_oop((oop*) &_pending_async_exception);
2698 2805
2699 if (has_last_Java_frame()) { 2806 if (has_last_Java_frame()) {
2700 // Traverse the execution stack 2807 // Traverse the execution stack
2701 for(StackFrameStream fst(this); !fst.is_done(); fst.next()) { 2808 for(StackFrameStream fst(this); !fst.is_done(); fst.next()) {
2702 fst.current()->nmethods_do(cf); 2809 fst.current()->nmethods_do(cf);
2810 }
2811 }
2812 }
2813
2814 void JavaThread::metadata_do(void f(Metadata*)) {
2815 Thread::metadata_do(f);
2816 if (has_last_Java_frame()) {
2817 // Traverse the execution stack to call f() on the methods in the stack
2818 for(StackFrameStream fst(this); !fst.is_done(); fst.next()) {
2819 fst.current()->metadata_do(f);
2820 }
2821 } else if (is_Compiler_thread()) {
2822 // need to walk ciMetadata in current compile tasks to keep alive.
2823 CompilerThread* ct = (CompilerThread*)this;
2824 if (ct->env() != NULL) {
2825 ct->env()->metadata_do(f);
2703 } 2826 }
2704 } 2827 }
2705 } 2828 }
2706 2829
2707 // Printing 2830 // Printing
2733 2856
2734 // Called by Threads::print() for VM_PrintThreads operation 2857 // Called by Threads::print() for VM_PrintThreads operation
2735 void JavaThread::print_on(outputStream *st) const { 2858 void JavaThread::print_on(outputStream *st) const {
2736 st->print("\"%s\" ", get_thread_name()); 2859 st->print("\"%s\" ", get_thread_name());
2737 oop thread_oop = threadObj(); 2860 oop thread_oop = threadObj();
2738 if (thread_oop != NULL && java_lang_Thread::is_daemon(thread_oop)) st->print("daemon "); 2861 if (thread_oop != NULL) {
2862 st->print("#" INT64_FORMAT " ", java_lang_Thread::thread_id(thread_oop));
2863 if (java_lang_Thread::is_daemon(thread_oop)) st->print("daemon ");
2864 st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
2865 }
2739 Thread::print_on(st); 2866 Thread::print_on(st);
2740 // print guess for valid stack memory region (assume 4K pages); helps lock debugging 2867 // print guess for valid stack memory region (assume 4K pages); helps lock debugging
2741 st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12)); 2868 st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
2742 if (thread_oop != NULL && JDK_Version::is_gte_jdk15x_version()) { 2869 if (thread_oop != NULL && JDK_Version::is_gte_jdk15x_version()) {
2743 st->print_cr(" java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop)); 2870 st->print_cr(" java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
2892 // Set the thread field (a JavaThread *) of the 3019 // Set the thread field (a JavaThread *) of the
2893 // oop representing the java_lang_Thread to the new thread (a JavaThread *). 3020 // oop representing the java_lang_Thread to the new thread (a JavaThread *).
2894 3021
2895 Handle thread_oop(Thread::current(), 3022 Handle thread_oop(Thread::current(),
2896 JNIHandles::resolve_non_null(jni_thread)); 3023 JNIHandles::resolve_non_null(jni_thread));
2897 assert(instanceKlass::cast(thread_oop->klass())->is_linked(), 3024 assert(InstanceKlass::cast(thread_oop->klass())->is_linked(),
2898 "must be initialized"); 3025 "must be initialized");
2899 set_threadObj(thread_oop()); 3026 set_threadObj(thread_oop());
2900 java_lang_Thread::set_thread(thread_oop(), this); 3027 java_lang_Thread::set_thread(thread_oop(), this);
2901 3028
2902 if (prio == NoPriority) { 3029 if (prio == NoPriority) {
3093 } 3220 }
3094 return NULL; 3221 return NULL;
3095 } 3222 }
3096 3223
3097 3224
3098 klassOop JavaThread::security_get_caller_class(int depth) { 3225 Klass* JavaThread::security_get_caller_class(int depth) {
3099 vframeStream vfst(this); 3226 vframeStream vfst(this);
3100 vfst.security_get_caller_frame(depth); 3227 vfst.security_get_caller_frame(depth);
3101 if (!vfst.at_end()) { 3228 if (!vfst.at_end()) {
3102 return vfst.method()->method_holder(); 3229 return vfst.method()->method_holder();
3103 } 3230 }
3133 JavaThread* Threads::_thread_list = NULL; 3260 JavaThread* Threads::_thread_list = NULL;
3134 int Threads::_number_of_threads = 0; 3261 int Threads::_number_of_threads = 0;
3135 int Threads::_number_of_non_daemon_threads = 0; 3262 int Threads::_number_of_non_daemon_threads = 0;
3136 int Threads::_return_code = 0; 3263 int Threads::_return_code = 0;
3137 size_t JavaThread::_stack_size_at_create = 0; 3264 size_t JavaThread::_stack_size_at_create = 0;
3265 #ifdef ASSERT
3266 bool Threads::_vm_complete = false;
3267 #endif
3138 3268
3139 // All JavaThreads 3269 // All JavaThreads
3140 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next()) 3270 #define ALL_JAVA_THREADS(X) for (JavaThread* X = _thread_list; X; X = X->next())
3141 3271
3142 void os_stream(); 3272 void os_stream();
3330 EXCEPTION_MARK; 3460 EXCEPTION_MARK;
3331 3461
3332 // At this point, the Universe is initialized, but we have not executed 3462 // At this point, the Universe is initialized, but we have not executed
3333 // any byte code. Now is a good time (the only time) to dump out the 3463 // any byte code. Now is a good time (the only time) to dump out the
3334 // internal state of the JVM for sharing. 3464 // internal state of the JVM for sharing.
3335
3336 if (DumpSharedSpaces) { 3465 if (DumpSharedSpaces) {
3337 Universe::heap()->preload_and_dump(CHECK_0); 3466 MetaspaceShared::preload_and_dump(CHECK_0);
3338 ShouldNotReachHere(); 3467 ShouldNotReachHere();
3339 } 3468 }
3340 3469
3341 // Always call even when there are not JVMTI environments yet, since environments 3470 // Always call even when there are not JVMTI environments yet, since environments
3342 // may be attached late and JVMTI must track phases of VM execution 3471 // may be attached late and JVMTI must track phases of VM execution
3361 if (AggressiveOpts) { 3490 if (AggressiveOpts) {
3362 { 3491 {
3363 // Forcibly initialize java/util/HashMap and mutate the private 3492 // Forcibly initialize java/util/HashMap and mutate the private
3364 // static final "frontCacheEnabled" field before we start creating instances 3493 // static final "frontCacheEnabled" field before we start creating instances
3365 #ifdef ASSERT 3494 #ifdef ASSERT
3366 klassOop tmp_k = SystemDictionary::find(vmSymbols::java_util_HashMap(), Handle(), Handle(), CHECK_0); 3495 Klass* tmp_k = SystemDictionary::find(vmSymbols::java_util_HashMap(), Handle(), Handle(), CHECK_0);
3367 assert(tmp_k == NULL, "java/util/HashMap should not be loaded yet"); 3496 assert(tmp_k == NULL, "java/util/HashMap should not be loaded yet");
3368 #endif 3497 #endif
3369 klassOop k_o = SystemDictionary::resolve_or_null(vmSymbols::java_util_HashMap(), Handle(), Handle(), CHECK_0); 3498 Klass* k_o = SystemDictionary::resolve_or_null(vmSymbols::java_util_HashMap(), Handle(), Handle(), CHECK_0);
3370 KlassHandle k = KlassHandle(THREAD, k_o); 3499 KlassHandle k = KlassHandle(THREAD, k_o);
3371 guarantee(k.not_null(), "Must find java/util/HashMap"); 3500 guarantee(k.not_null(), "Must find java/util/HashMap");
3372 instanceKlassHandle ik = instanceKlassHandle(THREAD, k()); 3501 instanceKlassHandle ik = instanceKlassHandle(THREAD, k());
3373 ik->initialize(CHECK_0); 3502 ik->initialize(CHECK_0);
3374 fieldDescriptor fd; 3503 fieldDescriptor fd;
3379 } 3508 }
3380 3509
3381 if (UseStringCache) { 3510 if (UseStringCache) {
3382 // Forcibly initialize java/lang/StringValue and mutate the private 3511 // Forcibly initialize java/lang/StringValue and mutate the private
3383 // static final "stringCacheEnabled" field before we start creating instances 3512 // static final "stringCacheEnabled" field before we start creating instances
3384 klassOop k_o = SystemDictionary::resolve_or_null(vmSymbols::java_lang_StringValue(), Handle(), Handle(), CHECK_0); 3513 Klass* k_o = SystemDictionary::resolve_or_null(vmSymbols::java_lang_StringValue(), Handle(), Handle(), CHECK_0);
3385 // Possible that StringValue isn't present: if so, silently don't break 3514 // Possible that StringValue isn't present: if so, silently don't break
3386 if (k_o != NULL) { 3515 if (k_o != NULL) {
3387 KlassHandle k = KlassHandle(THREAD, k_o); 3516 KlassHandle k = KlassHandle(THREAD, k_o);
3388 instanceKlassHandle ik = instanceKlassHandle(THREAD, k()); 3517 instanceKlassHandle ik = instanceKlassHandle(THREAD, k());
3389 ik->initialize(CHECK_0); 3518 ik->initialize(CHECK_0);
3417 initialize_class(vmSymbols::java_lang_Class(), CHECK_0); 3546 initialize_class(vmSymbols::java_lang_Class(), CHECK_0);
3418 call_initializeSystemClass(CHECK_0); 3547 call_initializeSystemClass(CHECK_0);
3419 3548
3420 // get the Java runtime name after java.lang.System is initialized 3549 // get the Java runtime name after java.lang.System is initialized
3421 JDK_Version::set_runtime_name(get_java_runtime_name(THREAD)); 3550 JDK_Version::set_runtime_name(get_java_runtime_name(THREAD));
3551 JDK_Version::set_runtime_version(get_java_runtime_version(THREAD));
3422 } else { 3552 } else {
3423 warning("java.lang.System not initialized"); 3553 warning("java.lang.System not initialized");
3424 } 3554 }
3425 3555
3426 // an instance of OutOfMemory exception has been allocated earlier 3556 // an instance of OutOfMemory exception has been allocated earlier
3484 #else /* USDT2 */ 3614 #else /* USDT2 */
3485 HOTSPOT_VM_INIT_END(); 3615 HOTSPOT_VM_INIT_END();
3486 #endif /* USDT2 */ 3616 #endif /* USDT2 */
3487 3617
3488 // record VM initialization completion time 3618 // record VM initialization completion time
3619 #if INCLUDE_MANAGEMENT
3489 Management::record_vm_init_completed(); 3620 Management::record_vm_init_completed();
3621 #endif // INCLUDE_MANAGEMENT
3490 3622
3491 // Compute system loader. Note that this has to occur after set_init_completed, since 3623 // Compute system loader. Note that this has to occur after set_init_completed, since
3492 // valid exceptions may be thrown in the process. 3624 // valid exceptions may be thrown in the process.
3493 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and 3625 // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
3494 // set_init_completed has just been called, causing exceptions not to be shortcut 3626 // set_init_completed has just been called, causing exceptions not to be shortcut
3545 if (CleanChunkPoolAsync) { 3677 if (CleanChunkPoolAsync) {
3546 Chunk::start_chunk_pool_cleaner_task(); 3678 Chunk::start_chunk_pool_cleaner_task();
3547 } 3679 }
3548 3680
3549 // initialize compiler(s) 3681 // initialize compiler(s)
3682 #if defined(COMPILER1) || defined(COMPILER2)
3550 CompileBroker::compilation_init(); 3683 CompileBroker::compilation_init();
3551 3684 #endif
3685
3686 #if INCLUDE_MANAGEMENT
3552 Management::initialize(THREAD); 3687 Management::initialize(THREAD);
3688 #endif // INCLUDE_MANAGEMENT
3689
3553 if (HAS_PENDING_EXCEPTION) { 3690 if (HAS_PENDING_EXCEPTION) {
3554 // management agent fails to start possibly due to 3691 // management agent fails to start possibly due to
3555 // configuration problem and is responsible for printing 3692 // configuration problem and is responsible for printing
3556 // stack trace if appropriate. Simply exit VM. 3693 // stack trace if appropriate. Simply exit VM.
3557 vm_exit(1); 3694 vm_exit(1);
3572 if (HAS_PENDING_EXCEPTION) { 3709 if (HAS_PENDING_EXCEPTION) {
3573 CLEAR_PENDING_EXCEPTION; 3710 CLEAR_PENDING_EXCEPTION;
3574 } 3711 }
3575 } 3712 }
3576 3713
3577 // Start up the WatcherThread if there are any periodic tasks 3714 {
3578 // NOTE: All PeriodicTasks should be registered by now. If they 3715 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
3579 // aren't, late joiners might appear to start slowly (we might 3716 // Make sure the watcher thread can be started by WatcherThread::start()
3580 // take a while to process their first tick). 3717 // or by dynamic enrollment.
3581 if (PeriodicTask::num_tasks() > 0) { 3718 WatcherThread::make_startable();
3582 WatcherThread::start(); 3719 // Start up the WatcherThread if there are any periodic tasks
3720 // NOTE: All PeriodicTasks should be registered by now. If they
3721 // aren't, late joiners might appear to start slowly (we might
3722 // take a while to process their first tick).
3723 if (PeriodicTask::num_tasks() > 0) {
3724 WatcherThread::start();
3725 }
3583 } 3726 }
3584 3727
3585 // Give os specific code one last chance to start 3728 // Give os specific code one last chance to start
3586 os::init_3(); 3729 os::init_3();
3587 3730
3588 create_vm_timer.end(); 3731 create_vm_timer.end();
3732 #ifdef ASSERT
3733 _vm_complete = true;
3734 #endif
3589 return JNI_OK; 3735 return JNI_OK;
3590 } 3736 }
3591 3737
3592 // type for the Agent_OnLoad and JVM_OnLoad entry points 3738 // type for the Agent_OnLoad and JVM_OnLoad entry points
3593 extern "C" { 3739 extern "C" {
3714 void Threads::create_vm_init_agents() { 3860 void Threads::create_vm_init_agents() {
3715 extern struct JavaVM_ main_vm; 3861 extern struct JavaVM_ main_vm;
3716 AgentLibrary* agent; 3862 AgentLibrary* agent;
3717 3863
3718 JvmtiExport::enter_onload_phase(); 3864 JvmtiExport::enter_onload_phase();
3865
3719 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) { 3866 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
3720 OnLoadEntry_t on_load_entry = lookup_agent_on_load(agent); 3867 OnLoadEntry_t on_load_entry = lookup_agent_on_load(agent);
3721 3868
3722 if (on_load_entry != NULL) { 3869 if (on_load_entry != NULL) {
3723 // Invoke the Agent_OnLoad function 3870 // Invoke the Agent_OnLoad function
3791 if (this->has_pending_exception()) { 3938 if (this->has_pending_exception()) {
3792 this->clear_pending_exception(); 3939 this->clear_pending_exception();
3793 } 3940 }
3794 3941
3795 EXCEPTION_MARK; 3942 EXCEPTION_MARK;
3796 klassOop k = 3943 Klass* k =
3797 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(), 3944 SystemDictionary::resolve_or_null(vmSymbols::java_lang_Shutdown(),
3798 THREAD); 3945 THREAD);
3799 if (k != NULL) { 3946 if (k != NULL) {
3800 // SystemDictionary::resolve_or_null will return null if there was 3947 // SystemDictionary::resolve_or_null will return null if there was
3801 // an exception. If we cannot load the Shutdown class, just don't 3948 // an exception. If we cannot load the Shutdown class, just don't
3850 // + Return to caller 3997 // + Return to caller
3851 3998
3852 bool Threads::destroy_vm() { 3999 bool Threads::destroy_vm() {
3853 JavaThread* thread = JavaThread::current(); 4000 JavaThread* thread = JavaThread::current();
3854 4001
4002 #ifdef ASSERT
4003 _vm_complete = false;
4004 #endif
3855 // Wait until we are the last non-daemon thread to execute 4005 // Wait until we are the last non-daemon thread to execute
3856 { MutexLocker nu(Threads_lock); 4006 { MutexLocker nu(Threads_lock);
3857 while (Threads::number_of_non_daemon_threads() > 1 ) 4007 while (Threads::number_of_non_daemon_threads() > 1 )
3858 // This wait should make safepoint checks, wait without a timeout, 4008 // This wait should make safepoint checks, wait without a timeout,
3859 // and wait as a suspend-equivalent condition. 4009 // and wait as a suspend-equivalent condition.
4027 // of this thread since it is removed from the queue. 4177 // of this thread since it is removed from the queue.
4028 p->set_terminated_value(); 4178 p->set_terminated_value();
4029 4179
4030 // Now, this thread is not visible to safepoint 4180 // Now, this thread is not visible to safepoint
4031 p->set_safepoint_visible(false); 4181 p->set_safepoint_visible(false);
4032 4182 // once the thread becomes safepoint invisible, we can not use its per-thread
4183 // recorder. And Threads::do_threads() no longer walks this thread, so we have
4184 // to release its per-thread recorder here.
4185 MemTracker::thread_exiting(p);
4033 } // unlock Threads_lock 4186 } // unlock Threads_lock
4034 4187
4035 // Since Events::log uses a lock, we grab it outside the Threads_lock 4188 // Since Events::log uses a lock, we grab it outside the Threads_lock
4036 Events::log(p, "Thread exited: " INTPTR_FORMAT, p); 4189 Events::log(p, "Thread exited: " INTPTR_FORMAT, p);
4037 } 4190 }
4110 p->nmethods_do(cf); 4263 p->nmethods_do(cf);
4111 } 4264 }
4112 VMThread::vm_thread()->nmethods_do(cf); 4265 VMThread::vm_thread()->nmethods_do(cf);
4113 } 4266 }
4114 4267
4268 void Threads::metadata_do(void f(Metadata*)) {
4269 ALL_JAVA_THREADS(p) {
4270 p->metadata_do(f);
4271 }
4272 }
4273
4115 void Threads::gc_epilogue() { 4274 void Threads::gc_epilogue() {
4116 ALL_JAVA_THREADS(p) { 4275 ALL_JAVA_THREADS(p) {
4117 p->gc_epilogue(); 4276 p->gc_epilogue();
4118 } 4277 }
4119 } 4278 }
4233 4392
4234 VMThread::vm_thread()->print_on(st); 4393 VMThread::vm_thread()->print_on(st);
4235 st->cr(); 4394 st->cr();
4236 Universe::heap()->print_gc_threads_on(st); 4395 Universe::heap()->print_gc_threads_on(st);
4237 WatcherThread* wt = WatcherThread::watcher_thread(); 4396 WatcherThread* wt = WatcherThread::watcher_thread();
4238 if (wt != NULL) wt->print_on(st); 4397 if (wt != NULL) {
4239 st->cr(); 4398 wt->print_on(st);
4399 st->cr();
4400 }
4240 CompileBroker::print_compiler_threads_on(st); 4401 CompileBroker::print_compiler_threads_on(st);
4241 st->flush(); 4402 st->flush();
4242 } 4403 }
4243 4404
4244 // Threads::print_on_error() is called by fatal error handler. It's possible 4405 // Threads::print_on_error() is called by fatal error handler. It's possible