src/hotspot/share/runtime/safepoint.cpp
changeset 54621 0b6dc5b93306
parent 54495 941db9c0b5b5
child 54807 33fe50b6d707
equal deleted inserted replaced
54620:13b67c1420b8 54621:0b6dc5b93306
   184   }
   184   }
   185   assert(a == still_running, "Must be the same");
   185   assert(a == still_running, "Must be the same");
   186 }
   186 }
   187 #endif // ASSERT
   187 #endif // ASSERT
   188 
   188 
   189 static void back_off(int iteration) {
   189 static void back_off(int64_t start_time) {
   190   // iteration will be 1 the first time we enter this spin back-off.
   190   // We start with fine-grained nanosleeping until a millisecond has
   191   // naked_short_nanosleep takes tenths of micros which means that
   191   // passed, at which point we resort to plain naked_short_sleep.
   192   // number of nanoseconds is irrelevant if it's below that. We do
   192   if (os::javaTimeNanos() - start_time < NANOSECS_PER_MILLISEC) {
   193   // 20 1 ns sleeps with a total cost of ~1 ms, then we do 1 ms sleeps.
   193     os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS));
   194   jlong sleep_ns = 1;
   194   } else {
   195   if (iteration > 20) {
   195     os::naked_short_sleep(1);
   196     sleep_ns = NANOUNITS / MILLIUNITS;  // 1 ms
   196   }
   197   }
       
   198   os::naked_short_nanosleep(sleep_ns);
       
   199 }
   197 }
   200 
   198 
   201 int SafepointSynchronize::synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running)
   199 int SafepointSynchronize::synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running)
   202 {
   200 {
   203   JavaThreadIteratorWithHandle jtiwh;
   201   JavaThreadIteratorWithHandle jtiwh;
   227 
   225 
   228   DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   226   DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   229 
   227 
   230   *initial_running = still_running;
   228   *initial_running = still_running;
   231 
   229 
       
   230   // If there is no thread still running, we are already done.
       
   231   if (still_running <= 0) {
       
   232     assert(tss_head == NULL, "Must be empty");
       
   233     return 1;
       
   234   }
       
   235 
   232   int iterations = 1; // The first iteration is above.
   236   int iterations = 1; // The first iteration is above.
   233 
   237   int64_t start_time = os::javaTimeNanos();
   234   while (still_running > 0) {
   238 
       
   239   do {
   235     // Check if this has taken too long:
   240     // Check if this has taken too long:
   236     if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
   241     if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
   237       print_safepoint_timeout();
   242       print_safepoint_timeout();
   238     }
   243     }
   239     if (int(iterations) == -1) { // overflow - something is wrong.
   244     if (int(iterations) == -1) { // overflow - something is wrong.
   262     }
   267     }
   263 
   268 
   264     DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   269     DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   265 
   270 
   266     if (still_running > 0) {
   271     if (still_running > 0) {
   267       back_off(iterations);
   272       back_off(start_time);
   268     }
   273     }
   269 
   274 
   270     iterations++;
   275     iterations++;
   271   }
   276   } while (still_running > 0);
   272 
   277 
   273   assert(tss_head == NULL, "Must be empty");
   278   assert(tss_head == NULL, "Must be empty");
   274 
   279 
   275   return iterations;
   280   return iterations;
   276 }
   281 }