00001 /* 00002 * Copyright (c) 2010, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #ifndef NODELET_CALLBACK_QUEUE_MANAGER_H 00031 #define NODELET_CALLBACK_QUEUE_MANAGER_H 00032 00033 #include <boost/shared_ptr.hpp> 00034 #include <boost/unordered_map.hpp> 00035 #include <boost/thread/mutex.hpp> 00036 #include <boost/thread/thread.hpp> 00037 00038 #include <vector> 00039 #include <deque> 00040 00041 namespace nodelet 00042 { 00043 namespace detail 00044 { 00045 class CallbackQueue; 00046 typedef boost::shared_ptr<CallbackQueue> CallbackQueuePtr; 00047 00059 class CallbackQueueManager 00060 { 00061 public: 00062 CallbackQueueManager(uint32_t num_worker_threads=boost::thread::hardware_concurrency()); 00063 ~CallbackQueueManager(); 00064 00065 void addQueue(const CallbackQueuePtr& queue, bool threaded); 00066 void removeQueue(const CallbackQueuePtr& queue); 00067 void callbackAdded(const CallbackQueuePtr& queue); 00068 00069 uint32_t getNumWorkerThreads(); 00070 00071 private: 00072 void managerThread(); 00073 struct ThreadInfo; 00074 void workerThread(ThreadInfo*); 00075 00076 class ThreadInfo; 00077 ThreadInfo* getSmallestQueue(); 00078 00079 struct QueueInfo 00080 { 00081 QueueInfo() 00082 : threaded(false) 00083 , thread_index(0xffffffff) 00084 , in_thread(0) 00085 {} 00086 00087 CallbackQueuePtr queue; 00088 bool threaded; 00089 00090 // Only used if threaded == false 00091 boost::mutex st_mutex; 00092 // TODO: atomic 00093 uint32_t thread_index; 00094 uint32_t in_thread; 00095 }; 00096 typedef boost::shared_ptr<QueueInfo> QueueInfoPtr; 00097 00098 typedef boost::unordered_map<CallbackQueue*, QueueInfoPtr> M_Queue; 00099 M_Queue queues_; 00100 boost::mutex queues_mutex_; 00101 00102 // TODO: srmw lockfree queue. waiting_mutex_ has the potential for a lot of contention 00103 typedef std::vector<CallbackQueuePtr> V_Queue; 00104 V_Queue waiting_; 00105 boost::mutex waiting_mutex_; 00106 boost::condition_variable waiting_cond_; 00107 boost::thread_group tg_; 00108 00109 struct ThreadInfo 00110 { 00111 ThreadInfo() 00112 : queue_mutex(new boost::mutex) 00113 , queue_cond(new boost::condition_variable) 00114 , calling(0) 00115 {} 00116 00117 // TODO: srsw lockfree queue 00118 boost::shared_ptr<boost::mutex> queue_mutex; 00119 boost::shared_ptr<boost::condition_variable> queue_cond; 00120 std::vector<std::pair<CallbackQueuePtr, QueueInfoPtr> > queue; 00121 uint32_t calling; 00122 00123 uint8_t pad[sizeof(128 - sizeof(V_Queue) - sizeof(uint32_t) - 2 * sizeof(boost::shared_ptr<void>))]; 00124 }; 00125 // TODO: Once the allocators package moves mainstream, align to cache-line boundary 00126 typedef std::vector<ThreadInfo> V_ThreadInfo; 00127 V_ThreadInfo thread_info_; 00128 00129 bool running_; 00130 uint32_t num_worker_threads_; 00131 }; 00132 00133 } // namespace detail 00134 } // namespace nodelet 00135 00136 #endif // NODELET_CALLBACK_QUEUE_MANAGER_H