Standardizing Fortran sleep

We’ve added this subroutine to our electronic structure code:

subroutine idle
use modmain
implicit none
! time interval between checking for the STOP file
integer, parameter :: tcheck=30
integer i
real(8) ts0,ts1
! set the stop signal to .false.
tstop=.false.
do i=1,tidle/tcheck
! wait for tcheck seconds
  call timesec(ts0)
  ts1=ts0
  do while (abs(ts1-ts0) < tcheck)
    call timesec(ts1)
  end do
! check for STOP file
  call checkstop
  if (tstop) return
end do
end subroutine

What this does is idle the calculation for tidle seconds. This is useful on HPC clusters when it takes time for a job in the queue to start running because resources are unavailable. It can be time-consuming if you need to repeatedly adjust parameters and resubmit jobs.

This routine scans for a file named STOP every 30 seconds and if it’s found it deletes it and restarts the calculation with a new input file. It works very well and saves a lot of (human) time.

However, it also burns 100% CPU just idling and I was wondering if this could be reduced.

Now, I’m aware that the sleep command exists as an extension in both GNU and Intel Fortran (and possibly others). I’m also aware that there are ways to get Fortran to sleep by calls to C++ or POSIX functions. An interface to usleep and winsleep is also available in Fortran stdlib. However, I’d like to avoid mixing languages or adding an entire library for this single purpose.

I presume that it’s not possible in standard Fortran as it has to involve system interrupts in order to suspend a thread (although I’d be happy to be wrong). If so, then I think it would be useful to standardize the sleep subroutine in Fortran.

I guess, so far the best you can achieve in terms of Fortran-based solution involve the use of iso_c_binding. I reported a bug some time ago of stdlib related to sleep which may contain some useful pieces of information.
I am no export with OpenMP, but is it not possible in your case to fork a new thread and play with the wait policy. Your description makes me think of a passive wait policy.