Skip to content

Commit 61cff5b

Browse files
authored
Rollup merge of rust-lang#141690 - Patrick-6:intercept-mutex, r=m-ou-se
Add `rustc_diagnostic_item` to `sys::Mutex` methods For an ongoing project for adding a concurrency model checker to Miri we need to be able to intercept locking/unlocking operations on standard library mutexes. This PR adds diagnostic items to the relevant calls `lock`, `try_lock` and `unlock` for the `sys::Mutex` implementation on the targets we care about. This PR also makes the internals of `pthread::Mutex` less public, to reduce the chance of anyone locking/unlocking a mutex without going through the intercepted methods. r? `@RalfJung`
2 parents 7726b94 + 8237107 commit 61cff5b

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,9 @@ symbols! {
20742074
sym,
20752075
sync,
20762076
synthetic,
2077+
sys_mutex_lock,
2078+
sys_mutex_try_lock,
2079+
sys_mutex_unlock,
20772080
t32,
20782081
target,
20792082
target_abi,

library/std/src/sys/sync/mutex/futex.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ impl Mutex {
1919
}
2020

2121
#[inline]
22+
// Make this a diagnostic item for Miri's concurrency model checker.
23+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_try_lock")]
2224
pub fn try_lock(&self) -> bool {
2325
self.futex.compare_exchange(UNLOCKED, LOCKED, Acquire, Relaxed).is_ok()
2426
}
2527

2628
#[inline]
29+
// Make this a diagnostic item for Miri's concurrency model checker.
30+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_lock")]
2731
pub fn lock(&self) {
2832
if self.futex.compare_exchange(UNLOCKED, LOCKED, Acquire, Relaxed).is_err() {
2933
self.lock_contended();
@@ -80,6 +84,8 @@ impl Mutex {
8084
}
8185

8286
#[inline]
87+
// Make this a diagnostic item for Miri's concurrency model checker.
88+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_unlock")]
8389
pub unsafe fn unlock(&self) {
8490
if self.futex.swap(UNLOCKED, Release) == CONTENDED {
8591
// We only wake up one thread. When that thread locks the mutex, it

library/std/src/sys/sync/mutex/pthread.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::sys::pal::sync as pal;
66
use crate::sys::sync::OnceBox;
77

88
pub struct Mutex {
9-
pub pal: OnceBox<pal::Mutex>,
9+
pub(in crate::sys::sync) pal: OnceBox<pal::Mutex>,
1010
}
1111

1212
impl Mutex {
@@ -28,20 +28,26 @@ impl Mutex {
2828
}
2929

3030
#[inline]
31+
// Make this a diagnostic item for Miri's concurrency model checker.
32+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_lock")]
3133
pub fn lock(&self) {
3234
// SAFETY: we call `init` above, therefore reentrant locking is safe.
3335
// In `drop` we ensure that the mutex is not destroyed while locked.
3436
unsafe { self.get().lock() }
3537
}
3638

3739
#[inline]
40+
// Make this a diagnostic item for Miri's concurrency model checker.
41+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_unlock")]
3842
pub unsafe fn unlock(&self) {
3943
// SAFETY: the mutex can only be locked if it is already initialized
4044
// and we observed this initialization since we observed the locking.
4145
unsafe { self.pal.get_unchecked().unlock() }
4246
}
4347

4448
#[inline]
49+
// Make this a diagnostic item for Miri's concurrency model checker.
50+
#[cfg_attr(not(test), rustc_diagnostic_item = "sys_mutex_try_lock")]
4551
pub fn try_lock(&self) -> bool {
4652
// SAFETY: we call `init` above, therefore reentrant locking is safe.
4753
// In `drop` we ensure that the mutex is not destroyed while locked.

0 commit comments

Comments
 (0)