Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConcurrentCircularBuffer.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, The Cinder Project
3  All rights reserved.
4 
5  This code is designed for use with the Cinder C++ library, http://libcinder.org
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8  the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and
11  the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13  the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 
26 #pragma once
27 
28 #include <boost/circular_buffer.hpp>
29 #include <boost/noncopyable.hpp>
30 #include "cinder/Thread.h"
31 
32 namespace cinder {
33 
34 template<typename T>
35 class ConcurrentCircularBuffer : public boost::noncopyable {
36  public:
37  typedef boost::circular_buffer<T> container_type;
38  typedef typename container_type::size_type size_type;
39  typedef typename container_type::value_type value_type;
40  typedef typename boost::call_traits<value_type>::param_type param_type;
41 
42  explicit ConcurrentCircularBuffer( size_type capacity )
43  : mNumUnread( 0 ), mContainer( capacity ), mCanceled( false )
44  {}
45 
46  void pushFront( param_type item ) {
47  // param_type represents the "best" way to pass a parameter of type value_type to a method
48  std::unique_lock<std::mutex> lock( mMutex );
49  while( ! is_not_full_impl() && ! mCanceled ) {
50  mNotFullCond.wait( lock );
51  }
52  if( mCanceled )
53  return;
54  mContainer.push_front( item );
55  ++mNumUnread;
56  mNotEmptyCond.notify_one();
57  }
58 
59  void popBack(value_type* pItem) {
60  std::unique_lock<std::mutex> lock( mMutex );
61  while( ! is_not_empty_impl() && ! mCanceled ) {
62  mNotEmptyCond.wait( lock );
63  }
64  if( mCanceled )
65  return;
66  *pItem = mContainer[--mNumUnread];
67  mNotFullCond.notify_one();
68  }
69 
71  bool tryPushFront( param_type item ) {
72  // param_type represents the "best" way to pass a parameter of type value_type to a method
73  std::unique_lock<std::mutex> lock( mMutex );
74  if( ! is_not_full_impl() )
75  return false;
76  mContainer.push_front( item );
77  ++mNumUnread;
78  mNotEmptyCond.notify_one();
79  return true;
80  }
81 
83  bool tryPopBack( value_type* pItem ) {
84  std::unique_lock<std::mutex> lock( mMutex );
85  if( ! is_not_empty_impl() )
86  return false;
87  *pItem = mContainer[--mNumUnread];
88  mNotFullCond.notify_one();
89  return true;
90  }
91 
92  bool isNotEmpty() {
93  std::unique_lock<std::mutex> lock( mMutex );
94  return is_not_empty_impl();
95  }
96 
97  bool isNotFull() {
98  std::unique_lock<std::mutex> lock( mMutex );
99  return is_not_full_impl();
100  }
101 
102  void cancel() {
103  std::lock_guard<std::mutex> lock( mMutex );
104  mCanceled = true;
105  mNotFullCond.notify_all();
106  mNotEmptyCond.notify_all();
107  }
108 
110  size_t size() const { return (size_t)mContainer.capacity(); }
111 
112  private:
113  bool is_not_empty_impl() const { return mNumUnread > 0; }
114  bool is_not_full_impl() const { return mNumUnread < mContainer.capacity(); }
115 
116  size_type mNumUnread;
117  container_type mContainer;
118  std::mutex mMutex;
119  std::condition_variable mNotEmptyCond;
120  std::condition_variable mNotFullCond;
121  bool mCanceled;
122 };
123 
124 } // namespace cinder
bool tryPushFront(param_type item)
Attempts to push item to the front of the buffer, but does not wait for an availability. Returns success as true or false.
Definition: ConcurrentCircularBuffer.h:71
container_type::value_type value_type
Definition: ConcurrentCircularBuffer.h:39
size_t size() const
Returns the number of items the buffer can hold.
Definition: ConcurrentCircularBuffer.h:110
Definition: ConcurrentCircularBuffer.h:35
bool isNotEmpty()
Definition: ConcurrentCircularBuffer.h:92
boost::call_traits< value_type >::param_type param_type
Definition: ConcurrentCircularBuffer.h:40
bool tryPopBack(value_type *pItem)
Attempts to pop an item from the back of the buffer, but does not wait for an availability. Returns success as true or false.
Definition: ConcurrentCircularBuffer.h:83
void popBack(value_type *pItem)
Definition: ConcurrentCircularBuffer.h:59
void cancel()
Definition: ConcurrentCircularBuffer.h:102
boost::circular_buffer< T > container_type
Definition: ConcurrentCircularBuffer.h:37
void pushFront(param_type item)
Definition: ConcurrentCircularBuffer.h:46
ConcurrentCircularBuffer(size_type capacity)
Definition: ConcurrentCircularBuffer.h:42
container_type::size_type size_type
Definition: ConcurrentCircularBuffer.h:38
bool isNotFull()
Definition: ConcurrentCircularBuffer.h:97