Skip to content

Commit 5fa30f7

Browse files
committed
make release_clock always work on the current thread
1 parent 331bb3f commit 5fa30f7

File tree

8 files changed

+115
-106
lines changed

8 files changed

+115
-106
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,8 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
171171
memory_kind,
172172
ecx.get_active_thread(),
173173
) {
174-
if let Some(clock) = clock
175-
&& let Some(data_race) = &ecx.machine.data_race
176-
{
177-
data_race.acquire_clock(&clock, ecx.get_active_thread());
174+
if let Some(clock) = clock {
175+
ecx.acquire_clock(&clock);
178176
}
179177
reuse_addr
180178
} else {
@@ -372,9 +370,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
372370
let thread = self.threads.get_active_thread_id();
373371
global_state.reuse.add_addr(rng, addr, size, align, kind, thread, || {
374372
if let Some(data_race) = &self.data_race {
375-
data_race
376-
.release_clock(thread, self.threads.active_thread_ref().current_span())
377-
.clone()
373+
data_race.release_clock(&self.threads).clone()
378374
} else {
379375
VClock::default()
380376
}

src/tools/miri/src/concurrency/data_race.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,26 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriInterpCxExt<'mir, 'tcx> {
822822
assert!(!old, "cannot nest allow_data_races");
823823
}
824824
}
825+
826+
/// Returns the `release` clock of the current thread.
827+
/// Other threads can acquire this clock in the future to establish synchronization
828+
/// with this program point.
829+
fn release_clock<'a>(&'a self) -> Option<Ref<'a, VClock>>
830+
where
831+
'mir: 'a,
832+
{
833+
let this = self.eval_context_ref();
834+
Some(this.machine.data_race.as_ref()?.release_clock(&this.machine.threads))
835+
}
836+
837+
/// Acquire the given clock into the current thread, establishing synchronization with
838+
/// the moment when that clock snapshot was taken via `release_clock`.
839+
fn acquire_clock(&self, clock: &VClock) {
840+
let this = self.eval_context_ref();
841+
if let Some(data_race) = &this.machine.data_race {
842+
data_race.acquire_clock(clock, this.get_active_thread());
843+
}
844+
}
825845
}
826846

827847
/// Vector clock metadata for a logical memory allocation.
@@ -1706,19 +1726,24 @@ impl GlobalState {
17061726
/// the moment when that clock snapshot was taken via `release_clock`.
17071727
/// As this is an acquire operation, the thread timestamp is not
17081728
/// incremented.
1709-
pub fn acquire_clock(&self, lock: &VClock, thread: ThreadId) {
1729+
pub fn acquire_clock(&self, clock: &VClock, thread: ThreadId) {
17101730
let (_, mut clocks) = self.thread_state_mut(thread);
1711-
clocks.clock.join(lock);
1731+
clocks.clock.join(clock);
17121732
}
17131733

1714-
/// Returns the `release` clock of the given thread.
1734+
/// Returns the `release` clock of the current thread.
17151735
/// Other threads can acquire this clock in the future to establish synchronization
17161736
/// with this program point.
1717-
pub fn release_clock(&self, thread: ThreadId, current_span: Span) -> Ref<'_, VClock> {
1737+
pub fn release_clock<'mir, 'tcx>(
1738+
&self,
1739+
threads: &ThreadManager<'mir, 'tcx>,
1740+
) -> Ref<'_, VClock> {
1741+
let thread = threads.get_active_thread_id();
1742+
let span = threads.active_thread_ref().current_span();
17181743
// We increment the clock each time this happens, to ensure no two releases
17191744
// can be confused with each other.
17201745
let (index, mut clocks) = self.thread_state_mut(thread);
1721-
clocks.increment_clock(index, current_span);
1746+
clocks.increment_clock(index, span);
17221747
drop(clocks);
17231748
// To return a read-only view, we need to release the RefCell
17241749
// and borrow it again.

src/tools/miri/src/concurrency/init_once.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6161
let current_thread = this.get_active_thread();
6262

6363
if let Some(data_race) = &this.machine.data_race {
64-
data_race
65-
.acquire_clock(&this.machine.threads.sync.init_onces[id].clock, current_thread);
64+
data_race.acquire_clock(&this.machine.sync.init_onces[id].clock, current_thread);
6665
}
6766
}
6867

@@ -112,11 +111,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
112111
) -> InterpResult<'tcx, Option<InitOnceId>>,
113112
{
114113
let this = self.eval_context_mut();
115-
let next_index = this.machine.threads.sync.init_onces.next_index();
114+
let next_index = this.machine.sync.init_onces.next_index();
116115
if let Some(old) = existing(this, next_index)? {
117116
Ok(old)
118117
} else {
119-
let new_index = this.machine.threads.sync.init_onces.push(Default::default());
118+
let new_index = this.machine.sync.init_onces.push(Default::default());
120119
assert_eq!(next_index, new_index);
121120
Ok(new_index)
122121
}
@@ -125,7 +124,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
125124
#[inline]
126125
fn init_once_status(&mut self, id: InitOnceId) -> InitOnceStatus {
127126
let this = self.eval_context_ref();
128-
this.machine.threads.sync.init_onces[id].status
127+
this.machine.sync.init_onces[id].status
129128
}
130129

131130
/// Put the thread into the queue waiting for the initialization.
@@ -137,7 +136,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
137136
callback: Box<dyn MachineCallback<'mir, 'tcx> + 'tcx>,
138137
) {
139138
let this = self.eval_context_mut();
140-
let init_once = &mut this.machine.threads.sync.init_onces[id];
139+
let init_once = &mut this.machine.sync.init_onces[id];
141140
assert_ne!(init_once.status, InitOnceStatus::Complete, "queueing on complete init once");
142141
init_once.waiters.push_back(InitOnceWaiter { thread, callback });
143142
this.block_thread(thread, BlockReason::InitOnce(id));
@@ -148,7 +147,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
148147
#[inline]
149148
fn init_once_begin(&mut self, id: InitOnceId) {
150149
let this = self.eval_context_mut();
151-
let init_once = &mut this.machine.threads.sync.init_onces[id];
150+
let init_once = &mut this.machine.sync.init_onces[id];
152151
assert_eq!(
153152
init_once.status,
154153
InitOnceStatus::Uninitialized,
@@ -160,9 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
160159
#[inline]
161160
fn init_once_complete(&mut self, id: InitOnceId) -> InterpResult<'tcx> {
162161
let this = self.eval_context_mut();
163-
let current_thread = this.get_active_thread();
164-
let current_span = this.machine.current_span();
165-
let init_once = &mut this.machine.threads.sync.init_onces[id];
162+
let init_once = &mut this.machine.sync.init_onces[id];
166163

167164
assert_eq!(
168165
init_once.status,
@@ -174,7 +171,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
174171

175172
// Each complete happens-before the end of the wait
176173
if let Some(data_race) = &this.machine.data_race {
177-
init_once.clock.clone_from(&data_race.release_clock(current_thread, current_span));
174+
init_once.clock.clone_from(&data_race.release_clock(&this.machine.threads));
178175
}
179176

180177
// Wake up everyone.
@@ -189,9 +186,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
189186
#[inline]
190187
fn init_once_fail(&mut self, id: InitOnceId) -> InterpResult<'tcx> {
191188
let this = self.eval_context_mut();
192-
let current_thread = this.get_active_thread();
193-
let current_span = this.machine.current_span();
194-
let init_once = &mut this.machine.threads.sync.init_onces[id];
189+
let init_once = &mut this.machine.sync.init_onces[id];
195190
assert_eq!(
196191
init_once.status,
197192
InitOnceStatus::Begun,
@@ -200,7 +195,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
200195

201196
// Each complete happens-before the end of the wait
202197
if let Some(data_race) = &this.machine.data_race {
203-
init_once.clock.clone_from(&data_race.release_clock(current_thread, current_span));
198+
init_once.clock.clone_from(&data_race.release_clock(&this.machine.threads));
204199
}
205200

206201
// Wake up one waiting thread, so they can go ahead and try to init this.

0 commit comments

Comments
 (0)