< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




1297 
1298 WatcherThread::WatcherThread() : NonJavaThread() {
1299   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1300   if (os::create_thread(this, os::watcher_thread)) {
1301     _watcher_thread = this;
1302 
1303     // Set the watcher thread to the highest OS priority which should not be
1304     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1305     // is created. The only normal thread using this priority is the reference
1306     // handler thread, which runs for very short intervals only.
1307     // If the VMThread's priority is not lower than the WatcherThread profiling
1308     // will be inaccurate.
1309     os::set_priority(this, MaxPriority);
1310     if (!DisableStartThread) {
1311       os::start_thread(this);
1312     }
1313   }
1314 }
1315 
1316 int WatcherThread::sleep() const {
1317   // The WatcherThread does not participate in the safepoint protocol
1318   // for the PeriodicTask_lock because it is not a JavaThread.
1319   MutexLockerEx ml(PeriodicTask_lock, Mutex::_no_safepoint_check_flag);
1320 
1321   if (_should_terminate) {
1322     // check for termination before we do any housekeeping or wait
1323     return 0;  // we did not sleep.
1324   }
1325 
1326   // remaining will be zero if there are no tasks,
1327   // causing the WatcherThread to sleep until a task is
1328   // enrolled
1329   int remaining = PeriodicTask::time_to_wait();
1330   int time_slept = 0;
1331 
1332   // we expect this to timeout - we only ever get unparked when
1333   // we should terminate or when a new task has been enrolled
1334   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1335 
1336   jlong time_before_loop = os::javaTimeNanos();
1337 
1338   while (true) {
1339     bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag,


1410           err.print_raw_cr("# [ timer expired, abort... ]");
1411           // skip atexit/vm_exit/vm_abort hooks
1412           os::die();
1413         }
1414 
1415         // Wait a second, then recheck for timeout.
1416         os::naked_short_sleep(999);
1417       }
1418     }
1419 
1420     if (_should_terminate) {
1421       // check for termination before posting the next tick
1422       break;
1423     }
1424 
1425     PeriodicTask::real_time_tick(time_waited);
1426   }
1427 
1428   // Signal that it is terminated
1429   {
1430     MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
1431     _watcher_thread = NULL;
1432     Terminator_lock->notify();
1433   }
1434 }
1435 
1436 void WatcherThread::start() {
1437   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1438 
1439   if (watcher_thread() == NULL && _startable) {
1440     _should_terminate = false;
1441     // Create the single instance of WatcherThread
1442     new WatcherThread();
1443   }
1444 }
1445 
1446 void WatcherThread::make_startable() {
1447   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1448   _startable = true;
1449 }
1450 




1297 
1298 WatcherThread::WatcherThread() : NonJavaThread() {
1299   assert(watcher_thread() == NULL, "we can only allocate one WatcherThread");
1300   if (os::create_thread(this, os::watcher_thread)) {
1301     _watcher_thread = this;
1302 
1303     // Set the watcher thread to the highest OS priority which should not be
1304     // used, unless a Java thread with priority java.lang.Thread.MAX_PRIORITY
1305     // is created. The only normal thread using this priority is the reference
1306     // handler thread, which runs for very short intervals only.
1307     // If the VMThread's priority is not lower than the WatcherThread profiling
1308     // will be inaccurate.
1309     os::set_priority(this, MaxPriority);
1310     if (!DisableStartThread) {
1311       os::start_thread(this);
1312     }
1313   }
1314 }
1315 
1316 int WatcherThread::sleep() const {
1317   MutexLocker ml(PeriodicTask_lock);


1318 
1319   if (_should_terminate) {
1320     // check for termination before we do any housekeeping or wait
1321     return 0;  // we did not sleep.
1322   }
1323 
1324   // remaining will be zero if there are no tasks,
1325   // causing the WatcherThread to sleep until a task is
1326   // enrolled
1327   int remaining = PeriodicTask::time_to_wait();
1328   int time_slept = 0;
1329 
1330   // we expect this to timeout - we only ever get unparked when
1331   // we should terminate or when a new task has been enrolled
1332   OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
1333 
1334   jlong time_before_loop = os::javaTimeNanos();
1335 
1336   while (true) {
1337     bool timedout = PeriodicTask_lock->wait(Mutex::_no_safepoint_check_flag,


1408           err.print_raw_cr("# [ timer expired, abort... ]");
1409           // skip atexit/vm_exit/vm_abort hooks
1410           os::die();
1411         }
1412 
1413         // Wait a second, then recheck for timeout.
1414         os::naked_short_sleep(999);
1415       }
1416     }
1417 
1418     if (_should_terminate) {
1419       // check for termination before posting the next tick
1420       break;
1421     }
1422 
1423     PeriodicTask::real_time_tick(time_waited);
1424   }
1425 
1426   // Signal that it is terminated
1427   {
1428     MutexLocker mu(Terminator_lock);
1429     _watcher_thread = NULL;
1430     Terminator_lock->notify();
1431   }
1432 }
1433 
1434 void WatcherThread::start() {
1435   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1436 
1437   if (watcher_thread() == NULL && _startable) {
1438     _should_terminate = false;
1439     // Create the single instance of WatcherThread
1440     new WatcherThread();
1441   }
1442 }
1443 
1444 void WatcherThread::make_startable() {
1445   assert(PeriodicTask_lock->owned_by_self(), "PeriodicTask_lock required");
1446   _startable = true;
1447 }
1448 


< prev index next >