]> git.lizzy.rs Git - rust.git/blobdiff - src/thread.rs
Auto merge of #2427 - Nilstrieb:doc-fix, r=saethlin
[rust.git] / src / thread.rs
index 8edd6672a7478e635fc827b02aa39352df41c7bc..683694f482eaab896b441574de2674d727842271 100644 (file)
@@ -12,6 +12,7 @@
 use rustc_index::vec::{Idx, IndexVec};
 use rustc_middle::mir::Mutability;
 
+use crate::concurrency::data_race;
 use crate::sync::SynchronizationState;
 use crate::*;
 
@@ -69,7 +70,7 @@ fn from(id: u32) -> Self {
 }
 
 impl ThreadId {
-    pub fn to_u32_scalar(&self) -> Scalar<Tag> {
+    pub fn to_u32_scalar(&self) -> Scalar<Provenance> {
         Scalar::from_u32(self.0)
     }
 }
@@ -111,7 +112,7 @@ pub struct Thread<'mir, 'tcx> {
     thread_name: Option<Vec<u8>>,
 
     /// The virtual call stack.
-    stack: Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>>,
+    stack: Vec<Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>>,
 
     /// The join status.
     join_status: ThreadJoinStatus,
@@ -119,10 +120,10 @@ pub struct Thread<'mir, 'tcx> {
     /// The temporary used for storing the argument of
     /// the call to `miri_start_panic` (the panic payload) when unwinding.
     /// This is pointer-sized, and matches the `Payload` type in `src/libpanic_unwind/miri.rs`.
-    pub(crate) panic_payload: Option<Scalar<Tag>>,
+    pub(crate) panic_payload: Option<Scalar<Provenance>>,
 
     /// Last OS error location in memory. It is a 32-bit integer.
-    pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
+    pub(crate) last_error: Option<MPlaceTy<'tcx, Provenance>>,
 }
 
 impl<'mir, 'tcx> Thread<'mir, 'tcx> {
@@ -169,6 +170,14 @@ fn default() -> Self {
     }
 }
 
+impl<'mir, 'tcx> Thread<'mir, 'tcx> {
+    fn new(name: &str) -> Self {
+        let mut thread = Thread::default();
+        thread.thread_name = Some(Vec::from(name.as_bytes()));
+        thread
+    }
+}
+
 /// A specific moment in time.
 #[derive(Debug)]
 pub enum Time {
@@ -218,7 +227,7 @@ pub struct ThreadManager<'mir, 'tcx> {
     pub(crate) sync: SynchronizationState,
     /// A mapping from a thread-local static to an allocation id of a thread
     /// specific allocation.
-    thread_local_alloc_ids: RefCell<FxHashMap<(DefId, ThreadId), Pointer<Tag>>>,
+    thread_local_alloc_ids: RefCell<FxHashMap<(DefId, ThreadId), Pointer<Provenance>>>,
     /// A flag that indicates that we should change the active thread.
     yield_active_thread: bool,
     /// Callbacks that are called once the specified time passes.
@@ -229,7 +238,7 @@ impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
     fn default() -> Self {
         let mut threads = IndexVec::new();
         // Create the main thread and add it to the list of threads.
-        let mut main_thread = Thread::default();
+        let mut main_thread = Thread::new("main");
         // The main thread can *not* be joined on.
         main_thread.join_status = ThreadJoinStatus::Detached;
         threads.push(main_thread);
@@ -247,7 +256,7 @@ fn default() -> Self {
 impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
     /// Check if we have an allocation for the given thread local static for the
     /// active thread.
-    fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<Pointer<Tag>> {
+    fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<Pointer<Provenance>> {
         self.thread_local_alloc_ids.borrow().get(&(def_id, self.active_thread)).cloned()
     }
 
@@ -255,7 +264,7 @@ fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<Pointer<Tag>> {
     /// static for the active thread.
     ///
     /// Panics if a thread local is initialized twice for the same thread.
-    fn set_thread_local_alloc(&self, def_id: DefId, ptr: Pointer<Tag>) {
+    fn set_thread_local_alloc(&self, def_id: DefId, ptr: Pointer<Provenance>) {
         self.thread_local_alloc_ids
             .borrow_mut()
             .try_insert((def_id, self.active_thread), ptr)
@@ -263,15 +272,23 @@ fn set_thread_local_alloc(&self, def_id: DefId, ptr: Pointer<Tag>) {
     }
 
     /// Borrow the stack of the active thread.
-    fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
+    pub fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>] {
         &self.threads[self.active_thread].stack
     }
 
     /// Mutably borrow the stack of the active thread.
-    fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
+    fn active_thread_stack_mut(
+        &mut self,
+    ) -> &mut Vec<Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>> {
         &mut self.threads[self.active_thread].stack
     }
 
+    pub fn all_stacks(
+        &self,
+    ) -> impl Iterator<Item = &[Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>]> {
+        self.threads.iter().map(|t| &t.stack[..])
+    }
+
     /// Create a new thread and returns its id.
     fn create_thread(&mut self) -> ThreadId {
         let new_thread_id = ThreadId::new(self.threads.len());
@@ -288,15 +305,21 @@ fn set_active_thread_id(&mut self, id: ThreadId) -> ThreadId {
     }
 
     /// Get the id of the currently active thread.
-    fn get_active_thread_id(&self) -> ThreadId {
+    pub fn get_active_thread_id(&self) -> ThreadId {
         self.active_thread
     }
 
     /// Get the total number of threads that were ever spawn by this program.
-    fn get_total_thread_count(&self) -> usize {
+    pub fn get_total_thread_count(&self) -> usize {
         self.threads.len()
     }
 
+    /// Get the total of threads that are currently live, i.e., not yet terminated.
+    /// (They might be blocked.)
+    pub fn get_live_thread_count(&self) -> usize {
+        self.threads.iter().filter(|t| !matches!(t.state, ThreadState::Terminated)).count()
+    }
+
     /// Has the given thread terminated?
     fn has_terminated(&self, thread_id: ThreadId) -> bool {
         self.threads[thread_id].state == ThreadState::Terminated
@@ -365,22 +388,27 @@ fn join_thread(
         } else {
             // The thread has already terminated - mark join happens-before
             if let Some(data_race) = data_race {
-                data_race.thread_joined(self.active_thread, joined_thread_id);
+                data_race.thread_joined(self, self.active_thread, joined_thread_id);
             }
         }
         Ok(())
     }
 
     /// Set the name of the active thread.
-    fn set_thread_name(&mut self, new_thread_name: Vec<u8>) {
+    fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
         self.active_thread_mut().thread_name = Some(new_thread_name);
     }
 
     /// Get the name of the active thread.
-    fn get_thread_name(&self) -> &[u8] {
+    pub fn get_active_thread_name(&self) -> &[u8] {
         self.active_thread_ref().thread_name()
     }
 
+    /// Get the name of the given thread.
+    pub fn get_thread_name(&self, thread: ThreadId) -> &[u8] {
+        self.threads[thread].thread_name()
+    }
+
     /// Put the thread into the blocked state.
     fn block_thread(&mut self, thread: ThreadId) {
         let state = &mut self.threads[thread].state;
@@ -444,7 +472,7 @@ fn get_ready_callback(&mut self) -> Option<(ThreadId, TimeoutCallback<'mir, 'tcx
     fn thread_terminated(
         &mut self,
         mut data_race: Option<&mut data_race::GlobalState>,
-    ) -> Vec<Pointer<Tag>> {
+    ) -> Vec<Pointer<Provenance>> {
         let mut free_tls_statics = Vec::new();
         {
             let mut thread_local_statics = self.thread_local_alloc_ids.borrow_mut();
@@ -459,21 +487,25 @@ fn thread_terminated(
                 false
             });
         }
-        // Set the thread into a terminated state in the data-race detector
+        // Set the thread into a terminated state in the data-race detector.
         if let Some(ref mut data_race) = data_race {
-            data_race.thread_terminated();
+            data_race.thread_terminated(self);
         }
         // Check if we need to unblock any threads.
+        let mut joined_threads = vec![]; // store which threads joined, we'll need it
         for (i, thread) in self.threads.iter_enumerated_mut() {
             if thread.state == ThreadState::BlockedOnJoin(self.active_thread) {
                 // The thread has terminated, mark happens-before edge to joining thread
-                if let Some(ref mut data_race) = data_race {
-                    data_race.thread_joined(i, self.active_thread);
+                if data_race.is_some() {
+                    joined_threads.push(i);
                 }
                 trace!("unblocking {:?} because {:?} terminated", i, self.active_thread);
                 thread.state = ThreadState::Enabled;
             }
         }
+        for &i in &joined_threads {
+            data_race.as_mut().unwrap().thread_joined(self, i, self.active_thread);
+        }
         free_tls_statics
     }
 
@@ -483,10 +515,7 @@ fn thread_terminated(
     /// used in stateless model checkers such as Loom: run the active thread as
     /// long as we can and switch only when we have to (the active thread was
     /// blocked, terminated, or has explicitly asked to be preempted).
-    fn schedule(
-        &mut self,
-        data_race: &Option<data_race::GlobalState>,
-    ) -> InterpResult<'tcx, SchedulingAction> {
+    fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
         // Check whether the thread has **just** terminated (`check_terminated`
         // checks whether the thread has popped all its stack and if yes, sets
         // the thread state to terminated).
@@ -518,16 +547,23 @@ fn schedule(
             return Ok(SchedulingAction::ExecuteTimeoutCallback);
         }
         // No callbacks scheduled, pick a regular thread to execute.
-        // We need to pick a new thread for execution.
-        for (id, thread) in self.threads.iter_enumerated() {
+        // The active thread blocked or yielded. So we go search for another enabled thread.
+        // Curcially, we start searching at the current active thread ID, rather than at 0, since we
+        // want to avoid always scheduling threads 0 and 1 without ever making progress in thread 2.
+        //
+        // `skip(N)` means we start iterating at thread N, so we skip 1 more to start just *after*
+        // the active thread. Then after that we look at `take(N)`, i.e., the threads *before* the
+        // active thread.
+        let threads = self
+            .threads
+            .iter_enumerated()
+            .skip(self.active_thread.index() + 1)
+            .chain(self.threads.iter_enumerated().take(self.active_thread.index()));
+        for (id, thread) in threads {
+            debug_assert_ne!(self.active_thread, id);
             if thread.state == ThreadState::Enabled {
-                if !self.yield_active_thread || id != self.active_thread {
-                    self.active_thread = id;
-                    if let Some(data_race) = data_race {
-                        data_race.thread_set_active(self.active_thread);
-                    }
-                    break;
-                }
+                self.active_thread = id;
+                break;
             }
         }
         self.yield_active_thread = false;
@@ -557,7 +593,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
     fn get_or_create_thread_local_alloc(
         &mut self,
         def_id: DefId,
-    ) -> InterpResult<'tcx, Pointer<Tag>> {
+    ) -> InterpResult<'tcx, Pointer<Provenance>> {
         let this = self.eval_context_mut();
         let tcx = this.tcx;
         if let Some(old_alloc) = this.machine.threads.get_thread_local_alloc_id(def_id) {
@@ -576,7 +612,7 @@ fn get_or_create_thread_local_alloc(
             // This allocation will be deallocated when the thread dies, so it is not in read-only memory.
             allocation.mutability = Mutability::Mut;
             // Create a fresh allocation with this content.
-            let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into());
+            let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into())?;
             this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
             Ok(new_alloc)
         }
@@ -587,7 +623,7 @@ fn create_thread(&mut self) -> ThreadId {
         let this = self.eval_context_mut();
         let id = this.machine.threads.create_thread();
         if let Some(data_race) = &mut this.machine.data_race {
-            data_race.thread_created(id);
+            data_race.thread_created(&this.machine.threads, id);
         }
         id
     }
@@ -608,9 +644,6 @@ fn join_thread(&mut self, joined_thread_id: ThreadId) -> InterpResult<'tcx> {
     #[inline]
     fn set_active_thread(&mut self, thread_id: ThreadId) -> ThreadId {
         let this = self.eval_context_mut();
-        if let Some(data_race) = &this.machine.data_race {
-            data_race.thread_set_active(thread_id);
-        }
         this.machine.threads.set_active_thread_id(thread_id)
     }
 
@@ -657,13 +690,15 @@ fn enable_thread(&mut self, thread_id: ThreadId) {
     }
 
     #[inline]
-    fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
+    fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>] {
         let this = self.eval_context_ref();
         this.machine.threads.active_thread_stack()
     }
 
     #[inline]
-    fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
+    fn active_thread_stack_mut(
+        &mut self,
+    ) -> &mut Vec<Frame<'mir, 'tcx, Provenance, FrameData<'tcx>>> {
         let this = self.eval_context_mut();
         this.machine.threads.active_thread_stack_mut()
     }
@@ -671,12 +706,7 @@ fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameDa
     #[inline]
     fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
         let this = self.eval_context_mut();
-        if let Some(data_race) = &mut this.machine.data_race {
-            if let Ok(string) = String::from_utf8(new_thread_name.clone()) {
-                data_race.thread_set_name(this.machine.threads.active_thread, string);
-            }
-        }
-        this.machine.threads.set_thread_name(new_thread_name);
+        this.machine.threads.set_active_thread_name(new_thread_name);
     }
 
     #[inline]
@@ -685,7 +715,7 @@ fn get_active_thread_name<'c>(&'c self) -> &'c [u8]
         'mir: 'c,
     {
         let this = self.eval_context_ref();
-        this.machine.threads.get_thread_name()
+        this.machine.threads.get_active_thread_name()
     }
 
     #[inline]
@@ -706,6 +736,16 @@ fn yield_active_thread(&mut self) {
         this.machine.threads.yield_active_thread();
     }
 
+    #[inline]
+    fn maybe_preempt_active_thread(&mut self) {
+        use rand::Rng as _;
+
+        let this = self.eval_context_mut();
+        if this.machine.rng.get_mut().gen_bool(this.machine.preemption_rate) {
+            this.yield_active_thread();
+        }
+    }
+
     #[inline]
     fn register_timeout_callback(
         &mut self,
@@ -755,8 +795,7 @@ fn run_timeout_callback(&mut self) -> InterpResult<'tcx> {
     #[inline]
     fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
         let this = self.eval_context_mut();
-        let data_race = &this.machine.data_race;
-        this.machine.threads.schedule(data_race)
+        this.machine.threads.schedule()
     }
 
     /// Handles thread termination of the active thread: wakes up threads joining on this one,