Skip to content

Commit e16b362

Browse files
committed
Auto merge of #2663 - RalfJung:sync, r=RalfJung
pthread: slight refactoring of how we access the sync object fields
2 parents 4392b27 + 40520b0 commit e16b362

File tree

4 files changed

+90
-175
lines changed

4 files changed

+90
-175
lines changed

src/concurrency/data_race.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::{
4949
use rustc_ast::Mutability;
5050
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5151
use rustc_index::vec::{Idx, IndexVec};
52-
use rustc_middle::{mir, ty::layout::TyAndLayout};
52+
use rustc_middle::mir;
5353
use rustc_target::abi::{Align, Size};
5454

5555
use crate::*;
@@ -440,33 +440,6 @@ impl MemoryCellClocks {
440440
/// Evaluation context extensions.
441441
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for MiriInterpCx<'mir, 'tcx> {}
442442
pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
443-
/// Atomic variant of read_scalar_at_offset.
444-
fn read_scalar_at_offset_atomic(
445-
&self,
446-
op: &OpTy<'tcx, Provenance>,
447-
offset: u64,
448-
layout: TyAndLayout<'tcx>,
449-
atomic: AtomicReadOrd,
450-
) -> InterpResult<'tcx, Scalar<Provenance>> {
451-
let this = self.eval_context_ref();
452-
let value_place = this.deref_operand_and_offset(op, offset, layout)?;
453-
this.read_scalar_atomic(&value_place, atomic)
454-
}
455-
456-
/// Atomic variant of write_scalar_at_offset.
457-
fn write_scalar_at_offset_atomic(
458-
&mut self,
459-
op: &OpTy<'tcx, Provenance>,
460-
offset: u64,
461-
value: impl Into<Scalar<Provenance>>,
462-
layout: TyAndLayout<'tcx>,
463-
atomic: AtomicWriteOrd,
464-
) -> InterpResult<'tcx> {
465-
let this = self.eval_context_mut();
466-
let value_place = this.deref_operand_and_offset(op, offset, layout)?;
467-
this.write_scalar_atomic(value.into(), &value_place, atomic)
468-
}
469-
470443
/// Perform an atomic read operation at the memory location.
471444
fn read_scalar_atomic(
472445
&self,

src/concurrency/sync.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ impl<'mir, 'tcx: 'mir> EvalContextExtPriv<'mir, 'tcx> for crate::MiriInterpCx<'m
193193
pub(super) trait EvalContextExtPriv<'mir, 'tcx: 'mir>:
194194
crate::MiriInterpCxExt<'mir, 'tcx>
195195
{
196+
/// Lazily initialize the ID of this Miri sync structure.
197+
/// ('0' indicates uninit.)
196198
#[inline]
197-
// Miri sync structures contain zero-initialized ids stored at some offset behind a pointer
198199
fn get_or_create_id<Id: SyncId>(
199200
&mut self,
200201
next_id: Id,
@@ -205,6 +206,7 @@ pub(super) trait EvalContextExtPriv<'mir, 'tcx: 'mir>:
205206
let value_place =
206207
this.deref_operand_and_offset(lock_op, offset, this.machine.layouts.u32)?;
207208

209+
// Since we are lazy, this update has to be atomic.
208210
let (old, success) = this
209211
.atomic_compare_exchange_scalar(
210212
&value_place,

src/helpers.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
667667
layout: TyAndLayout<'tcx>,
668668
) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
669669
let this = self.eval_context_ref();
670-
let op_place = this.deref_operand(op)?;
670+
let op_place = this.deref_operand(op)?; // FIXME: we still deref with the original type!
671671
let offset = Size::from_bytes(offset);
672672

673673
// Ensure that the access is within bounds.
@@ -687,25 +687,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
687687
this.read_scalar(&value_place.into())
688688
}
689689

690-
fn write_immediate_at_offset(
691-
&mut self,
692-
op: &OpTy<'tcx, Provenance>,
693-
offset: u64,
694-
value: &ImmTy<'tcx, Provenance>,
695-
) -> InterpResult<'tcx, ()> {
696-
let this = self.eval_context_mut();
697-
let value_place = this.deref_operand_and_offset(op, offset, value.layout)?;
698-
this.write_immediate(**value, &value_place.into())
699-
}
700-
701690
fn write_scalar_at_offset(
702691
&mut self,
703692
op: &OpTy<'tcx, Provenance>,
704693
offset: u64,
705694
value: impl Into<Scalar<Provenance>>,
706695
layout: TyAndLayout<'tcx>,
707696
) -> InterpResult<'tcx, ()> {
708-
self.write_immediate_at_offset(op, offset, &ImmTy::from_scalar(value.into(), layout))
697+
let this = self.eval_context_mut();
698+
let value_place = this.deref_operand_and_offset(op, offset, layout)?;
699+
this.write_scalar(value, &value_place.into())
709700
}
710701

711702
/// Parse a `timespec` struct and return it as a `std::time::Duration`. It returns `None`

0 commit comments

Comments
 (0)