Skip to content

Commit e09c571

Browse files
committed
avoid some borrow_mut calls in data_race
1 parent 543777a commit e09c571

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

src/data_race.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
598598
// of the time, based on `rate`.
599599
let rate = this.memory.extra.cmpxchg_weak_failure_rate;
600600
let cmpxchg_success = eq.to_bool()?
601-
&& (!can_fail_spuriously || this.memory.extra.rng.borrow_mut().gen::<f64>() < rate);
601+
&& (!can_fail_spuriously || this.memory.extra.rng.get_mut().gen::<f64>() < rate);
602602
let res = Immediate::ScalarPair(
603603
old.to_scalar_or_uninit(),
604604
Scalar::from_bool(cmpxchg_success).into(),
@@ -647,7 +647,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
647647
place: &MPlaceTy<'tcx, Tag>,
648648
atomic: AtomicWriteOp,
649649
) -> InterpResult<'tcx> {
650-
let this = self.eval_context_ref();
650+
let this = self.eval_context_mut();
651651
this.validate_atomic_op(
652652
place,
653653
atomic,
@@ -672,7 +672,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
672672
use AtomicRwOp::*;
673673
let acquire = matches!(atomic, Acquire | AcqRel | SeqCst);
674674
let release = matches!(atomic, Release | AcqRel | SeqCst);
675-
let this = self.eval_context_ref();
675+
let this = self.eval_context_mut();
676676
this.validate_atomic_op(place, atomic, "Atomic RMW", move |memory, clocks, index, _| {
677677
if acquire {
678678
memory.load_acquire(clocks, index)?;
@@ -690,7 +690,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
690690
/// Update the data-race detector for an atomic fence on the current thread.
691691
fn validate_atomic_fence(&mut self, atomic: AtomicFenceOp) -> InterpResult<'tcx> {
692692
let this = self.eval_context_mut();
693-
if let Some(data_race) = &this.memory.extra.data_race {
693+
if let Some(data_race) = &mut this.memory.extra.data_race {
694694
data_race.maybe_perform_sync_operation(move |index, mut clocks| {
695695
log::trace!("Atomic fence on {:?} with ordering {:?}", index, atomic);
696696

@@ -771,7 +771,7 @@ impl VClockAlloc {
771771
}
772772

773773
fn reset_clocks(&mut self, offset: Size, len: Size) {
774-
let mut alloc_ranges = self.alloc_ranges.borrow_mut();
774+
let alloc_ranges = self.alloc_ranges.get_mut();
775775
for (_, range) in alloc_ranges.iter_mut(offset, len) {
776776
// Reset the portion of the range
777777
*range = MemoryCellClocks::new(0, VectorIdx::MAX_INDEX);
@@ -1025,6 +1025,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
10251025
if let Some(data_race) = &this.memory.extra.data_race {
10261026
if data_race.multi_threaded.get() {
10271027
// Load and log the atomic operation.
1028+
// Note that atomic loads are possible even from read-only allocations, so `get_alloc_extra_mut` is not an option.
10281029
let place_ptr = place.ptr.assert_ptr();
10291030
let size = place.layout.size;
10301031
let alloc_meta =
@@ -1105,6 +1106,7 @@ struct ThreadExtraState {
11051106
/// Global data-race detection state, contains the currently
11061107
/// executing thread as well as the vector-clocks associated
11071108
/// with each of the threads.
1109+
// FIXME: it is probably better to have one large RefCell, than to have so many small ones.
11081110
#[derive(Debug, Clone)]
11091111
pub struct GlobalState {
11101112
/// Set to true once the first additional
@@ -1158,7 +1160,7 @@ impl GlobalState {
11581160
/// Create a new global state, setup with just thread-id=0
11591161
/// advanced to timestamp = 1.
11601162
pub fn new() -> Self {
1161-
let global_state = GlobalState {
1163+
let mut global_state = GlobalState {
11621164
multi_threaded: Cell::new(false),
11631165
vector_clocks: RefCell::new(IndexVec::new()),
11641166
vector_info: RefCell::new(IndexVec::new()),
@@ -1172,9 +1174,9 @@ impl GlobalState {
11721174
// Setup the main-thread since it is not explicitly created:
11731175
// uses vector index and thread-id 0, also the rust runtime gives
11741176
// the main-thread a name of "main".
1175-
let index = global_state.vector_clocks.borrow_mut().push(ThreadClockSet::default());
1176-
global_state.vector_info.borrow_mut().push(ThreadId::new(0));
1177-
global_state.thread_info.borrow_mut().push(ThreadExtraState {
1177+
let index = global_state.vector_clocks.get_mut().push(ThreadClockSet::default());
1178+
global_state.vector_info.get_mut().push(ThreadId::new(0));
1179+
global_state.thread_info.get_mut().push(ThreadExtraState {
11781180
vector_index: Some(index),
11791181
thread_name: Some("main".to_string().into_boxed_str()),
11801182
termination_vector_clock: None,
@@ -1221,7 +1223,7 @@ impl GlobalState {
12211223
// Hook for thread creation, enabled multi-threaded execution and marks
12221224
// the current thread timestamp as happening-before the current thread.
12231225
#[inline]
1224-
pub fn thread_created(&self, thread: ThreadId) {
1226+
pub fn thread_created(&mut self, thread: ThreadId) {
12251227
let current_index = self.current_index();
12261228

12271229
// Increment the number of active threads.
@@ -1241,12 +1243,12 @@ impl GlobalState {
12411243
let created_index = if let Some(reuse_index) = self.find_vector_index_reuse_candidate() {
12421244
// Now re-configure the re-use candidate, increment the clock
12431245
// for the new sync use of the vector.
1244-
let mut vector_clocks = self.vector_clocks.borrow_mut();
1246+
let vector_clocks = self.vector_clocks.get_mut();
12451247
vector_clocks[reuse_index].increment_clock(reuse_index);
12461248

12471249
// Locate the old thread the vector was associated with and update
12481250
// it to represent the new thread instead.
1249-
let mut vector_info = self.vector_info.borrow_mut();
1251+
let vector_info = self.vector_info.get_mut();
12501252
let old_thread = vector_info[reuse_index];
12511253
vector_info[reuse_index] = thread;
12521254

@@ -1258,7 +1260,7 @@ impl GlobalState {
12581260
} else {
12591261
// No vector re-use candidates available, instead create
12601262
// a new vector index.
1261-
let mut vector_info = self.vector_info.borrow_mut();
1263+
let vector_info = self.vector_info.get_mut();
12621264
vector_info.push(thread)
12631265
};
12641266

@@ -1268,7 +1270,7 @@ impl GlobalState {
12681270
thread_info[thread].vector_index = Some(created_index);
12691271

12701272
// Create a thread clock set if applicable.
1271-
let mut vector_clocks = self.vector_clocks.borrow_mut();
1273+
let vector_clocks = self.vector_clocks.get_mut();
12721274
if created_index == vector_clocks.next_index() {
12731275
vector_clocks.push(ThreadClockSet::default());
12741276
}
@@ -1289,9 +1291,9 @@ impl GlobalState {
12891291
/// Hook on a thread join to update the implicit happens-before relation
12901292
/// between the joined thread and the current thread.
12911293
#[inline]
1292-
pub fn thread_joined(&self, current_thread: ThreadId, join_thread: ThreadId) {
1293-
let mut clocks_vec = self.vector_clocks.borrow_mut();
1294-
let thread_info = self.thread_info.borrow();
1294+
pub fn thread_joined(&mut self, current_thread: ThreadId, join_thread: ThreadId) {
1295+
let clocks_vec = self.vector_clocks.get_mut();
1296+
let thread_info = self.thread_info.get_mut();
12951297

12961298
// Load the vector clock of the current thread.
12971299
let current_index = thread_info[current_thread]
@@ -1329,9 +1331,9 @@ impl GlobalState {
13291331

13301332
// If the thread is marked as terminated but not joined
13311333
// then move the thread to the re-use set.
1332-
let mut termination = self.terminated_threads.borrow_mut();
1334+
let termination = self.terminated_threads.get_mut();
13331335
if let Some(index) = termination.remove(&join_thread) {
1334-
let mut reuse = self.reuse_candidates.borrow_mut();
1336+
let reuse = self.reuse_candidates.get_mut();
13351337
reuse.insert(index);
13361338
}
13371339
}
@@ -1344,28 +1346,28 @@ impl GlobalState {
13441346
/// This should be called strictly before any calls to
13451347
/// `thread_joined`.
13461348
#[inline]
1347-
pub fn thread_terminated(&self) {
1349+
pub fn thread_terminated(&mut self) {
13481350
let current_index = self.current_index();
13491351

13501352
// Increment the clock to a unique termination timestamp.
1351-
let mut vector_clocks = self.vector_clocks.borrow_mut();
1353+
let vector_clocks = self.vector_clocks.get_mut();
13521354
let current_clocks = &mut vector_clocks[current_index];
13531355
current_clocks.increment_clock(current_index);
13541356

13551357
// Load the current thread id for the executing vector.
1356-
let vector_info = self.vector_info.borrow();
1358+
let vector_info = self.vector_info.get_mut();
13571359
let current_thread = vector_info[current_index];
13581360

13591361
// Load the current thread metadata, and move to a terminated
13601362
// vector state. Setting up the vector clock all join operations
13611363
// will use.
1362-
let mut thread_info = self.thread_info.borrow_mut();
1364+
let thread_info = self.thread_info.get_mut();
13631365
let current = &mut thread_info[current_thread];
13641366
current.termination_vector_clock = Some(current_clocks.clock.clone());
13651367

13661368
// Add this thread as a candidate for re-use after a thread join
13671369
// occurs.
1368-
let mut termination = self.terminated_threads.borrow_mut();
1370+
let termination = self.terminated_threads.get_mut();
13691371
termination.insert(current_thread, current_index);
13701372

13711373
// Reduce the number of active threads, now that a thread has
@@ -1392,9 +1394,9 @@ impl GlobalState {
13921394
/// the thread name is used for improved diagnostics
13931395
/// during a data-race.
13941396
#[inline]
1395-
pub fn thread_set_name(&self, thread: ThreadId, name: String) {
1397+
pub fn thread_set_name(&mut self, thread: ThreadId, name: String) {
13961398
let name = name.into_boxed_str();
1397-
let mut thread_info = self.thread_info.borrow_mut();
1399+
let thread_info = self.thread_info.get_mut();
13981400
thread_info[thread].thread_name = Some(name);
13991401
}
14001402

src/shims/posix/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn mutexattr_set_kind<'mir, 'tcx: 'mir>(
5858
// (the kind has to be at its offset for compatibility with static initializer macros)
5959

6060
fn mutex_get_kind<'mir, 'tcx: 'mir>(
61-
ecx: &mut MiriEvalContext<'mir, 'tcx>,
61+
ecx: &MiriEvalContext<'mir, 'tcx>,
6262
mutex_op: &OpTy<'tcx, Tag>,
6363
) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
6464
let offset = if ecx.pointer_size().bytes() == 8 { 16 } else { 12 };

src/thread.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
332332
fn join_thread(
333333
&mut self,
334334
joined_thread_id: ThreadId,
335-
data_race: &Option<data_race::GlobalState>,
335+
data_race: Option<&mut data_race::GlobalState>,
336336
) -> InterpResult<'tcx> {
337337
if self.threads[joined_thread_id].join_status != ThreadJoinStatus::Joinable {
338338
throw_ub_format!("trying to join a detached or already joined thread");
@@ -436,7 +436,10 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
436436

437437
/// Wakes up threads joining on the active one and deallocates thread-local statics.
438438
/// The `AllocId` that can now be freed is returned.
439-
fn thread_terminated(&mut self, data_race: &Option<data_race::GlobalState>) -> Vec<AllocId> {
439+
fn thread_terminated(
440+
&mut self,
441+
mut data_race: Option<&mut data_race::GlobalState>,
442+
) -> Vec<AllocId> {
440443
let mut free_tls_statics = Vec::new();
441444
{
442445
let mut thread_local_statics = self.thread_local_alloc_ids.borrow_mut();
@@ -452,14 +455,14 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
452455
});
453456
}
454457
// Set the thread into a terminated state in the data-race detector
455-
if let Some(data_race) = data_race {
458+
if let Some(ref mut data_race) = data_race {
456459
data_race.thread_terminated();
457460
}
458461
// Check if we need to unblock any threads.
459462
for (i, thread) in self.threads.iter_enumerated_mut() {
460463
if thread.state == ThreadState::BlockedOnJoin(self.active_thread) {
461464
// The thread has terminated, mark happens-before edge to joining thread
462-
if let Some(data_race) = data_race {
465+
if let Some(ref mut data_race) = data_race {
463466
data_race.thread_joined(i, self.active_thread);
464467
}
465468
trace!("unblocking {:?} because {:?} terminated", i, self.active_thread);
@@ -584,7 +587,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
584587
fn create_thread(&mut self) -> ThreadId {
585588
let this = self.eval_context_mut();
586589
let id = this.machine.threads.create_thread();
587-
if let Some(data_race) = &this.memory.extra.data_race {
590+
if let Some(data_race) = &mut this.memory.extra.data_race {
588591
data_race.thread_created(id);
589592
}
590593
id
@@ -599,8 +602,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
599602
#[inline]
600603
fn join_thread(&mut self, joined_thread_id: ThreadId) -> InterpResult<'tcx> {
601604
let this = self.eval_context_mut();
602-
let data_race = &this.memory.extra.data_race;
603-
this.machine.threads.join_thread(joined_thread_id, data_race)?;
605+
this.machine.threads.join_thread(joined_thread_id, this.memory.extra.data_race.as_mut())?;
604606
Ok(())
605607
}
606608

@@ -664,7 +666,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
664666
#[inline]
665667
fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) {
666668
let this = self.eval_context_mut();
667-
if let Some(data_race) = &this.memory.extra.data_race {
669+
if let Some(data_race) = &mut this.memory.extra.data_race {
668670
if let Ok(string) = String::from_utf8(new_thread_name.clone()) {
669671
data_race.thread_set_name(this.machine.threads.active_thread, string);
670672
}
@@ -759,8 +761,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
759761
#[inline]
760762
fn thread_terminated(&mut self) -> InterpResult<'tcx> {
761763
let this = self.eval_context_mut();
762-
let data_race = &this.memory.extra.data_race;
763-
for alloc_id in this.machine.threads.thread_terminated(data_race) {
764+
for alloc_id in this.machine.threads.thread_terminated(this.memory.extra.data_race.as_mut())
765+
{
764766
let ptr = this.memory.global_base_pointer(alloc_id.into())?;
765767
this.memory.deallocate(ptr, None, MiriMemoryKind::Tls.into())?;
766768
}

0 commit comments

Comments
 (0)