Skip to content

Commit 044a068

Browse files
Vytautas Astrauskasvakaras
authored andcommitted
Improve code readability and comments.
1 parent d0de439 commit 044a068

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

src/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
210210
SchedulingAction::ExecuteStep => {
211211
assert!(ecx.step()?, "a terminated thread was scheduled for execution");
212212
}
213-
SchedulingAction::ExecuteCallback => {
213+
SchedulingAction::ExecuteTimeoutCallback => {
214214
assert!(ecx.machine.communicate,
215215
"scheduler callbacks require disabled isolation, but the code \
216216
that created the callback did not check it");
217-
ecx.run_scheduler_callback()?;
217+
ecx.run_timeout_callback()?;
218218
}
219219
SchedulingAction::ExecuteDtors => {
220220
// This will either enable the thread again (so we go back

src/shims/sync.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn mutex_get_or_create_id<'mir, 'tcx: 'mir>(
128128
mutex_set_id(ecx, mutex_op, id.to_u32_scalar())?;
129129
Ok(id)
130130
} else {
131-
Ok(id.into())
131+
Ok(MutexId::from_u32(id))
132132
}
133133
}
134134

@@ -168,7 +168,7 @@ fn rwlock_get_or_create_id<'mir, 'tcx: 'mir>(
168168
rwlock_set_id(ecx, rwlock_op, id.to_u32_scalar())?;
169169
Ok(id)
170170
} else {
171-
Ok(id.into())
171+
Ok(RwLockId::from_u32(id))
172172
}
173173
}
174174

@@ -232,7 +232,7 @@ fn cond_get_or_create_id<'mir, 'tcx: 'mir>(
232232
cond_set_id(ecx, cond_op, id.to_u32_scalar())?;
233233
Ok(id)
234234
} else {
235-
Ok(id.into())
235+
Ok(CondvarId::from_u32(id))
236236
}
237237
}
238238

@@ -656,7 +656,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
656656
let id = cond_get_or_create_id(this, cond_op)?;
657657
if let Some((thread, mutex)) = this.condvar_signal(id) {
658658
reacquire_cond_mutex(this, thread, mutex)?;
659-
this.unregister_callback_if_exists(thread)?;
659+
this.unregister_timeout_callback_if_exists(thread)?;
660660
}
661661

662662
Ok(0)
@@ -668,7 +668,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
668668

669669
while let Some((thread, mutex)) = this.condvar_signal(id) {
670670
reacquire_cond_mutex(this, thread, mutex)?;
671-
this.unregister_callback_if_exists(thread)?;
671+
this.unregister_timeout_callback_if_exists(thread)?;
672672
}
673673

674674
Ok(0)
@@ -739,7 +739,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
739739
};
740740

