@@ -500,7 +500,6 @@ int VM_Exit::wait_for_threads_in_native_to_block() {
500500 assert (SafepointSynchronize::is_at_safepoint (), " must be at safepoint already" );
501501
502502 Thread * thr_cur = Thread::current ();
503- Monitor timer (Mutex::nosafepoint, " VM_ExitTimer_lock" );
504503
505504 // Compiler threads need longer wait because they can access VM data directly
506505 // while in native. If they are active and some structures being used are
@@ -509,12 +508,23 @@ int VM_Exit::wait_for_threads_in_native_to_block() {
509508 // data, and they will be stopped during state transition. In theory, we
510509 // don't have to wait for user threads to be quiescent, but it's always
511510 // better to terminate VM when current thread is the only active thread, so
512- // wait for user threads too. Numbers are in 10 milliseconds.
513- int wait_time_per_attempt = 10 ; // in milliseconds
514- int max_wait_attempts_user_thread = UserThreadWaitAttemptsAtExit;
515- int max_wait_attempts_compiler_thread = 1000 ; // at least 10 seconds
511+ // wait for user threads too.
512+
513+ // Time per attempt. It is practical to start waiting with 10us delays
514+ // (around scheduling delay / timer slack), and exponentially ramp up
515+ // to 10ms if compiler threads are not responding.
516+ jlong max_wait_time = millis_to_nanos (10 );
517+ jlong wait_time = 10000 ;
518+
519+ jlong start_time = os::javaTimeNanos ();
520+
521+ // Deadline for user threads in native code.
522+ // User-settable flag counts "attempts" in 10ms units, to a maximum of 10s.
523+ jlong user_threads_deadline = start_time + (UserThreadWaitAttemptsAtExit * millis_to_nanos (10 ));
524+
525+ // Deadline for compiler threads: at least 10 seconds.
526+ jlong compiler_threads_deadline = start_time + millis_to_nanos (10000 );
516527
517- int attempts = 0 ;
518528 JavaThreadIteratorWithHandle jtiwh;
519529 while (true ) {
520530 int num_active = 0 ;
@@ -543,19 +553,20 @@ int VM_Exit::wait_for_threads_in_native_to_block() {
543553 }
544554 }
545555
556+ jlong time = os::javaTimeNanos ();
557+
546558 if (num_active == 0 ) {
547- return 0 ;
548- } else if (attempts >= max_wait_attempts_compiler_thread) {
549- return num_active;
550- } else if (num_active_compiler_thread == 0 &&
551- attempts >= max_wait_attempts_user_thread) {
552- return num_active;
559+ return 0 ;
560+ }
561+ if (time >= compiler_threads_deadline) {
562+ return num_active;
563+ }
564+ if ((num_active_compiler_thread == 0 ) && (time >= user_threads_deadline)) {
565+ return num_active;
553566 }
554567
555- attempts++;
556-
557- MonitorLocker ml (&timer, Mutex::_no_safepoint_check_flag);
558- ml.wait (wait_time_per_attempt);
568+ os::naked_short_nanosleep (wait_time);
569+ wait_time = MIN2 (max_wait_time, wait_time * 2 );
559570 }
560571}
561572
0 commit comments