Skip to content

Commit 109e270

Browse files
committed
Clean up mutex_simple test
1 parent e00e070 commit 109e270

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

tests/genmc/pass/shims/mutex_simple.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
// Miri running with GenMC intercepts the Mutex functions `lock`, `try_lock` and `unlock`, instead of running their actual implementation.
66
// This interception should not break any functionality.
77
//
8+
// FIXME(genmc): Once GenMC supports mixed size accesses, add stack/heap allocated Mutexes to the test.
89
// FIXME(genmc): Once the actual implementation of mutexes can be used in GenMC mode and there is a setting to disable Mutex interception: Add test revision without interception.
910
//
1011
// Miri provides annotations to GenMC for the condition required to unblock a thread blocked on a Mutex lock call.
1112
// This massively reduces the number of blocked executions we need to explore (in this test we require zero blocked execution).
12-
// We use verbose output to test that there are no blocked executions introduces by future changes, only completed executions.
13+
// We use verbose output to check that this test always explores zero blocked executions.
1314

1415
#![no_main]
1516
#![feature(abort_unwind)]
@@ -23,7 +24,8 @@ use crate::genmc::*;
2324

2425
const REPS: u64 = 3;
2526

26-
static LOCK: Mutex<[u64; 32]> = Mutex::new([1234; 32]);
27+
static LOCK: Mutex<u64> = Mutex::new(0);
28+
static OTHER_LOCK: Mutex<u64> = Mutex::new(1234);
2729

2830
#[unsafe(no_mangle)]
2931
fn miri_start(_argc: isize, _argv: *const *const u8) -> isize {
@@ -32,44 +34,35 @@ fn miri_start(_argc: isize, _argv: *const *const u8) -> isize {
3234
}
3335

3436
fn main_() {
35-
// FIXME(genmc): Try using stack/heap allocated Mutexes once GenMC has mixed-size access support.
37+
// Two mutexes should not interfere, holding this guard does not affect the other mutex.
38+
let other_guard = OTHER_LOCK.lock().unwrap();
3639

37-
let mut guard = LOCK.lock().unwrap();
38-
for &v in guard.iter() {
39-
assert!(v == 1234); // Check that mutex values are initialized correctly
40-
}
41-
guard[0] = 0;
42-
guard[1] = 10;
43-
assert!(guard[0] == 0 && guard[1] == 10); // Check if changes are accepted
44-
45-
// Trying to lock should fail if the lock is already held
40+
let guard = LOCK.lock().unwrap();
41+
// Trying to lock should fail if the mutex is already held.
4642
assert!(LOCK.try_lock().is_err());
47-
4843
// Dropping the guard should unlock the mutex correctly.
4944
drop(guard);
50-
{
51-
// Trying to lock now should succeed since the lock is unlocked.
52-
assert!(LOCK.try_lock().is_ok());
53-
}
45+
// Trying to lock now should succeed.
46+
assert!(LOCK.try_lock().is_ok());
5447

5548
// Spawn multiple threads interacting with the same mutex.
5649
unsafe {
5750
let ids = [
5851
spawn_pthread_closure(|| {
5952
for _ in 0..REPS {
60-
let mut guard = LOCK.lock().unwrap();
61-
guard[0] += 2;
53+
*LOCK.lock().unwrap() += 2;
6254
}
6355
}),
6456
spawn_pthread_closure(|| {
6557
for _ in 0..REPS {
66-
let mut guard = LOCK.lock().unwrap();
67-
guard[0] += 4;
58+
*LOCK.lock().unwrap() += 4;
6859
}
6960
}),
7061
];
7162
join_pthreads(ids);
7263
}
73-
// Due to locking, all increments should be visible in every execution GenMC explores.
74-
assert!(LOCK.lock().unwrap()[0] == REPS * 6);
64+
// Due to the Mutex, all increments should be visible in every explored execution.
65+
assert!(*LOCK.lock().unwrap() == REPS * 6);
66+
67+
drop(other_guard);
7568
}

0 commit comments

Comments
 (0)