Skip to main content

current_label

Function current_label 

Source
pub fn current_label<R>(f: impl for<'a> FnOnce(Option<TaskLabel<'a>>) -> R) -> R
Expand description

Passes to f the label of the task currently running on this thread (None: no labels).

Neither allocates nor blocks, so it is fit for a panic hook. The label is borrowed only for the duration of f: it (or the name) cannot be moved out; copy what you need.

Labels are set by the scheduler itself (pool tasks, graph nodes, async tasks, and pragma_jobs::labeled for the application’s own code). The example sets them by hand through the internal run_labeled only to stay inside this crate:

use pragma_sync::__private::run_labeled;
use pragma_sync::current_label;

run_labeled("graph", "physics", || {
    run_labeled("scope", "parallel_for chunk", || {
        current_label(|label| {
            let label = label.expect("inside run_labeled");
            assert_eq!(label.name(), "parallel_for chunk");
            assert_eq!(label.parent().map(|p| p.name()), Some("physics"));
            assert_eq!(label.to_string(), "scope `parallel_for chunk` <- graph `physics`");
        });
    });
});
assert!(current_label(|label| label.is_none()));

A panic hook that names the culprit (the hook runs on the panicking thread, before unwinding):

use pragma_sync::current_label;

let default_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
    current_label(|label| match label {
        Some(label) => eprintln!("panic in task {label}"),
        None => eprintln!("panic outside pool tasks"),
    });
    default_hook(info);
}));