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 00041 #include <boost/shared_ptr.hpp> 00042 #include <boost/thread/mutex.hpp> 00043 #include <boost/thread/shared_mutex.hpp> 00044 #include <boost/thread/condition_variable.hpp> 00045 #include <boost/thread/tss.hpp> 00046 00047 #include <list> 00048 #include <deque> 00049 00050 namespace ros 00051 { 00052 00056 class CallbackQueue : public CallbackQueueInterface 00057 { 00058 public: 00059 CallbackQueue(bool enabled = true); 00060 virtual ~CallbackQueue(); 00061 00062 virtual void addCallback(const CallbackInterfacePtr& callback, uint64_t removal_id = 0); 00063 virtual void removeByID(uint64_t removal_id); 00064 00065 enum CallOneResult 00066 { 00067 Called, 00068 TryAgain, 00069 Disabled, 00070 Empty, 00071 }; 00072 00077 CallOneResult callOne() 00078 { 00079 return callOne(ros::WallDuration()); 00080 } 00081 00090 CallOneResult callOne(ros::WallDuration timeout); 00091 00095 void callAvailable() 00096 { 00097 callAvailable(ros::WallDuration()); 00098 } 00106 void callAvailable(ros::WallDuration timeout); 00107 00111 bool empty() { return isEmpty(); } 00115 bool isEmpty(); 00119 void clear(); 00120 00124 void enable(); 00128 void disable(); 00132 bool isEnabled(); 00133 00134 protected: 00135 void setupTLS(); 00136 00137 struct TLS; 00138 CallOneResult callOneCB(TLS* tls); 00139 00140 struct IDInfo 00141 { 00142 uint64_t id; 00143 boost::shared_mutex calling_rw_mutex; 00144 }; 00145 typedef boost::shared_ptr<IDInfo> IDInfoPtr; 00146 typedef std::map<uint64_t, IDInfoPtr> M_IDInfo; 00147 00148 IDInfoPtr getIDInfo(uint64_t id); 00149 00150 struct CallbackInfo 00151 { 00152 CallbackInfo() 00153 : removal_id(0) 00154 , marked_for_removal(false) 00155 {} 00156 CallbackInterfacePtr callback; 00157 uint64_t removal_id; 00158 bool marked_for_removal; 00159 }; 00160 typedef std::list<CallbackInfo> L_CallbackInfo; 00161 typedef std::deque<CallbackInfo> D_CallbackInfo; 00162 D_CallbackInfo callbacks_; 00163 size_t calling_; 00164 boost::mutex mutex_; 00165 boost::condition_variable condition_; 00166 00167 boost::mutex id_info_mutex_; 00168 M_IDInfo id_info_; 00169 00170 struct TLS 00171 { 00172 TLS() 00173 : calling_in_this_thread(0xffffffffffffffffULL) 00174 , cb_it(callbacks.end()) 00175 {} 00176 uint64_t calling_in_this_thread; 00177 D_CallbackInfo callbacks; 00178 D_CallbackInfo::iterator cb_it; 00179 }; 00180 boost::thread_specific_ptr<TLS> tls_; 00181 00182 bool enabled_; 00183 }; 00184 typedef boost::shared_ptr<CallbackQueue> CallbackQueuePtr; 00185 00186 } 00187 00188 #endif