mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
optimize lock_shared
This commit is contained in:
parent
238f769f7a
commit
7b812c3c6e
@ -36,15 +36,18 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lock_shared() {
|
void lock_shared() {
|
||||||
|
auto old = lc_.load(std::memory_order_relaxed);
|
||||||
for (unsigned k = 0;; ++k) {
|
for (unsigned k = 0;; ++k) {
|
||||||
auto old = lc_.load(std::memory_order_relaxed);
|
// if w_flag set, just continue
|
||||||
// if w_flag set, just continue; otherwise cas lc + 1 (set r-lock)
|
if (old & w_flag) {
|
||||||
if (!(old & w_flag) &&
|
yield(k);
|
||||||
lc_.compare_exchange_weak(old, old + 1, std::memory_order_acquire)) {
|
old = lc_.load(std::memory_order_acquire);
|
||||||
|
}
|
||||||
|
// otherwise try cas lc + 1 (set r-lock)
|
||||||
|
else if (lc_.compare_exchange_weak(old, old + 1, std::memory_order_acquire)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
yield(k);
|
// set r-lock failed, old has been updated
|
||||||
std::atomic_thread_fence(std::memory_order_acquire);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user