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

View File

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