mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add comments
This commit is contained in:
parent
3e7c97d9b6
commit
ff0dbc8e2e
@ -103,7 +103,7 @@ public:
|
|||||||
|
|
||||||
void* acquire(void) {
|
void* acquire(void) {
|
||||||
auto el = elem(wt_.fetch_add(1, std::memory_order_consume));
|
auto el = elem(wt_.fetch_add(1, std::memory_order_consume));
|
||||||
// check read flag
|
// check read finished by all consumers
|
||||||
do {
|
do {
|
||||||
uc_t expected = 0;
|
uc_t expected = 0;
|
||||||
if (el->head_.rf_.compare_exchange_weak(
|
if (el->head_.rf_.compare_exchange_weak(
|
||||||
@ -116,26 +116,39 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void commit(void* ptr) {
|
void commit(void* ptr) {
|
||||||
auto el = elem(ptr);
|
auto el = elem(ptr); // get the commit element
|
||||||
ui_t wt = index_of(el);
|
ui_t cm = index_of(el); // get the index of this element
|
||||||
do {
|
do {
|
||||||
bool no_next;
|
bool no_next_check;
|
||||||
uc_t curr;
|
uc_t curr;
|
||||||
do {
|
do {
|
||||||
curr = cr_.load(std::memory_order_relaxed);
|
curr = cr_.load(std::memory_order_relaxed);
|
||||||
no_next = (index_of(curr) != wt);
|
no_next_check = (index_of(curr) != cm);
|
||||||
if (no_next) {
|
if (no_next_check) {
|
||||||
|
/*
|
||||||
|
* set wf_ for the other producer thread which is commiting
|
||||||
|
* the element matches cr_ could see it has commited
|
||||||
|
*/
|
||||||
el->head_.wf_.store(1, std::memory_order_relaxed);
|
el->head_.wf_.store(1, std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
/*
|
||||||
|
* no thread changes the cr_ except current thread at here
|
||||||
|
* so we could just fetch_add & break, no need to check cr_ again
|
||||||
|
*/
|
||||||
cr_.fetch_add(1, std::memory_order_relaxed);
|
cr_.fetch_add(1, std::memory_order_relaxed);
|
||||||
el->head_.wf_.store(0, std::memory_order_release);
|
el->head_.wf_.store(0, std::memory_order_release);
|
||||||
no_next = false;
|
no_next_check = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* it needs to go back and judge again
|
||||||
|
* when cr_ has been changed by the other producer thread
|
||||||
|
*/
|
||||||
} while(curr != cr_.load(std::memory_order_acq_rel));
|
} while(curr != cr_.load(std::memory_order_acq_rel));
|
||||||
if (no_next) return;
|
// check next element has commited or not
|
||||||
} while(el = elem(++wt), el->head_.wf_.load(std::memory_order_consume));
|
if (no_next_check) return;
|
||||||
|
} while(el = elem(++cm), el->head_.wf_.load(std::memory_order_consume));
|
||||||
}
|
}
|
||||||
|
|
||||||
uc_t cursor(void) const {
|
uc_t cursor(void) const {
|
||||||
|
|||||||
@ -31,9 +31,9 @@ void Unit::test_inst(void) {
|
|||||||
std::cout << "cq_t::elem_size = " << cq_t::elem_size << std::endl;
|
std::cout << "cq_t::elem_size = " << cq_t::elem_size << std::endl;
|
||||||
std::cout << "cq_t::block_size = " << cq_t::block_size << std::endl;
|
std::cout << "cq_t::block_size = " << cq_t::block_size << std::endl;
|
||||||
|
|
||||||
QCOMPARE(cq_t::data_size , 12);
|
QCOMPARE(static_cast<std::size_t>(cq_t::data_size) , static_cast<std::size_t>(12));
|
||||||
QCOMPARE(cq_t::block_size, 4096);
|
QCOMPARE(static_cast<std::size_t>(cq_t::block_size), static_cast<std::size_t>(4096));
|
||||||
QCOMPARE(sizeof(cq_t), cq_t::block_size + cq_t::head_size);
|
QCOMPARE(sizeof(cq_t), static_cast<std::size_t>(cq_t::block_size + cq_t::head_size));
|
||||||
|
|
||||||
cq__ = new cq_t;
|
cq__ = new cq_t;
|
||||||
std::cout << "sizeof(ipc::circ::elem_array<4096>) = " << sizeof(*cq__) << std::endl;
|
std::cout << "sizeof(ipc::circ::elem_array<4096>) = " << sizeof(*cq__) << std::endl;
|
||||||
@ -50,7 +50,7 @@ void Unit::test_prod_cons_1vN(void) {
|
|||||||
std::thread consumers[1];
|
std::thread consumers[1];
|
||||||
std::atomic_int fini { 0 };
|
std::atomic_int fini { 0 };
|
||||||
capo::stopwatch<> sw;
|
capo::stopwatch<> sw;
|
||||||
constexpr static int loops = 1000000;
|
constexpr static int loops = 10000000;
|
||||||
|
|
||||||
for (auto& c : consumers) {
|
for (auto& c : consumers) {
|
||||||
c = std::thread{[&] {
|
c = std::thread{[&] {
|
||||||
@ -78,6 +78,7 @@ void Unit::test_prod_cons_1vN(void) {
|
|||||||
auto ts = sw.elapsed<std::chrono::microseconds>();
|
auto ts = sw.elapsed<std::chrono::microseconds>();
|
||||||
std::cout << "performance: " << (double(ts) / double(loops)) << " us/d" << std::endl;
|
std::cout << "performance: " << (double(ts) / double(loops)) << " us/d" << std::endl;
|
||||||
}
|
}
|
||||||
|
std::cout << "confirming..." << std::endl;
|
||||||
for (int d : list) {
|
for (int d : list) {
|
||||||
QCOMPARE(i, d);
|
QCOMPARE(i, d);
|
||||||
++i;
|
++i;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user