Don't fail on "try" fns if failed to get spinLock
In SharedSpinLock: Only fail on "try" fns after spinLock was acquired and condition is not met.
This commit is contained in:
parent
a0f8bf0b41
commit
4917fc47f6
1 changed files with 54 additions and 38 deletions
|
@ -40,7 +40,9 @@ 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() {
|
||||||
bool expected = false;
|
bool expected;
|
||||||
|
while (true) {
|
||||||
|
expected = false;
|
||||||
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||||
if (!write) {
|
if (!write) {
|
||||||
++read;
|
++read;
|
||||||
|
@ -48,6 +50,8 @@ UDPC::LockObj<false> UDPC::SharedSpinLock::try_spin_read_lock() {
|
||||||
return LockObj<false>(selfWeakPtr, Badge{});
|
return LockObj<false>(selfWeakPtr, Badge{});
|
||||||
} else {
|
} else {
|
||||||
spinLock.store(false, std::memory_order_release);
|
spinLock.store(false, std::memory_order_release);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return LockObj<false>{};
|
return LockObj<false>{};
|
||||||
|
@ -87,7 +91,9 @@ 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() {
|
||||||
bool expected = false;
|
bool expected;
|
||||||
|
while (true) {
|
||||||
|
expected = false;
|
||||||
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||||
if (!write && read == 0) {
|
if (!write && read == 0) {
|
||||||
write = true;
|
write = true;
|
||||||
|
@ -95,6 +101,8 @@ UDPC::LockObj<true> UDPC::SharedSpinLock::try_spin_write_lock() {
|
||||||
return LockObj<true>(selfWeakPtr, Badge{});
|
return LockObj<true>(selfWeakPtr, Badge{});
|
||||||
} else {
|
} else {
|
||||||
spinLock.store(false, std::memory_order_release);
|
spinLock.store(false, std::memory_order_release);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return LockObj<true>{};
|
return LockObj<true>{};
|
||||||
|
@ -142,7 +150,9 @@ UDPC::LockObj<false> UDPC::SharedSpinLock::trade_write_for_read_lock(UDPC::LockO
|
||||||
|
|
||||||
UDPC::LockObj<false> UDPC::SharedSpinLock::try_trade_write_for_read_lock(UDPC::LockObj<true> &lockObj) {
|
UDPC::LockObj<false> UDPC::SharedSpinLock::try_trade_write_for_read_lock(UDPC::LockObj<true> &lockObj) {
|
||||||
if (lockObj.isValid() && lockObj.badge.isValid) {
|
if (lockObj.isValid() && lockObj.badge.isValid) {
|
||||||
bool expected = false;
|
bool expected;
|
||||||
|
while (true) {
|
||||||
|
expected = false;
|
||||||
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||||
if (write && read == 0) {
|
if (write && read == 0) {
|
||||||
read = 1;
|
read = 1;
|
||||||
|
@ -153,6 +163,8 @@ UDPC::LockObj<false> UDPC::SharedSpinLock::try_trade_write_for_read_lock(UDPC::L
|
||||||
return LockObj<false>(selfWeakPtr, Badge{});
|
return LockObj<false>(selfWeakPtr, Badge{});
|
||||||
} else {
|
} else {
|
||||||
spinLock.store(false, std::memory_order_release);
|
spinLock.store(false, std::memory_order_release);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,7 +196,9 @@ UDPC::LockObj<true> UDPC::SharedSpinLock::trade_read_for_write_lock(UDPC::LockOb
|
||||||
|
|
||||||
UDPC::LockObj<true> UDPC::SharedSpinLock::try_trade_read_for_write_lock(UDPC::LockObj<false> &lockObj) {
|
UDPC::LockObj<true> UDPC::SharedSpinLock::try_trade_read_for_write_lock(UDPC::LockObj<false> &lockObj) {
|
||||||
if (lockObj.isValid() && lockObj.badge.isValid) {
|
if (lockObj.isValid() && lockObj.badge.isValid) {
|
||||||
bool expected = false;
|
bool expected;
|
||||||
|
while (true) {
|
||||||
|
expected = false;
|
||||||
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
if (spinLock.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||||
if (!write && read == 1) {
|
if (!write && read == 1) {
|
||||||
read = 0;
|
read = 0;
|
||||||
|
@ -195,6 +209,8 @@ UDPC::LockObj<true> UDPC::SharedSpinLock::try_trade_read_for_write_lock(UDPC::Lo
|
||||||
return LockObj<true>(selfWeakPtr, Badge{});
|
return LockObj<true>(selfWeakPtr, Badge{});
|
||||||
} else {
|
} else {
|
||||||
spinLock.store(false, std::memory_order_release);
|
spinLock.store(false, std::memory_order_release);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue