watch_time Module

Sleep utilities and low-CPU waiting.

fpm-watch is primarily poll-based. This module provides two waiting strategies:

  • Spin wait (sleep_spin): uses system_clock in a tight loop. This is portable and offers good timing resolution, but it can consume CPU.

  • OS sleep (sleep_os): delegates to a small C shim (fpm_watch_sleep_seconds) which performs an efficient, platform-native sleep:

    • Windows: Sleep(milliseconds)
    • POSIX: nanosleep(timespec) (with EINTR retry)

The public entry point sleep_seconds dispatches between these strategies depending on the global low-CPU mode flag set by set_low_cpu.

Design notes

  • The OS sleep is implemented in C to avoid link-time issues where referencing platform-specific symbols (e.g. Sleep or nanosleep) from Fortran can break linking on the “other” platform even if the routine is never called.
  • sleep_seconds is intentionally lightweight and safe to call frequently.

Usage

use watch_time, only: set_low_cpu, sleep_seconds

call set_low_cpu(.true.)   ! prefer OS sleep (near-zero idle CPU)
call sleep_seconds(0.25)   ! sleep for ~250 ms

Threading/Signals

  • Spin sleep does not yield; it runs until time has elapsed.
  • OS sleep yields to the OS scheduler; on POSIX the C shim typically retries on interrupt (EINTR) to honor the requested duration.

Uses

  • module~~watch_time~~UsesGraph module~watch_time watch_time fpm_environment fpm_environment module~watch_time->fpm_environment iso_c_binding iso_c_binding module~watch_time->iso_c_binding iso_fortran_env iso_fortran_env module~watch_time->iso_fortran_env

Used by

  • module~~watch_time~~UsedByGraph module~watch_time watch_time module~watch_engine watch_engine module~watch_engine->module~watch_time module~watch_restart watch_restart module~watch_restart->module~watch_time

Variables

Type Visibility Attributes Name Initial
logical, private, save :: low_cpu_mode = .false.

Global mode switch that selects OS sleep vs spin sleep.

  • .false. (default): sleep_seconds uses sleep_spin (busy wait).
  • .true. : sleep_seconds uses sleep_os (OS-backed sleep).

Interfaces

interface

C shim providing a portable OS sleep implementation.

The actual platform-specific sleep is implemented in C to prevent link-time failures that can occur if Fortran directly references Sleep (Windows) or nanosleep (POSIX) symbols in the same object.

Arguments

  • sec: requested sleep duration in seconds.
  • private subroutine fpm_watch_sleep_seconds(sec) bind(C, name="fpm_watch_sleep_seconds")

    Arguments

    Type IntentOptional Attributes Name
    real(kind=c_double), value :: sec

Subroutines

public subroutine set_low_cpu(flag)

Enable or disable low-CPU mode.

Read more…

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: flag

public subroutine sleep_seconds(s)

Sleep for approximately s seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
real, intent(in) :: s

private subroutine sleep_os(s)

OS-backed sleep for approximately s seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
real, intent(in) :: s

private subroutine sleep_spin(s)

Busy-wait for approximately s seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
real, intent(in) :: s