| | |
| | | #ifndef ROBUST_Q31RCWYU |
| | | #define ROBUST_Q31RCWYU |
| | | |
| | | #include "log.h" |
| | | #include <atomic> |
| | | #include <chrono> |
| | | #include <memory> |
| | |
| | | } else if (!is_alive(Locker(old))) { |
| | | r = static_cast<int>(MetaCas(old, Meta(1, this_locker()))) << 1; |
| | | if (r) { |
| | | printf("captured locker %ld -> %ld, locker = %d\n", int64_t(Locker(old)), int64_t(this_locker()), r); |
| | | LOG_DEBUG() << "captured locker " << int64_t(Locker(old)) << " -> " << int64_t(this_locker()) << ", locker = " << r; |
| | | } |
| | | } |
| | | return r; |
| | |
| | | size_type tail() const { return tail_.load(); } |
| | | bool like_empty() const { return head() == tail() && Empty(buf[head()]); } |
| | | bool like_full() const { return head() == tail() && !Empty(buf[head()]); } |
| | | bool push_back(const Data d, bool try_more = false) |
| | | bool push(const Data d, bool try_more = false) |
| | | { |
| | | bool r = false; |
| | | size_type i = 0; |
| | | do { |
| | | auto pos = tail(); |
| | | auto cur = buf[pos].load(); |
| | | r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d)); |
| | | tail_.compare_exchange_strong(pos, Next(pos)); |
| | | if (tail_.compare_exchange_strong(pos, Next(pos))) { |
| | | auto cur = buf[pos].load(); |
| | | r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d)); |
| | | } |
| | | } while (try_more && !r && ++i < capacity); |
| | | return r; |
| | | } |
| | | bool pop_front(Data &d, bool try_more = false) |
| | | bool pop(Data &d, bool try_more = false) |
| | | { |
| | | bool r = false; |
| | | Data cur; |
| | | size_type i = 0; |
| | | do { |
| | | auto pos = head(); |
| | | cur = buf[pos].load(); |
| | | r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0); |
| | | head_.compare_exchange_strong(pos, Next(pos)); |
| | | if (head_.compare_exchange_strong(pos, Next(pos))) { |
| | | cur = buf[pos].load(); |
| | | r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0); |
| | | } |
| | | } while (try_more && !r && ++i < capacity); |
| | | if (r) { d = Dec(cur); } |
| | | return r; |