miri/concurrency/
init_once.rs

1use std::cell::RefCell;
2use std::collections::VecDeque;
3use std::rc::Rc;
4
5use super::thread::DynUnblockCallback;
6use super::vector_clock::VClock;
7use crate::*;
8
9#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
10/// The current status of a one time initialization.
11pub enum InitOnceStatus {
12    #[default]
13    Uninitialized,
14    Begun,
15    Complete,
16}
17
18/// The one time initialization state.
19#[derive(Default, Debug)]
20pub(super) struct InitOnce {
21    status: InitOnceStatus,
22    waiters: VecDeque<ThreadId>,
23    clock: VClock,
24}
25
26impl InitOnce {
27    #[inline]
28    pub fn status(&self) -> InitOnceStatus {
29        self.status
30    }
31
32    /// Begin initializing this InitOnce. Must only be called after checking that it is currently
33    /// uninitialized.
34    #[inline]
35    pub fn begin(&mut self) {
36        assert_eq!(
37            self.status(),
38            InitOnceStatus::Uninitialized,
39            "beginning already begun or complete init once"
40        );
41        self.status = InitOnceStatus::Begun;
42    }
43}
44
45#[derive(Default, Clone, Debug)]
46pub struct InitOnceRef(Rc<RefCell<InitOnce>>);
47
48impl InitOnceRef {
49    pub fn new() -> Self {
50        Self(Default::default())
51    }
52
53    pub fn status(&self) -> InitOnceStatus {
54        self.0.borrow().status()
55    }
56
57    pub fn begin(&self) {
58        self.0.borrow_mut().begin();
59    }
60}
61
62impl VisitProvenance for InitOnceRef {
63    // InitOnce contains no provenance.
64    fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {}
65}
66
67impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
68pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
69    /// Put the thread into the queue waiting for the initialization.
70    #[inline]
71    fn init_once_enqueue_and_block(
72        &mut self,
73        init_once_ref: InitOnceRef,
74        callback: DynUnblockCallback<'tcx>,
75    ) {
76        let this = self.eval_context_mut();
77        let thread = this.active_thread();
78        let mut init_once = init_once_ref.0.borrow_mut();
79        assert_ne!(init_once.status, InitOnceStatus::Complete, "queueing on complete init once");
80
81        init_once.waiters.push_back(thread);
82        this.block_thread(BlockReason::InitOnce, None, callback);
83    }
84
85    #[inline]
86    fn init_once_complete(&mut self, init_once_ref: &InitOnceRef) -> InterpResult<'tcx> {
87        let this = self.eval_context_mut();
88
89        let mut init_once = init_once_ref.0.borrow_mut();
90        assert_eq!(
91            init_once.status,
92            InitOnceStatus::Begun,
93            "completing already complete or uninit init once"
94        );
95
96        init_once.status = InitOnceStatus::Complete;
97
98        // Each complete happens-before the end of the wait
99        this.release_clock(|clock| init_once.clock.clone_from(clock))?;
100
101        // Wake up everyone.
102        // need to take the queue to avoid having `this` be borrowed multiple times
103        let waiters = std::mem::take(&mut init_once.waiters);
104        drop(init_once);
105        for waiter in waiters {
106            this.unblock_thread(waiter, BlockReason::InitOnce)?;
107        }
108
109        interp_ok(())
110    }
111
112    #[inline]
113    fn init_once_fail(&mut self, init_once_ref: &InitOnceRef) -> InterpResult<'tcx> {
114        let this = self.eval_context_mut();
115        let mut init_once = init_once_ref.0.borrow_mut();
116        assert_eq!(
117            init_once.status,
118            InitOnceStatus::Begun,
119            "failing already completed or uninit init once"
120        );
121        // This is again uninitialized.
122        init_once.status = InitOnceStatus::Uninitialized;
123
124        // Each complete happens-before the end of the wait
125        this.release_clock(|clock| init_once.clock.clone_from(clock))?;
126
127        // Wake up one waiting thread, so they can go ahead and try to init this.
128        if let Some(waiter) = init_once.waiters.pop_front() {
129            drop(init_once);
130            this.unblock_thread(waiter, BlockReason::InitOnce)?;
131        }
132
133        interp_ok(())
134    }
135
136    /// Synchronize with the previous completion of an InitOnce.
137    /// Must only be called after checking that it is complete.
138    #[inline]
139    fn init_once_observe_completed(&mut self, init_once_ref: &InitOnceRef) -> InterpResult<'tcx> {
140        let this = self.eval_context_mut();
141        let init_once = init_once_ref.0.borrow();
142
143        assert_eq!(
144            init_once.status,
145            InitOnceStatus::Complete,
146            "observing the completion of incomplete init once"
147        );
148
149        this.acquire_clock(&init_once.clock)
150    }
151}