$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2009, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00035 #ifndef ROSCPP_CALLBACK_QUEUE_H 00036 #define ROSCPP_CALLBACK_QUEUE_H 00037 00038 #include "ros/callback_queue_interface.h" 00039 #include "ros/time.h" 00040 #include "common.h" 00041 00042 #include <boost/shared_ptr.hpp> 00043 #include <boost/thread/mutex.hpp> 00044 #include <boost/thread/shared_mutex.hpp> 00045 #include <boost/thread/condition_variable.hpp> 00046 #include <boost/thread/tss.hpp> 00047 00048 #include <list> 00049 #include <deque> 00050 00051 namespace ros 00052 { 00053 00057 class ROSCPP_DECL CallbackQueue : public CallbackQueueInterface 00058 { 00059 public: 00060 CallbackQueue(bool enabled = true); 00061 virtual ~CallbackQueue(); 00062 00063 virtual void addCallback(const CallbackInterfacePtr& callback, uint64_t removal_id = 0); 00064 virtual void removeByID(uint64_t removal_id); 00065 00066 enum CallOneResult 00067 { 00068 Called, 00069 TryAgain, 00070 Disabled, 00071 Empty, 00072 }; 00073 00078 CallOneResult callOne() 00079 { 00080 return callOne(ros::WallDuration()); 00081 } 00082 00091 CallOneResult callOne(ros::WallDuration timeout); 00092 00096 void callAvailable() 00097 { 00098 callAvailable(ros::WallDuration()); 00099 } 00107 void callAvailable(ros::WallDuration timeout); 00108 00112 bool empty() { return isEmpty(); } 00116 bool isEmpty(); 00120 void clear(); 00121 00125 void enable(); 00129 void disable(); 00133 bool isEnabled(); 00134 00135 protected: 00136 void setupTLS(); 00137 00138 struct TLS; 00139 CallOneResult callOneCB(TLS* tls); 00140 00141 struct IDInfo 00142 { 00143 uint64_t id; 00144 boost::shared_mutex calling_rw_mutex; 00145 }; 00146 typedef boost::shared_ptr<IDInfo> IDInfoPtr; 00147 typedef std::map<uint64_t, IDInfoPtr> M_IDInfo; 00148 00149 IDInfoPtr getIDInfo(uint64_t id); 00150 00151 struct CallbackInfo 00152 { 00153 CallbackInfo() 00154 : removal_id(0) 00155 , marked_for_removal(false) 00156 {} 00157 CallbackInterfacePtr callback; 00158 uint64_t removal_id; 00159 bool marked_for_removal; 00160 }; 00161 typedef std::list<CallbackInfo> L_CallbackInfo; 00162 typedef std::deque<CallbackInfo> D_CallbackInfo; 00163 D_CallbackInfo callbacks_; 00164 size_t calling_; 00165 boost::mutex mutex_; 00166 boost::condition_variable condition_; 00167 00168 boost::mutex id_info_mutex_; 00169 M_IDInfo id_info_; 00170 00171 struct TLS 00172 { 00173 TLS() 00174 : calling_in_this_thread(0xffffffffffffffffULL) 00175 , cb_it(callbacks.end()) 00176 {} 00177 uint64_t calling_in_this_thread; 00178 D_CallbackInfo callbacks; 00179 D_CallbackInfo::iterator cb_it; 00180 }; 00181 boost::thread_specific_ptr<TLS> tls_; 00182 00183 bool enabled_; 00184 }; 00185 typedef boost::shared_ptr<CallbackQueue> CallbackQueuePtr; 00186 00187 } 00188 00189 #endif