iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConcurrentQueue.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 
36 #ifndef INCLUDE_NRT_CORE_DESIGN_CONCURRENTQUEUE_H
37 #define INCLUDE_NRT_CORE_DESIGN_CONCURRENTQUEUE_H
38 
39 #include <memory>
40 #include <atomic>
41 #include <mutex>
42 #include <condition_variable>
43 
44 namespace nrt
45 {
46  //! A thread safe queue with blocking and waiting
47  /*! This class provides a thread safe queue that blocks and waits
48  while pushing and popping data.
49 
50  Based off of an implementation in C++ Concurrency in Action: Practical Multithreading
51  by Anthony Williams */
52  template <class T>
54  {
55  private:
56  //! Represents a node held by the queue
57  struct Node;
58 
59  public:
60  //! Constructs an empty queue
62 
63  //! Queues cannot be copy constructed
64  ConcurrentQueue( ConcurrentQueue const & other ) = delete;
65 
66  //! Queues cannot be copy assigned
67  ConcurrentQueue & operator=( ConcurrentQueue const & other ) = delete;
68 
69  //! Destructor
71 
72  //! Pops a value off of the queue if available, otherwise returns a nullptr
73  std::shared_ptr<T> tryPop();
74 
75  //! Pops a value off of the queue if available, returning true if successful
76  bool tryPop( T & value );
77 
78  //! Pops a value off of the queue, waiting until a value is available
79  std::shared_ptr<T> waitAndPop();
80 
81  //! Pops a value off of the queue, waiting until a value is available
82  void waitAndPop( T & value );
83 
84  //! Pushes a new value onto the queue
85  void push( T newValue );
86 
87  //! Checks if the queue is empty
88  bool empty();
89 
90  //! Empties the queue
91  void clear();
92 
93  protected:
94  //! Gets the current tail node
95  Node * getTail();
96 
97  //! Pops the head node
98  std::unique_ptr<Node> popHead();
99 
100  //! Creates a lock used to sleep until data is available
101  std::unique_lock<std::mutex> waitForData();
102 
103  //! Pops the head node, sleeping until data is available
104  std::unique_ptr<Node> waitPopHead();
105 
106  //! Pops the head node, sleeping until data is available
107  std::unique_ptr<Node> waitPopHead( T & value );
108 
109  //! Pops the head node if it exists, otherwise returns nullptr
110  std::unique_ptr<Node> tryPopHead();
111 
112  //! Pops the head node if it exists, otherwise returns nullptr
113  std::unique_ptr<Node> tryPopHead( T & value );
114 
115  private:
116  std::mutex itsHeadMutex; //!< Controls access to the head
117  std::unique_ptr<Node> itsHead; //!< The head node
118 
119  std::mutex itsTailMutex; //!< Controls access to the tail
120  Node * itsTail; //!< The tail node
121 
122  std::condition_variable itsConditionVariable; //!< Allows the queue to sleep while waiting for data
123  };
124 }
125 
127 
128 #endif // INCLUDE_NRT_CORE_DESIGN_CONCURRENTQUEUE_H