forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

seat.rs48 lines, 1.6 KiBHistoryRawDownload
1//! The decode seat for a test, shared by the engine's test binaries.
2//!
3//! `VE_SW` is process-wide and the test harness runs a binary's tests on many
4//! threads, so a test that pins it for its own seat must not share the process
5//! with one that unpins it. Every test that pins a seat takes [`SEAT`] for as
6//! long as its guard lives, and the guard leaves `VE_SW` as it found it: the
7//! software seat's tests are then one at a time, and none of them can see a
8//! hardware pin that another test is holding.
9//!
10//! A test that means "the software seat" takes [`software`]; one that means "the
11//! plugin, if the machine has it" takes [`hardware`]. A test that takes neither
12//! is one whose assertions hold on either seat.
13
14use std::sync::{Mutex, MutexGuard};
15
16static SEAT: Mutex<()> = Mutex::new(());
17
18/// Holds the seat for the test's duration. Dropping it removes `VE_SW`.
19#[must_use = "the seat holds only while its guard is alive"]
20pub struct Seat {
21 _lock: MutexGuard<'static, ()>,
22}
23
24fn lock() -> MutexGuard<'static, ()> {
25 SEAT.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
26}
27
28/// The software seat: the plugin forced off for as long as the guard lives.
29#[allow(dead_code)]
30pub fn software() -> Seat {
31 let guard = lock();
32 engine::envcfg::set("VE_SW", "1");
33 Seat { _lock: guard }
34}
35
36/// The plugin, as the machine has it: `VE_SW` unset for as long as the guard lives.
37#[allow(dead_code)]
38pub fn hardware() -> Seat {
39 let guard = lock();
40 engine::envcfg::unset("VE_SW");
41 Seat { _lock: guard }
42}
43
44impl Drop for Seat {
45 fn drop(&mut self) {
46 engine::envcfg::unset("VE_SW");
47 }
48}