]> git.seodisparate.com - UDPConnection/commitdiff
Replace unnecessary use of atomic in "shared_lock"
authorStephen Seo <seo.disparate@gmail.com>
Sat, 22 Jul 2023 07:45:52 +0000 (16:45 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 22 Jul 2023 07:46:38 +0000 (16:46 +0900)
src/CXX11_shared_spin_lock.cpp
src/CXX11_shared_spin_lock.hpp

index 1c0ab84d9a3828246284d3971b62caa580619629..44acd0688635aa942860f4dc9d985ae8382ebf9f 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.load()) {
+        if (!write) {
             ++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.load()) {
+    if (!write) {
         ++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.load() > 0) {
+        if (read > 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.load() && read.load() == 0) {
-            write.store(true);
+        if (!write && read == 0) {
+            write = 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.load() && read.load() == 0) {
-        write.store(true);
+    if (!write && read == 0) {
+        write = 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.store(false);
+        write = false;
         badge.isValid = false;
     }
 }
index 8b524ed30642bb1bd7427fc3b6624651ead778db..4fab36a3b38ce7ae1faa748a2ec371ea3b75fad5 100644 (file)
@@ -84,8 +84,8 @@ private:
 
     Weak selfWeakPtr;
     std::mutex mutex;
-    std::atomic_uint read;
-    std::atomic_bool write;
+    unsigned int read;
+    bool write;
 
 };