@@ -49,7 +49,7 @@ end function process_system_kill
4949 subroutine process_wait (seconds ) bind(C,name= ' process_wait' )
5050 import c_float
5151 implicit none
52- real (c_float), intent (in ) :: seconds
52+ real (c_float), intent (in ), value :: seconds
5353 end subroutine process_wait
5454
5555 ! Return path to the null device
@@ -76,8 +76,12 @@ end function process_has_win32
7676 ! Call system-dependent wait implementation
7777 module subroutine sleep (millisec )
7878 integer , intent (in ) :: millisec
79+
80+ real (c_float) :: seconds
81+
82+ seconds = 0.001_c_float * max (0 ,millisec)
7983
80- call process_wait(real ( 0.001 * real ( max ( 0 ,millisec),c_float),c_float) )
84+ call process_wait(seconds )
8185
8286 end subroutine sleep
8387
@@ -262,8 +266,16 @@ module subroutine wait_for_completion(process, max_wait_time)
262266 ! Optional max wait time in seconds
263267 real , optional , intent (in ) :: max_wait_time
264268
269+ integer :: sleep_interval
265270 real (RTICKS) :: wait_time, elapsed
266271 integer (TICKS) :: start_time, current_time, count_rate
272+
273+ ! Sleep interval ms
274+ integer , parameter :: MIN_WAIT_MS = 1
275+ integer , parameter :: MAX_WAIT_MS = 100
276+
277+ ! Starting sleep interval: 1ms
278+ sleep_interval = MIN_WAIT_MS
267279
268280 ! Determine the wait time
269281 if (present (max_wait_time)) then
@@ -279,9 +291,11 @@ module subroutine wait_for_completion(process, max_wait_time)
279291
280292 ! Wait loop
281293 wait_loop: do while (process_is_running(process) .and. elapsed <= wait_time)
282-
283- ! Small sleep to avoid CPU hogging (1 ms)
284- call sleep(1 )
294+
295+ ! Small sleep to avoid CPU hogging, with exponential backoff (1 ms)
296+ ! from 1ms up to 100ms
297+ call sleep(millisec= sleep_interval)
298+ sleep_interval = min (sleep_interval* 2 , MAX_WAIT_MS)
285299
286300 call system_clock (current_time)
287301 elapsed = real (current_time - start_time, RTICKS) / count_rate
0 commit comments