Replace unnecessary use of atomic in "shared_lock"

This commit is contained in:
Stephen Seo 2023-07-22 16:45:52 +09:00
parent cf27a6bb76
commit b50e50b5fb
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.load()) { if (!write) {
++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.load()) { if (!write) {
++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.load() > 0) { if (read > 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.load() && read.load() == 0) { if (!write && read == 0) {
write.store(true); write = 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.load() && read.load() == 0) { if (!write && read == 0) {
write.store(true); write = 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.store(false); write = false;
badge.isValid = false; badge.isValid = false;
} }
} }

View file

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