mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
use elem_array<ipc::circ::prod_cons>
This commit is contained in:
parent
8e104ded0f
commit
224ea72547
@ -1,27 +1,216 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <utility>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
|
|
||||||
namespace ipc {
|
namespace ipc {
|
||||||
namespace circ {
|
namespace circ {
|
||||||
|
|
||||||
template <std::size_t N>
|
namespace detail {
|
||||||
struct alignas(std::max_align_t) elem_array_head {
|
|
||||||
using u1_t = uint_t<N>;
|
|
||||||
using u2_t = uint_t<N * 2>;
|
|
||||||
|
|
||||||
std::atomic<u2_t> cc_ { 0 }; // connection counter, using for broadcast
|
using u1_t = uint_t<8>;
|
||||||
std::atomic<u2_t> wt_ { 0 }; // write index
|
using u2_t = uint_t<16>;
|
||||||
|
|
||||||
constexpr static u1_t index_of(u2_t c) noexcept {
|
constexpr u1_t index_of(u2_t c) noexcept {
|
||||||
return static_cast<u1_t>(c);
|
return static_cast<u1_t>(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct elem_head {
|
||||||
|
std::atomic<std::size_t> rc_ { 0 }; // read counter
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t DataSize>
|
||||||
|
struct elem_t {
|
||||||
|
elem_head head_;
|
||||||
|
byte_t data_[DataSize] {};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct elem_t<0> {
|
||||||
|
elem_head head_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t S>
|
||||||
|
elem_t<S>* elem_of(void* ptr) noexcept {
|
||||||
|
return reinterpret_cast<elem_t<S>*>(static_cast<byte_t*>(ptr) - sizeof(elem_head));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
enum class relat { // multiplicity of the relationship
|
||||||
|
single,
|
||||||
|
multi
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class trans { // transmission
|
||||||
|
unicast,
|
||||||
|
broadcast
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
/// producer-consumer policies
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <relat Rp, relat Rc, trans Ts>
|
||||||
|
struct prod_cons;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct prod_cons<relat::single, relat::single, trans::unicast> {
|
||||||
|
std::atomic<detail::u2_t> rd_ { 0 }; // read index
|
||||||
|
std::atomic<detail::u2_t> wt_ { 0 }; // write index
|
||||||
|
|
||||||
|
template <std::size_t DataSize>
|
||||||
|
constexpr static std::size_t elem_param = DataSize - sizeof(detail::elem_head);
|
||||||
|
|
||||||
|
constexpr detail::u2_t cursor() const noexcept {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool push(E* /*elems*/, F&& f, detail::elem_t<S>* elem_start) {
|
||||||
|
auto cur_wt = detail::index_of(wt_.load(std::memory_order_acquire));
|
||||||
|
if (cur_wt == detail::index_of(rd_.load(std::memory_order_relaxed) - 1)) {
|
||||||
|
return false; // full
|
||||||
|
}
|
||||||
|
std::forward<F>(f)(elem_start + cur_wt);
|
||||||
|
wt_.fetch_add(1, std::memory_order_release);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool pop(E* /*elems*/, detail::u2_t& /*cur*/, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
||||||
|
auto cur_rd = detail::index_of(rd_.load(std::memory_order_acquire));
|
||||||
|
if (cur_rd == detail::index_of(wt_.load(std::memory_order_relaxed))) {
|
||||||
|
return false; // empty
|
||||||
|
}
|
||||||
|
std::forward<F>(f)(elem_start + cur_rd);
|
||||||
|
rd_.fetch_add(1, std::memory_order_release);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct prod_cons<relat::single, relat::multi, trans::unicast>
|
||||||
|
: prod_cons<relat::single, relat::single, trans::unicast> {
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool pop(E* /*elems*/, detail::u2_t& /*cur*/, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
||||||
|
byte_t buff[sizeof(detail::elem_t<S>)];
|
||||||
|
while (1) {
|
||||||
|
auto cur_rd = rd_.load(std::memory_order_acquire);
|
||||||
|
if (detail::index_of(cur_rd) ==
|
||||||
|
detail::index_of(wt_.load(std::memory_order_relaxed))) {
|
||||||
|
return false; // empty
|
||||||
|
}
|
||||||
|
std::memcpy(buff, elem_start + detail::index_of(cur_rd), sizeof(buff));
|
||||||
|
if (rd_.compare_exchange_weak(cur_rd, cur_rd + 1, std::memory_order_release)) {
|
||||||
|
std::forward<F>(f)(buff);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct prod_cons<relat::multi, relat::multi, trans::unicast>
|
||||||
|
: prod_cons<relat::single, relat::multi, trans::unicast> {
|
||||||
|
|
||||||
|
std::atomic<detail::u2_t> ct_ { 0 }; // commit index
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool push(E* /*elems*/, F&& f, detail::elem_t<S>* elem_start) {
|
||||||
|
detail::u2_t cur_ct, nxt_ct;
|
||||||
|
while (1) {
|
||||||
|
cur_ct = ct_.load(std::memory_order_acquire);
|
||||||
|
if (detail::index_of(nxt_ct = cur_ct + 1) ==
|
||||||
|
detail::index_of(rd_.load(std::memory_order_relaxed))) {
|
||||||
|
return false; // full
|
||||||
|
}
|
||||||
|
if (ct_.compare_exchange_weak(cur_ct, nxt_ct, std::memory_order_relaxed)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
std::forward<F>(f)(elem_start + detail::index_of(cur_ct));
|
||||||
|
while (1) {
|
||||||
|
auto exp_wt = cur_ct;
|
||||||
|
if (wt_.compare_exchange_weak(exp_wt, nxt_ct, std::memory_order_release)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct prod_cons<relat::single, relat::multi, trans::broadcast> {
|
||||||
|
std::atomic<detail::u2_t> wt_ { 0 }; // write index
|
||||||
|
|
||||||
|
template <std::size_t DataSize>
|
||||||
|
constexpr static std::size_t elem_param = DataSize;
|
||||||
|
|
||||||
|
/*
|
||||||
|
<Remarks> std::atomic<T> may not have value_type.
|
||||||
|
See: https://stackoverflow.com/questions/53648614/what-happened-to-stdatomicxvalue-type
|
||||||
|
*/
|
||||||
|
using rc_t = decltype(detail::elem_head::rc_.load());
|
||||||
|
|
||||||
|
detail::u2_t cursor() const noexcept {
|
||||||
|
return wt_.load(std::memory_order_acquire);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool push(E* elems, F&& f, detail::elem_t<S>* elem_start) {
|
||||||
|
auto conn_cnt = elems->conn_count(); // acquire
|
||||||
|
if (conn_cnt == 0) return false;
|
||||||
|
auto el = elem_start + detail::index_of(wt_.load(std::memory_order_relaxed));
|
||||||
|
// check all consumers have finished reading this element
|
||||||
|
while (1) {
|
||||||
|
rc_t expected = 0;
|
||||||
|
if (el->head_.rc_.compare_exchange_weak(
|
||||||
|
expected, static_cast<rc_t>(conn_cnt), std::memory_order_relaxed)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::this_thread::yield();
|
||||||
|
conn_cnt = elems->conn_count(); // acquire
|
||||||
|
if (conn_cnt == 0) return false;
|
||||||
|
}
|
||||||
|
std::forward<F>(f)(el->data_);
|
||||||
|
wt_.fetch_add(1, std::memory_order_release);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename E, typename F, std::size_t S>
|
||||||
|
bool pop(E* /*elems*/, detail::u2_t& cur, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
||||||
|
if (cur == cursor()) return false; // acquire
|
||||||
|
auto el = elem_start + detail::index_of(cur++);
|
||||||
|
std::forward<F>(f)(el->data_);
|
||||||
|
while (1) {
|
||||||
|
rc_t cur_rc = el->head_.rc_.load(std::memory_order_acquire);
|
||||||
|
if (cur_rc == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (el->head_.rc_.compare_exchange_weak(
|
||||||
|
cur_rc, cur_rc - 1, std::memory_order_release)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
/// element-array implementation
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct elem_head {
|
||||||
|
std::atomic<detail::u2_t> cc_ { 0 }; // connection counter
|
||||||
|
|
||||||
std::size_t connect() noexcept {
|
std::size_t connect() noexcept {
|
||||||
return cc_.fetch_add(1, std::memory_order_release);
|
return cc_.fetch_add(1, std::memory_order_release);
|
||||||
}
|
}
|
||||||
@ -30,185 +219,52 @@ struct alignas(std::max_align_t) elem_array_head {
|
|||||||
return cc_.fetch_sub(1, std::memory_order_release);
|
return cc_.fetch_sub(1, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t conn_count() const noexcept {
|
std::size_t conn_count(std::memory_order order = std::memory_order_acquire) const noexcept {
|
||||||
return cc_.load(std::memory_order_acquire);
|
return cc_.load(order);
|
||||||
}
|
|
||||||
|
|
||||||
u2_t cursor() const noexcept {
|
|
||||||
return wt_.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto acquire(std::memory_order order = std::memory_order_acquire) noexcept {
|
|
||||||
return index_of(wt_.load(order));
|
|
||||||
}
|
|
||||||
|
|
||||||
void commit() noexcept {
|
|
||||||
wt_.fetch_add(1, std::memory_order_release);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t N>
|
template <std::size_t DataSize, typename Policy>
|
||||||
constexpr std::size_t elem_array_head_size =
|
class elem_array : private Policy {
|
||||||
(sizeof(elem_array_head<N>) % alignof(std::max_align_t)) ?
|
|
||||||
((sizeof(elem_array_head<N>) / alignof(std::max_align_t)) + 1) * alignof(std::max_align_t) :
|
|
||||||
sizeof(elem_array_head<N>);
|
|
||||||
|
|
||||||
struct elem_head {
|
|
||||||
std::atomic<uint_t<32>> rc_ { 0 }; // read counter
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t DataSize, std::size_t BaseIntSize = 8>
|
|
||||||
class elem_array : protected elem_array_head<BaseIntSize> {
|
|
||||||
public:
|
public:
|
||||||
using base_t = elem_array_head<BaseIntSize>;
|
using policy_t = Policy;
|
||||||
|
using base_t = Policy;
|
||||||
using head_t = elem_head;
|
using head_t = elem_head;
|
||||||
|
using elem_t = detail::elem_t<policy_t::template elem_param<DataSize>>;
|
||||||
using typename base_t::u1_t;
|
|
||||||
using typename base_t::u2_t;
|
|
||||||
|
|
||||||
enum : std::size_t {
|
enum : std::size_t {
|
||||||
head_size = elem_array_head_size<BaseIntSize>,
|
head_size = sizeof(policy_t) + sizeof(head_t),
|
||||||
data_size = DataSize,
|
data_size = DataSize,
|
||||||
elem_max = (std::numeric_limits<u1_t>::max)() + 1, // default is 255 + 1
|
elem_max = (std::numeric_limits<uint_t<8>>::max)() + 1, // default is 255 + 1
|
||||||
elem_size = sizeof(head_t) + DataSize,
|
elem_size = sizeof(elem_t),
|
||||||
block_size = elem_size * elem_max
|
block_size = elem_size * elem_max
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
struct elem_t {
|
|
||||||
head_t head_;
|
head_t head_;
|
||||||
byte_t data_[data_size] {};
|
|
||||||
};
|
|
||||||
elem_t block_[elem_max];
|
elem_t block_[elem_max];
|
||||||
|
|
||||||
elem_t* elem_start() noexcept {
|
|
||||||
return block_;
|
|
||||||
}
|
|
||||||
|
|
||||||
static elem_t* elem(void* ptr) noexcept { return reinterpret_cast<elem_t*>(static_cast<byte_t*>(ptr) - sizeof(head_t)); }
|
|
||||||
elem_t* elem(u1_t i ) noexcept { return elem_start() + i; }
|
|
||||||
|
|
||||||
template <typename Acq, typename... P>
|
|
||||||
void* acquire(std::memory_order order, Acq&& acq, P&&... params) noexcept {
|
|
||||||
uint_t<32> conn_cnt = this->cc_.load(order);
|
|
||||||
if (conn_cnt == 0) return nullptr;
|
|
||||||
elem_t* el = elem(std::forward<Acq>(acq)(std::memory_order_relaxed,
|
|
||||||
std::forward<P>(params)...));
|
|
||||||
// check all consumers have finished reading
|
|
||||||
while(1) {
|
|
||||||
uint_t<32> expected = 0;
|
|
||||||
if (el->head_.rc_.compare_exchange_weak(
|
|
||||||
expected, conn_cnt, std::memory_order_relaxed)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
conn_cnt = this->cc_.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
return el->data_;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
elem_array() = default;
|
elem_array() = default;
|
||||||
|
|
||||||
elem_array(const elem_array&) = delete;
|
elem_array(const elem_array&) = delete;
|
||||||
elem_array& operator=(const elem_array&) = delete;
|
elem_array& operator=(const elem_array&) = delete;
|
||||||
elem_array(elem_array&&) = delete;
|
|
||||||
elem_array& operator=(elem_array&&) = delete;
|
|
||||||
|
|
||||||
using base_t::connect;
|
std::size_t connect () noexcept { return head_.connect (); }
|
||||||
using base_t::disconnect;
|
std::size_t disconnect() noexcept { return head_.disconnect(); }
|
||||||
using base_t::conn_count;
|
std::size_t conn_count() const noexcept { return head_.conn_count(); }
|
||||||
|
|
||||||
using base_t::cursor;
|
using base_t::cursor;
|
||||||
|
|
||||||
void* acquire(std::memory_order order = std::memory_order_acquire) noexcept {
|
template <typename F>
|
||||||
return this->acquire(order, [this](auto o) {
|
bool push(F&& f) noexcept {
|
||||||
return base_t::acquire(o);
|
return base_t::push(this, std::forward<F>(f), block_);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void commit(void* /*ptr*/) noexcept {
|
|
||||||
base_t::commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
bool fetch(F&& f) noexcept {
|
bool pop(detail::u2_t& cur, F&& f) noexcept {
|
||||||
auto p = this->acquire();
|
return base_t::pop(this, cur, std::forward<F>(f), block_);
|
||||||
if (p == nullptr) return false;
|
|
||||||
std::forward<F>(f)(p);
|
|
||||||
this->commit(p);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* take(u2_t cursor) noexcept {
|
|
||||||
return elem(base_t::index_of(cursor))->data_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void put(void* ptr) noexcept {
|
|
||||||
auto el = elem(ptr);
|
|
||||||
uint_t<32> cur_rc;
|
|
||||||
do {
|
|
||||||
cur_rc = el->head_.rc_.load(std::memory_order_relaxed);
|
|
||||||
if (cur_rc == 0) return;
|
|
||||||
} while (!el->head_.rc_.compare_exchange_weak(
|
|
||||||
cur_rc, cur_rc - 1, std::memory_order_release));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/*
|
|
||||||
template <std::size_t DataSize, std::size_t BaseIntSize = 8>
|
|
||||||
class multi_write_array : protected elem_array<DataSize, BaseIntSize> {
|
|
||||||
public:
|
|
||||||
using base_t = elem_array<DataSize, BaseIntSize>;
|
|
||||||
using head_t = typename base_t::head_t;
|
|
||||||
|
|
||||||
using typename base_t::u1_t;
|
|
||||||
using typename base_t::u2_t;
|
|
||||||
|
|
||||||
using base_t::head_size;
|
|
||||||
using base_t::data_size;
|
|
||||||
using base_t::elem_max;
|
|
||||||
using base_t::elem_size;
|
|
||||||
using base_t::block_size;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::atomic<u2_t> rd_ { 0 }; // ready index
|
|
||||||
|
|
||||||
public:
|
|
||||||
using base_t::connect;
|
|
||||||
using base_t::disconnect;
|
|
||||||
using base_t::conn_count;
|
|
||||||
|
|
||||||
u2_t cursor() const noexcept {
|
|
||||||
return rd_.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
bool fetch(F&& f) noexcept {
|
|
||||||
u2_t cur_rd;
|
|
||||||
auto p = base_t::acquire(std::memory_order_acquire, [this, &cur_rd](auto o) {
|
|
||||||
while (1) {
|
|
||||||
u2_t cur_wt = wt_.load(o), nxt_wt = cur_wt + 1;
|
|
||||||
if (base_t::index_of(nxt_wt) ==
|
|
||||||
base_t::index_of(cur_rd = rd_.load(std::memory_order_relaxed))) {
|
|
||||||
// is full
|
|
||||||
}
|
|
||||||
else if (wt_.compare_exchange_weak(cur_wt, nxt_wt, std::memory_order_relaxed)) {
|
|
||||||
return base_t::index_of(nxt_wt);
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
std::atomic_thread_fence(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (p == nullptr) return false;
|
|
||||||
std::forward<F>(f)(p);
|
|
||||||
while (1) {
|
|
||||||
if (rd_.compare_exchange_weak(cur_rd, cur_rd + 1, std::memory_order_release)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
} // namespace circ
|
} // namespace circ
|
||||||
} // namespace ipc
|
} // namespace ipc
|
||||||
|
|||||||
@ -1,265 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <thread>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "def.h"
|
|
||||||
|
|
||||||
namespace ipc {
|
|
||||||
namespace circ {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
using u1_t = uint_t<8>;
|
|
||||||
using u2_t = uint_t<16>;
|
|
||||||
|
|
||||||
constexpr u1_t index_of(u2_t c) noexcept {
|
|
||||||
return static_cast<u1_t>(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct elem_head {
|
|
||||||
std::atomic<std::size_t> rc_ { 0 }; // read counter
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t DataSize>
|
|
||||||
struct elem_t {
|
|
||||||
elem_head head_;
|
|
||||||
byte_t data_[DataSize] {};
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct elem_t<0> {
|
|
||||||
elem_head head_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t S>
|
|
||||||
elem_t<S>* elem_of(void* ptr) noexcept {
|
|
||||||
return reinterpret_cast<elem_t<S>*>(static_cast<byte_t*>(ptr) - sizeof(elem_head));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
enum class relat { // multiplicity of the relationship
|
|
||||||
single,
|
|
||||||
multi
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class trans { // transmission
|
|
||||||
unicast,
|
|
||||||
broadcast
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
/// producer-consumer policies
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template <relat Rp, relat Rc, trans Ts>
|
|
||||||
struct prod_cons;
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct prod_cons<relat::single, relat::single, trans::unicast> {
|
|
||||||
std::atomic<detail::u2_t> rd_ { 0 }; // read index
|
|
||||||
std::atomic<detail::u2_t> wt_ { 0 }; // write index
|
|
||||||
|
|
||||||
template <std::size_t DataSize>
|
|
||||||
constexpr static std::size_t elem_param = DataSize - sizeof(detail::elem_head);
|
|
||||||
|
|
||||||
constexpr detail::u2_t cursor() const noexcept {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool push(E* /*elems*/, F&& f, detail::elem_t<S>* elem_start) {
|
|
||||||
auto cur_wt = detail::index_of(wt_.load(std::memory_order_acquire));
|
|
||||||
if (cur_wt == detail::index_of(rd_.load(std::memory_order_relaxed) - 1)) {
|
|
||||||
return false; // full
|
|
||||||
}
|
|
||||||
std::forward<F>(f)(elem_start + cur_wt);
|
|
||||||
wt_.fetch_add(1, std::memory_order_release);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool pop(E* /*elems*/, detail::u2_t& /*cur*/, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
|
||||||
auto cur_rd = detail::index_of(rd_.load(std::memory_order_acquire));
|
|
||||||
if (cur_rd == detail::index_of(wt_.load(std::memory_order_relaxed))) {
|
|
||||||
return false; // empty
|
|
||||||
}
|
|
||||||
std::forward<F>(f)(elem_start + cur_rd);
|
|
||||||
rd_.fetch_add(1, std::memory_order_release);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct prod_cons<relat::single, relat::multi, trans::unicast>
|
|
||||||
: prod_cons<relat::single, relat::single, trans::unicast> {
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool pop(E* /*elems*/, detail::u2_t& /*cur*/, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
|
||||||
byte_t buff[sizeof(detail::elem_t<S>)];
|
|
||||||
while (1) {
|
|
||||||
auto cur_rd = rd_.load(std::memory_order_acquire);
|
|
||||||
if (detail::index_of(cur_rd) ==
|
|
||||||
detail::index_of(wt_.load(std::memory_order_relaxed))) {
|
|
||||||
return false; // empty
|
|
||||||
}
|
|
||||||
std::memcpy(buff, elem_start + detail::index_of(cur_rd), sizeof(buff));
|
|
||||||
if (rd_.compare_exchange_weak(cur_rd, cur_rd + 1, std::memory_order_release)) {
|
|
||||||
std::forward<F>(f)(buff);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct prod_cons<relat::multi, relat::multi, trans::unicast>
|
|
||||||
: prod_cons<relat::single, relat::multi, trans::unicast> {
|
|
||||||
|
|
||||||
std::atomic<detail::u2_t> ct_ { 0 }; // commit index
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool push(E* /*elems*/, F&& f, detail::elem_t<S>* elem_start) {
|
|
||||||
detail::u2_t cur_ct, nxt_ct;
|
|
||||||
while (1) {
|
|
||||||
cur_ct = ct_.load(std::memory_order_acquire);
|
|
||||||
if (detail::index_of(nxt_ct = cur_ct + 1) ==
|
|
||||||
detail::index_of(rd_.load(std::memory_order_relaxed))) {
|
|
||||||
return false; // full
|
|
||||||
}
|
|
||||||
if (ct_.compare_exchange_weak(cur_ct, nxt_ct, std::memory_order_relaxed)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
std::forward<F>(f)(elem_start + detail::index_of(cur_ct));
|
|
||||||
while (1) {
|
|
||||||
auto exp_wt = cur_ct;
|
|
||||||
if (wt_.compare_exchange_weak(exp_wt, nxt_ct, std::memory_order_release)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct prod_cons<relat::single, relat::multi, trans::broadcast> {
|
|
||||||
std::atomic<detail::u2_t> wt_ { 0 }; // write index
|
|
||||||
|
|
||||||
template <std::size_t DataSize>
|
|
||||||
constexpr static std::size_t elem_param = DataSize;
|
|
||||||
|
|
||||||
/*
|
|
||||||
<Remarks> std::atomic<T> may not have value_type.
|
|
||||||
See: https://stackoverflow.com/questions/53648614/what-happened-to-stdatomicxvalue-type
|
|
||||||
*/
|
|
||||||
using rc_t = decltype(detail::elem_head::rc_.load());
|
|
||||||
|
|
||||||
detail::u2_t cursor() const noexcept {
|
|
||||||
return wt_.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool push(E* elems, F&& f, detail::elem_t<S>* elem_start) {
|
|
||||||
auto conn_cnt = elems->conn_count(); // acquire
|
|
||||||
if (conn_cnt == 0) return false;
|
|
||||||
auto el = elem_start + detail::index_of(wt_.load(std::memory_order_relaxed));
|
|
||||||
// check all consumers have finished reading this element
|
|
||||||
rc_t expected = 0;
|
|
||||||
if (!el->head_.rc_.compare_exchange_weak(
|
|
||||||
expected, static_cast<rc_t>(conn_cnt), std::memory_order_relaxed)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::forward<F>(f)(el->data_);
|
|
||||||
wt_.fetch_add(1, std::memory_order_release);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename E, typename F, std::size_t S>
|
|
||||||
bool pop(E* /*elems*/, detail::u2_t& cur, F&& f, detail::elem_t<S>* elem_start) noexcept {
|
|
||||||
if (cur == cursor()) return false; // acquire
|
|
||||||
auto el = elem_start + detail::index_of(cur++);
|
|
||||||
std::forward<F>(f)(el->data_);
|
|
||||||
do {
|
|
||||||
rc_t cur_rc = el->head_.rc_.load(std::memory_order_acquire);
|
|
||||||
if (cur_rc == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (el->head_.rc_.compare_exchange_weak(
|
|
||||||
cur_rc, cur_rc - 1, std::memory_order_release)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
} while (1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
/// element-array implementation
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct elems_head {
|
|
||||||
std::atomic<detail::u2_t> cc_ { 0 }; // connection counter
|
|
||||||
|
|
||||||
std::size_t connect() noexcept {
|
|
||||||
return cc_.fetch_add(1, std::memory_order_release);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t disconnect() noexcept {
|
|
||||||
return cc_.fetch_sub(1, std::memory_order_release);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t conn_count(std::memory_order order = std::memory_order_acquire) const noexcept {
|
|
||||||
return cc_.load(order);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <std::size_t DataSize, typename Policy>
|
|
||||||
class elems_array : private Policy {
|
|
||||||
public:
|
|
||||||
using policy_t = Policy;
|
|
||||||
using base_t = Policy;
|
|
||||||
using head_t = elems_head;
|
|
||||||
using elem_t = detail::elem_t<policy_t::template elem_param<DataSize>>;
|
|
||||||
|
|
||||||
enum : std::size_t {
|
|
||||||
head_size = sizeof(head_t),
|
|
||||||
data_size = DataSize,
|
|
||||||
elem_max = (std::numeric_limits<uint_t<8>>::max)() + 1, // default is 255 + 1
|
|
||||||
elem_size = sizeof(elem_t),
|
|
||||||
block_size = elem_size * elem_max
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
head_t head_;
|
|
||||||
elem_t block_[elem_max];
|
|
||||||
|
|
||||||
public:
|
|
||||||
elems_array() = default;
|
|
||||||
elems_array(const elems_array&) = delete;
|
|
||||||
elems_array& operator=(const elems_array&) = delete;
|
|
||||||
|
|
||||||
std::size_t connect () noexcept { return head_.connect (); }
|
|
||||||
std::size_t disconnect() noexcept { return head_.disconnect(); }
|
|
||||||
std::size_t conn_count() const noexcept { return head_.conn_count(); }
|
|
||||||
|
|
||||||
using base_t::cursor;
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
bool push(F&& f) noexcept {
|
|
||||||
return base_t::push(this, std::forward<F>(f), block_);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
bool pop(detail::u2_t& cur, F&& f) noexcept {
|
|
||||||
return base_t::pop(this, cur, std::forward<F>(f), block_);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace circ
|
|
||||||
} // namespace ipc
|
|
||||||
@ -15,14 +15,15 @@
|
|||||||
namespace ipc {
|
namespace ipc {
|
||||||
namespace circ {
|
namespace circ {
|
||||||
|
|
||||||
template <typename T, template <std::size_t...> class ElemArray = elem_array>
|
template <typename T, typename Policy = prod_cons<relat::single, relat::multi, trans::broadcast>>
|
||||||
class queue {
|
class queue {
|
||||||
public:
|
public:
|
||||||
using array_t = ElemArray<sizeof(T)>;
|
using array_t = elem_array<sizeof(T), Policy>;
|
||||||
|
using policy_t = typename array_t::policy_t;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
array_t* elems_ = nullptr;
|
array_t* elems_ = nullptr;
|
||||||
typename array_t::u2_t cursor_ = 0;
|
decltype(std::declval<array_t>().cursor()) cursor_ = 0;
|
||||||
std::atomic_bool connected_ { false };
|
std::atomic_bool connected_ { false };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -89,7 +90,7 @@ public:
|
|||||||
template <typename... P>
|
template <typename... P>
|
||||||
auto push(P&&... params) noexcept {
|
auto push(P&&... params) noexcept {
|
||||||
if (elems_ == nullptr) return false;
|
if (elems_ == nullptr) return false;
|
||||||
return elems_->fetch([&](void* p) {
|
return elems_->push([&](void* p) {
|
||||||
::new (p) T(std::forward<P>(params)...);
|
::new (p) T(std::forward<P>(params)...);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -121,9 +122,12 @@ public:
|
|||||||
if (que->elems_ == nullptr) {
|
if (que->elems_ == nullptr) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto item_ptr = static_cast<T*>(que->elems_->take(que->cursor_++));
|
T item;
|
||||||
T item = std::move(*item_ptr);
|
if (!que->elems_->pop(que->cursor_, [&item](void* p) {
|
||||||
que->elems_->put(item_ptr);
|
::new (&item) T(std::move(*static_cast<T*>(p)));
|
||||||
|
})) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -41,27 +41,29 @@ enum : std::size_t {
|
|||||||
base_size = sizeof(void*)
|
base_size = sizeof(void*)
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t Radix>
|
constexpr std::size_t classify(std::size_t size) {
|
||||||
constexpr std::size_t roundup(std::size_t n) {
|
constexpr static std::size_t mapping[] = {
|
||||||
return ((n - 1) & (~(Radix - 1))) + Radix;
|
/* 1 */
|
||||||
|
0 , 1 , 2 , 3 ,
|
||||||
|
/* 2 */
|
||||||
|
5 , 5 , 7 , 7 ,
|
||||||
|
9 , 9 , 11, 11,
|
||||||
|
13, 13, 15, 15,
|
||||||
|
/* 4 */
|
||||||
|
19, 19, 19, 19,
|
||||||
|
23, 23, 23, 23,
|
||||||
|
27, 27, 27, 27,
|
||||||
|
31, 31, 31, 31
|
||||||
|
};
|
||||||
|
size = (size - 1) / base_size;
|
||||||
|
return (size < std::size(mapping)) ? mapping[size] : 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
using fixed_sequence_t = std::index_sequence<
|
|
||||||
base_size , base_size * 2 ,
|
|
||||||
base_size * 3 , base_size * 4 ,
|
|
||||||
base_size * 5 , base_size * 6 ,
|
|
||||||
base_size * 7 , base_size * 8 ,
|
|
||||||
base_size * 9 , base_size * 10,
|
|
||||||
base_size * 11, base_size * 12,
|
|
||||||
base_size * 13, base_size * 14,
|
|
||||||
base_size * 15, base_size * 16
|
|
||||||
>;
|
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
decltype(auto) choose(std::size_t size, F&& f) {
|
decltype(auto) choose(std::size_t size, F&& f) {
|
||||||
return detail::static_switch(roundup<base_size>(size), fixed_sequence_t {
|
return detail::static_switch(classify(size), std::make_index_sequence<32> {
|
||||||
}, [&f](auto index) {
|
}, [&f](auto index) {
|
||||||
return f(fixed<decltype(index)::value>());
|
return f(fixed<(decltype(index)::value + 1) * base_size>());
|
||||||
}, [&f] {
|
}, [&f] {
|
||||||
return f(static_alloc{});
|
return f(static_alloc{});
|
||||||
});
|
});
|
||||||
@ -70,8 +72,8 @@ decltype(auto) choose(std::size_t size, F&& f) {
|
|||||||
class pool_alloc {
|
class pool_alloc {
|
||||||
public:
|
public:
|
||||||
static void clear() {
|
static void clear() {
|
||||||
static_for(fixed_sequence_t {}, [](auto index) {
|
static_for(std::make_index_sequence<32> {}, [](auto index) {
|
||||||
fixed<decltype(index)::value>().clear();
|
fixed<(decltype(index)::value + 1) * base_size>().clear();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "circ_elems_array.h"
|
|
||||||
#include "circ_elem_array.h"
|
#include "circ_elem_array.h"
|
||||||
#include "circ_queue.h"
|
#include "circ_queue.h"
|
||||||
#include "memory/resource.hpp"
|
#include "memory/resource.hpp"
|
||||||
@ -19,7 +18,10 @@ struct msg_t {
|
|||||||
int dat_;
|
int dat_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using cq_t = ipc::circ::elem_array<sizeof(msg_t)>;
|
using cq_t = ipc::circ::elem_array<sizeof(msg_t),
|
||||||
|
ipc::circ::prod_cons<ipc::circ::relat::single,
|
||||||
|
ipc::circ::relat::multi,
|
||||||
|
ipc::circ::trans::broadcast>>;
|
||||||
cq_t* cq__;
|
cq_t* cq__;
|
||||||
|
|
||||||
bool operator==(msg_t const & m1, msg_t const & m2) {
|
bool operator==(msg_t const & m1, msg_t const & m2) {
|
||||||
@ -28,8 +30,8 @@ bool operator==(msg_t const & m1, msg_t const & m2) {
|
|||||||
|
|
||||||
} // internal-linkage
|
} // internal-linkage
|
||||||
|
|
||||||
template <>
|
template <std::size_t D, typename P>
|
||||||
struct test_verify<cq_t> {
|
struct test_verify<ipc::circ::elem_array<D, P>> {
|
||||||
std::vector<std::unordered_map<int, std::vector<int>>> list_;
|
std::vector<std::unordered_map<int, std::vector<int>>> list_;
|
||||||
|
|
||||||
test_verify(int M)
|
test_verify(int M)
|
||||||
@ -99,14 +101,14 @@ struct quit_mode<ipc::circ::prod_cons<Rp, Rc, ipc::circ::trans::unicast>> {
|
|||||||
template <ipc::circ::relat Rp, ipc::circ::relat Rc>
|
template <ipc::circ::relat Rp, ipc::circ::relat Rc>
|
||||||
struct quit_mode<ipc::circ::prod_cons<Rp, Rc, ipc::circ::trans::broadcast>> {
|
struct quit_mode<ipc::circ::prod_cons<Rp, Rc, ipc::circ::trans::broadcast>> {
|
||||||
struct type {
|
struct type {
|
||||||
type(bool) {}
|
constexpr type(bool) {}
|
||||||
constexpr operator bool() const { return false; }
|
constexpr operator bool() const { return false; }
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t D, typename P>
|
template <std::size_t D, typename P>
|
||||||
struct test_cq<ipc::circ::elems_array<D, P>> {
|
struct test_cq<ipc::circ::elem_array<D, P>> {
|
||||||
using ca_t = ipc::circ::elems_array<D, P>;
|
using ca_t = ipc::circ::elem_array<D, P>;
|
||||||
using cn_t = decltype(std::declval<ca_t>().cursor());
|
using cn_t = decltype(std::declval<ca_t>().cursor());
|
||||||
|
|
||||||
typename quit_mode<P>::type quit_ = false;
|
typename quit_mode<P>::type quit_ = false;
|
||||||
@ -132,7 +134,7 @@ struct test_cq<ipc::circ::elems_array<D, P>> {
|
|||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
void recv(cn_t cur, F&& proc) {
|
void recv(cn_t cur, F&& proc) {
|
||||||
while(1) {
|
while (1) {
|
||||||
msg_t msg;
|
msg_t msg;
|
||||||
while (ca_->pop(cur, [&msg](void* p) {
|
while (ca_->pop(cur, [&msg](void* p) {
|
||||||
msg = *static_cast<msg_t*>(p);
|
msg = *static_cast<msg_t*>(p);
|
||||||
@ -161,59 +163,6 @@ struct test_cq<ipc::circ::elems_array<D, P>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t D>
|
|
||||||
struct test_cq<ipc::circ::elem_array<D>> {
|
|
||||||
using ca_t = ipc::circ::elem_array<D>;
|
|
||||||
using cn_t = typename ca_t::u2_t;
|
|
||||||
|
|
||||||
ca_t* ca_;
|
|
||||||
|
|
||||||
test_cq(ca_t* ca) : ca_(ca) {
|
|
||||||
::new (ca) ca_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
cn_t connect() {
|
|
||||||
auto cur = ca_->cursor();
|
|
||||||
ca_->connect();
|
|
||||||
return cur;
|
|
||||||
}
|
|
||||||
|
|
||||||
void disconnect(cn_t) {
|
|
||||||
ca_->disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
void wait_start(int M) {
|
|
||||||
while (ca_->conn_count() != static_cast<std::size_t>(M)) {
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename F>
|
|
||||||
void recv(cn_t cur, F&& proc) {
|
|
||||||
while(1) {
|
|
||||||
while (cur != ca_->cursor()) {
|
|
||||||
msg_t* pmsg = static_cast<msg_t*>(ca_->take(cur)),
|
|
||||||
msg = *pmsg;
|
|
||||||
ca_->put(pmsg);
|
|
||||||
if (msg.pid_ < 0) return;
|
|
||||||
++cur;
|
|
||||||
proc(msg);
|
|
||||||
}
|
|
||||||
std::this_thread::yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ca_t* connect_send() {
|
|
||||||
return ca_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void send(ca_t* ca, msg_t const & msg) {
|
|
||||||
msg_t* pmsg = static_cast<msg_t*>(ca->acquire());
|
|
||||||
(*pmsg) = msg;
|
|
||||||
ca->commit(pmsg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct test_cq<ipc::circ::queue<T>> {
|
struct test_cq<ipc::circ::queue<T>> {
|
||||||
using cn_t = ipc::circ::queue<T>;
|
using cn_t = ipc::circ::queue<T>;
|
||||||
@ -283,7 +232,7 @@ private slots:
|
|||||||
|
|
||||||
#include "test_circ.moc"
|
#include "test_circ.moc"
|
||||||
|
|
||||||
constexpr int LoopCount = 10000000;
|
constexpr int LoopCount = 1000000;
|
||||||
//constexpr int LoopCount = 1000/*0000*/;
|
//constexpr int LoopCount = 1000/*0000*/;
|
||||||
|
|
||||||
void Unit::initTestCase() {
|
void Unit::initTestCase() {
|
||||||
@ -302,15 +251,8 @@ void Unit::test_inst() {
|
|||||||
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(static_cast<std::size_t>(cq_t::data_size), sizeof(msg_t));
|
QCOMPARE(static_cast<std::size_t>(cq_t::data_size), sizeof(msg_t));
|
||||||
QCOMPARE(sizeof(cq_t), static_cast<std::size_t>(cq_t::block_size + cq_t::head_size));
|
|
||||||
|
|
||||||
std::cout << "sizeof(ipc::circ::elem_array<4096>) = " << sizeof(*cq__) << std::endl;
|
std::cout << "sizeof(ipc::circ::elem_array<sizeof(msg_t)>) = " << sizeof(*cq__) << std::endl;
|
||||||
|
|
||||||
auto a = cq__->take(1);
|
|
||||||
auto b = cq__->take(2);
|
|
||||||
QCOMPARE(static_cast<std::size_t>(static_cast<ipc::byte_t*>(b) -
|
|
||||||
static_cast<ipc::byte_t*>(a)),
|
|
||||||
static_cast<std::size_t>(cq_t::elem_size));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int N, int M, bool V = true, int Loops = LoopCount>
|
template <int N, int M, bool V = true, int Loops = LoopCount>
|
||||||
@ -319,9 +261,7 @@ void test_prod_cons() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Unit::test_prod_cons_1v1() {
|
void Unit::test_prod_cons_1v1() {
|
||||||
test_prod_cons<1, 1>();
|
ipc::circ::elem_array<
|
||||||
|
|
||||||
ipc::circ::elems_array<
|
|
||||||
sizeof(msg_t),
|
sizeof(msg_t),
|
||||||
ipc::circ::prod_cons<ipc::circ::relat::single,
|
ipc::circ::prod_cons<ipc::circ::relat::single,
|
||||||
ipc::circ::relat::single,
|
ipc::circ::relat::single,
|
||||||
@ -329,12 +269,30 @@ void Unit::test_prod_cons_1v1() {
|
|||||||
> el_arr_ss;
|
> el_arr_ss;
|
||||||
benchmark_prod_cons<1, 1, LoopCount, cq_t>(&el_arr_ss);
|
benchmark_prod_cons<1, 1, LoopCount, cq_t>(&el_arr_ss);
|
||||||
benchmark_prod_cons<1, 1, LoopCount, void>(&el_arr_ss);
|
benchmark_prod_cons<1, 1, LoopCount, void>(&el_arr_ss);
|
||||||
|
|
||||||
|
ipc::circ::elem_array<
|
||||||
|
sizeof(msg_t),
|
||||||
|
ipc::circ::prod_cons<ipc::circ::relat::single,
|
||||||
|
ipc::circ::relat::multi,
|
||||||
|
ipc::circ::trans::unicast>
|
||||||
|
> el_arr_smn;
|
||||||
|
benchmark_prod_cons<1, 1, LoopCount, decltype(el_arr_smn)::policy_t>(&el_arr_smn);
|
||||||
|
benchmark_prod_cons<1, 1, LoopCount, void>(&el_arr_smn);
|
||||||
|
|
||||||
|
ipc::circ::elem_array<
|
||||||
|
sizeof(msg_t),
|
||||||
|
ipc::circ::prod_cons<ipc::circ::relat::multi,
|
||||||
|
ipc::circ::relat::multi,
|
||||||
|
ipc::circ::trans::unicast>
|
||||||
|
> el_arr_mmn;
|
||||||
|
benchmark_prod_cons<1, 1, LoopCount, decltype(el_arr_mmn)::policy_t>(&el_arr_mmn);
|
||||||
|
benchmark_prod_cons<1, 1, LoopCount, void>(&el_arr_mmn);
|
||||||
|
|
||||||
|
test_prod_cons<1, 1>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unit::test_prod_cons_1v3() {
|
void Unit::test_prod_cons_1v3() {
|
||||||
test_prod_cons<1, 3>();
|
ipc::circ::elem_array<
|
||||||
|
|
||||||
ipc::circ::elems_array<
|
|
||||||
sizeof(msg_t),
|
sizeof(msg_t),
|
||||||
ipc::circ::prod_cons<ipc::circ::relat::single,
|
ipc::circ::prod_cons<ipc::circ::relat::single,
|
||||||
ipc::circ::relat::multi,
|
ipc::circ::relat::multi,
|
||||||
@ -343,7 +301,7 @@ void Unit::test_prod_cons_1v3() {
|
|||||||
benchmark_prod_cons<1, 3, LoopCount, decltype(el_arr_smn)::policy_t>(&el_arr_smn);
|
benchmark_prod_cons<1, 3, LoopCount, decltype(el_arr_smn)::policy_t>(&el_arr_smn);
|
||||||
benchmark_prod_cons<1, 3, LoopCount, void>(&el_arr_smn);
|
benchmark_prod_cons<1, 3, LoopCount, void>(&el_arr_smn);
|
||||||
|
|
||||||
ipc::circ::elems_array<
|
ipc::circ::elem_array<
|
||||||
sizeof(msg_t),
|
sizeof(msg_t),
|
||||||
ipc::circ::prod_cons<ipc::circ::relat::multi,
|
ipc::circ::prod_cons<ipc::circ::relat::multi,
|
||||||
ipc::circ::relat::multi,
|
ipc::circ::relat::multi,
|
||||||
@ -351,20 +309,34 @@ void Unit::test_prod_cons_1v3() {
|
|||||||
> el_arr_mmn;
|
> el_arr_mmn;
|
||||||
benchmark_prod_cons<1, 3, LoopCount, decltype(el_arr_mmn)::policy_t>(&el_arr_mmn);
|
benchmark_prod_cons<1, 3, LoopCount, decltype(el_arr_mmn)::policy_t>(&el_arr_mmn);
|
||||||
benchmark_prod_cons<1, 3, LoopCount, void>(&el_arr_mmn);
|
benchmark_prod_cons<1, 3, LoopCount, void>(&el_arr_mmn);
|
||||||
benchmark_prod_cons<3, 3, LoopCount, decltype(el_arr_mmn)::policy_t>(&el_arr_mmn);
|
|
||||||
benchmark_prod_cons<3, 3, LoopCount, void>(&el_arr_mmn);
|
|
||||||
|
|
||||||
ipc::circ::elems_array<
|
test_prod_cons<1, 3>();
|
||||||
sizeof(msg_t),
|
|
||||||
ipc::circ::prod_cons<ipc::circ::relat::single,
|
|
||||||
ipc::circ::relat::multi,
|
|
||||||
ipc::circ::trans::broadcast>
|
|
||||||
> el_arr_smm;
|
|
||||||
benchmark_prod_cons<1, 3, LoopCount, cq_t>(&el_arr_smm);
|
|
||||||
benchmark_prod_cons<1, 3, LoopCount, void>(&el_arr_smm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unit::test_prod_cons_performance() {
|
void Unit::test_prod_cons_performance() {
|
||||||
|
ipc::circ::elem_array<
|
||||||
|
sizeof(msg_t),
|
||||||
|
ipc::circ::prod_cons<ipc::circ::relat::single,
|
||||||
|
ipc::circ::relat::multi,
|
||||||
|
ipc::circ::trans::unicast>
|
||||||
|
> el_arr_smn;
|
||||||
|
ipc::mem::detail::static_for(std::make_index_sequence<10>{}, [&el_arr_smn](auto index) {
|
||||||
|
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount, void>(&el_arr_smn);
|
||||||
|
});
|
||||||
|
|
||||||
|
ipc::circ::elem_array<
|
||||||
|
sizeof(msg_t),
|
||||||
|
ipc::circ::prod_cons<ipc::circ::relat::multi,
|
||||||
|
ipc::circ::relat::multi,
|
||||||
|
ipc::circ::trans::unicast>
|
||||||
|
> el_arr_mmn;
|
||||||
|
ipc::mem::detail::static_for(std::make_index_sequence<10>{}, [&el_arr_mmn](auto index) {
|
||||||
|
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmn);
|
||||||
|
});
|
||||||
|
ipc::mem::detail::static_for(std::make_index_sequence<10>{}, [&el_arr_mmn](auto index) {
|
||||||
|
benchmark_prod_cons<decltype(index)::value + 1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmn);
|
||||||
|
});
|
||||||
|
|
||||||
ipc::mem::detail::static_for(std::make_index_sequence<10>{}, [](auto index) {
|
ipc::mem::detail::static_for(std::make_index_sequence<10>{}, [](auto index) {
|
||||||
test_prod_cons<1, decltype(index)::value + 1, false>();
|
test_prod_cons<1, decltype(index)::value + 1, false>();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user