hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
MonoLockFreeBuffer.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/pattern/LockFreeBufferMisc.hpp"
5 #include "hmbdc/os/Allocators.hpp"
6 #include "hmbdc/Config.hpp"
7 
8 #include <utility>
9 #include <functional>
10 #include <limits>
11 #include <stdexcept>
12 #include <cstddef>
13 
14 namespace hmbdc { namespace pattern {
16  using Sequence = HMBDC_SEQ_TYPE;
18  using value_type = void *;
20  enum {
21  max_parallel_consumer = 0xffff
22  };
23 
24  template <typename Allocator = os::DefaultAllocator>
25  MonoLockFreeBuffer(size_t, uint32_t, Allocator& allocator = os::DefaultAllocator::instance);
27 
28  size_t maxItemSize() const;
29  size_t capacity() const;
30  void put(void const*, size_t sizeHint = 0);
31  template <typename T> void put(T const& item) {put(&item, sizeof(item));}
32  template <typename T> void putSome(T const& item) {put(&item, std::min(sizeof(item), maxItemSize()));}
33  template <typename T, typename ...Args>
34  void putInPlace(Args&&... args) {
35  auto s = claim();
36  new (*s) T(std::forward<Args>(args)...);
37  commit(s);
38  }
39  template <typename T, typename ...Args>
40  bool tryPutInPlace(Args&&... args) {
41  auto s = tryClaim();
42  if (!s) return false;
43  new (*s) T(std::forward<Args>(args)...);
44  commit(s);
45  return true;
46  }
47 
48  bool tryPut(void const*, size_t sizeHint = 0);
49  bool isFull() const;
50  Sequence readSeq() const;
51  iterator claim();
52  iterator tryClaim();
53  iterator claim(size_t);
54  iterator tryClaim(size_t);
55  iterator killClaim();
56  iterator killClaim(size_t);
57  void commit(iterator);
58  void commit(iterator, size_t);
59  void take(void *, size_t = 0);
60  bool tryTake(void *, size_t = 0);
61  iterator peek();
62  size_t peek(iterator&, iterator&
63  , size_t maxPeekSize = std::numeric_limits<size_t>::max());
64  /**
65  * @brief if size not matching - please refer to the impl for details
66  */
67  void wasteAfterPeek(iterator, size_t, bool = false);
68  size_t remainingSize() const;
69  size_t parallelConsumerAlive() const {return 1;}
70  void reset();
71 
72  static
73  size_t footprint(size_t, uint32_t);
74 private:
75  std::ptrdiff_t impl_;
76  std::function<void()> freer_;
77 };
78 }}
Definition: MonoLockFreeBuffer.hpp:15
Definition: LockFreeBufferMisc.hpp:23
void wasteAfterPeek(iterator, size_t, bool=false)
if size not matching - please refer to the impl for details
Definition: LockFreeBufferMisc.hpp:89