2020-03-16 18:48:44 -05:00
|
|
|
//! Implements threads.
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
2020-04-21 18:38:14 -05:00
|
|
|
use std::collections::hash_map::Entry;
|
2020-04-30 15:47:12 -05:00
|
|
|
use std::num::TryFromIntError;
|
2020-05-18 09:28:19 -05:00
|
|
|
use std::time::{Duration, Instant, SystemTime};
|
2020-03-16 18:48:44 -05:00
|
|
|
|
|
|
|
use log::trace;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-04-16 15:32:40 -05:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-16 18:48:44 -05:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
|
|
|
|
2020-04-21 18:38:14 -05:00
|
|
|
use crate::sync::SynchronizationState;
|
2020-03-16 18:48:44 -05:00
|
|
|
use crate::*;
|
|
|
|
|
2020-04-16 21:40:02 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub enum SchedulingAction {
|
|
|
|
/// Execute step on the active thread.
|
|
|
|
ExecuteStep,
|
2020-04-30 16:07:07 -05:00
|
|
|
/// Execute a timeout callback.
|
|
|
|
ExecuteTimeoutCallback,
|
2020-04-16 21:40:02 -05:00
|
|
|
/// Execute destructors of the active thread.
|
|
|
|
ExecuteDtors,
|
|
|
|
/// Stop the program.
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:18:15 -05:00
|
|
|
/// Timeout callbacks can be created by synchronization primitives to tell the
|
2020-04-30 16:07:07 -05:00
|
|
|
/// scheduler that they should be called once some period of time passes.
|
|
|
|
type TimeoutCallback<'mir, 'tcx> =
|
2020-04-21 18:38:14 -05:00
|
|
|
Box<dyn FnOnce(&mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>) -> InterpResult<'tcx> + 'tcx>;
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// A thread identifier.
|
|
|
|
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
2020-04-26 23:01:03 -05:00
|
|
|
pub struct ThreadId(u32);
|
2020-03-16 18:48:44 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
/// The main thread. When it terminates, the whole application terminates.
|
|
|
|
const MAIN_THREAD: ThreadId = ThreadId(0);
|
|
|
|
|
2020-04-26 17:52:45 -05:00
|
|
|
impl ThreadId {
|
2020-04-29 15:16:22 -05:00
|
|
|
pub fn to_u32(self) -> u32 {
|
|
|
|
self.0
|
2020-04-26 17:52:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
impl Idx for ThreadId {
|
|
|
|
fn new(idx: usize) -> Self {
|
2020-04-26 23:01:03 -05:00
|
|
|
ThreadId(u32::try_from(idx).unwrap())
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-27 14:32:57 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
fn index(self) -> usize {
|
2020-04-26 23:01:03 -05:00
|
|
|
usize::try_from(self.0).unwrap()
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:32:57 -05:00
|
|
|
impl TryFrom<u64> for ThreadId {
|
|
|
|
type Error = TryFromIntError;
|
|
|
|
fn try_from(id: u64) -> Result<Self, Self::Error> {
|
|
|
|
u32::try_from(id).map(|id_u32| Self(id_u32))
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:20:41 -05:00
|
|
|
impl From<u32> for ThreadId {
|
|
|
|
fn from(id: u32) -> Self {
|
2020-04-27 14:32:57 -05:00
|
|
|
Self(id)
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ThreadId {
|
|
|
|
pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
|
|
|
|
Scalar::from_u32(u32::try_from(self.0).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// The state of a thread.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ThreadState {
|
|
|
|
/// The thread is enabled and can be executed.
|
|
|
|
Enabled,
|
|
|
|
/// The thread tried to join the specified thread and is blocked until that
|
|
|
|
/// thread terminates.
|
2020-04-07 22:20:41 -05:00
|
|
|
BlockedOnJoin(ThreadId),
|
2020-04-30 15:47:12 -05:00
|
|
|
/// The thread is blocked on some synchronization primitive. It is the
|
|
|
|
/// responsibility of the synchronization primitives to track threads that
|
|
|
|
/// are blocked by them.
|
|
|
|
BlockedOnSync,
|
2020-06-27 05:36:20 -05:00
|
|
|
/// The thread has terminated its execution. We do not delete terminated
|
|
|
|
/// threads (FIXME: why?).
|
2020-03-16 18:48:44 -05:00
|
|
|
Terminated,
|
|
|
|
}
|
|
|
|
|
2020-04-19 18:43:40 -05:00
|
|
|
/// The join status of a thread.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
enum ThreadJoinStatus {
|
|
|
|
/// The thread can be joined.
|
|
|
|
Joinable,
|
|
|
|
/// A thread is detached if its join handle was destroyed and no other
|
|
|
|
/// thread can join it.
|
|
|
|
Detached,
|
|
|
|
/// The thread was already joined by some thread and cannot be joined again.
|
|
|
|
Joined,
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// A thread.
|
|
|
|
pub struct Thread<'mir, 'tcx> {
|
|
|
|
state: ThreadState,
|
2020-08-29 21:38:37 -05:00
|
|
|
|
2020-04-06 18:12:13 -05:00
|
|
|
/// Name of the thread.
|
|
|
|
thread_name: Option<Vec<u8>>,
|
2020-08-29 21:38:37 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// The virtual call stack.
|
|
|
|
stack: Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>>,
|
2020-08-29 21:38:37 -05:00
|
|
|
|
2020-04-19 18:43:40 -05:00
|
|
|
/// The join status.
|
|
|
|
join_status: ThreadJoinStatus,
|
2020-08-29 21:38:37 -05:00
|
|
|
|
|
|
|
/// 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`.
|
2020-08-31 19:32:14 -05:00
|
|
|
pub(crate) panic_payload: Option<Scalar<Tag>>,
|
2020-08-31 21:29:09 -05:00
|
|
|
|
|
|
|
/// Last OS error location in memory. It is a 32-bit integer.
|
|
|
|
pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> Thread<'mir, 'tcx> {
|
2020-04-26 16:42:07 -05:00
|
|
|
/// Check if the thread is done executing (no more stack frames). If yes,
|
|
|
|
/// change the state to terminated and return `true`.
|
2020-03-16 18:48:44 -05:00
|
|
|
fn check_terminated(&mut self) -> bool {
|
|
|
|
if self.state == ThreadState::Enabled {
|
|
|
|
if self.stack.is_empty() {
|
|
|
|
self.state = ThreadState::Terminated;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-05-03 05:56:38 -05:00
|
|
|
|
|
|
|
/// Get the name of the current thread, or `<unnamed>` if it was not set.
|
|
|
|
fn thread_name(&self) -> &[u8] {
|
2021-05-16 04:28:01 -05:00
|
|
|
if let Some(ref thread_name) = self.thread_name { thread_name } else { b"<unnamed>" }
|
2020-05-03 05:56:38 -05:00
|
|
|
}
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> std::fmt::Debug for Thread<'mir, 'tcx> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2021-05-16 04:28:01 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}({:?}, {:?})",
|
|
|
|
String::from_utf8_lossy(self.thread_name()),
|
|
|
|
self.state,
|
|
|
|
self.join_status
|
|
|
|
)
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'mir, 'tcx> Default for Thread<'mir, 'tcx> {
|
|
|
|
fn default() -> Self {
|
2020-04-19 18:43:40 -05:00
|
|
|
Self {
|
|
|
|
state: ThreadState::Enabled,
|
|
|
|
thread_name: None,
|
|
|
|
stack: Vec::new(),
|
|
|
|
join_status: ThreadJoinStatus::Joinable,
|
2020-08-29 21:38:37 -05:00
|
|
|
panic_payload: None,
|
2020-08-31 21:29:09 -05:00
|
|
|
last_error: None,
|
2020-04-19 18:43:40 -05:00
|
|
|
}
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 11:44:32 -05:00
|
|
|
/// A specific moment in time.
|
2020-05-18 09:28:19 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Time {
|
|
|
|
Monotonic(Instant),
|
|
|
|
RealTime(SystemTime),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Time {
|
|
|
|
/// How long do we have to wait from now until the specified time?
|
|
|
|
fn get_wait_time(&self) -> Duration {
|
|
|
|
match self {
|
|
|
|
Time::Monotonic(instant) => instant.saturating_duration_since(Instant::now()),
|
|
|
|
Time::RealTime(time) =>
|
|
|
|
time.duration_since(SystemTime::now()).unwrap_or(Duration::new(0, 0)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:38:14 -05:00
|
|
|
/// Callbacks are used to implement timeouts. For example, waiting on a
|
|
|
|
/// conditional variable with a timeout creates a callback that is called after
|
|
|
|
/// the specified time and unblocks the thread. If another thread signals on the
|
|
|
|
/// conditional variable, the signal handler deletes the callback.
|
2020-04-30 16:07:07 -05:00
|
|
|
struct TimeoutCallbackInfo<'mir, 'tcx> {
|
2020-04-21 18:38:14 -05:00
|
|
|
/// The callback should be called no earlier than this time.
|
2020-05-18 09:28:19 -05:00
|
|
|
call_time: Time,
|
2020-04-21 18:38:14 -05:00
|
|
|
/// The called function.
|
2020-04-30 16:07:07 -05:00
|
|
|
callback: TimeoutCallback<'mir, 'tcx>,
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
|
2020-04-30 16:07:07 -05:00
|
|
|
impl<'mir, 'tcx> std::fmt::Debug for TimeoutCallbackInfo<'mir, 'tcx> {
|
2020-04-21 18:38:14 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2020-05-18 10:18:15 -05:00
|
|
|
write!(f, "TimeoutCallback({:?})", self.call_time)
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// A set of threads.
|
|
|
|
#[derive(Debug)]
|
2020-04-09 14:06:33 -05:00
|
|
|
pub struct ThreadManager<'mir, 'tcx> {
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Identifier of the currently active thread.
|
|
|
|
active_thread: ThreadId,
|
|
|
|
/// Threads used in the program.
|
|
|
|
///
|
|
|
|
/// Note that this vector also contains terminated threads.
|
|
|
|
threads: IndexVec<ThreadId, Thread<'mir, 'tcx>>,
|
2020-04-30 16:07:07 -05:00
|
|
|
/// This field is pub(crate) because the synchronization primitives
|
|
|
|
/// (`crate::sync`) need a way to access it.
|
2020-04-21 18:38:14 -05:00
|
|
|
pub(crate) sync: SynchronizationState,
|
2020-04-16 15:32:40 -05:00
|
|
|
/// A mapping from a thread-local static to an allocation id of a thread
|
|
|
|
/// specific allocation.
|
2021-07-15 13:33:08 -05:00
|
|
|
thread_local_alloc_ids: RefCell<FxHashMap<(DefId, ThreadId), Pointer<Tag>>>,
|
2020-04-18 17:39:53 -05:00
|
|
|
/// A flag that indicates that we should change the active thread.
|
|
|
|
yield_active_thread: bool,
|
2020-04-21 18:38:14 -05:00
|
|
|
/// Callbacks that are called once the specified time passes.
|
2020-04-30 16:07:07 -05:00
|
|
|
timeout_callbacks: FxHashMap<ThreadId, TimeoutCallbackInfo<'mir, 'tcx>>,
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:06:33 -05:00
|
|
|
impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
|
2020-03-16 18:48:44 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
let mut threads = IndexVec::new();
|
2020-04-19 16:21:18 -05:00
|
|
|
// Create the main thread and add it to the list of threads.
|
2020-04-20 15:22:28 -05:00
|
|
|
let mut main_thread = Thread::default();
|
2020-04-26 16:42:07 -05:00
|
|
|
// The main thread can *not* be joined on.
|
2020-04-20 15:22:28 -05:00
|
|
|
main_thread.join_status = ThreadJoinStatus::Detached;
|
|
|
|
threads.push(main_thread);
|
2020-04-15 16:34:34 -05:00
|
|
|
Self {
|
|
|
|
active_thread: ThreadId::new(0),
|
|
|
|
threads: threads,
|
2020-04-21 18:38:14 -05:00
|
|
|
sync: SynchronizationState::default(),
|
2020-04-15 16:34:34 -05:00
|
|
|
thread_local_alloc_ids: Default::default(),
|
2020-04-18 17:39:53 -05:00
|
|
|
yield_active_thread: false,
|
2020-04-30 16:07:07 -05:00
|
|
|
timeout_callbacks: FxHashMap::default(),
|
2020-04-15 16:34:34 -05:00
|
|
|
}
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:06:33 -05:00
|
|
|
impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
|
2020-04-15 16:34:34 -05:00
|
|
|
/// Check if we have an allocation for the given thread local static for the
|
|
|
|
/// active thread.
|
2021-07-15 13:33:08 -05:00
|
|
|
fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<Pointer<Tag>> {
|
2020-04-16 15:32:40 -05:00
|
|
|
self.thread_local_alloc_ids.borrow().get(&(def_id, self.active_thread)).cloned()
|
2020-04-15 16:34:34 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
/// Set the pointer for the allocation of the given thread local
|
2020-04-15 16:34:34 -05:00
|
|
|
/// static for the active thread.
|
2020-04-19 16:21:18 -05:00
|
|
|
///
|
|
|
|
/// Panics if a thread local is initialized twice for the same thread.
|
2021-07-15 13:33:08 -05:00
|
|
|
fn set_thread_local_alloc(&self, def_id: DefId, ptr: Pointer<Tag>) {
|
2020-04-19 16:21:18 -05:00
|
|
|
self.thread_local_alloc_ids
|
|
|
|
.borrow_mut()
|
2021-07-15 13:33:08 -05:00
|
|
|
.try_insert((def_id, self.active_thread), ptr)
|
2021-03-08 09:57:09 -06:00
|
|
|
.unwrap();
|
2020-04-15 16:34:34 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Borrow the stack of the active thread.
|
|
|
|
fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
|
|
|
|
&self.threads[self.active_thread].stack
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Mutably borrow the stack of the active thread.
|
|
|
|
fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
|
|
|
|
&mut self.threads[self.active_thread].stack
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Create a new thread and returns its id.
|
|
|
|
fn create_thread(&mut self) -> ThreadId {
|
|
|
|
let new_thread_id = ThreadId::new(self.threads.len());
|
|
|
|
self.threads.push(Default::default());
|
|
|
|
new_thread_id
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Set an active thread and return the id of the thread that was active before.
|
2020-04-06 18:12:13 -05:00
|
|
|
fn set_active_thread_id(&mut self, id: ThreadId) -> ThreadId {
|
2020-03-16 18:48:44 -05:00
|
|
|
let active_thread_id = self.active_thread;
|
|
|
|
self.active_thread = id;
|
|
|
|
assert!(self.active_thread.index() < self.threads.len());
|
|
|
|
active_thread_id
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Get the id of the currently active thread.
|
2020-04-06 18:12:13 -05:00
|
|
|
fn get_active_thread_id(&self) -> ThreadId {
|
2020-03-16 18:48:44 -05:00
|
|
|
self.active_thread
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 22:52:53 -05:00
|
|
|
/// Get the total number of threads that were ever spawn by this program.
|
|
|
|
fn get_total_thread_count(&self) -> usize {
|
|
|
|
self.threads.len()
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:40:02 -05:00
|
|
|
/// Has the given thread terminated?
|
|
|
|
fn has_terminated(&self, thread_id: ThreadId) -> bool {
|
|
|
|
self.threads[thread_id].state == ThreadState::Terminated
|
|
|
|
}
|
|
|
|
|
2021-07-25 07:08:12 -05:00
|
|
|
/// Have all threads terminated?
|
|
|
|
fn have_all_terminated(&self) -> bool {
|
|
|
|
self.threads.iter().all(|thread| thread.state == ThreadState::Terminated)
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:16:24 -05:00
|
|
|
/// Enable the thread for execution. The thread must be terminated.
|
|
|
|
fn enable_thread(&mut self, thread_id: ThreadId) {
|
|
|
|
assert!(self.has_terminated(thread_id));
|
|
|
|
self.threads[thread_id].state = ThreadState::Enabled;
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:46:51 -05:00
|
|
|
/// Get a mutable borrow of the currently active thread.
|
2020-04-06 18:12:13 -05:00
|
|
|
fn active_thread_mut(&mut self) -> &mut Thread<'mir, 'tcx> {
|
|
|
|
&mut self.threads[self.active_thread]
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-24 18:46:51 -05:00
|
|
|
/// Get a shared borrow of the currently active thread.
|
|
|
|
fn active_thread_ref(&self) -> &Thread<'mir, 'tcx> {
|
|
|
|
&self.threads[self.active_thread]
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Mark the thread as detached, which means that no other thread will try
|
|
|
|
/// to join it and the thread is responsible for cleaning up.
|
2020-04-19 18:43:40 -05:00
|
|
|
fn detach_thread(&mut self, id: ThreadId) -> InterpResult<'tcx> {
|
|
|
|
if self.threads[id].join_status != ThreadJoinStatus::Joinable {
|
|
|
|
throw_ub_format!("trying to detach thread that was already detached or joined");
|
|
|
|
}
|
|
|
|
self.threads[id].join_status = ThreadJoinStatus::Detached;
|
|
|
|
Ok(())
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-03-16 18:48:44 -05:00
|
|
|
/// Mark that the active thread tries to join the thread with `joined_thread_id`.
|
2021-05-16 04:28:01 -05:00
|
|
|
fn join_thread(
|
|
|
|
&mut self,
|
|
|
|
joined_thread_id: ThreadId,
|
2021-05-23 04:00:25 -05:00
|
|
|
data_race: Option<&mut data_race::GlobalState>,
|
2021-05-16 04:28:01 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-04-19 18:43:40 -05:00
|
|
|
if self.threads[joined_thread_id].join_status != ThreadJoinStatus::Joinable {
|
|
|
|
throw_ub_format!("trying to join a detached or already joined thread");
|
2020-04-19 16:21:18 -05:00
|
|
|
}
|
|
|
|
if joined_thread_id == self.active_thread {
|
|
|
|
throw_ub_format!("trying to join itself");
|
|
|
|
}
|
2020-04-19 18:43:40 -05:00
|
|
|
assert!(
|
|
|
|
self.threads
|
|
|
|
.iter()
|
|
|
|
.all(|thread| thread.state != ThreadState::BlockedOnJoin(joined_thread_id)),
|
2020-04-26 16:42:07 -05:00
|
|
|
"a joinable thread already has threads waiting for its termination"
|
2020-04-19 18:43:40 -05:00
|
|
|
);
|
|
|
|
// Mark the joined thread as being joined so that we detect if other
|
|
|
|
// threads try to join it.
|
|
|
|
self.threads[joined_thread_id].join_status = ThreadJoinStatus::Joined;
|
2020-03-16 18:48:44 -05:00
|
|
|
if self.threads[joined_thread_id].state != ThreadState::Terminated {
|
|
|
|
// The joined thread is still running, we need to wait for it.
|
2020-04-07 22:20:41 -05:00
|
|
|
self.active_thread_mut().state = ThreadState::BlockedOnJoin(joined_thread_id);
|
2020-03-16 18:48:44 -05:00
|
|
|
trace!(
|
|
|
|
"{:?} blocked on {:?} when trying to join",
|
|
|
|
self.active_thread,
|
|
|
|
joined_thread_id
|
|
|
|
);
|
2020-11-15 12:30:26 -06:00
|
|
|
} else {
|
2020-11-01 18:23:27 -06:00
|
|
|
// The thread has already terminated - mark join happens-before
|
2020-11-15 12:30:26 -06:00
|
|
|
if let Some(data_race) = data_race {
|
|
|
|
data_race.thread_joined(self.active_thread, joined_thread_id);
|
|
|
|
}
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-19 16:21:18 -05:00
|
|
|
Ok(())
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-06 18:12:13 -05:00
|
|
|
/// Set the name of the active thread.
|
|
|
|
fn set_thread_name(&mut self, new_thread_name: Vec<u8>) {
|
|
|
|
self.active_thread_mut().thread_name = Some(new_thread_name);
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
/// Get the name of the active thread.
|
2020-04-30 10:35:59 -05:00
|
|
|
fn get_thread_name(&self) -> &[u8] {
|
2020-05-03 05:56:38 -05:00
|
|
|
self.active_thread_ref().thread_name()
|
2020-04-19 16:21:18 -05:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:38:14 -05:00
|
|
|
/// Put the thread into the blocked state.
|
|
|
|
fn block_thread(&mut self, thread: ThreadId) {
|
|
|
|
let state = &mut self.threads[thread].state;
|
2020-04-07 22:20:41 -05:00
|
|
|
assert_eq!(*state, ThreadState::Enabled);
|
2020-04-30 15:47:12 -05:00
|
|
|
*state = ThreadState::BlockedOnSync;
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-21 18:38:14 -05:00
|
|
|
/// Put the blocked thread into the enabled state.
|
|
|
|
fn unblock_thread(&mut self, thread: ThreadId) {
|
|
|
|
let state = &mut self.threads[thread].state;
|
2020-04-30 15:47:12 -05:00
|
|
|
assert_eq!(*state, ThreadState::BlockedOnSync);
|
2020-04-21 18:38:14 -05:00
|
|
|
*state = ThreadState::Enabled;
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-18 17:39:53 -05:00
|
|
|
/// Change the active thread to some enabled thread.
|
|
|
|
fn yield_active_thread(&mut self) {
|
2020-07-17 06:39:25 -05:00
|
|
|
// We do not yield immediately, as swapping out the current stack while executing a MIR statement
|
2020-07-17 05:54:38 -05:00
|
|
|
// could lead to all sorts of confusion.
|
|
|
|
// We should only switch stacks between steps.
|
2020-04-18 17:39:53 -05:00
|
|
|
self.yield_active_thread = true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:38:14 -05:00
|
|
|
/// Register the given `callback` to be called once the `call_time` passes.
|
2020-05-24 13:29:56 -05:00
|
|
|
///
|
|
|
|
/// The callback will be called with `thread` being the active thread, and
|
|
|
|
/// the callback may not change the active thread.
|
2020-04-30 16:07:07 -05:00
|
|
|
fn register_timeout_callback(
|
2020-04-21 18:38:14 -05:00
|
|
|
&mut self,
|
|
|
|
thread: ThreadId,
|
2020-05-18 09:28:19 -05:00
|
|
|
call_time: Time,
|
2020-04-30 16:07:07 -05:00
|
|
|
callback: TimeoutCallback<'mir, 'tcx>,
|
2020-04-21 18:38:14 -05:00
|
|
|
) {
|
2020-04-30 16:07:07 -05:00
|
|
|
self.timeout_callbacks
|
2021-03-08 09:57:09 -06:00
|
|
|
.try_insert(thread, TimeoutCallbackInfo { call_time, callback })
|
|
|
|
.unwrap();
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregister the callback for the `thread`.
|
2020-04-30 16:07:07 -05:00
|
|
|
fn unregister_timeout_callback_if_exists(&mut self, thread: ThreadId) {
|
|
|
|
self.timeout_callbacks.remove(&thread);
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a callback that is ready to be called.
|
2020-05-18 09:28:19 -05:00
|
|
|
fn get_ready_callback(&mut self) -> Option<(ThreadId, TimeoutCallback<'mir, 'tcx>)> {
|
2020-05-18 10:18:15 -05:00
|
|
|
// We iterate over all threads in the order of their indices because
|
|
|
|
// this allows us to have a deterministic scheduler.
|
2020-04-21 18:38:14 -05:00
|
|
|
for thread in self.threads.indices() {
|
2020-04-30 16:07:07 -05:00
|
|
|
match self.timeout_callbacks.entry(thread) {
|
2020-04-21 18:38:14 -05:00
|
|
|
Entry::Occupied(entry) =>
|
2020-05-18 09:28:19 -05:00
|
|
|
if entry.get().call_time.get_wait_time() == Duration::new(0, 0) {
|
2020-04-21 18:38:14 -05:00
|
|
|
return Some((thread, entry.remove().callback));
|
|
|
|
},
|
|
|
|
Entry::Vacant(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-07-27 05:53:39 -05:00
|
|
|
/// Wakes up threads joining on the active one and deallocates thread-local statics.
|
2021-07-15 13:33:08 -05:00
|
|
|
/// The `AllocId` that can now be freed are returned.
|
2021-05-23 04:00:25 -05:00
|
|
|
fn thread_terminated(
|
|
|
|
&mut self,
|
|
|
|
mut data_race: Option<&mut data_race::GlobalState>,
|
2021-07-15 13:33:08 -05:00
|
|
|
) -> Vec<Pointer<Tag>> {
|
2020-07-27 05:53:39 -05:00
|
|
|
let mut free_tls_statics = Vec::new();
|
|
|
|
{
|
|
|
|
let mut thread_local_statics = self.thread_local_alloc_ids.borrow_mut();
|
|
|
|
thread_local_statics.retain(|&(_def_id, thread), &mut alloc_id| {
|
|
|
|
if thread != self.active_thread {
|
|
|
|
// Keep this static around.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Delete this static from the map and from memory.
|
|
|
|
// We cannot free directly here as we cannot use `?` in this context.
|
|
|
|
free_tls_statics.push(alloc_id);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2020-11-06 11:29:54 -06:00
|
|
|
// Set the thread into a terminated state in the data-race detector
|
2021-05-23 04:00:25 -05:00
|
|
|
if let Some(ref mut data_race) = data_race {
|
2020-11-15 12:30:26 -06:00
|
|
|
data_race.thread_terminated();
|
|
|
|
}
|
2020-07-27 05:53:39 -05:00
|
|
|
// Check if we need to unblock any threads.
|
2020-07-26 08:53:02 -05:00
|
|
|
for (i, thread) in self.threads.iter_enumerated_mut() {
|
|
|
|
if thread.state == ThreadState::BlockedOnJoin(self.active_thread) {
|
2020-11-01 18:23:27 -06:00
|
|
|
// The thread has terminated, mark happens-before edge to joining thread
|
2021-05-23 04:00:25 -05:00
|
|
|
if let Some(ref mut data_race) = data_race {
|
2020-11-15 12:30:26 -06:00
|
|
|
data_race.thread_joined(i, self.active_thread);
|
|
|
|
}
|
2020-07-26 08:53:02 -05:00
|
|
|
trace!("unblocking {:?} because {:?} terminated", i, self.active_thread);
|
|
|
|
thread.state = ThreadState::Enabled;
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 05:53:39 -05:00
|
|
|
return free_tls_statics;
|
2020-07-26 08:53:02 -05:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:40:02 -05:00
|
|
|
/// Decide which action to take next and on which thread.
|
2020-04-19 16:21:18 -05:00
|
|
|
///
|
|
|
|
/// The currently implemented scheduling policy is the one that is commonly
|
|
|
|
/// 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
|
2020-04-26 16:42:07 -05:00
|
|
|
/// blocked, terminated, or has explicitly asked to be preempted).
|
2021-05-16 04:28:01 -05:00
|
|
|
fn schedule(
|
|
|
|
&mut self,
|
2021-05-22 07:47:14 -05:00
|
|
|
data_race: &Option<data_race::GlobalState>,
|
2021-05-16 04:28:01 -05:00
|
|
|
) -> InterpResult<'tcx, SchedulingAction> {
|
2020-04-27 13:01:35 -05:00
|
|
|
// Check whether the thread has **just** terminated (`check_terminated`
|
|
|
|
// checks whether the thread has popped all its stack and if yes, sets
|
2020-04-29 15:16:22 -05:00
|
|
|
// the thread state to terminated).
|
2020-03-16 18:48:44 -05:00
|
|
|
if self.threads[self.active_thread].check_terminated() {
|
2020-04-16 21:40:02 -05:00
|
|
|
return Ok(SchedulingAction::ExecuteDtors);
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2021-04-11 06:23:37 -05:00
|
|
|
// If we get here again and the thread is *still* terminated, there are no more dtors to run.
|
2020-04-19 16:21:18 -05:00
|
|
|
if self.threads[MAIN_THREAD].state == ThreadState::Terminated {
|
|
|
|
// The main thread terminated; stop the program.
|
2021-07-25 07:08:12 -05:00
|
|
|
// We do *not* run TLS dtors of remaining threads, which seems to match rustc behavior.
|
2020-04-19 16:21:18 -05:00
|
|
|
return Ok(SchedulingAction::Stop);
|
|
|
|
}
|
2021-04-11 06:23:37 -05:00
|
|
|
// This thread and the program can keep going.
|
2020-04-18 17:39:53 -05:00
|
|
|
if self.threads[self.active_thread].state == ThreadState::Enabled
|
|
|
|
&& !self.yield_active_thread
|
|
|
|
{
|
2020-04-19 16:21:18 -05:00
|
|
|
// The currently active thread is still enabled, just continue with it.
|
2020-04-16 21:40:02 -05:00
|
|
|
return Ok(SchedulingAction::ExecuteStep);
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2021-04-11 06:23:37 -05:00
|
|
|
// The active thread yielded. Let's see if there are any timeouts to take care of. We do
|
|
|
|
// this *before* running any other thread, to ensure that timeouts "in the past" fire before
|
|
|
|
// any other thread can take an action. This ensures that for `pthread_cond_timedwait`, "an
|
|
|
|
// error is returned if [...] the absolute time specified by abstime has already been passed
|
|
|
|
// at the time of the call".
|
|
|
|
// <https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html>
|
|
|
|
let potential_sleep_time =
|
|
|
|
self.timeout_callbacks.values().map(|info| info.call_time.get_wait_time()).min();
|
|
|
|
if potential_sleep_time == Some(Duration::new(0, 0)) {
|
|
|
|
return Ok(SchedulingAction::ExecuteTimeoutCallback);
|
|
|
|
}
|
|
|
|
// No callbacks scheduled, pick a regular thread to execute.
|
2020-04-19 16:21:18 -05:00
|
|
|
// We need to pick a new thread for execution.
|
2020-04-18 17:39:53 -05:00
|
|
|
for (id, thread) in self.threads.iter_enumerated() {
|
|
|
|
if thread.state == ThreadState::Enabled {
|
2020-04-19 16:21:18 -05:00
|
|
|
if !self.yield_active_thread || id != self.active_thread {
|
2020-04-18 17:39:53 -05:00
|
|
|
self.active_thread = id;
|
2020-11-15 12:30:26 -06:00
|
|
|
if let Some(data_race) = data_race {
|
|
|
|
data_race.thread_set_active(self.active_thread);
|
|
|
|
}
|
2020-04-18 17:39:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.yield_active_thread = false;
|
|
|
|
if self.threads[self.active_thread].state == ThreadState::Enabled {
|
2020-04-16 21:40:02 -05:00
|
|
|
return Ok(SchedulingAction::ExecuteStep);
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-19 16:21:18 -05:00
|
|
|
// We have not found a thread to execute.
|
2020-03-16 18:48:44 -05:00
|
|
|
if self.threads.iter().all(|thread| thread.state == ThreadState::Terminated) {
|
2020-05-18 09:28:19 -05:00
|
|
|
unreachable!("all threads terminated without the main thread terminating?!");
|
2020-05-19 11:44:32 -05:00
|
|
|
} else if let Some(sleep_time) = potential_sleep_time {
|
2020-04-21 18:38:14 -05:00
|
|
|
// All threads are currently blocked, but we have unexecuted
|
2020-04-30 16:07:07 -05:00
|
|
|
// timeout_callbacks, which may unblock some of the threads. Hence,
|
2020-04-21 18:38:14 -05:00
|
|
|
// sleep until the first callback.
|
2020-05-18 09:28:19 -05:00
|
|
|
std::thread::sleep(sleep_time);
|
2020-04-30 16:07:07 -05:00
|
|
|
Ok(SchedulingAction::ExecuteTimeoutCallback)
|
2020-03-16 18:48:44 -05:00
|
|
|
} else {
|
2020-04-07 22:20:41 -05:00
|
|
|
throw_machine_stop!(TerminationInfo::Deadlock);
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
// Public interface to thread management.
|
2020-03-16 18:48:44 -05:00
|
|
|
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2020-04-16 15:32:40 -05:00
|
|
|
/// Get a thread-specific allocation id for the given thread-local static.
|
|
|
|
/// If needed, allocate a new one.
|
2021-07-15 13:33:08 -05:00
|
|
|
fn get_or_create_thread_local_alloc(
|
2021-05-16 04:28:01 -05:00
|
|
|
&mut self,
|
|
|
|
def_id: DefId,
|
2021-07-15 13:33:08 -05:00
|
|
|
) -> InterpResult<'tcx, Pointer<Tag>> {
|
2020-07-27 05:53:39 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-04-16 15:32:40 -05:00
|
|
|
let tcx = this.tcx;
|
2021-07-15 13:33:08 -05:00
|
|
|
if let Some(old_alloc) = this.machine.threads.get_thread_local_alloc_id(def_id) {
|
2020-04-16 15:32:40 -05:00
|
|
|
// We already have a thread-specific allocation id for this
|
|
|
|
// thread-local static.
|
2021-07-15 13:33:08 -05:00
|
|
|
Ok(old_alloc)
|
2020-04-16 15:32:40 -05:00
|
|
|
} else {
|
|
|
|
// We need to allocate a thread-specific allocation id for this
|
|
|
|
// thread-local static.
|
2020-07-27 05:53:39 -05:00
|
|
|
// First, we compute the initial value for this static.
|
2020-04-16 15:32:40 -05:00
|
|
|
if tcx.is_foreign_item(def_id) {
|
|
|
|
throw_unsup_format!("foreign thread-local statics are not supported");
|
|
|
|
}
|
2020-08-11 04:37:29 -05:00
|
|
|
let allocation = tcx.eval_static_initializer(def_id)?;
|
2020-07-27 05:53:39 -05:00
|
|
|
// Create a fresh allocation with this content.
|
2021-07-15 13:33:08 -05:00
|
|
|
let new_alloc =
|
2022-04-03 15:12:52 -05:00
|
|
|
this.allocate_raw_ptr(allocation.inner().clone(), MiriMemoryKind::Tls.into());
|
2021-07-15 13:33:08 -05:00
|
|
|
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
|
|
|
|
Ok(new_alloc)
|
2020-04-16 15:32:40 -05:00
|
|
|
}
|
2020-04-15 16:34:34 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn create_thread(&mut self) -> ThreadId {
|
2020-03-16 18:48:44 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-11-01 18:23:27 -06:00
|
|
|
let id = this.machine.threads.create_thread();
|
2022-04-03 15:12:52 -05:00
|
|
|
if let Some(data_race) = &mut this.machine.data_race {
|
2020-11-15 12:30:26 -06:00
|
|
|
data_race.thread_created(id);
|
|
|
|
}
|
2020-11-01 18:23:27 -06:00
|
|
|
id
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-03-16 18:48:44 -05:00
|
|
|
fn detach_thread(&mut self, thread_id: ThreadId) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
2020-04-19 18:43:40 -05:00
|
|
|
this.machine.threads.detach_thread(thread_id)
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-03-16 18:48:44 -05:00
|
|
|
fn join_thread(&mut self, joined_thread_id: ThreadId) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
2022-04-03 15:12:52 -05:00
|
|
|
this.machine.threads.join_thread(joined_thread_id, this.machine.data_race.as_mut())?;
|
2020-11-01 18:23:27 -06:00
|
|
|
Ok(())
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn set_active_thread(&mut self, thread_id: ThreadId) -> ThreadId {
|
2020-03-16 18:48:44 -05:00
|
|
|
let this = self.eval_context_mut();
|
2022-04-03 15:12:52 -05:00
|
|
|
if let Some(data_race) = &this.machine.data_race {
|
2020-11-15 12:30:26 -06:00
|
|
|
data_race.thread_set_active(thread_id);
|
|
|
|
}
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.set_active_thread_id(thread_id)
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn get_active_thread(&self) -> ThreadId {
|
2020-03-16 18:48:44 -05:00
|
|
|
let this = self.eval_context_ref();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.get_active_thread_id()
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-08-31 19:32:14 -05:00
|
|
|
#[inline]
|
|
|
|
fn active_thread_mut(&mut self) -> &mut Thread<'mir, 'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.machine.threads.active_thread_mut()
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:29:09 -05:00
|
|
|
#[inline]
|
|
|
|
fn active_thread_ref(&self) -> &Thread<'mir, 'tcx> {
|
|
|
|
let this = self.eval_context_ref();
|
|
|
|
this.machine.threads.active_thread_ref()
|
|
|
|
}
|
|
|
|
|
2020-04-19 22:52:53 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn get_total_thread_count(&self) -> usize {
|
2020-04-19 22:52:53 -05:00
|
|
|
let this = self.eval_context_ref();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.get_total_thread_count()
|
2020-04-19 22:52:53 -05:00
|
|
|
}
|
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn has_terminated(&self, thread_id: ThreadId) -> bool {
|
2020-04-16 21:40:02 -05:00
|
|
|
let this = self.eval_context_ref();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.has_terminated(thread_id)
|
2020-04-16 21:40:02 -05:00
|
|
|
}
|
|
|
|
|
2021-07-25 07:08:12 -05:00
|
|
|
#[inline]
|
|
|
|
fn have_all_terminated(&self) -> bool {
|
|
|
|
let this = self.eval_context_ref();
|
|
|
|
this.machine.threads.have_all_terminated()
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:16:24 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn enable_thread(&mut self, thread_id: ThreadId) {
|
2020-04-24 17:16:24 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.machine.threads.enable_thread(thread_id);
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-03-16 18:48:44 -05:00
|
|
|
fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
|
|
|
|
let this = self.eval_context_ref();
|
|
|
|
this.machine.threads.active_thread_stack()
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-03-16 18:48:44 -05:00
|
|
|
fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.machine.threads.active_thread_stack_mut()
|
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
|
2020-04-06 18:12:13 -05:00
|
|
|
let this = self.eval_context_mut();
|
2022-04-03 15:12:52 -05:00
|
|
|
if let Some(data_race) = &mut this.machine.data_race {
|
2020-11-15 12:30:26 -06:00
|
|
|
if let Ok(string) = String::from_utf8(new_thread_name.clone()) {
|
2021-05-16 04:28:01 -05:00
|
|
|
data_race.thread_set_name(this.machine.threads.active_thread, string);
|
2020-11-15 12:30:26 -06:00
|
|
|
}
|
2020-11-01 18:23:27 -06:00
|
|
|
}
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.set_thread_name(new_thread_name);
|
2020-04-06 18:12:13 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn get_active_thread_name<'c>(&'c self) -> &'c [u8]
|
2020-04-24 18:46:51 -05:00
|
|
|
where
|
|
|
|
'mir: 'c,
|
|
|
|
{
|
|
|
|
let this = self.eval_context_ref();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.get_thread_name()
|
2020-04-19 16:21:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn block_thread(&mut self, thread: ThreadId) {
|
2020-04-07 22:20:41 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.block_thread(thread);
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn unblock_thread(&mut self, thread: ThreadId) {
|
2020-04-07 22:20:41 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-05-30 15:29:27 -05:00
|
|
|
this.machine.threads.unblock_thread(thread);
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn yield_active_thread(&mut self) {
|
2020-04-07 22:20:41 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-04-21 18:38:14 -05:00
|
|
|
this.machine.threads.yield_active_thread();
|
2020-04-07 22:20:41 -05:00
|
|
|
}
|
2020-04-15 23:25:12 -05:00
|
|
|
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-04-30 16:07:07 -05:00
|
|
|
fn register_timeout_callback(
|
2020-04-21 18:38:14 -05:00
|
|
|
&mut self,
|
|
|
|
thread: ThreadId,
|
2020-05-18 09:28:19 -05:00
|
|
|
call_time: Time,
|
2020-04-30 16:07:07 -05:00
|
|
|
callback: TimeoutCallback<'mir, 'tcx>,
|
2020-05-30 15:29:27 -05:00
|
|
|
) {
|
2020-04-18 17:39:53 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-04-30 16:07:07 -05:00
|
|
|
this.machine.threads.register_timeout_callback(thread, call_time, callback);
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-05-30 15:29:27 -05:00
|
|
|
fn unregister_timeout_callback_if_exists(&mut self, thread: ThreadId) {
|
2020-04-21 18:38:14 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-04-30 16:07:07 -05:00
|
|
|
this.machine.threads.unregister_timeout_callback_if_exists(thread);
|
2020-04-21 18:38:14 -05:00
|
|
|
}
|
|
|
|
|
2020-04-30 16:07:07 -05:00
|
|
|
/// Execute a timeout callback on the callback's thread.
|
2020-04-21 18:38:14 -05:00
|
|
|
#[inline]
|
2020-04-30 16:07:07 -05:00
|
|
|
fn run_timeout_callback(&mut self) -> InterpResult<'tcx> {
|
2020-04-21 18:38:14 -05:00
|
|
|
let this = self.eval_context_mut();
|
2020-05-18 09:28:19 -05:00
|
|
|
let (thread, callback) =
|
2021-04-10 13:23:32 -05:00
|
|
|
if let Some((thread, callback)) = this.machine.threads.get_ready_callback() {
|
|
|
|
(thread, callback)
|
|
|
|
} else {
|
2021-04-11 06:39:03 -05:00
|
|
|
// get_ready_callback can return None if the computer's clock
|
|
|
|
// was shifted after calling the scheduler and before the call
|
|
|
|
// to get_ready_callback (see issue
|
|
|
|
// https://github.com/rust-lang/miri/issues/1763). In this case,
|
|
|
|
// just do nothing, which effectively just returns to the
|
|
|
|
// scheduler.
|
2021-04-10 13:23:32 -05:00
|
|
|
return Ok(());
|
|
|
|
};
|
2020-05-24 13:29:56 -05:00
|
|
|
// This back-and-forth with `set_active_thread` is here because of two
|
|
|
|
// design decisions:
|
|
|
|
// 1. Make the caller and not the callback responsible for changing
|
|
|
|
// thread.
|
|
|
|
// 2. Make the scheduler the only place that can change the active
|
|
|
|
// thread.
|
2020-05-30 15:29:27 -05:00
|
|
|
let old_thread = this.set_active_thread(thread);
|
2020-04-21 18:38:14 -05:00
|
|
|
callback(this)?;
|
2020-05-30 15:29:27 -05:00
|
|
|
this.set_active_thread(old_thread);
|
2020-04-18 17:39:53 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:40:02 -05:00
|
|
|
/// Decide which action to take next and on which thread.
|
2020-04-19 16:21:18 -05:00
|
|
|
#[inline]
|
2020-04-16 21:40:02 -05:00
|
|
|
fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
|
2020-03-16 18:48:44 -05:00
|
|
|
let this = self.eval_context_mut();
|
2022-04-03 15:12:52 -05:00
|
|
|
let data_race = &this.machine.data_race;
|
2020-11-01 18:23:27 -06:00
|
|
|
this.machine.threads.schedule(data_race)
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|
2020-07-26 08:53:02 -05:00
|
|
|
|
2020-07-27 05:53:39 -05:00
|
|
|
/// Handles thread termination of the active thread: wakes up threads joining on this one,
|
|
|
|
/// and deallocated thread-local statics.
|
|
|
|
///
|
|
|
|
/// This is called from `tls.rs` after handling the TLS dtors.
|
2020-07-26 08:53:02 -05:00
|
|
|
#[inline]
|
2020-07-27 05:53:39 -05:00
|
|
|
fn thread_terminated(&mut self) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
2022-04-03 15:12:52 -05:00
|
|
|
for ptr in this.machine.threads.thread_terminated(this.machine.data_race.as_mut()) {
|
|
|
|
this.deallocate_ptr(ptr.into(), None, MiriMemoryKind::Tls.into())?;
|
2020-07-27 05:53:39 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
2020-07-26 08:53:02 -05:00
|
|
|
}
|
2020-03-16 18:48:44 -05:00
|
|
|
}
|