716 // safepoint requests from the VMThread |
716 // safepoint requests from the VMThread |
717 |
717 |
718 // temporarily drops SR_lock while doing wait with safepoint check |
718 // temporarily drops SR_lock while doing wait with safepoint check |
719 // (if we're a JavaThread - the WatcherThread can also call this) |
719 // (if we're a JavaThread - the WatcherThread can also call this) |
720 // and increase delay with each retry |
720 // and increase delay with each retry |
721 SR_lock()->wait(!Thread::current()->is_Java_thread(), i * delay); |
721 if (Thread::current()->is_Java_thread()) { |
|
722 SR_lock()->wait(i * delay); |
|
723 } else { |
|
724 SR_lock()->wait_without_safepoint_check(i * delay); |
|
725 } |
722 |
726 |
723 // check the actual thread state instead of what we saved above |
727 // check the actual thread state instead of what we saved above |
724 if (thread_state() != _thread_in_native_trans) { |
728 if (thread_state() != _thread_in_native_trans) { |
725 // the thread has transitioned to another thread state so |
729 // the thread has transitioned to another thread state so |
726 // try all the checks (except this one) one more time. |
730 // try all the checks (except this one) one more time. |
757 // each retry loop. This allows the caller to make use of any |
761 // each retry loop. This allows the caller to make use of any |
758 // unused bits for their own marking purposes. |
762 // unused bits for their own marking purposes. |
759 reset_bits = *bits; |
763 reset_bits = *bits; |
760 |
764 |
761 { |
765 { |
762 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
766 MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
763 is_suspended = is_ext_suspend_completed(true /* called_by_wait */, |
767 is_suspended = is_ext_suspend_completed(true /* called_by_wait */, |
764 delay, bits); |
768 delay, bits); |
765 pending = is_external_suspend(); |
769 pending = is_external_suspend(); |
766 } |
770 } |
767 // must release SR_lock to allow suspension to complete |
771 // must release SR_lock to allow suspension to complete |
791 |
795 |
792 { |
796 { |
793 MutexLocker ml(SR_lock()); |
797 MutexLocker ml(SR_lock()); |
794 // wait with safepoint check (if we're a JavaThread - the WatcherThread |
798 // wait with safepoint check (if we're a JavaThread - the WatcherThread |
795 // can also call this) and increase delay with each retry |
799 // can also call this) and increase delay with each retry |
796 SR_lock()->wait(!Thread::current()->is_Java_thread(), i * delay); |
800 if (Thread::current()->is_Java_thread()) { |
|
801 SR_lock()->wait(i * delay); |
|
802 } else { |
|
803 SR_lock()->wait_without_safepoint_check(i * delay); |
|
804 } |
797 |
805 |
798 is_suspended = is_ext_suspend_completed(true /* called_by_wait */, |
806 is_suspended = is_ext_suspend_completed(true /* called_by_wait */, |
799 delay, bits); |
807 delay, bits); |
800 |
808 |
801 // It is possible for the external suspend request to be cancelled |
809 // It is possible for the external suspend request to be cancelled |
1287 } |
1295 } |
1288 |
1296 |
1289 NonJavaThread::~NonJavaThread() { } |
1297 NonJavaThread::~NonJavaThread() { } |
1290 |
1298 |
1291 void NonJavaThread::add_to_the_list() { |
1299 void NonJavaThread::add_to_the_list() { |
1292 MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1300 MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1293 // Initialize BarrierSet-related data before adding to list. |
1301 // Initialize BarrierSet-related data before adding to list. |
1294 BarrierSet::barrier_set()->on_thread_attach(this); |
1302 BarrierSet::barrier_set()->on_thread_attach(this); |
1295 OrderAccess::release_store(&_next, _the_list._head); |
1303 OrderAccess::release_store(&_next, _the_list._head); |
1296 OrderAccess::release_store(&_the_list._head, this); |
1304 OrderAccess::release_store(&_the_list._head, this); |
1297 } |
1305 } |
1298 |
1306 |
1299 void NonJavaThread::remove_from_the_list() { |
1307 void NonJavaThread::remove_from_the_list() { |
1300 { |
1308 { |
1301 MutexLockerEx ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1309 MutexLocker ml(NonJavaThreadsList_lock, Mutex::_no_safepoint_check_flag); |
1302 // Cleanup BarrierSet-related data before removing from list. |
1310 // Cleanup BarrierSet-related data before removing from list. |
1303 BarrierSet::barrier_set()->on_thread_detach(this); |
1311 BarrierSet::barrier_set()->on_thread_detach(this); |
1304 NonJavaThread* volatile* p = &_the_list._head; |
1312 NonJavaThread* volatile* p = &_the_list._head; |
1305 for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) { |
1313 for (NonJavaThread* t = *p; t != NULL; p = &t->_next, t = *p) { |
1306 if (t == this) { |
1314 if (t == this) { |
1310 } |
1318 } |
1311 } |
1319 } |
1312 // Wait for any in-progress iterators. Concurrent synchronize is not |
1320 // Wait for any in-progress iterators. Concurrent synchronize is not |
1313 // allowed, so do it while holding a dedicated lock. Outside and distinct |
1321 // allowed, so do it while holding a dedicated lock. Outside and distinct |
1314 // from NJTList_lock in case an iteration attempts to lock it. |
1322 // from NJTList_lock in case an iteration attempts to lock it. |
1315 MutexLockerEx ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag); |
1323 MutexLocker ml(NonJavaThreadsListSync_lock, Mutex::_no_safepoint_check_flag); |
1316 _the_list._protect.synchronize(); |
1324 _the_list._protect.synchronize(); |
1317 _next = NULL; // Safe to drop the link now. |
1325 _next = NULL; // Safe to drop the link now. |
1318 } |
1326 } |
1319 |
1327 |
1320 void NonJavaThread::pre_run() { |
1328 void NonJavaThread::pre_run() { |
1395 } |
1403 } |
1396 |
1404 |
1397 int WatcherThread::sleep() const { |
1405 int WatcherThread::sleep() const { |
1398 // The WatcherThread does not participate in the safepoint protocol |
1406 // The WatcherThread does not participate in the safepoint protocol |
1399 // for the PeriodicTask_lock because it is not a JavaThread. |
1407 // for the PeriodicTask_lock because it is not a JavaThread. |
1400 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
1408 MutexLocker ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
1401 |
1409 |
1402 if (_should_terminate) { |
1410 if (_should_terminate) { |
1403 // check for termination before we do any housekeeping or wait |
1411 // check for termination before we do any housekeeping or wait |
1404 return 0; // we did not sleep. |
1412 return 0; // we did not sleep. |
1405 } |
1413 } |
1415 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); |
1423 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); |
1416 |
1424 |
1417 jlong time_before_loop = os::javaTimeNanos(); |
1425 jlong time_before_loop = os::javaTimeNanos(); |
1418 |
1426 |
1419 while (true) { |
1427 while (true) { |
1420 bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, |
1428 bool timedout = PeriodicTask_lock->wait_without_safepoint_check(remaining); |
1421 remaining); |
|
1422 jlong now = os::javaTimeNanos(); |
1429 jlong now = os::javaTimeNanos(); |
1423 |
1430 |
1424 if (remaining == 0) { |
1431 if (remaining == 0) { |
1425 // if we didn't have any tasks we could have waited for a long time |
1432 // if we didn't have any tasks we could have waited for a long time |
1426 // consider the time_slept zero and reset time_before_loop |
1433 // consider the time_slept zero and reset time_before_loop |
1504 PeriodicTask::real_time_tick(time_waited); |
1511 PeriodicTask::real_time_tick(time_waited); |
1505 } |
1512 } |
1506 |
1513 |
1507 // Signal that it is terminated |
1514 // Signal that it is terminated |
1508 { |
1515 { |
1509 MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag); |
1516 MutexLocker mu(Terminator_lock, Mutex::_no_safepoint_check_flag); |
1510 _watcher_thread = NULL; |
1517 _watcher_thread = NULL; |
1511 Terminator_lock->notify_all(); |
1518 Terminator_lock->notify_all(); |
1512 } |
1519 } |
1513 } |
1520 } |
1514 |
1521 |
1544 MutexLocker mu(Terminator_lock); |
1551 MutexLocker mu(Terminator_lock); |
1545 |
1552 |
1546 while (watcher_thread() != NULL) { |
1553 while (watcher_thread() != NULL) { |
1547 // This wait should make safepoint checks, wait without a timeout, |
1554 // This wait should make safepoint checks, wait without a timeout, |
1548 // and wait as a suspend-equivalent condition. |
1555 // and wait as a suspend-equivalent condition. |
1549 Terminator_lock->wait(!Mutex::_no_safepoint_check_flag, 0, |
1556 Terminator_lock->wait(0, Mutex::_as_suspend_equivalent_flag); |
1550 Mutex::_as_suspend_equivalent_flag); |
|
1551 } |
1557 } |
1552 } |
1558 } |
1553 |
1559 |
1554 void WatcherThread::unpark() { |
1560 void WatcherThread::unpark() { |
1555 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); |
1561 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); |
1988 // We have notified the agents that we are exiting, before we go on, |
1994 // We have notified the agents that we are exiting, before we go on, |
1989 // we must check for a pending external suspend request and honor it |
1995 // we must check for a pending external suspend request and honor it |
1990 // in order to not surprise the thread that made the suspend request. |
1996 // in order to not surprise the thread that made the suspend request. |
1991 while (true) { |
1997 while (true) { |
1992 { |
1998 { |
1993 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
1999 MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
1994 if (!is_external_suspend()) { |
2000 if (!is_external_suspend()) { |
1995 set_terminated(_thread_exiting); |
2001 set_terminated(_thread_exiting); |
1996 ThreadService::current_thread_exiting(this, is_daemon(threadObj())); |
2002 ThreadService::current_thread_exiting(this, is_daemon(threadObj())); |
1997 break; |
2003 break; |
1998 } |
2004 } |
2357 ThreadsListHandle tlh; |
2363 ThreadsListHandle tlh; |
2358 if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) { |
2364 if (!tlh.includes(this) || threadObj() == NULL || is_exiting()) { |
2359 return; |
2365 return; |
2360 } |
2366 } |
2361 |
2367 |
2362 { MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2368 { MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2363 if (!is_external_suspend()) { |
2369 if (!is_external_suspend()) { |
2364 // a racing resume has cancelled us; bail out now |
2370 // a racing resume has cancelled us; bail out now |
2365 return; |
2371 return; |
2366 } |
2372 } |
2367 |
2373 |
2416 |
2422 |
2417 assert(_anchor.walkable() || |
2423 assert(_anchor.walkable() || |
2418 (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()), |
2424 (is_Java_thread() && !((JavaThread*)this)->has_last_Java_frame()), |
2419 "must have walkable stack"); |
2425 "must have walkable stack"); |
2420 |
2426 |
2421 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2427 MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2422 |
2428 |
2423 assert(!this->is_ext_suspended(), |
2429 assert(!this->is_ext_suspended(), |
2424 "a thread trying to self-suspend should not already be suspended"); |
2430 "a thread trying to self-suspend should not already be suspended"); |
2425 |
2431 |
2426 if (this->is_suspend_equivalent()) { |
2432 if (this->is_suspend_equivalent()) { |
2586 ThreadsListHandle tlh; |
2592 ThreadsListHandle tlh; |
2587 if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) { |
2593 if (!tlh.includes(this) || is_exiting() || !is_external_suspend()) { |
2588 return; |
2594 return; |
2589 } |
2595 } |
2590 |
2596 |
2591 MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2597 MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag); |
2592 |
2598 |
2593 clear_external_suspend(); |
2599 clear_external_suspend(); |
2594 |
2600 |
2595 if (is_ext_suspended()) { |
2601 if (is_ext_suspended()) { |
2596 clear_ext_suspended(); |
2602 clear_ext_suspended(); |
4336 // Wait until we are the last non-daemon thread to execute |
4342 // Wait until we are the last non-daemon thread to execute |
4337 { MutexLocker nu(Threads_lock); |
4343 { MutexLocker nu(Threads_lock); |
4338 while (Threads::number_of_non_daemon_threads() > 1) |
4344 while (Threads::number_of_non_daemon_threads() > 1) |
4339 // This wait should make safepoint checks, wait without a timeout, |
4345 // This wait should make safepoint checks, wait without a timeout, |
4340 // and wait as a suspend-equivalent condition. |
4346 // and wait as a suspend-equivalent condition. |
4341 Threads_lock->wait(!Mutex::_no_safepoint_check_flag, 0, |
4347 Threads_lock->wait(0, Mutex::_as_suspend_equivalent_flag); |
4342 Mutex::_as_suspend_equivalent_flag); |
|
4343 } |
4348 } |
4344 |
4349 |
4345 EventShutdown e; |
4350 EventShutdown e; |
4346 if (e.should_commit()) { |
4351 if (e.should_commit()) { |
4347 e.set_reason("No remaining non-daemon Java threads"); |
4352 e.set_reason("No remaining non-daemon Java threads"); |
4368 // heap is unparseable if they are caught. Grab the Heap_lock |
4373 // heap is unparseable if they are caught. Grab the Heap_lock |
4369 // to prevent this. The GC vm_operations will not be able to |
4374 // to prevent this. The GC vm_operations will not be able to |
4370 // queue until after the vm thread is dead. After this point, |
4375 // queue until after the vm thread is dead. After this point, |
4371 // we'll never emerge out of the safepoint before the VM exits. |
4376 // we'll never emerge out of the safepoint before the VM exits. |
4372 |
4377 |
4373 MutexLockerEx ml(Heap_lock, Mutex::_no_safepoint_check_flag); |
4378 MutexLocker ml(Heap_lock, Mutex::_no_safepoint_check_flag); |
4374 |
4379 |
4375 VMThread::wait_for_vm_thread_exit(); |
4380 VMThread::wait_for_vm_thread_exit(); |
4376 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint"); |
4381 assert(SafepointSynchronize::is_at_safepoint(), "VM thread should exit at Safepoint"); |
4377 VMThread::destroy(); |
4382 VMThread::destroy(); |
4378 } |
4383 } |