Skip to content

iceoryx_utils/internal/concurrent/trigger_queue.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::concurrent

Classes🔗

Name
class iox::concurrent::LockFreeQueue
implements a lock free queue (i.e. container with FIFO order) of elements of type T with a fixed Capacity
class iox::concurrent::ResizeableLockFreeQueue
implements a lock free queue (i.e. container with FIFO order) of elements of type T with a maximum capacity MaxCapacity. The capacity can be defined to be anything between 0 and MaxCapacity at construction time or later at runtime using setCapacity. This is even possible while concurrent push and pop operations are executed, i.e. the queue does not have to be empty. Only one thread will succeed setting its desired capacity if there are more threads trying to change the capacity at the same time (it is unpredictable which thread).
struct iox::concurrent::QueueAdapter
struct iox::concurrent::QueueAdapter< T, Capacity, LockFreeQueue >
struct iox::concurrent::QueueAdapter< T, Capacity, ResizeableLockFreeQueue >
class iox::concurrent::TriggerQueue
TriggerQueue is behaves exactly like a normal queue (fifo) except that this queue is threadsafe and offers a blocking push which blocks the the caller until the queue has space for at least one element which can be pushed.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP
#define IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP

#include "iceoryx_utils/cxx/optional.hpp"

#include <atomic>
#include <cstdint>
#include <thread>

namespace iox
{
namespace concurrent
{
template <typename ElementType, uint64_t Capacity>
class LockFreeQueue;
template <typename ElementType, uint64_t Capacity>
class ResizeableLockFreeQueue;

template <typename T, uint64_t Capacity, template <typename, uint64_t> class Queue>
struct QueueAdapter
{
    static bool push(Queue<T, Capacity>& queue, const T& in) noexcept;
};

template <typename T, uint64_t Capacity>
struct QueueAdapter<T, Capacity, LockFreeQueue>
{
    static bool push(LockFreeQueue<T, Capacity>& queue, const T& in) noexcept;
};

template <typename T, uint64_t Capacity>
struct QueueAdapter<T, Capacity, ResizeableLockFreeQueue>
{
    static bool push(ResizeableLockFreeQueue<T, Capacity>& queue, const T& in) noexcept;
};


template <typename T, uint64_t Capacity, template <typename, uint64_t> class QueueType>
class TriggerQueue
{
  public:
    using ValueType = T;
    static constexpr uint64_t CAPACITY = Capacity;

    bool push(const T& in) noexcept;

    cxx::optional<T> pop() noexcept;

    bool empty() const noexcept;

    uint64_t size() const noexcept;

    static constexpr uint64_t capacity() noexcept;

    void destroy() noexcept;

    bool setCapacity(const uint64_t capacity) noexcept;

  private:
    QueueType<T, Capacity> m_queue;
    std::atomic_bool m_toBeDestroyed{false};
};
} // namespace concurrent
} // namespace iox

#include "iceoryx_utils/internal/concurrent/trigger_queue.inl"

#endif // IOX_UTILS_CONCURRENT_TRIGGER_QUEUE_HPP

Updated on 17 June 2021 at 11:15:26 CEST