DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
StdDeQueue.hpp
Go to the documentation of this file.
1#ifndef IOMANAGER_INCLUDE_IOMANAGER_STDDEQUEUE_HPP_
2#define IOMANAGER_INCLUDE_IOMANAGER_STDDEQUEUE_HPP_
3
16
17#include <atomic>
18#include <cassert>
19#include <chrono>
20#include <condition_variable>
21#include <deque>
22#include <functional>
23#include <iostream>
24#include <mutex>
25#include <sstream>
26#include <string>
27#include <thread>
28#include <type_traits>
29#include <utility>
30
31namespace dunedaq::iomanager {
36template<class T>
37class StdDeQueue : public Queue<T>
38{
39public:
40 using value_t = T;
42
47 explicit StdDeQueue(const std::string& name, size_t capacity);
48
49 bool can_pop() const noexcept override { return this->get_num_elements() > 0; }
50 void pop(value_t& val, const duration_t&) override; // Throws QueueTimeoutExpired if a timeout occurs
51 bool try_pop(value_t& val, const duration_t&) override;
52
53 bool can_push() const noexcept override { return this->get_num_elements() < this->get_capacity(); }
54 void push(value_t&&, const duration_t&) override; // Throws QueueTimeoutExpired if a timeout occurs
55 bool try_push(value_t&&, const duration_t&) override;
56
57 size_t get_capacity() const override { return m_capacity; }
58
59 size_t get_num_elements() const override { return m_size.load(std::memory_order_acquire); }
60
61 // Delete the copy and move operations since various member data instances
62 // (e.g., of std::mutex or of std::atomic) aren't copyable or movable
63
64 StdDeQueue(const StdDeQueue&) = delete;
65 StdDeQueue& operator=(const StdDeQueue&) = delete;
66 StdDeQueue(StdDeQueue&&) = delete;
68
69private:
70 void try_lock_for(std::unique_lock<std::mutex>&, const duration_t&);
71
72 std::deque<value_t> m_deque;
73 size_t m_capacity;
74 std::atomic<size_t> m_size = 0;
75
76 std::mutex m_mutex;
77 std::condition_variable m_no_longer_full;
78 std::condition_variable m_no_longer_empty;
79};
80
81} // namespace dunedaq::iomanager
82
83#include "detail/StdDeQueue.hxx"
84
85#endif // IOMANAGER_INCLUDE_IOMANAGER_STDDEQUEUE_HPP_
Implementations of the Queue class are responsible for relaying data between DAQModules within a DAQ ...
Definition Queue.hpp:40
std::chrono::milliseconds duration_t
Base duration type for timeouts.
Definition Queue.hpp:43
A Queue Implementation that uses a std::deque as its backend.
void pop(value_t &val, const duration_t &) override
Pop the first value off of the queue.
StdDeQueue & operator=(const StdDeQueue &)=delete
StdDeQueue is not copy-assignable.
std::deque< value_t > m_deque
void push(value_t &&, const duration_t &) override
Push a value onto the Queue.
void try_lock_for(std::unique_lock< std::mutex > &, const duration_t &)
std::atomic< size_t > m_size
bool can_pop() const noexcept override
Determine whether the Queue may be popped from.
StdDeQueue(const std::string &name, size_t capacity)
StdDeQueue Constructor.
Definition StdDeQueue.hxx:7
bool can_push() const noexcept override
Determine whether the Queue may be pushed onto.
bool try_pop(value_t &val, const duration_t &) override
typename Queue< T >::duration_t duration_t
Type used for expressing timeouts.
std::condition_variable m_no_longer_full
bool try_push(value_t &&, const duration_t &) override
StdDeQueue(StdDeQueue &&)=delete
StdDeQueue is not move-constructible.
std::condition_variable m_no_longer_empty
StdDeQueue(const StdDeQueue &)=delete
StdDeQueue is not copy-constructible.
T value_t
Type of data stored in the StdDeQueue.
size_t get_num_elements() const override
StdDeQueue & operator=(StdDeQueue &&)=delete
StdDeQueue is not move-assignable.
size_t get_capacity() const override
Get the capacity (max size) of the queue.