1199 } |
1199 } |
1200 } |
1200 } |
1201 } |
1201 } |
1202 |
1202 |
1203 int WatcherThread::sleep() const { |
1203 int WatcherThread::sleep() const { |
|
1204 // The WatcherThread does not participate in the safepoint protocol |
|
1205 // for the PeriodicTask_lock because it is not a JavaThread. |
1204 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
1206 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
|
1207 |
|
1208 if (_should_terminate) { |
|
1209 // check for termination before we do any housekeeping or wait |
|
1210 return 0; // we did not sleep. |
|
1211 } |
1205 |
1212 |
1206 // remaining will be zero if there are no tasks, |
1213 // remaining will be zero if there are no tasks, |
1207 // causing the WatcherThread to sleep until a task is |
1214 // causing the WatcherThread to sleep until a task is |
1208 // enrolled |
1215 // enrolled |
1209 int remaining = PeriodicTask::time_to_wait(); |
1216 int remaining = PeriodicTask::time_to_wait(); |
1213 // we should terminate or when a new task has been enrolled |
1220 // we should terminate or when a new task has been enrolled |
1214 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); |
1221 OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */); |
1215 |
1222 |
1216 jlong time_before_loop = os::javaTimeNanos(); |
1223 jlong time_before_loop = os::javaTimeNanos(); |
1217 |
1224 |
1218 for (;;) { |
1225 while (true) { |
1219 bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, remaining); |
1226 bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag, |
|
1227 remaining); |
1220 jlong now = os::javaTimeNanos(); |
1228 jlong now = os::javaTimeNanos(); |
1221 |
1229 |
1222 if (remaining == 0) { |
1230 if (remaining == 0) { |
1223 // if we didn't have any tasks we could have waited for a long time |
1231 // if we didn't have any tasks we could have waited for a long time |
1224 // consider the time_slept zero and reset time_before_loop |
1232 // consider the time_slept zero and reset time_before_loop |
1255 |
1263 |
1256 this->record_stack_base_and_size(); |
1264 this->record_stack_base_and_size(); |
1257 this->initialize_thread_local_storage(); |
1265 this->initialize_thread_local_storage(); |
1258 this->set_native_thread_name(this->name()); |
1266 this->set_native_thread_name(this->name()); |
1259 this->set_active_handles(JNIHandleBlock::allocate_block()); |
1267 this->set_active_handles(JNIHandleBlock::allocate_block()); |
1260 while (!_should_terminate) { |
1268 while (true) { |
1261 assert(watcher_thread() == Thread::current(), "thread consistency check"); |
1269 assert(watcher_thread() == Thread::current(), "thread consistency check"); |
1262 assert(watcher_thread() == this, "thread consistency check"); |
1270 assert(watcher_thread() == this, "thread consistency check"); |
1263 |
1271 |
1264 // Calculate how long it'll be until the next PeriodicTask work |
1272 // Calculate how long it'll be until the next PeriodicTask work |
1265 // should be done, and sleep that amount of time. |
1273 // should be done, and sleep that amount of time. |
1291 // ShowMessageBoxOnError when it is ready to abort. |
1299 // ShowMessageBoxOnError when it is ready to abort. |
1292 os::sleep(this, 5 * 1000, false); |
1300 os::sleep(this, 5 * 1000, false); |
1293 } |
1301 } |
1294 } |
1302 } |
1295 |
1303 |
|
1304 if (_should_terminate) { |
|
1305 // check for termination before posting the next tick |
|
1306 break; |
|
1307 } |
|
1308 |
1296 PeriodicTask::real_time_tick(time_waited); |
1309 PeriodicTask::real_time_tick(time_waited); |
1297 } |
1310 } |
1298 |
1311 |
1299 // Signal that it is terminated |
1312 // Signal that it is terminated |
1300 { |
1313 { |
1321 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); |
1334 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); |
1322 _startable = true; |
1335 _startable = true; |
1323 } |
1336 } |
1324 |
1337 |
1325 void WatcherThread::stop() { |
1338 void WatcherThread::stop() { |
1326 // Get the PeriodicTask_lock if we can. If we cannot, then the |
1339 { |
1327 // WatcherThread is using it and we don't want to block on that lock |
1340 // Follow normal safepoint aware lock enter protocol since the |
1328 // here because that might cause a safepoint deadlock depending on |
1341 // WatcherThread is stopped by another JavaThread. |
1329 // what the current WatcherThread tasks are doing. |
1342 MutexLocker ml(PeriodicTask_lock); |
1330 bool have_lock = PeriodicTask_lock->try_lock(); |
1343 _should_terminate = true; |
1331 |
1344 |
1332 _should_terminate = true; |
|
1333 OrderAccess::fence(); // ensure WatcherThread sees update in main loop |
|
1334 |
|
1335 if (have_lock) { |
|
1336 WatcherThread* watcher = watcher_thread(); |
1345 WatcherThread* watcher = watcher_thread(); |
1337 if (watcher != NULL) { |
1346 if (watcher != NULL) { |
1338 // If we managed to get the lock, then we should unpark the |
1347 // unpark the WatcherThread so it can see that it should terminate |
1339 // WatcherThread so that it can see we want it to stop. |
|
1340 watcher->unpark(); |
1348 watcher->unpark(); |
1341 } |
1349 } |
1342 |
1350 } |
1343 PeriodicTask_lock->unlock(); |
1351 |
1344 } |
|
1345 |
|
1346 // it is ok to take late safepoints here, if needed |
|
1347 MutexLocker mu(Terminator_lock); |
1352 MutexLocker mu(Terminator_lock); |
1348 |
1353 |
1349 while (watcher_thread() != NULL) { |
1354 while (watcher_thread() != NULL) { |
1350 // This wait should make safepoint checks, wait without a timeout, |
1355 // This wait should make safepoint checks, wait without a timeout, |
1351 // and wait as a suspend-equivalent condition. |
1356 // and wait as a suspend-equivalent condition. |
1361 Mutex::_as_suspend_equivalent_flag); |
1366 Mutex::_as_suspend_equivalent_flag); |
1362 } |
1367 } |
1363 } |
1368 } |
1364 |
1369 |
1365 void WatcherThread::unpark() { |
1370 void WatcherThread::unpark() { |
1366 MutexLockerEx ml(PeriodicTask_lock->owned_by_self() |
1371 assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required"); |
1367 ? NULL |
|
1368 : PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
|
1369 PeriodicTask_lock->notify(); |
1372 PeriodicTask_lock->notify(); |
1370 } |
1373 } |
1371 |
1374 |
1372 void WatcherThread::print_on(outputStream* st) const { |
1375 void WatcherThread::print_on(outputStream* st) const { |
1373 st->print("\"%s\" ", name()); |
1376 st->print("\"%s\" ", name()); |
3560 CLEAR_PENDING_EXCEPTION; |
3563 CLEAR_PENDING_EXCEPTION; |
3561 } |
3564 } |
3562 } |
3565 } |
3563 |
3566 |
3564 { |
3567 { |
3565 MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag); |
3568 MutexLocker ml(PeriodicTask_lock); |
3566 // Make sure the watcher thread can be started by WatcherThread::start() |
3569 // Make sure the WatcherThread can be started by WatcherThread::start() |
3567 // or by dynamic enrollment. |
3570 // or by dynamic enrollment. |
3568 WatcherThread::make_startable(); |
3571 WatcherThread::make_startable(); |
3569 // Start up the WatcherThread if there are any periodic tasks |
3572 // Start up the WatcherThread if there are any periodic tasks |
3570 // NOTE: All PeriodicTasks should be registered by now. If they |
3573 // NOTE: All PeriodicTasks should be registered by now. If they |
3571 // aren't, late joiners might appear to start slowly (we might |
3574 // aren't, late joiners might appear to start slowly (we might |