iceoryx_doc  1.0.1
index_queue.hpp
1 // Copyright (c) 2019, 2020 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 
17 #ifndef IOX_UTILS_LOCKFREE_QUEUE_INDEX_QUEUE_HPP
18 #define IOX_UTILS_LOCKFREE_QUEUE_INDEX_QUEUE_HPP
19 
20 #include "iceoryx_utils/cxx/optional.hpp"
21 #include "iceoryx_utils/internal/concurrent/lockfree_queue/buffer.hpp"
22 #include "iceoryx_utils/internal/concurrent/lockfree_queue/cyclic_index.hpp"
23 
24 #include <atomic>
25 #include <type_traits>
26 
27 namespace iox
28 {
29 namespace concurrent
30 {
31 template <typename ElementType, uint64_t Capacity>
32 class LockFreeQueue;
33 
34 template <typename ElementType, uint64_t Capacity>
35 class ResizeableLockFreeQueue;
36 
38 template <uint64_t Capacity, typename ValueType = uint64_t>
40 {
41  public:
42  static_assert(std::is_unsigned<ValueType>::value, "ValueType must be an unsigned integral type");
43 
44  using value_t = ValueType;
45 
47  {
48  };
49 
51  {
52  };
53 
54  static constexpr ConstructFull_t ConstructFull{};
55  static constexpr ConstructEmpty_t ConstructEmpty{};
56 
57  ~IndexQueue() = default;
58  IndexQueue(const IndexQueue&) = delete;
59  IndexQueue(IndexQueue&&) = delete;
60  IndexQueue& operator=(const IndexQueue&) = delete;
61  IndexQueue& operator=(IndexQueue&&) = delete;
62 
64  IndexQueue(ConstructEmpty_t = ConstructEmpty) noexcept;
65 
67  IndexQueue(ConstructFull_t) noexcept;
68 
72  constexpr uint64_t capacity() const noexcept;
73 
79  bool empty() const noexcept;
80 
86  void push(const ValueType index) noexcept;
87 
90  cxx::optional<ValueType> pop() noexcept;
91 
94  cxx::optional<ValueType> popIfFull() noexcept;
95 
100  cxx::optional<ValueType> popIfSizeIsAtLeast(uint64_t size) noexcept;
101 
102  private:
103  template <typename ElementType, uint64_t Cap>
104  friend class LockFreeQueue;
105 
106  template <typename ElementType, uint64_t Cap>
107  friend class ResizeableLockFreeQueue;
108 
109  // remark: a compile time check whether Index is actually lock free would be nice
110  // note: there is a way with is_always_lock_free in c++17 (which we cannot use here)
111  using Index = CyclicIndex<Capacity>;
112  using Cell = std::atomic<Index>;
113 
118  Cell m_cells[Capacity];
119 
120  std::atomic<Index> m_readPosition;
121  std::atomic<Index> m_writePosition;
122 
127  Index loadvalueAt(const Index& position, std::memory_order memoryOrder = std::memory_order_relaxed) const;
128 
132  bool pop(ValueType& index) noexcept;
133 
138  bool popIfSizeIsAtLeast(uint64_t minSize, ValueType& index) noexcept;
139 
143  bool popIfFull(ValueType& index) noexcept;
144 };
145 } // namespace concurrent
146 } // namespace iox
147 
148 #include "index_queue.inl"
149 
150 #endif // IOX_UTILS_LOCKFREE_QUEUE_INDEX_QUEUE_HPP
lockfree queue capable of storing indices 0,1,... Capacity-1
Definition: index_queue.hpp:40
cxx::optional< ValueType > popIfFull() noexcept
pop an index from the queue in FIFO order if the queue is full
Definition: index_queue.inl:288
cxx::optional< ValueType > pop() noexcept
pop an index from the queue in FIFO order if the queue not empty
Definition: index_queue.inl:277
void push(const ValueType index) noexcept
push index into the queue in FIFO order
Definition: index_queue.inl:50
constexpr uint64_t capacity() const noexcept
get the capacity of the IndexQueue
Definition: index_queue.inl:44
bool empty() const noexcept
check whether the queue is empty
Definition: index_queue.inl:310
cxx::optional< ValueType > popIfSizeIsAtLeast(uint64_t size) noexcept
pop an index from the queue in FIFO order if the queue contains at least a specified number number of...
Definition: index_queue.inl:299
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: index_queue.hpp:51
Definition: index_queue.hpp:47