JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
BoundedBufferImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 // ##############################################################################################################
21 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
23  itsSize(siz), itsEmptySemaphore(siz), itsFullSemaphore(0)
24 { }
25 
26 // ##############################################################################################################
27 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
29 {
30  itsEmptySemaphore.decrement(1);
31 
32  itsMutex.lock();
33  itsQueue.push(val);
34  itsMutex.unlock();
35 
36  itsFullSemaphore.increment(1);
37 }
38 
39 // ##############################################################################################################
40 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
42 {
43  itsEmptySemaphore.decrement(1);
44 
45  itsMutex.lock();
46  itsQueue.push(std::move(val));
47  itsMutex.unlock();
48 
49  itsFullSemaphore.increment(1);
50 }
51 
52 // ##############################################################################################################
53 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
55 {
56  itsFullSemaphore.decrement(1);
57 
58  itsMutex.lock();
59  T val = itsQueue.front();
60  itsQueue.pop();
61  itsMutex.unlock();
62 
63  itsEmptySemaphore.increment(1);
64 
65  return val;
66 }
67 
68 // ##############################################################################################################
69 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
71 { return itsFullSemaphore.count(); }
72 
73 // ##############################################################################################################
74 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
76 { return itsSize; }
77 
78 // ##############################################################################################################
79 template <typename T, jevois::BlockingBehavior WhenFull, jevois::BlockingBehavior WhenEmpty> inline
81 {
82  itsMutex.lock();
83 
84  while (itsQueue.size())
85  {
86  itsFullSemaphore.decrement(1);
87  itsQueue.pop();
88  itsEmptySemaphore.increment(1);
89  }
90 
91  itsMutex.unlock();
92 }
93 
BoundedBuffer(size_t const siz)
Create a new BoundedBuffer with no data and a given size.
void push(T const &val)
Push a new data element into the buffer, potentially sleeping or throwing if buffer is full...
size_t size() const
Max (allocated at construction) size of the buffer.
size_t filled_size() const
Current number of items actually in the buffer.
void clear()
Clear all contents, resetting filled_size() to zero (size() remains unchanged at the max possible siz...
T pop()
Pop oldest data element off of the buffer, potentially sleeping until one is available or throwing if...