Orxonox  0.0.5 Codename: Arcturus
TclThreadList.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Fabian 'x3n' Landau
24  * Co-authors:
25  * ...
26  *
27  */
28 
35 #ifndef _TclThreadList_H__
36 #define _TclThreadList_H__
37 
38 #include "core/CorePrereqs.h"
39 
40 #include <list>
41 
42 #include <boost/thread/condition_variable.hpp>
43 #include <boost/thread/shared_mutex.hpp>
44 #include <boost/thread/locks.hpp>
45 
46 namespace orxonox
47 {
51  template <class T>
53  {
54  public:
55  void push_front(const T& value);
56  void push_back(const T& value);
57  template <class InputIterator> void insert(typename std::list<T>::iterator position, InputIterator begin, InputIterator end);
58 
59  void wait_and_pop_front(T* value);
60  void wait_and_pop_back(T* value);
61  bool try_pop_front(T* value);
62  bool try_pop_back(T* value);
63  void clear();
64 
65  size_t size() const;
66  bool empty() const;
67  bool is_in(const T& value) const;
68 
72  inline std::list<T>& getList()
73  { return this->list_; }
74 
78  inline const std::list<T>& getList() const
79  { return this->list_; }
80 
84  inline boost::shared_mutex& getMutex() const
85  { return this->mutex_; }
86 
87  private:
88  std::list<T> list_;
89  mutable boost::shared_mutex mutex_;
90  boost::condition_variable_any condition_;
91  };
92 
96  template <class T>
97  void TclThreadList<T>::push_front(const T& value)
98  {
99  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
100  this->list_.push_front(value);
101  lock.unlock(); // unlock the mutex...
102  this->condition_.notify_all(); // ...then call notify_all to wake threads waiting in wait_and_pop_front/back
103  }
104 
108  template <class T>
109  void TclThreadList<T>::push_back(const T& value)
110  {
111  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
112  this->list_.push_back(value);
113  lock.unlock(); // unlock the mutex...
114  this->condition_.notify_all(); // ...then call notify_all to wake threads waiting in wait_and_pop_front/back
115  }
116 
120  template <class T>
121  template <class InputIterator> void TclThreadList<T>::insert(typename std::list<T>::iterator position, InputIterator begin, InputIterator end)
122  {
123  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
124  this->list_.insert(position, begin, end);
125  lock.unlock(); // unlock the mutex...
126  this->condition_.notify_all(); // ...then call notify_all to wake threads waiting in wait_and_pop_front/back
127  }
128 
133  template <class T>
135  {
136  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
137 
138  while (this->list_.empty()) // check if there's an element in the list
139  this->condition_.wait(lock); // wait until the condition becomes true (a notification comes from push_front, push_back or insert
140 
141  *value = this->list_.front();
142  this->list_.pop_front();
143  }
144 
149  template <class T>
151  {
152  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
153 
154  while (this->list_.empty()) // check if there's an element in the list
155  this->condition_.wait(lock); // wait until the condition becomes true (a notification comes from push_front, push_back or insert
156 
157  *value = this->list_.back();
158  this->list_.pop_back();
159  }
160 
168  template <class T>
170  {
171  boost::upgrade_lock<boost::shared_mutex> lock(this->mutex_); // gain shared lock
172 
173  if (this->list_.empty())
174  {
175  // No elements - return immediately
176  return false;
177  }
178  else
179  {
180  // At least one element - write it into the passed variable and pop it from the list
181  boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(lock); // upgrade to unique lock to modify the list
182  *value = this->list_.front();
183  this->list_.pop_front();
184  }
185  return true;
186  }
187 
195  template <class T>
197  {
198  boost::upgrade_lock<boost::shared_mutex> lock(this->mutex_); // gain shared lock
199 
200  if (this->list_.empty())
201  {
202  // No elements - return immediately
203  return false;
204  }
205  else
206  {
207  // At least one element - write it into the passed variable and pop it from the list
208  boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(lock); // upgrade to unique lock to modify the list
209  *value = this->list_.back();
210  this->list_.pop_back();
211  }
212  return true;
213  }
214 
218  template <class T>
220  {
221  boost::unique_lock<boost::shared_mutex> lock(this->mutex_);
222  this->list_.clear();
223  }
224 
232  template <class T>
233  size_t TclThreadList<T>::size() const
234  {
235  boost::shared_lock<boost::shared_mutex> lock(this->mutex_);
236  return this->list_.size();
237  }
238 
246  template <class T>
248  {
249  boost::shared_lock<boost::shared_mutex> lock(this->mutex_);
250  return this->list_.empty();
251  }
252 
259  template <class T>
260  bool TclThreadList<T>::is_in(const T& value) const
261  {
262  boost::shared_lock<boost::shared_mutex> lock(this->mutex_);
263 
264  for (const T& element : this->list_)
265  if (element == value)
266  return true;
267 
268  return false;
269  }
270 }
271 
272 #endif /* _TclThreadList_H__ */
const std::list< T > & getList() const
Returns a reference to the list.
Definition: TclThreadList.h:78
Shared library macros, enums, constants and forward declarations for the core library ...
bool try_pop_front(T *value)
Pops and returns the front element if there&#39;s at least one element in the list.
Definition: TclThreadList.h:169
std::list< T > list_
A standard list for type T.
Definition: TclThreadList.h:88
boost::condition_variable_any condition_
A condition variable to wake threads waiting for the mutex to become ready.
Definition: TclThreadList.h:90
bool is_in(const T &value) const
Returns true if a given element is in the list, false otherwise.
Definition: TclThreadList.h:260
void clear()
Clears the list.
Definition: TclThreadList.h:219
boost::shared_mutex mutex_
A mutex to grant exclusive access to the list.
Definition: TclThreadList.h:89
A thread-safe implementation of a message queue, used by TclThreadManager.
Definition: TclThreadList.h:52
void wait_and_pop_back(T *value)
Waits until the list contains at least one element and then pops and returns the back element...
Definition: TclThreadList.h:150
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
boost::shared_mutex & getMutex() const
Returns a reference to the mutex which might be useful if you want to iterate through the list (see g...
Definition: TclThreadList.h:84
void push_back(const T &value)
Pushes a new element to the back of the list.
Definition: TclThreadList.h:109
Definition: InputPrereqs.h:78
void insert(typename std::list< T >::iterator position, InputIterator begin, InputIterator end)
Inserts new elements into the list.
Definition: TclThreadList.h:121
std::list< T > & getList()
Returns a reference to the list.
Definition: TclThreadList.h:72
bool empty() const
Returns true if the list is empty, false otherwise.
Definition: TclThreadList.h:247
void push_front(const T &value)
Pushes a new element to the front of the list.
Definition: TclThreadList.h:97
size_t size() const
Returns the size of the list.
Definition: TclThreadList.h:233
void wait_and_pop_front(T *value)
Waits until the list contains at least one element and then pops and returns the front element...
Definition: TclThreadList.h:134
bool try_pop_back(T *value)
Pops and returns the back element if there&#39;s at least one element in the list.
Definition: TclThreadList.h:196