Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Async-tokio DelayNs implementation inaccurate for delays shorter than a few ms

Aperta
#111 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
rust

Direzione di ricerca

Inizia individuando l’implementazione di Async-tokio DelayNs e verificando come tokio::time::sleep gestisce i ritardi inferiori al millisecondo. Confronta il comportamento attuale con la precisione richiesta, quindi valuta gli approcci proposti con epoll_wait e Instant::elapsed; il lavoro è completato quando i ritardi inferiori a pochi millisecondi vengono gestiti accuratamente senza un ciclo illimitato e pesante per la CPU.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

hey fyi tokio::time::sleep is only good down to ms resolutions, for more granularity we typically need something nanosleep or a hot loop, so this implementation won't be accurate for anything less than a couple of ms.

having had to solve this a couple of times i suspect a more accurate way to do this would involve using epoll_wait with a timeout for > 1us delays (more accurate kernel timing than sleep is, particularly under the tokio runtime) and a hot loop over Instant::elapsed() for > 1ns delays (check elapsed and hit the waker every round).

possibly combining both into something like:

let now = Instant::now()
loop {
  // Grab elapsed at the start of the loop
  let elapsed = now.elapsed();

  // Break once we exceed the delay duration
  if elapsed > duration {
    break;
  }

  // Calculate the remaining sleep time
  let remainder = duration - elapsed;

  // epoll or spin depending on remainder
  if remainder > Duration::from_millis(1) {
    epoll_sleep().await;
  } else {
    spin_sleep().await;
  }
}

some folks do more complex things to balance accuracy and cpu use, but, this is fairly straightforward and would be closer to operating as intended.
(it's also good to keep track of elapsed times because there are reasons a sleep might end early, which has caused problems for me in the past)

Originally posted by @ryankurte in https://github.com/rust-embedded/linux-embedded-hal/issues/109#issuecomment-1913732707

Lingua principale
Rust
Stelle
319
Fork
60
Merge medio
12h 31m
PR unite (30g)
1

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-embedded/linux-embedded-hal

Tutte le issue di rust-embedded/linux-embedded-hal

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.