UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
synchronized_queue.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2013-2023 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef SYNCHRONIZED_QUEUE_H_
39#define SYNCHRONIZED_QUEUE_H_
40
41#include <condition_variable>
42#include <mutex>
43#include <queue>
44#include <utility>
45
46struct msg {
47 virtual ~msg() {}
48};
49
50struct msg_quit : public msg {};
51
61template<typename T = struct msg *, int max_len = 1>
63public:
64 int size()
65 {
66 std::unique_lock<std::mutex> l(m_lock);
67 return m_queue.size();
68 }
69
70 void push(T const & message)
71 {
72 std::unique_lock<std::mutex> l(m_lock);
73 if (max_len != -1) {
74 m_queue_decremented.wait(l, [this]{return m_queue.size() < (unsigned int) max_len;});
75 }
76 m_queue.push(message);
77 l.unlock();
78 m_queue_incremented.notify_one();
79 }
80
81 void push(T && message)
82 {
83 std::unique_lock<std::mutex> l(m_lock);
84 if (max_len != -1) {
85 m_queue_decremented.wait(l, [this]{return m_queue.size() < (unsigned int) max_len;});
86 }
87 m_queue.push(std::move(message));
88 l.unlock();
89 m_queue_incremented.notify_one();
90 }
91
92 T pop(bool nonblocking = false)
93 {
94 std::unique_lock<std::mutex> l(m_lock);
95 if (m_queue.size() == 0 && nonblocking) {
96 return T();
97 }
98
99 m_queue_incremented.wait(l, [this]{return m_queue.size() > 0;});
100 T ret = std::move(m_queue.front());
101 m_queue.pop();
102
103 l.unlock();
104 m_queue_decremented.notify_one();
105 return ret;
106 }
107
108 template<typename Rep, typename Period>
109 bool timed_pop(T& result, std::chrono::duration<Rep, Period> const& timeout)
110 {
111 std::unique_lock<std::mutex> l(m_lock);
112 if (!m_queue_incremented.wait_for(l, timeout, [this]{return m_queue.size() > 0;})) {
113 return false;
114 }
115 result = std::move(m_queue.front());
116 m_queue.pop();
117 l.unlock();
118 m_queue_decremented.notify_one();
119 return true;
120 }
121
122private:
123 std::queue<T> m_queue;
124 std::mutex m_lock;
125 std::condition_variable m_queue_decremented;
126 std::condition_variable m_queue_incremented;
127};
128
129#ifndef NO_EXTERN_MSGQ_MSG
130extern template class synchronized_queue<msg *, -1>;
131extern template class synchronized_queue<msg *, 1>;
132#endif
133
134#endif // SYNCHRONIZED_QUEUE_H_
135
simple blocking synchronized queue
T pop(bool nonblocking=false)
void push(T &&message)
bool timed_pop(T &result, std::chrono::duration< Rep, Period > const &timeout)
std::queue< T > m_queue
std::condition_variable m_queue_decremented
std::condition_variable m_queue_incremented
void push(T const &message)
virtual ~msg()