use cas instead of load & store; modify "commit" to "put"

This commit is contained in:
mutouyun 2018-11-22 15:51:17 +08:00
parent 4e67eebb4f
commit aef0ed035d
2 changed files with 6 additions and 5 deletions

View File

@ -83,10 +83,11 @@ public:
void* acquire(void) { void* acquire(void) {
auto st = elem_start() + id(cr_.load(std::memory_order_relaxed)); auto st = elem_start() + id(cr_.load(std::memory_order_relaxed));
// check remain count of consumers
do { do {
// check remain count of consumers std::size_t expected = 0;
if (!st->head_.load(std::memory_order_acquire)) { if (st->head_.compare_exchange_weak(expected, conn_count(),
st->head_.store(conn_count()); std::memory_order_acquire, std::memory_order_relaxed)) {
break; break;
} }
} while(1); } while(1);
@ -105,7 +106,7 @@ public:
return (elem_start() + id(index))->data_; return (elem_start() + id(index))->data_;
} }
void commit(std::uint16_t index) { void put(std::uint16_t index) {
auto st = elem_start() + id(index); auto st = elem_start() + id(index);
st->head_.fetch_sub(1, std::memory_order_release); st->head_.fetch_sub(1, std::memory_order_release);
} }

View File

@ -53,7 +53,7 @@ void Unit::test_producer(void) {
do { do {
while (cur != cq__->cursor()) { while (cur != cq__->cursor()) {
int d = *static_cast<const int*>(cq__->get(cur)); int d = *static_cast<const int*>(cq__->get(cur));
cq__->commit(cur); cq__->put(cur);
if (d < 0) return; if (d < 0) return;
cur = cq__->next(cur); cur = cq__->next(cur);
list.push_back(d); list.push_back(d);