From 49a1184bcfabf45e44b85cf1ee8a5c434f46b595 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 18 May 2021 14:41:45 +0200 Subject: [PATCH] Improve `Canceled` API --- src/lib.rs | 37 +++++++++++++++++++------------------ tests/parallel/race.rs | 4 ++-- tests/parallel/stress.rs | 4 ++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index da4da81c..39ab7246 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -647,13 +647,28 @@ where /// A panic payload indicating that a salsa revision was canceled. #[derive(Debug)] -pub struct Canceled { - _private: (), -} +#[non_exhaustive] +pub struct Canceled; impl Canceled { fn throw() -> ! { - std::panic::resume_unwind(Box::new(Self { _private: () })); + // We use resume and not panic here to avoid running the panic + // hook (that is, to avoid collecting and printing backtrace). + std::panic::resume_unwind(Box::new(Self)); + } + + /// Runs `f`, and catches any salsa cancellation. + pub fn catch(f: F) -> Result + where + F: FnOnce() -> T + UnwindSafe, + { + match panic::catch_unwind(f) { + Ok(t) => Ok(t), + Err(payload) => match payload.downcast() { + Ok(canceled) => Err(*canceled), + Err(payload) => panic::resume_unwind(payload), + }, + } } } @@ -665,20 +680,6 @@ impl std::fmt::Display for Canceled { impl std::error::Error for Canceled {} -/// Runs `f`, and catches any salsa cancelation. -pub fn catch_cancellation(f: F) -> Result -where - F: FnOnce() -> T + UnwindSafe, -{ - match panic::catch_unwind(f) { - Ok(t) => Ok(t), - Err(payload) => match payload.downcast() { - Ok(canceled) => Err(*canceled), - Err(payload) => panic::resume_unwind(payload), - }, - } -} - // Re-export the procedural macros. #[allow(unused_imports)] #[macro_use] diff --git a/tests/parallel/race.rs b/tests/parallel/race.rs index 4d44e521..6247a9bb 100644 --- a/tests/parallel/race.rs +++ b/tests/parallel/race.rs @@ -1,7 +1,7 @@ use std::panic::AssertUnwindSafe; use crate::setup::{ParDatabase, ParDatabaseImpl}; -use salsa::ParallelDatabase; +use salsa::{Canceled, ParallelDatabase}; /// Test where a read and a set are racing with one another. /// Should be atomic. @@ -16,7 +16,7 @@ fn in_par_get_set_race() { let thread1 = std::thread::spawn({ let db = db.snapshot(); move || { - salsa::catch_cancellation(AssertUnwindSafe(|| { + Canceled::catch(AssertUnwindSafe(|| { let v = db.sum("abc"); v })) diff --git a/tests/parallel/stress.rs b/tests/parallel/stress.rs index 07dd9c95..bafb0db8 100644 --- a/tests/parallel/stress.rs +++ b/tests/parallel/stress.rs @@ -1,10 +1,10 @@ use rand::seq::SliceRandom; use rand::Rng; -use salsa::Database; use salsa::ParallelDatabase; use salsa::Snapshot; use salsa::SweepStrategy; +use salsa::{Canceled, Database}; // Number of operations a reader performs const N_MUTATOR_OPS: usize = 100; @@ -191,7 +191,7 @@ fn stress_test() { check_cancellation, } => all_threads.push(std::thread::spawn({ let db = db.snapshot(); - move || salsa::catch_cancellation(|| db_reader_thread(&db, ops, check_cancellation)) + move || Canceled::catch(|| db_reader_thread(&db, ops, check_cancellation)) })), } }