Skip to content

Instant accounts for system sleep on Windows but not on linux #79462

Description

@mahkoh

On linux, Instant uses clock_gettime with CLOCK_MONOTONIC which does not count time spent in a sleep state such as hiberation or standby.

On Windows, Instant uses QueryPerformanceCounter which does count time spent in a sleep state:

QueryPerformanceCounter reads the performance counter and returns the total number of ticks that have occurred since the Windows operating system was started, including the time when the machine was in a sleep state such as standby, hibernate, or connected standby.

Neither behavior is currently documented.

Consider the following code (adapted from similar code in Tokio):

use std::time::{Duration, SystemTime, Instant};

fn main() {
    let mut next = Instant::now();
    loop {
        let now = Instant::now();
        if next > now {
            std::thread::sleep(next - now);
        }
        next += Duration::from_secs(1);
        println!("{:?} tick", SystemTime::UNIX_EPOCH.elapsed().unwrap().as_millis());
    }
}

Behavior on Linux: Ticks during sleep are lost.

Behavior on Windows: Ticks during sleep occur in rapid succession after resume.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-timeArea: TimeC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions