iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConcurrentQueueImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_DESIGN_DETAILS_CONCURRENTQUEUE_IMPL_H
36 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_CONCURRENTQUEUE_IMPL_H
37 
38 template <class T>
39 struct nrt::ConcurrentQueue<T>::Node
40 {
41  std::shared_ptr<T> data;
42  std::unique_ptr<Node> next;
43 };
44 
45 template <class T> inline
47  itsHead( new Node() ), itsTail( itsHead.get() )
48 { }
49 
50 template <class T> inline
52 {
53  clear();
54 }
55 
56 template <class T> inline
57 std::shared_ptr<T> nrt::ConcurrentQueue<T>::tryPop()
58 {
59  std::unique_ptr<Node> oldHead = tryPopHead();
60  return oldHead ? oldHead->data : std::shared_ptr<T>();
61 }
62 
63 template <class T> inline
65 {
66  std::unique_ptr<Node> oldHead = tryPopHead( value );
67  return oldHead.get();
68 }
69 
70 template <class T> inline
72 {
73  std::unique_ptr<Node> const oldHead = waitPopHead();
74  return oldHead->data;
75 }
76 
77 template <class T> inline
79 {
80  std::unique_ptr<Node> const oldHead = waitPopHead( value );
81 }
82 
83 template <class T> inline
85 {
86  std::shared_ptr<T> newData = std::make_shared<T>( std::move( newValue ) );
87  std::unique_ptr<Node> p( new Node() );
88 
89  {
90  std::lock_guard<std::mutex> lock( itsTailMutex );
91 
92  itsTail->data = newData;
93  Node * const newTail = p.get();
94  itsTail->next = std::move( p );
95  itsTail = newTail;
96  }
97 
98  itsConditionVariable.notify_one();
99 }
100 
101 template <class T> inline
103 {
104  std::lock_guard<std::mutex> lock( itsHeadMutex );
105  return itsHead.get() == getTail();
106 }
107 
108 template <class T> inline
110 {
111  while( tryPop().get() );
112 }
113 
114 template <class T> inline
116 {
117  std::lock_guard<std::mutex> lock( itsTailMutex );
118  return itsTail;
119 }
120 
121 template <class T> inline
122 auto nrt::ConcurrentQueue<T>::popHead() -> std::unique_ptr<Node>
123 {
124  std::unique_ptr<Node> oldHead = std::move( itsHead );
125  itsHead = std::move( oldHead->next );
126  return oldHead;
127 }
128 
129 template <class T> inline
130 std::unique_lock<std::mutex> nrt::ConcurrentQueue<T>::waitForData()
131 {
132  std::unique_lock<std::mutex> lock( itsHeadMutex );
133  itsConditionVariable.wait( lock, [&](){return itsHead.get() != getTail();} );
134  return std::move( lock );
135 }
136 
137 template <class T> inline
138 auto nrt::ConcurrentQueue<T>::waitPopHead() -> std::unique_ptr<Node>
139 {
140  auto lock( waitForData() );
141  return popHead();
142 }
143 
144 template <class T> inline
145 auto nrt::ConcurrentQueue<T>::waitPopHead( T & value ) -> std::unique_ptr<Node>
146 {
147  auto lock( waitForData() );
148  value = std::move( *itsHead->data );
149  return popHead();
150 }
151 
152 template <class T> inline
153 auto nrt::ConcurrentQueue<T>::tryPopHead() -> std::unique_ptr<Node>
154 {
155  std::lock_guard<std::mutex> lock( itsHeadMutex );
156 
157  if( itsHead.get() == getTail() )
158  return std::unique_ptr<Node>();
159 
160  return popHead();
161 }
162 
163 template <class T> inline
164 auto nrt::ConcurrentQueue<T>::tryPopHead( T & value ) -> std::unique_ptr<Node>
165 {
166  std::lock_guard<std::mutex> lock( itsHeadMutex );
167 
168  if( itsHead.get() == getTail() )
169  return std::unique_ptr<Node>();
170 
171  value = std::move( *itsHead->data );
172  return popHead();
173 }
174 
175 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_CONCURRENTQUEUE_IMPL_H