breadthread/
directive.rs

1// MIT/Apache2 License
2
3use crate::{Driver, Pinned, Value, Wrapped};
4use alloc::{boxed::Box, vec::Vec};
5
6/// A directive to run a thread-unsafe operation using thread-unsafe primitives.
7pub(crate) struct Directive<'lt> {
8    // this is actually a wrapper around a closure that does everything we
9    // need to for now
10    closure: Box<dyn FnOnce(&mut Driver<'lt, Pinned>) + Send + 'lt>,
11}
12
13impl<'lt> Directive<'lt> {
14    /// Creates a new directive.
15    pub(crate) fn new<
16        Tag,
17        Input: 'lt + Wrapped<Tag>,
18        NtsOutput: 'lt + Send + Sync,
19        TsOutput: 'lt + Wrapped<Tag>,
20    >(
21        input: Input,
22        op: impl FnOnce(Input::Unwrapped) -> DirectiveOutput<NtsOutput, TsOutput::Unwrapped>
23            + Send
24            + 'lt,
25    ) -> (Directive<'lt>, Value<(NtsOutput, TsOutput)>) {
26        // create two value slots
27        let (ret_slot, in_slot) = Value::new();
28
29        let closure = Box::new(move |driver: &mut Driver<'lt, Pinned>| {
30            // first, ensure that, if we're in fallback mode, that we know of all of
31            // the values we're going to need
32            #[cfg(feature = "fallback")]
33            if driver.is_fallback() {
34                let known = driver.known_values();
35
36                // SAFETY: we're on the thread
37                unsafe {
38                    input.for_each_representative(|value| {
39                        if !known.contains(&value) {
40                            panic!("unknown value passed to driver: {:X}", value);
41                        }
42                    });
43                }
44            }
45
46            // unwrap the input
47            // SAFETY: this will be executed on the given thread
48            let unwrapped = unsafe { input.unwrap() };
49
50            // run the op
51            let DirectiveOutput {
52                thread_safe_value,
53                thread_unsafe_value,
54                deleted_values,
55            } = op(unwrapped);
56
57            // wrap the out
58            let ts_out = unsafe { TsOutput::wrap(thread_unsafe_value) };
59
60            // delete any values that were deleted, and add any values that are added
61            #[cfg(feature = "fallback")]
62            {
63                let known = driver.known_values_mut();
64
65                deleted_values.into_iter().for_each(|value| {
66                    known.remove(&value);
67                });
68
69                // SAFETY: we're on the thread
70                unsafe {
71                    ts_out.for_each_representative(|value| {
72                        known.insert(value);
73                    });
74                }
75            }
76
77            // store the output in the return slot
78            in_slot.put((thread_safe_value, ts_out));
79        });
80
81        (Self { closure }, ret_slot)
82    }
83
84    pub(crate) fn run(self, driver: &mut Driver<'lt, Pinned>) {
85        (self.closure)(driver);
86    }
87}
88
89/// The expected output of a directive.
90pub struct DirectiveOutput<NtsOutput, TsOutput> {
91    /// The output value that is thread safe
92    pub thread_safe_value: NtsOutput,
93    /// The output value that is thread unsafe
94    pub thread_unsafe_value: TsOutput,
95    /// The list of values that can be removed from the driver's known values
96    pub deleted_values: Vec<usize>,
97}