Skip to content

Commit f449797

Browse files
committed
Clean up mutex code.
1 parent ccd4081 commit f449797

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

genmc-sys/cpp/src/MiriInterface/Mutex.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// CXX.rs generated headers:
66
#include "genmc-sys/src/lib.rs.h"
77

8+
#define MUTEX_UNLOCKED SVal(0)
9+
#define MUTEX_LOCKED SVal(1)
10+
811
auto MiriGenmcShim::handle_mutex_lock(ThreadId thread_id, uint64_t address, uint64_t size)
912
-> MutexLockResult {
1013
// FIXME(genmc,multithreading): ensure this annotation id is unique even if Miri runs GenMC mode
@@ -17,17 +20,18 @@ auto MiriGenmcShim::handle_mutex_lock(ThreadId thread_id, uint64_t address, uint
1720
} else {
1821
annot_id = it->second;
1922
}
23+
const auto size_bits = size * 8;
2024
const auto annot = std::move(Annotation(
2125
AssumeType::Spinloop,
2226
Annotation::ExprVP(NeExpr<AnnotID>::create(
23-
RegisterExpr<AnnotID>::create(ASize(size).getBits(), annot_id),
24-
ConcreteExpr<AnnotID>::create(ASize(size).getBits(), SVal(1))
27+
RegisterExpr<AnnotID>::create(size_bits, annot_id),
28+
ConcreteExpr<AnnotID>::create(size_bits, MUTEX_LOCKED)
2529
)
2630
.release())
2731
));
2832

2933
// Mutex starts out unlocked, so we always say the previous value is "unlocked".
30-
const auto old_val = SVal(0);
34+
const auto old_val = MUTEX_UNLOCKED;
3135
const auto load_ret = handle_load_reset_if_none<EventLabel::EventLabelKind::LockCasRead>(
3236
thread_id,
3337
old_val,
@@ -47,8 +51,11 @@ auto MiriGenmcShim::handle_mutex_lock(ThreadId thread_id, uint64_t address, uint
4751

4852
const auto* ret_val = std::get_if<SVal>(&load_ret);
4953
ERROR_ON(!ret_val, "Unimplemented: mutex lock returned unexpected result.");
50-
ERROR_ON(*ret_val != SVal(0) && *ret_val != SVal(1), "Mutex read value was neither 0 nor 1");
51-
const bool is_lock_acquired = *ret_val == SVal(0);
54+
ERROR_ON(
55+
*ret_val != MUTEX_UNLOCKED && *ret_val != MUTEX_LOCKED,
56+
"Mutex read value was neither 0 nor 1"
57+
);
58+
const bool is_lock_acquired = *ret_val == MUTEX_UNLOCKED;
5259
if (is_lock_acquired) {
5360
const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::LockCasWrite>(
5461
inc_pos(thread_id),
@@ -79,7 +86,7 @@ auto MiriGenmcShim::handle_mutex_try_lock(ThreadId thread_id, uint64_t address,
7986
-> MutexLockResult {
8087
auto& currPos = threads_action_[thread_id].event;
8188
// Mutex starts out unlocked, so we always say the previous value is "unlocked".
82-
const auto old_val = SVal(0);
89+
const auto old_val = MUTEX_UNLOCKED;
8390
const auto load_ret = GenMCDriver::handleLoad<EventLabel::EventLabelKind::TrylockCasRead>(
8491
++currPos,
8592
old_val,
@@ -93,8 +100,11 @@ auto MiriGenmcShim::handle_mutex_try_lock(ThreadId thread_id, uint64_t address,
93100
ERROR("Unimplemented: mutex trylock load returned unexpected result.");
94101
}
95102

96-
ERROR_ON(*ret_val != SVal(0) && *ret_val != SVal(1), "Mutex read value was neither 0 nor 1");
97-
const bool is_lock_acquired = *ret_val == SVal(0);
103+
ERROR_ON(
104+
*ret_val != MUTEX_UNLOCKED && *ret_val != MUTEX_LOCKED,
105+
"Mutex read value was neither 0 nor 1"
106+
);
107+
const bool is_lock_acquired = *ret_val == MUTEX_UNLOCKED;
98108
if (!is_lock_acquired) {
99109
return MutexLockResultExt::ok(false); /* Lock already held. */
100110
}
@@ -123,12 +133,12 @@ auto MiriGenmcShim::handle_mutex_unlock(ThreadId thread_id, uint64_t address, ui
123133
const auto ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::UnlockWrite>(
124134
pos,
125135
// Mutex starts out unlocked, so we always say the previous value is "unlocked".
126-
/* old_val */ SVal(0),
136+
/* old_val */ MUTEX_UNLOCKED,
127137
MemOrdering::Release,
128138
SAddr(address),
129139
ASize(size),
130140
AType::Signed,
131-
SVal(0),
141+
/* store_value */ MUTEX_UNLOCKED,
132142
EventDeps()
133143
);
134144
if (const auto* err = std::get_if<VerificationError>(&ret))

0 commit comments

Comments
 (0)