Revert "Replace unnecessary use of atomic in "shared_lock""

This reverts commit b50e50b5fb.
This commit is contained in:
Stephen Seo 2023-07-22 17:28:13 +09:00
parent b50e50b5fb
commit ce092f81f9
2 changed files with 10 additions and 10 deletions

View file

@ -20,7 +20,7 @@ write(false)
UDPC::LockObj<false> UDPC::SharedSpinLock::spin_read_lock() { UDPC::LockObj<false> UDPC::SharedSpinLock::spin_read_lock() {
while (true) { while (true) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!write) { if (!write.load()) {
++read; ++read;
return LockObj<false>(selfWeakPtr, Badge{}); return LockObj<false>(selfWeakPtr, Badge{});
} }
@ -29,7 +29,7 @@ UDPC::LockObj<false> UDPC::SharedSpinLock::spin_read_lock() {
UDPC::LockObj<false> UDPC::SharedSpinLock::try_spin_read_lock() { UDPC::LockObj<false> UDPC::SharedSpinLock::try_spin_read_lock() {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!write) { if (!write.load()) {
++read; ++read;
return LockObj<false>(selfWeakPtr, Badge{}); return LockObj<false>(selfWeakPtr, Badge{});
} }
@ -39,7 +39,7 @@ UDPC::LockObj<false> UDPC::SharedSpinLock::try_spin_read_lock() {
void UDPC::SharedSpinLock::read_unlock(UDPC::Badge &&badge) { void UDPC::SharedSpinLock::read_unlock(UDPC::Badge &&badge) {
if (badge.isValid) { if (badge.isValid) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (read > 0) { if (read.load() > 0) {
--read; --read;
badge.isValid = false; badge.isValid = false;
} }
@ -49,8 +49,8 @@ void UDPC::SharedSpinLock::read_unlock(UDPC::Badge &&badge) {
UDPC::LockObj<true> UDPC::SharedSpinLock::spin_write_lock() { UDPC::LockObj<true> UDPC::SharedSpinLock::spin_write_lock() {
while (true) { while (true) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!write && read == 0) { if (!write.load() && read.load() == 0) {
write = true; write.store(true);
return LockObj<true>(selfWeakPtr, Badge{}); return LockObj<true>(selfWeakPtr, Badge{});
} }
} }
@ -58,8 +58,8 @@ UDPC::LockObj<true> UDPC::SharedSpinLock::spin_write_lock() {
UDPC::LockObj<true> UDPC::SharedSpinLock::try_spin_write_lock() { UDPC::LockObj<true> UDPC::SharedSpinLock::try_spin_write_lock() {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!write && read == 0) { if (!write.load() && read.load() == 0) {
write = true; write.store(true);
return LockObj<true>(selfWeakPtr, Badge{}); return LockObj<true>(selfWeakPtr, Badge{});
} }
return LockObj<true>(Badge{}); return LockObj<true>(Badge{});
@ -68,7 +68,7 @@ UDPC::LockObj<true> UDPC::SharedSpinLock::try_spin_write_lock() {
void UDPC::SharedSpinLock::write_unlock(UDPC::Badge &&badge) { void UDPC::SharedSpinLock::write_unlock(UDPC::Badge &&badge) {
if (badge.isValid) { if (badge.isValid) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
write = false; write.store(false);
badge.isValid = false; badge.isValid = false;
} }
} }

View file

@ -84,8 +84,8 @@ private:
Weak selfWeakPtr; Weak selfWeakPtr;
std::mutex mutex; std::mutex mutex;
unsigned int read; std::atomic_uint read;
bool write; std::atomic_bool write;
}; };