1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/pattern/LockFreeBufferMisc.hpp" 5 #include "hmbdc/os/Allocators.hpp" 6 #include "hmbdc/Config.hpp" 7 #include "hmbdc/pattern/MemRingBuffer.hpp" 15 namespace hmbdc {
namespace pattern {
17 using Sequence = HMBDC_SEQ_TYPE;
19 using value_type =
void *;
22 max_parallel_consumer = 0xffff
25 template <
typename Allocator = os::DefaultAllocator>
26 MonoLockFreeBuffer(
size_t, uint32_t, Allocator& allocator = os::DefaultAllocator::instance());
29 size_t maxItemSize()
const;
30 size_t capacity()
const;
31 void put(
void const*,
size_t sizeHint = 0);
32 template <
typename T>
void put(T
const& item) {put(&item,
sizeof(item));}
33 template <
typename T>
void putSome(T
const& item) {put(&item, std::min(
sizeof(item), maxItemSize()));}
34 template <
typename T,
typename ...Args>
35 void putInPlace(Args&&... args) {
37 new (*s) T(std::forward<Args>(args)...);
40 template <
typename T,
typename ...Args>
41 bool tryPutInPlace(Args&&... args) {
44 new (*s) T(std::forward<Args>(args)...);
49 bool tryPut(
void const*,
size_t sizeHint = 0);
51 Sequence readSeq()
const;
60 void take(
void *,
size_t = 0);
61 bool tryTake(
void *,
size_t = 0);
64 ,
size_t maxPeekSize = std::numeric_limits<size_t>::max());
66 ,
size_t maxPeekSize = std::numeric_limits<size_t>::max());
71 size_t remainingSize()
const;
72 size_t parallelConsumerAlive()
const {
return 1;}
76 size_t footprint(
size_t, uint32_t);
79 std::function<void()> freer_;
84 namespace hmbdc {
namespace pattern {
85 using MMRB = MemRingBuffer<0u, typename MonoLockFreeBuffer::Sequence>;
89 uint32_t itemSizePower2Num(
size_t valueTypeSize) {
91 valueTypeSize +=
sizeof(MMRB::Sequence);
92 while ((1ul << res) < valueTypeSize) res++;
100 #define impl_vptr ((void*)(((char*)this) + impl_)) 105 maxItemSize()
const {
106 return static_cast<MMRB*
>(impl_vptr)->VALUE_TYPE_SIZE;
113 return static_cast<MMRB*
>(impl_vptr)->CAPACITY;
116 template <
typename Allocator>
118 MonoLockFreeBuffer(
size_t valueTypeSize, uint32_t ringSizePower2Num, Allocator& allocator)
120 : impl_(((char*)allocator.template allocate<MMRB>(SMP_CACHE_BYTES
121 , itemSizePower2Num(valueTypeSize), ringSizePower2Num
122 , (allocator))) - (char*)this)
123 , freer_([this, &allocator](){allocator.unallocate(static_cast<MMRB*>(impl_vptr));})
129 footprint(
size_t valueTypeSize, uint32_t ringSizePower2Num){
130 return sizeof(MonoLockFreeBuffer) + SMP_CACHE_BYTES + MMRB::footprint(
131 itemSizePower2Num(valueTypeSize), ringSizePower2Num) + SMP_CACHE_BYTES;
136 ~MonoLockFreeBuffer() {
145 inline void MonoLockFreeBuffer::put(
void const* item,
size_t sizeHint) HMBDC_RESTRICT {
static_cast<MMRB*
>(impl_vptr)->put(item, sizeHint);}
146 inline bool MonoLockFreeBuffer::tryPut(
void const* item,
size_t sizeHint) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->tryPut(item, sizeHint);}
147 inline bool MonoLockFreeBuffer::isFull() const HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->isFull();}
148 inline MonoLockFreeBuffer::Sequence MonoLockFreeBuffer::readSeq() const HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->readSeq();}
149 inline MonoLockFreeBuffer::iterator MonoLockFreeBuffer::claim() HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->claim();}
150 inline MonoLockFreeBuffer::iterator MonoLockFreeBuffer::tryClaim() HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->tryClaim();}
151 inline MonoLockFreeBuffer::iterator MonoLockFreeBuffer::claim(
size_t n) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->claim(n);}
152 inline MonoLockFreeBuffer::iterator MonoLockFreeBuffer::tryClaim(
size_t n) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->tryClaim(n);}
153 inline void MonoLockFreeBuffer::commit(MonoLockFreeBuffer::iterator it) HMBDC_RESTRICT {
static_cast<MMRB*
>(impl_vptr)->commit(it);}
154 inline void MonoLockFreeBuffer::commit(iterator it,
size_t n) HMBDC_RESTRICT {
static_cast<MMRB*
>(impl_vptr)->commit(it, n);}
155 inline void MonoLockFreeBuffer::take(
void *i,
size_t n) HMBDC_RESTRICT {
static_cast<MMRB*
>(impl_vptr)->take(i, n);}
156 inline bool MonoLockFreeBuffer::tryTake(
void *i,
size_t n) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->tryTake(i, n);}
157 inline MonoLockFreeBuffer::iterator MonoLockFreeBuffer::peek() HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->peek();}
158 inline size_t MonoLockFreeBuffer::peek(iterator& b, iterator& e,
size_t s) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->peek(b, e, s);}
159 inline size_t MonoLockFreeBuffer::peekSome(iterator& b, iterator& e,
size_t s) HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->peekSome(b, e, s);}
161 inline size_t MonoLockFreeBuffer::remainingSize() const HMBDC_RESTRICT {
return static_cast<MMRB*
>(impl_vptr)->remainingSize();}
162 inline void MonoLockFreeBuffer::reset() HMBDC_RESTRICT {
static_cast<MMRB*
>(impl_vptr)->reset();}
164 template MonoLockFreeBuffer::MonoLockFreeBuffer(
size_t, uint32_t, os::DefaultAllocator&);
165 template MonoLockFreeBuffer::MonoLockFreeBuffer(
size_t, uint32_t, os::ShmBasePtrAllocator&);
Definition: MonoLockFreeBuffer.hpp:16
Definition: LockFreeBufferMisc.hpp:23
Definition: MemRingBuffer.hpp:43
void wasteAfterPeek(iterator, size_t, bool=false)
if size not matching - please refer to the impl for details
Definition: MonoLockFreeBuffer.hpp:160
Definition: LockFreeBufferMisc.hpp:89