6 #include <cds/algo/backoff_strategy.h>
11 #define RW_LOCK_BIAS 0x00100000
12 #define WRITE_LOCK_CMP RW_LOCK_BIAS
14 using std::memory_order_acquire;
15 using std::memory_order_release;
16 using std::memory_order_relaxed;
17 using std::atomic_int;
22 lock.store(RW_LOCK_BIAS);
26 return atomic_load_explicit(&lock, memory_order_relaxed) > 0;
29 int write_can_lock() {
30 return atomic_load_explicit(&lock, memory_order_relaxed) == RW_LOCK_BIAS;
35 int priorvalue = atomic_fetch_sub_explicit(&lock, 1, memory_order_acquire);
36 while (priorvalue <= 0) {
37 atomic_fetch_add_explicit(&lock, 1, memory_order_relaxed);
38 while (atomic_load_explicit(&lock, memory_order_relaxed) <= 0) {
41 priorvalue = atomic_fetch_sub_explicit(&lock, 1, memory_order_acquire);
47 atomic_fetch_sub_explicit(&lock, RW_LOCK_BIAS, memory_order_acquire);
49 while (priorvalue != RW_LOCK_BIAS) {
50 atomic_fetch_add_explicit(&lock, RW_LOCK_BIAS, memory_order_relaxed);
51 while (atomic_load_explicit(&lock, memory_order_relaxed) !=
56 atomic_fetch_sub_explicit(&lock, RW_LOCK_BIAS, memory_order_acquire);
61 int priorvalue = atomic_fetch_sub_explicit(&lock, 1, memory_order_acquire);
65 atomic_fetch_add_explicit(&lock, 1, memory_order_relaxed);
71 atomic_fetch_sub_explicit(&lock, RW_LOCK_BIAS, memory_order_acquire);
72 if (priorvalue == RW_LOCK_BIAS)
75 atomic_fetch_add_explicit(&lock, RW_LOCK_BIAS, memory_order_relaxed);
80 atomic_fetch_add_explicit(&lock, 1, memory_order_release);
84 atomic_fetch_add_explicit(&lock, RW_LOCK_BIAS, memory_order_release);
91 } // namespace cds_others