]> git.sesse.net Git - casparcg/blob - common/blocking_bounded_queue_adapter.h
Refactored executor to support try_begin_invoke which does not block until room is...
[casparcg] / common / blocking_bounded_queue_adapter.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include "semaphore.h"
25
26 #include <boost/thread/mutex.hpp>
27 #include <boost/noncopyable.hpp>
28
29 namespace caspar {
30
31 /**
32  * Adapts an unbounded non-blocking concurrent queue into a blocking bounded
33  * concurrent queue.
34  *
35  * The queue Q to adapt must support the following use cases:
36  *
37  * Q q;
38  * Q::value_type elem;
39  * q.push(elem);
40  *
41  * and:
42  *
43  * Q q;
44  * Q::value_type elem;
45  * q.try_pop(elem);
46  *
47  * It must also guarantee thread safety for those operations.
48  */
49 template<class Q>
50 class blocking_bounded_queue_adapter : boost::noncopyable
51 {
52 public:
53         typedef typename Q::value_type value_type;
54         typedef unsigned int size_type;
55 private:
56         semaphore space_available_;
57         semaphore elements_available_;
58         Q queue_;
59         mutable boost::mutex capacity_mutex_;
60         size_type capacity_;
61 public:
62         /**
63          * Constructor.
64          *
65          * @param capacity The capacity of the queue.
66          */
67         blocking_bounded_queue_adapter(size_type capacity)
68                 : space_available_(capacity)
69                 , elements_available_(0)
70                 , capacity_(capacity)
71         {
72         }
73
74         /**
75          * Push an element to the queue, block until room is available.
76          *
77          * @param element The element to push.
78          */
79         void push(const value_type& element)
80         {
81                 space_available_.acquire();
82                 push_after_room_reserved(element);
83         }
84
85         /**
86          * Try to push an element to the queue, returning immediately if room is not
87          * available.
88          *
89          * @param element The element to push.
90          *
91          * @return true if there was room for the element.
92          */
93         bool try_push(const value_type& element)
94         {
95                 bool room_available = space_available_.try_acquire();
96
97                 if (!room_available)
98                         return false;
99
100                 push_after_room_reserved(element);
101
102                 return true;
103         }
104
105         /**
106          * Pop an element from the queue, will block until an element is available.
107          *
108          * @param element The element to store the result in.
109          */
110         void pop(value_type& element)
111         {
112                 elements_available_.acquire();
113                 queue_.try_pop(element);
114                 space_available_.release();
115         }
116
117         /**
118          * Try to pop an element from the queue, returning immediately if no
119          * element is available.
120          *
121          * @param element The element to store the result in.
122          *
123          * @return true if an element was popped.
124          */
125         bool try_pop(value_type& element)
126         {
127                 if (!elements_available_.try_acquire())
128                         return false;
129
130                 queue_.try_pop(element);
131                 space_available_.release();
132
133                 return true;
134         }
135
136         /**
137          * Modify the capacity of the queue. May block if reducing the capacity.
138          *
139          * @param capacity The new capacity.
140          */
141         void set_capacity(size_type capacity)
142         {
143                 boost::mutex::scoped_lock lock (capacity_mutex_);
144
145                 if (capacity_ < capacity)
146                 {
147                         auto to_grow_with = capacity - capacity_;
148
149                         space_available_.release(to_grow_with);
150                 }
151                 else if (capacity_ > capacity)
152                 {
153                         auto to_shrink_with = capacity_ - capacity;
154
155                         // Will block until the desired capacity has been reached.
156                         space_available_.acquire(to_shrink_with);
157                 }
158
159                 capacity_ = capacity;
160         }
161
162         /**
163          * @return the current capacity of the queue.
164          */
165         size_type capacity() const
166         {
167                 boost::mutex::scoped_lock lock (capacity_mutex_);
168
169                 return capacity_;
170         }
171
172         /**
173          * @return the current size of the queue (may have changed at the time of
174          *         returning).
175          */
176         size_type size() const
177         {
178                 return elements_available_.permits();
179         }
180 private:
181         void push_after_room_reserved(const value_type& element)
182         {
183                 try
184                 {
185                         queue_.push(element);
186                 }
187                 catch (...)
188                 {
189                         space_available_.release();
190
191                         throw;
192                 }
193
194                 elements_available_.release();
195         }
196 };
197
198 }