Skip to content

Commit bc5eee2

Browse files
committed
fix: resolved an issue that could cause a deadlock in certain cases
1 parent c0973ab commit bc5eee2

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

argon/vm/sync/rsm.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ using namespace argon::vm::sync;
4848

4949
// PRIVATE
5050
void RecursiveSharedMutex::lock_shared_slow(std::thread::id id) {
51-
auto current = this->_lock.load(std::memory_order_relaxed);
51+
auto current = this->_lock.load(std::memory_order_acquire);
5252
MutexBits desired{};
5353

5454
do {
5555
while (current.is_ulocked() && this->_id != id) {
5656
this->_pending++;
5757
OS_WAIT(&this->_lock, current.value());
58+
this->_pending--;
5859

59-
current = this->_lock.load(std::memory_order_relaxed);
60+
current = this->_lock.load(std::memory_order_acquire);
6061
}
6162

6263
desired = current;
@@ -74,6 +75,7 @@ void RecursiveSharedMutex::lock_slow() {
7475
while (!this->_lock.compare_exchange_strong(current, desired)) {
7576
this->_pending++;
7677
OS_WAIT(&this->_lock, current.value());
78+
this->_pending--;
7779

7880
current = MutexBits{};
7981
}
@@ -141,12 +143,10 @@ void RecursiveSharedMutex::unlock() {
141143
do {
142144
desired = current;
143145
desired.release_unique();
144-
} while (!this->_lock.compare_exchange_strong(current, desired));
146+
} while (!this->_lock.compare_exchange_strong(current, desired, std::memory_order_acq_rel));
145147

146-
if (this->_pending > 0) {
147-
this->_pending--;
148+
if (this->_pending > 0)
148149
OS_WAKE(&this->_lock);
149-
}
150150
}
151151

152152
void RecursiveSharedMutex::unlock_shared() {
@@ -157,10 +157,8 @@ void RecursiveSharedMutex::unlock_shared() {
157157
desired = current;
158158

159159
desired.dec_shared();
160-
} while (!this->_lock.compare_exchange_strong(current, desired));
160+
} while (!this->_lock.compare_exchange_strong(current, desired, std::memory_order_acq_rel));
161161

162-
if (this->_pending > 0) {
163-
this->_pending--;
162+
if (this->_pending > 0)
164163
OS_WAKE(&this->_lock);
165-
}
166164
}

0 commit comments

Comments
 (0)