Skip to content

Commit 841d1b2

Browse files
saethlinRalfJung
authored andcommitted
Finish TimeoutCallback
1 parent 1c6a624 commit 841d1b2

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/tools/miri/src/concurrency/thread.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ pub enum SchedulingAction {
3232

3333
/// Timeout callbacks can be created by synchronization primitives to tell the
3434
/// scheduler that they should be called once some period of time passes.
35-
pub trait TimeoutCallback<'mir, 'tcx>: VisitMachineValues + 'tcx {
35+
pub trait MachineCallback<'mir, 'tcx>: VisitMachineValues {
3636
fn call(&self, ecx: &mut InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>) -> InterpResult<'tcx>;
3737
}
3838

39+
type TimeoutCallback<'mir, 'tcx> = Box<dyn MachineCallback<'mir, 'tcx> + 'tcx>;
40+
3941
/// A thread identifier.
4042
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
4143
pub struct ThreadId(u32);
@@ -252,7 +254,7 @@ struct TimeoutCallbackInfo<'mir, 'tcx> {
252254
/// The callback should be called no earlier than this time.
253255
call_time: Time,
254256
/// The called function.
255-
callback: Box<dyn TimeoutCallback<'mir, 'tcx>>,
257+
callback: TimeoutCallback<'mir, 'tcx>,
256258
}
257259

258260
impl<'mir, 'tcx> std::fmt::Debug for TimeoutCallbackInfo<'mir, 'tcx> {
@@ -303,10 +305,10 @@ impl VisitMachineValues for ThreadManager<'_, '_> {
303305
let ThreadManager {
304306
threads,
305307
thread_local_alloc_ids,
308+
timeout_callbacks,
306309
active_thread: _,
307310
yield_active_thread: _,
308311
sync: _,
309-
timeout_callbacks: _,
310312
} = self;
311313

312314
for thread in threads {
@@ -315,8 +317,9 @@ impl VisitMachineValues for ThreadManager<'_, '_> {
315317
for ptr in thread_local_alloc_ids.borrow().values().copied() {
316318
visit.visit(ptr);
317319
}
318-
// FIXME: Do we need to do something for TimeoutCallback? That's a Box<dyn>, not sure what
319-
// to do.
320+
for callback in timeout_callbacks.values() {
321+
callback.callback.visit_machine_values(visit);
322+
}
320323
}
321324
}
322325

@@ -542,7 +545,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
542545
&mut self,
543546
thread: ThreadId,
544547
call_time: Time,
545-
callback: Box<dyn TimeoutCallback<'mir, 'tcx>>,
548+
callback: TimeoutCallback<'mir, 'tcx>,
546549
) {
547550
self.timeout_callbacks
548551
.try_insert(thread, TimeoutCallbackInfo { call_time, callback })
@@ -558,7 +561,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
558561
fn get_ready_callback(
559562
&mut self,
560563
clock: &Clock,
561-
) -> Option<(ThreadId, Box<dyn TimeoutCallback<'mir, 'tcx>>)> {
564+
) -> Option<(ThreadId, TimeoutCallback<'mir, 'tcx>)> {
562565
// We iterate over all threads in the order of their indices because
563566
// this allows us to have a deterministic scheduler.
564567
for thread in self.threads.indices() {
@@ -931,7 +934,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
931934
&mut self,
932935
thread: ThreadId,
933936
call_time: Time,
934-
callback: Box<dyn TimeoutCallback<'mir, 'tcx>>,
937+
callback: TimeoutCallback<'mir, 'tcx>,
935938
) {
936939
let this = self.eval_context_mut();
937940
if !this.machine.communicate() && matches!(call_time, Time::RealTime(..)) {

src/tools/miri/src/shims/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::{Duration, SystemTime};
22

3-
use crate::concurrency::thread::TimeoutCallback;
3+
use crate::concurrency::thread::MachineCallback;
44
use crate::*;
55

66
/// Returns the time elapsed between the provided time and the unix epoch as a `Duration`.
@@ -257,7 +257,7 @@ impl VisitMachineValues for Callback {
257257
fn visit_machine_values(&self, _visit: &mut ProvenanceVisitor) {}
258258
}
259259

260-
impl<'mir, 'tcx: 'mir> TimeoutCallback<'mir, 'tcx> for Callback {
260+
impl<'mir, 'tcx: 'mir> MachineCallback<'mir, 'tcx> for Callback {
261261
fn call(&self, ecx: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
262262
ecx.unblock_thread(self.active_thread);
263263
Ok(())

src/tools/miri/src/shims/unix/linux/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::concurrency::thread::{Time, TimeoutCallback};
1+
use crate::concurrency::thread::{MachineCallback, Time};
22
use crate::*;
33
use rustc_target::abi::{Align, Size};
44
use std::time::SystemTime;
@@ -268,7 +268,7 @@ impl<'tcx> VisitMachineValues for Callback<'tcx> {
268268
}
269269
}
270270

271-
impl<'mir, 'tcx: 'mir> TimeoutCallback<'mir, 'tcx> for Callback<'tcx> {
271+
impl<'mir, 'tcx: 'mir> MachineCallback<'mir, 'tcx> for Callback<'tcx> {
272272
fn call(&self, this: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
273273
this.unblock_thread(self.thread);
274274
this.futex_remove_waiter(self.addr_usize, self.thread);

src/tools/miri/src/shims/unix/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::SystemTime;
33
use rustc_hir::LangItem;
44
use rustc_middle::ty::{layout::TyAndLayout, query::TyCtxtAt, Ty};
55

6-
use crate::concurrency::thread::{Time, TimeoutCallback};
6+
use crate::concurrency::thread::{MachineCallback, Time};
77
use crate::*;
88

99
// pthread_mutexattr_t is either 4 or 8 bytes, depending on the platform.
@@ -901,7 +901,7 @@ impl<'tcx> VisitMachineValues for Callback<'tcx> {
901901
}
902902
}
903903

904-
impl<'mir, 'tcx: 'mir> TimeoutCallback<'mir, 'tcx> for Callback<'tcx> {
904+
impl<'mir, 'tcx: 'mir> MachineCallback<'mir, 'tcx> for Callback<'tcx> {
905905
fn call(&self, ecx: &mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx> {
906906
// We are not waiting for the condvar any more, wait for the
907907
// mutex instead.

0 commit comments

Comments
 (0)