sel4_panicking/count/
with_tls.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::cell::Cell;

use sel4_panicking_env::abort;

#[thread_local]
static PANIC_COUNT: Cell<usize> = Cell::new(0);

const MAX_PANIC_DEPTH: usize = if cfg!(feature = "alloc") { 3 } else { 1 };

pub(crate) fn count_panic() {
    if PANIC_COUNT.get() == MAX_PANIC_DEPTH {
        abort!("maximum panic depth of {MAX_PANIC_DEPTH} exceeded");
    }
    update(&PANIC_COUNT, |count| count + 1);
}

pub(crate) fn count_panic_caught() {
    update(&PANIC_COUNT, |count| count - 1);
}

fn update<T: Copy>(cell: &Cell<T>, f: impl FnOnce(T) -> T) -> T {
    let old = cell.get();
    let new = f(old);
    cell.set(new);
    new
}