mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 08:46:45 +08:00
modify get to take; fix bug
This commit is contained in:
parent
1f50cd6ac3
commit
2f206d677e
@ -49,11 +49,15 @@ private:
|
||||
return i & 0x00ff;
|
||||
}
|
||||
|
||||
static elem_t* elem(void* ptr) {
|
||||
return reinterpret_cast<elem_t*>(static_cast<std::uint8_t*>(ptr) - sizeof(el_t));
|
||||
}
|
||||
|
||||
std::uint8_t block_[block_size];
|
||||
|
||||
public:
|
||||
static std::uint16_t next(std::uint16_t i) {
|
||||
return (id(++i) == elem_max) ? 0 : i;
|
||||
return (id(++i) == elem_max) ? ++i : i;
|
||||
}
|
||||
|
||||
circ_queue(void) {
|
||||
@ -99,12 +103,12 @@ public:
|
||||
return cr_.load(std::memory_order_consume);
|
||||
}
|
||||
|
||||
void* get(std::uint16_t index) {
|
||||
void* take(std::uint16_t index) {
|
||||
return (elem_start() + id(index))->data_;
|
||||
}
|
||||
|
||||
void put(std::uint16_t index) {
|
||||
auto st = elem_start() + id(index);
|
||||
void put(void* ptr) {
|
||||
auto st = elem(ptr);
|
||||
st->head_.fetch_sub(1, std::memory_order_release);
|
||||
}
|
||||
};
|
||||
|
||||
@ -28,8 +28,8 @@ cq_t* cq__;
|
||||
void Unit::test_inst(void) {
|
||||
cq__ = new cq_t;
|
||||
QCOMPARE(sizeof(*cq__), static_cast<std::size_t>(cq_t::total_size));
|
||||
auto a = cq__->get(1);
|
||||
auto b = cq__->get(2);
|
||||
auto a = cq__->take(1);
|
||||
auto b = cq__->take(2);
|
||||
QCOMPARE(static_cast<std::size_t>(static_cast<std::uint8_t const *>(b) -
|
||||
static_cast<std::uint8_t const *>(a)),
|
||||
static_cast<std::size_t>(cq_t::elem_size));
|
||||
@ -52,8 +52,9 @@ void Unit::test_producer(void) {
|
||||
int i = 0;
|
||||
do {
|
||||
while (cur != cq__->cursor()) {
|
||||
int d = *static_cast<const int*>(cq__->get(cur));
|
||||
cq__->put(cur);
|
||||
auto p = static_cast<int*>(cq__->take(cur));
|
||||
int d = *p;
|
||||
cq__->put(p);
|
||||
if (d < 0) return;
|
||||
cur = cq__->next(cur);
|
||||
list.push_back(d);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user