Skip to content

TimeDelta::from_now ensures the execution is within critical section#9

Open
trnila wants to merge 1 commit intoChristopher-06:mainfrom
trnila:time_delta_from_now_critical_section
Open

TimeDelta::from_now ensures the execution is within critical section#9
trnila wants to merge 1 commit intoChristopher-06:mainfrom
trnila:time_delta_from_now_critical_section

Conversation

@trnila
Copy link
Copy Markdown

@trnila trnila commented Mar 15, 2026

In my stm32 app under the load, the deltas are sometimes corrupted - see us_timeticks values jumped compared to logged get_tracing_raw_ticks for tick 9 and 10. The defmt_timestamp_s is also correct.

#[task]
async fn test_task() {
    let mut ticker = Ticker::every(Duration::from_millis(100));
    let mut i = 0;
    loop {
        ticker.next().await;
        defmt::info!("Tick {} {}", i, unsafe { get_tracing_raw_ticks() });
        i += 1;
    }
}
    uc_timeticks  defmt_timestamp_s           message        diff
1         266957               0.10     Tick 0 266908         NaN
2         532585               0.20     Tick 1 532533   265628.00
3         798209               0.30     Tick 2 798158   265624.00
4        1063835               0.40    Tick 3 1063783   265626.00
5        1329457               0.50    Tick 4 1329408   265622.00
6        1595085               0.60    Tick 5 1595033   265628.00
7        1860709               0.70    Tick 6 1860658   265624.00
8        2126335               0.80    Tick 7 2126283   265626.00
9        2391959               0.90    Tick 8 2391908   265624.00
10      69766448               1.00    Tick 9 2657533 67374489.00
11     137140935               1.10   Tick 10 2923158 67374487.00

The TimeDelta::from_now function must execute atomically within a critical section, as it modifies the global variable LAST_TIMESTAMP, which could lead to corrupted time deltas.

Requiring CriticalSection in the function arguments ensures the caller is in critical section and its not used incorrectly.

The `from_now` function must execute atomically within a critical section,
as it modifies the global variable `LAST_TIMESTAMP`, which could lead to
corrupted time deltas.

Requiring `CriticalSection` in the function arguments ensures the caller
is in critical section and its not used incorrectly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant