Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error message when execution STATE is unavailable
Browse files Browse the repository at this point in the history
This path can trigger if user code accesses synchronization primitives
from outside a Loom execution (e.g., create a `Mutex` before calling
`check()`). Currently it fails with an opaque error message about a
scoped thread local being unavailable. This change adds a more helpful
error message.
jamesbornholt committed Nov 23, 2021

Verified

This commit was signed with the committer’s verified signature.
1 parent 5f0a1ab commit c1e8652
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/rt/scheduler.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ impl Scheduler {
where
F: FnOnce(&mut Execution) -> R,
{
STATE.with(|state| f(&mut state.borrow_mut().execution))
Self::with_state(|state| f(&mut state.execution))
}

/// Perform a context switch
@@ -76,9 +76,7 @@ impl Scheduler {
}

pub(crate) fn spawn(f: Box<dyn FnOnce()>) {
STATE.with(|state| {
state.borrow_mut().queued_spawn.push_back(f);
});
Self::with_state(|state| state.queued_spawn.push_back(f));
}

pub(crate) fn run<F>(&mut self, execution: &mut Execution, f: F)
@@ -120,6 +118,17 @@ impl Scheduler {
threads[thread.as_usize()].resume();
});
}

fn with_state<F, R>(f: F) -> R
where
F: FnOnce(&mut State<'_>) -> R,
{
if !STATE.is_set() {
panic!("cannot access Loom execution state from outside a Loom model. \
are you accessing a synchronization primitive from outside a Loom test (a call to `model` or `check`)?")
}
STATE.with(|state| f(&mut state.borrow_mut()))
}
}

impl fmt::Debug for Scheduler {

0 comments on commit c1e8652

Please sign in to comment.