queue.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2017, Alexander Stumpf, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Simulation, Systems Optimization and Robotics
13 // group, TU Darmstadt nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef VIGIR_FOOTSTEP_PLANNING_THREADING_QUEUE_H__
30 #define VIGIR_FOOTSTEP_PLANNING_THREADING_QUEUE_H__
31 
32 #include <ros/ros.h>
33 
34 #include <queue>
35 #include <boost/thread/mutex.hpp>
36 #include <boost/thread/condition_variable.hpp>
37 
38 
39 
41 {
42 namespace threading
43 {
44 template <class T>
45 class Queue
46 {
47 public:
49  : job_counter(0)
50  {
51  }
52 
53  virtual ~Queue()
54  {
55  }
56 
57  void clear()
58  {
59  boost::mutex::scoped_lock lock(queued_jobs_mutex);
60  queued_jobs = typename std::queue<boost::shared_ptr<T> >();
61  job_counter = 0;
63  }
64 
66  {
67  { // scope needed so mutex gets unlocked when other threads are notified
68  boost::mutex::scoped_lock lock(queued_jobs_mutex);
69  queued_jobs.push(job);
70  }
72  }
73 
74  void queueJobs(std::list<boost::shared_ptr<T> >& jobs)
75  {
76  { // scope needed so mutex gets unlocked when other threads are notified
77  boost::mutex::scoped_lock lock(queued_jobs_mutex);
78  for (typename std::list<boost::shared_ptr<T> >::iterator itr = jobs.begin(); itr != jobs.end(); itr++)
79  queued_jobs.push(*itr);
80  }
82  }
83 
85  {
86  boost::mutex::scoped_lock lock(queued_jobs_mutex);
87 
88  if (queued_jobs.empty())
89  {
90  ROS_DEBUG("[Queue] Waiting for Job getting queued...");
92  }
93 
94  ROS_DEBUG("[Queue] Pop Job.");
95 
96  boost::shared_ptr<T>& front = queued_jobs.front();
97  queued_jobs.pop();
98  job_counter++;
99 
100  return front;
101  }
102 
103  void waitAndDequeueJobs(std::vector<boost::shared_ptr<T> >& jobs, unsigned int n)
104  {
105  boost::mutex::scoped_lock lock(queued_jobs_mutex);
106 
107  if (queued_jobs.empty())
108  {
109  ROS_DEBUG("[Queue] Waiting for Job getting queued...");
111  }
112 
113  ROS_DEBUG("[Queue] Pop %u Job(s).", n);
114  n = std::min(n, static_cast<unsigned int>(queued_jobs.size()));
115  jobs.resize(n);
116 
117  for (unsigned int i = 0; i < n; i++)
118  {
119  jobs[i] = queued_jobs.front();
120  queued_jobs.pop();
121  job_counter++;
122  }
123  }
124 
125  void justFinishedJobs(unsigned int n)
126  {
127  boost::mutex::scoped_lock lock(queued_jobs_mutex);
128 
129  if (job_counter > n)
130  job_counter -= n;
131  else
132  job_counter = 0;
133 
134  if (queued_jobs.empty() && job_counter == 0)
135  {
136  ROS_DEBUG("[Queue] Notify for finished job.");
138  }
139  }
140 
141  bool hasOpenJobs() const
142  {
143  boost::mutex::scoped_lock lock(queued_jobs_mutex);
144  return !queued_jobs.empty() || job_counter != 0;
145  }
146 
148  {
149  boost::mutex::scoped_lock lock(queued_jobs_mutex);
150 
151  if (!queued_jobs.empty())
152  {
153  ROS_DEBUG("[Queue] Waiting for Jobs getting finished...");
155  ROS_DEBUG("[Queue] Waiting for Jobs getting finished...Done!");
156  }
157  }
158 
159 protected:
160  std::queue<boost::shared_ptr<T> > queued_jobs;
161  unsigned int job_counter;
162 
163  mutable boost::mutex queued_jobs_mutex;
164 
167 };
168 }
169 }
170 
171 #endif
void notify_all() BOOST_NOEXCEPT
void wait(unique_lock< mutex > &m)
boost::condition_variable jobs_finished_condition
Definition: queue.h:166
void waitAndDequeueJobs(std::vector< boost::shared_ptr< T > > &jobs, unsigned int n)
Definition: queue.h:103
void queueJob(boost::shared_ptr< T > &job)
Definition: queue.h:65
boost::condition_variable queued_jobs_condition
Definition: queue.h:165
std::queue< boost::shared_ptr< T > > queued_jobs
Definition: queue.h:160
void notify_one() BOOST_NOEXCEPT
boost::shared_ptr< T > & waitAndDequeueJob()
Definition: queue.h:84
void justFinishedJobs(unsigned int n)
Definition: queue.h:125
#define ROS_DEBUG(...)
void queueJobs(std::list< boost::shared_ptr< T > > &jobs)
Definition: queue.h:74


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Mon Jun 10 2019 15:47:33