2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include "semaphore.h"
26 #include <boost/thread/mutex.hpp>
27 #include <boost/noncopyable.hpp>
32 * Adapts an unbounded non-blocking concurrent queue into a blocking bounded
35 * The queue Q to adapt must support the following use cases:
47 * It must also guarantee thread safety for those operations.
50 class blocking_bounded_queue_adapter : boost::noncopyable
53 typedef typename Q::value_type value_type;
54 typedef unsigned int size_type;
56 mutable boost::mutex capacity_mutex_;
58 semaphore space_available_ = capacity_;
59 semaphore elements_available_ = 0;
65 * @param capacity The capacity of the queue.
67 blocking_bounded_queue_adapter(size_type capacity)
73 * Push an element to the queue, block until room is available.
75 * @param element The element to push.
77 void push(const value_type& element)
79 space_available_.acquire();
80 push_after_room_reserved(element);
84 * Try to push an element to the queue, returning immediately if room is not
87 * @param element The element to push.
89 * @return true if there was room for the element.
91 bool try_push(const value_type& element)
93 bool room_available = space_available_.try_acquire();
98 push_after_room_reserved(element);
104 * Pop an element from the queue, will block until an element is available.
106 * @param element The element to store the result in.
108 void pop(value_type& element)
110 elements_available_.acquire();
111 queue_.try_pop(element);
112 space_available_.release();
116 * Try to pop an element from the queue, returning immediately if no
117 * element is available.
119 * @param element The element to store the result in.
121 * @return true if an element was popped.
123 bool try_pop(value_type& element)
125 if (!elements_available_.try_acquire())
128 queue_.try_pop(element);
129 space_available_.release();
135 * Modify the capacity of the queue. May block if reducing the capacity.
137 * @param capacity The new capacity.
139 void set_capacity(size_type capacity)
141 boost::unique_lock<boost::mutex> lock (capacity_mutex_);
143 if (capacity_ < capacity)
145 auto to_grow_with = capacity - capacity_;
147 space_available_.release(to_grow_with);
149 else if (capacity_ > capacity)
151 auto to_shrink_with = capacity_ - capacity;
153 // Will block until the desired capacity has been reached.
154 space_available_.acquire(to_shrink_with);
157 capacity_ = capacity;
161 * @return the current capacity of the queue.
163 size_type capacity() const
165 boost::unique_lock<boost::mutex> lock (capacity_mutex_);
171 * @return the current size of the queue (may have changed at the time of
174 size_type size() const
176 return elements_available_.permits();
179 void push_after_room_reserved(const value_type& element)
183 queue_.push(element);
187 space_available_.release();
192 elements_available_.release();