UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
concurrent_queue.hpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2021-2022 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#pragma once
39
40#include <cstdint>
41#include <condition_variable>
42#include <mutex>
43#include <queue>
44
45
47
48constexpr size_t unlimited_size = SIZE_MAX;
49
50template<typename T, size_t max_size = unlimited_size>
52 std::queue<T> queue;
53 mutable std::mutex mutex{};
54 std::condition_variable queue_decremented_cv{};
55 std::condition_variable queue_incremented_cv{};
56
57 void push_and_unlock(std::unique_lock<std::mutex>& lock, T&& item){
58 queue.push(std::move(item));
59 lock.unlock();
60 queue_incremented_cv.notify_one();
61 }
62
63 T pop(bool nonblocking = false)
64 {
65 std::unique_lock<std::mutex> lock{mutex};
66 if (queue.size() == 0 && nonblocking) {
67 return T();
68 }
69
70 queue_incremented_cv.wait(lock, [this]{return queue.size() > 0;});
71 T result = std::move(queue.front());
72 queue.pop();
73
74 lock.unlock();
75 queue_decremented_cv.notify_one();
76 return result;
77 }
78public:
80 return pop(true);
81 }
82
84 return pop(false);
85 }
86
87 template<typename Rep, typename Period>
88 T timed_pop(std::chrono::duration<Rep, Period> timeout){
89 std::unique_lock<std::mutex> lock{mutex};
90
91 queue_incremented_cv.wait_for(lock, timeout, [this]{return queue.size() > 0;});
92 if (queue.empty()){
93 return T{};
94 }
95 T result = std::move(queue.front());
96 queue.pop();
97
98 lock.unlock();
99 queue_decremented_cv.notify_one();
100 return result;
101 }
102
104 std::unique_lock lock{mutex};
105 T result = {};
106 if (queue.size() >= max_size) {
107 result = std::move(queue.front());
108 queue.pop();
109 }
110 push_and_unlock(lock, std::move(item));
111 return result;
112 }
113
114 bool try_push(T item){
115 std::unique_lock lock{mutex};
116 if (queue.size() >= max_size) {
117 return false;
118 }
119 push_and_unlock(lock, std::move(item));
120 return true;
121 }
122
124 std::unique_lock lock{mutex};
125 if (max_size != unlimited_size) {
126 queue_decremented_cv.wait(lock, [this]{return queue.size() < max_size;});
127 }
128 push_and_unlock(lock, std::move(item));
129 }
130};
131
132} //namespace vulkan_display_detail
133
134
135
T timed_pop(std::chrono::duration< Rep, Period > timeout)
void push_and_unlock(std::unique_lock< std::mutex > &lock, T &&item)
constexpr size_t unlimited_size
Definition hd-rum.c:27