iceoryx_doc  1.0.1
trigger_queue.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP
18 #define IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP
19 
20 #include "iceoryx_utils/cxx/optional.hpp"
21 
22 #include <atomic>
23 #include <cstdint>
24 #include <thread>
25 
26 namespace iox
27 {
28 namespace concurrent
29 {
30 template <typename ElementType, uint64_t Capacity>
31 class LockFreeQueue;
32 template <typename ElementType, uint64_t Capacity>
33 class ResizeableLockFreeQueue;
34 
35 template <typename T, uint64_t Capacity, template <typename, uint64_t> class Queue>
37 {
38  static bool push(Queue<T, Capacity>& queue, const T& in) noexcept;
39 };
40 
41 template <typename T, uint64_t Capacity>
42 struct QueueAdapter<T, Capacity, LockFreeQueue>
43 {
44  static bool push(LockFreeQueue<T, Capacity>& queue, const T& in) noexcept;
45 };
46 
47 template <typename T, uint64_t Capacity>
49 {
50  static bool push(ResizeableLockFreeQueue<T, Capacity>& queue, const T& in) noexcept;
51 };
52 
53 
58 template <typename T, uint64_t Capacity, template <typename, uint64_t> class QueueType>
60 {
61  public:
62  using ValueType = T;
63  static constexpr uint64_t CAPACITY = Capacity;
64 
69  bool push(const T& in) noexcept;
70 
75  cxx::optional<T> pop() noexcept;
76 
78  bool empty() const noexcept;
79 
81  uint64_t size() const noexcept;
82 
84  static constexpr uint64_t capacity() noexcept;
85 
89  void destroy() noexcept;
90 
93  bool setCapacity(const uint64_t capacity) noexcept;
94 
95  private:
96  QueueType<T, Capacity> m_queue;
97  std::atomic_bool m_toBeDestroyed{false};
98 };
99 } // namespace concurrent
100 } // namespace iox
101 
102 #include "iceoryx_utils/internal/concurrent/trigger_queue.inl"
103 
104 #endif // IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP
implements a lock free queue (i.e. container with FIFO order) of elements of type T with a fixed Capa...
Definition: lockfree_queue.hpp:35
implements a lock free queue (i.e. container with FIFO order) of elements of type T with a maximum ca...
Definition: resizeable_lockfree_queue.hpp:48
TriggerQueue is behaves exactly like a normal queue (fifo) except that this queue is threadsafe and o...
Definition: trigger_queue.hpp:60
uint64_t size() const noexcept
Returns the number of elements which are currently in the queue.
Definition: trigger_queue.inl:69
bool push(const T &in) noexcept
Pushs an element into the trigger queue. If the queue is full it blocks until there is space again....
Definition: trigger_queue.inl:46
bool setCapacity(const uint64_t capacity) noexcept
resizes the queue.
Definition: trigger_queue.inl:87
cxx::optional< T > pop() noexcept
If the queue already contains an element it writes the contents of that element in out and returns tr...
Definition: trigger_queue.inl:57
bool empty() const noexcept
Returns true if the queue is empty, otherwise false.
Definition: trigger_queue.inl:63
void destroy() noexcept
when someone is waiting in push since the queue is full it unblocks push. after that call it is impos...
Definition: trigger_queue.inl:81
static constexpr uint64_t capacity() noexcept
Returns the capacity of the trigger queue.
Definition: trigger_queue.inl:75
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: trigger_queue.hpp:37