741741
// Register the timeout callback.
742-
this.register_callback(
742+
this.register_timeout_callback(
743743
active_thread,
744744
timeout_time,
745745
Box::new(move |ecx| {

src/sync.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ use crate::*;
99

1010
macro_rules! declare_id {
1111
($name: ident) => {
12+
/// 0 is used to indicate that the id was not yet assigned and,
13+
/// therefore, is not a valid identifier.
1214
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
1315
pub struct $name(NonZeroU32);
1416

17+
impl $name {
18+
// Panics if `id == 0`.
19+
pub fn from_u32(id: u32) -> Self {
20+
Self(NonZeroU32::new(id).unwrap())
21+
}
22+
}
23+
1524
impl Idx for $name {
1625
fn new(idx: usize) -> Self {
1726
$name(NonZeroU32::new(u32::try_from(idx).unwrap() + 1).unwrap())
@@ -21,12 +30,6 @@ macro_rules! declare_id {
2130
}
2231
}
2332

24-
impl From<u32> for $name {
25-
fn from(id: u32) -> Self {
26-
Self(NonZeroU32::new(id).unwrap())
27-
}
28-
}
29-
3033
impl $name {
3134
pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
3235
Scalar::from_u32(self.0.get())

src/thread.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ use crate::*;
2424
pub enum SchedulingAction {
2525
/// Execute step on the active thread.
2626
ExecuteStep,
27-
/// Execute a scheduler's callback.
28-
ExecuteCallback,
27+
/// Execute a timeout callback.
28+
ExecuteTimeoutCallback,
2929
/// Execute destructors of the active thread.
3030
ExecuteDtors,
3131
/// Stop the program.
3232
Stop,
3333
}
3434

35-
type EventCallback<'mir, 'tcx> =
35+
/// Timeout timeout_callbacks can be created by synchronization primitives to tell the
36+
/// scheduler that they should be called once some period of time passes.
37+
type TimeoutCallback<'mir, 'tcx> =
3638
Box<dyn FnOnce(&mut InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>) -> InterpResult<'tcx> + 'tcx>;
3739

3840
/// A thread identifier.
@@ -161,14 +163,14 @@ impl<'mir, 'tcx> Default for Thread<'mir, 'tcx> {
161163
/// conditional variable with a timeout creates a callback that is called after
162164
/// the specified time and unblocks the thread. If another thread signals on the
163165
/// conditional variable, the signal handler deletes the callback.
164-
struct CallBackInfo<'mir, 'tcx> {
166+
struct TimeoutCallbackInfo<'mir, 'tcx> {
165167
/// The callback should be called no earlier than this time.
166168
call_time: Instant,
167169
/// The called function.
168-
callback: EventCallback<'mir, 'tcx>,
170+
callback: TimeoutCallback<'mir, 'tcx>,
169171
}
170172

171-
impl<'mir, 'tcx> std::fmt::Debug for CallBackInfo<'mir, 'tcx> {
173+
impl<'mir, 'tcx> std::fmt::Debug for TimeoutCallbackInfo<'mir, 'tcx> {
172174
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173175
write!(f, "CallBack({:?})", self.call_time)
174176
}
@@ -183,17 +185,16 @@ pub struct ThreadManager<'mir, 'tcx> {
183185
///
184186
/// Note that this vector also contains terminated threads.
185187
threads: IndexVec<ThreadId, Thread<'mir, 'tcx>>,
186-
/// FIXME: make private.
188+
/// This field is pub(crate) because the synchronization primitives
189+
/// (`crate::sync`) need a way to access it.
187190
pub(crate) sync: SynchronizationState,
188-
/// A counter used to generate unique identifiers for blocksets.
189-
blockset_counter: u32,
190191
/// A mapping from a thread-local static to an allocation id of a thread
191192
/// specific allocation.
192193
thread_local_alloc_ids: RefCell<FxHashMap<(DefId, ThreadId), AllocId>>,
193194
/// A flag that indicates that we should change the active thread.
194195
yield_active_thread: bool,
195196
/// Callbacks that are called once the specified time passes.
196-
callbacks: FxHashMap<ThreadId, CallBackInfo<'mir, 'tcx>>,
197+
timeout_callbacks: FxHashMap<ThreadId, TimeoutCallbackInfo<'mir, 'tcx>>,
197198
}
198199

199200
impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
@@ -208,10 +209,9 @@ impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
208209
active_thread: ThreadId::new(0),
209210
threads: threads,
210211
sync: SynchronizationState::default(),
211-
blockset_counter: 0,
212212
thread_local_alloc_ids: Default::default(),
213213
yield_active_thread: false,
214-
callbacks: FxHashMap::default(),
214+
timeout_callbacks: FxHashMap::default(),
215215
}
216216
}
217217
}
@@ -359,28 +359,28 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
359359
}
360360

361361
/// Register the given `callback` to be called once the `call_time` passes.
362-
fn register_callback(
362+
fn register_timeout_callback(
363363
&mut self,
364364
thread: ThreadId,
365365
call_time: Instant,
366-
callback: EventCallback<'mir, 'tcx>,
366+
callback: TimeoutCallback<'mir, 'tcx>,
367367
) {
368-
self.callbacks
369-
.insert(thread, CallBackInfo { call_time: call_time, callback: callback })
368+
self.timeout_callbacks
369+
.insert(thread, TimeoutCallbackInfo { call_time: call_time, callback: callback })
370370
.unwrap_none();
371371
}
372372

373373
/// Unregister the callback for the `thread`.
374-
fn unregister_callback_if_exists(&mut self, thread: ThreadId) {
375-
self.callbacks.remove(&thread);
374+
fn unregister_timeout_callback_if_exists(&mut self, thread: ThreadId) {
375+
self.timeout_callbacks.remove(&thread);
376376
}
377377

378378
/// Get a callback that is ready to be called.
379-
fn get_callback(&mut self) -> Option<(ThreadId, EventCallback<'mir, 'tcx>)> {
379+
fn get_callback(&mut self) -> Option<(ThreadId, TimeoutCallback<'mir, 'tcx>)> {
380380
let current_time = Instant::now();
381381
// We use a for loop here to make the scheduler more deterministic.
382382
for thread in self.threads.indices() {
383-
match self.callbacks.entry(thread) {
383+
match self.timeout_callbacks.entry(thread) {
384384
Entry::Occupied(entry) =>
385385
if current_time >= entry.get().call_time {
386386
return Some((thread, entry.remove().callback));
@@ -447,17 +447,17 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
447447
if self.threads.iter().all(|thread| thread.state == ThreadState::Terminated) {
448448
unreachable!();
449449
} else if let Some(next_call_time) =
450-
self.callbacks.values().min_by_key(|info| info.call_time)
450+
self.timeout_callbacks.values().min_by_key(|info| info.call_time)
451451
{
452452
// All threads are currently blocked, but we have unexecuted
453-
// callbacks, which may unblock some of the threads. Hence,
453+
// timeout_callbacks, which may unblock some of the threads. Hence,
454454
// sleep until the first callback.
455455
if let Some(sleep_time) =
456456
next_call_time.call_time.checked_duration_since(Instant::now())
457457
{
458458
std::thread::sleep(sleep_time);
459459
}
460-
Ok(SchedulingAction::ExecuteCallback)
460+
Ok(SchedulingAction::ExecuteTimeoutCallback)
461461
} else {
462462
throw_machine_stop!(TerminationInfo::Deadlock);
463463
}
@@ -647,27 +647,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
647647
}
648648

649649
#[inline]
650-
fn register_callback(
650+
fn register_timeout_callback(
651651
&mut self,
652652
thread: ThreadId,
653653
call_time: Instant,
654-
callback: EventCallback<'mir, 'tcx>,
654+
callback: TimeoutCallback<'mir, 'tcx>,
655655
) -> InterpResult<'tcx> {
656656
let this = self.eval_context_mut();
657-
this.machine.threads.register_callback(thread, call_time, callback);
657+
this.machine.threads.register_timeout_callback(thread, call_time, callback);
658658
Ok(())
659659
}
660660

661661
#[inline]
662-
fn unregister_callback_if_exists(&mut self, thread: ThreadId) -> InterpResult<'tcx> {
662+
fn unregister_timeout_callback_if_exists(&mut self, thread: ThreadId) -> InterpResult<'tcx> {
663663
let this = self.eval_context_mut();
664-
this.machine.threads.unregister_callback_if_exists(thread);
664+
this.machine.threads.unregister_timeout_callback_if_exists(thread);
665665
Ok(())
666666
}
667667

668-
/// Execute the callback on the callback's thread.
668+
/// Execute a timeout callback on the callback's thread.
669669
#[inline]
670-
fn run_scheduler_callback(&mut self) -> InterpResult<'tcx> {
670+
fn run_timeout_callback(&mut self) -> InterpResult<'tcx> {
671671
let this = self.eval_context_mut();
672672
let (thread, callback) = this.machine.threads.get_callback().expect("no callback found");
673673
let old_thread = this.set_active_thread(thread)?;

0 commit comments

Comments
 (0)