]> git.seodisparate.com - UDPConnection/commitdiff
Revert "Replace unnecessary use of atomic in "shared_lock""
authorStephen Seo <seo.disparate@gmail.com>
Sat, 22 Jul 2023 08:28:13 +0000 (17:28 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 22 Jul 2023 08:28:13 +0000 (17:28 +0900)
This reverts commit b50e50b5fbdeaa3990984a6bda052bbb92c234a9.

src/CXX11_shared_spin_lock.cpp
src/CXX11_shared_spin_lock.hpp

index 44acd0688635aa942860f4dc9d985ae8382ebf9f..1c0ab84d9a3828246284d3971b62caa580619629 100644 (file)
@@ -20,7 +20,7 @@ write(false)
 UDPC::LockObj<false> UDPC::SharedSpinLock::spin_read_lock() {
     while (true) {
         std::lock_guard<std::mutex> lock(mutex);
-        if (!write) {
+        if (!write.load()) {
             ++read;
             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() {
     std::lock_guard<std::mutex> lock(mutex);
-    if (!write) {
+    if (!write.load()) {
         ++read;
         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) {
     if (badge.isValid) {
         std::lock_guard<std::mutex> lock(mutex);
-        if (read > 0) {
+        if (read.load() > 0) {
             --read;
             badge.isValid = false;
         }
@@ -49,8 +49,8 @@ void UDPC::SharedSpinLock::read_unlock(UDPC::Badge &&badge) {
 UDPC::LockObj<true> UDPC::SharedSpinLock::spin_write_lock() {
     while (true) {
         std::lock_guard<std::mutex> lock(mutex);
-        if (!write && read == 0) {
-            write = true;
+        if (!write.load() && read.load() == 0) {
+            write.store(true);
             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() {
     std::lock_guard<std::mutex> lock(mutex);
-    if (!write && read == 0) {
-        write = true;
+    if (!write.load() && read.load() == 0) {
+        write.store(true);
         return LockObj<true>(selfWeakPtr, 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) {
     if (badge.isValid) {
         std::lock_guard<std::mutex> lock(mutex);
-        write = false;
+        write.store(false);
         badge.isValid = false;
     }
 }
index 4fab36a3b38ce7ae1faa748a2ec371ea3b75fad5..8b524ed30642bb1bd7427fc3b6624651ead778db 100644 (file)
@@ -84,8 +84,8 @@ private:
 
     Weak selfWeakPtr;
     std::mutex mutex;
-    unsigned int read;
-    bool write;
+    std::atomic_uint read;
+    std::atomic_bool write;
 
 };