callback_queue.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef ROSCPP_CALLBACK_QUEUE_H
36 #define ROSCPP_CALLBACK_QUEUE_H
37 
40 #include "ros/time.h"
41 #include "common.h"
42 
43 #include <boost/shared_ptr.hpp>
44 #include <boost/thread/mutex.hpp>
45 #include <boost/thread/shared_mutex.hpp>
46 #include <boost/thread/tss.hpp>
47 
48 #include <list>
49 #include <deque>
50 
51 namespace ros
52 {
53 
57 class ROSCPP_DECL CallbackQueue : public CallbackQueueInterface
58 {
59 public:
60  CallbackQueue(bool enabled = true);
61  virtual ~CallbackQueue();
62 
63  virtual void addCallback(const CallbackInterfacePtr& callback, uint64_t removal_id = 0);
64  virtual void removeByID(uint64_t removal_id);
65 
67  {
72  };
73 
79  {
80  return callOne(ros::WallDuration());
81  }
82 
91  CallOneResult callOne(ros::WallDuration timeout);
92 
97  {
98  callAvailable(ros::WallDuration());
99  }
107  void callAvailable(ros::WallDuration timeout);
108 
112  bool empty() { return isEmpty(); }
116  bool isEmpty();
120  void clear();
121 
125  void enable();
129  void disable();
133  bool isEnabled();
134 
135 protected:
136  void setupTLS();
137 
138  struct TLS;
139  CallOneResult callOneCB(TLS* tls);
140 
141  struct IDInfo
142  {
143  uint64_t id;
144  boost::shared_mutex calling_rw_mutex;
145  };
147  typedef std::map<uint64_t, IDInfoPtr> M_IDInfo;
148 
149  IDInfoPtr getIDInfo(uint64_t id);
150 
152  {
154  : removal_id(0)
155  , marked_for_removal(false)
156  {}
158  uint64_t removal_id;
160  };
161  typedef std::list<CallbackInfo> L_CallbackInfo;
162  typedef std::deque<CallbackInfo> D_CallbackInfo;
163  D_CallbackInfo callbacks_;
164  size_t calling_;
165  boost::mutex mutex_;
167 
168  boost::mutex id_info_mutex_;
169  M_IDInfo id_info_;
170 
171  struct TLS
172  {
173  TLS()
174  : calling_in_this_thread(0xffffffffffffffffULL)
175  , cb_it(callbacks.end())
176  {}
178  D_CallbackInfo callbacks;
179  D_CallbackInfo::iterator cb_it;
180  };
181  boost::thread_specific_ptr<TLS> tls_;
182 
183  bool enabled_;
184 };
186 
187 }
188 
189 #endif
boost::mutex mutex_
boost::thread_specific_ptr< TLS > tls_
D_CallbackInfo::iterator cb_it
bool empty()
returns whether or not the queue is empty
This is the default implementation of the ros::CallbackQueueInterface.
boost::shared_mutex calling_rw_mutex
ros::internal::condition_variable_monotonic condition_
CallOneResult callOne()
Pop a single callback off the front of the queue and invoke it. If the callback was not ready to be c...
Abstract interface for a queue used to handle all callbacks within roscpp.
std::map< uint64_t, IDInfoPtr > M_IDInfo
D_CallbackInfo callbacks_
boost::shared_ptr< IDInfo > IDInfoPtr
boost::shared_ptr< CallbackQueue > CallbackQueuePtr
void callAvailable()
Invoke all callbacks currently in the queue. If a callback was not ready to be called, pushes it back onto the queue.
boost::condition_variable condition_variable_monotonic
D_CallbackInfo callbacks
boost::mutex id_info_mutex_
std::list< CallbackInfo > L_CallbackInfo
std::deque< CallbackInfo > D_CallbackInfo


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:26