00001 /********************************************************************** 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2009, Willow Garage, Inc. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Willow Garage nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 *********************************************************************/ 00035 00036 //#include <stdio.h> 00037 //#include <iostream> 00038 #include <vector> 00039 #include <deque> 00040 //#include <fstream> 00041 00042 #include <boost/thread/mutex.hpp> 00043 #include <boost/bind.hpp> 00044 #include "ros/console.h" 00045 #include "interval_intersection/interval_intersection.hpp" 00046 00047 using namespace std; 00048 using namespace interval_intersection; 00049 00050 IntervalIntersector::IntervalIntersector(boost::function<void (const calibration_msgs::Interval&)> output_callback): 00051 max_queue_size(200), 00052 output_callback_(output_callback) 00053 { 00054 } 00055 00056 IntervalIntersector::~IntervalIntersector() 00057 { 00058 // If callbacks continue during or after destruction, bad things can happen, 00059 // so ensure proper shutdown of the callback streams before destruction. 00060 } 00061 00062 boost::function<void (const calibration_msgs::IntervalConstPtr&)> IntervalIntersector::getNewInputStream() { 00063 // Changes the configuration of the set of queues, 00064 // so we lock all mutexes. 00065 boost::mutex::scoped_lock processing_lock(processing_mutex); 00066 size_t n = queues.size(); // Protected by above lock 00067 for (size_t i=0; i<n; i++) { 00068 mutexes[i]->lock(); 00069 } 00070 queues.resize(n+1); 00071 mutexes.resize(n+1); 00072 mutexes[n].reset(new boost::mutex()); 00073 for (size_t i=0; i<n; i++) { 00074 mutexes[i]->unlock(); 00075 } 00076 return boost::bind(&IntervalIntersector::inputCallback, this, _1, n); 00077 } 00078 00085 void IntervalIntersector::inputCallback(const calibration_msgs::IntervalConstPtr& interval_ptr, size_t i) { 00086 ROS_DEBUG("Got message on stream [%u]", i); 00087 boost::mutex::scoped_lock lock(*mutexes[i]); 00088 if (queues[i].size() < max_queue_size) { 00089 queues[i].push_back(interval_ptr); 00090 } 00091 lock.unlock(); 00092 00093 process_queues(); 00094 } 00095 00096 void IntervalIntersector::process_queues() { 00097 //cout << "processing" << endl; 00098 while (1) { 00099 // 1) Determine which interval to publish next 00100 ros::Time start = ros::TIME_MIN; 00101 ros::Time end = ros::TIME_MAX; 00102 int queue_to_pop = -1; 00103 boost::mutex::scoped_lock processing_lock(processing_mutex); 00104 for (size_t i=0; i<queues.size(); i++) { 00105 // It looks like we could get away without locking the mutexes to 00106 // determine the min and max since even though the queues may change 00107 // because new elements are added, the first element if it exists 00108 // does not change because we hold the processing mutex. 00109 // However there is a possibility that the location of queue[i][0] 00110 // changes after its address has been computed but before its value 00111 // has been read. In other words the statement 00112 // "start = queues[i][0]->start" is not atomic. 00113 // In fact even queues[i].empty() is only guaranteed to make sense 00114 // in between other method calls, not during. 00115 // Whether or not deque is in fact thread-safe here is implementation 00116 // dependent. 00117 boost::mutex::scoped_lock lock(*mutexes[i]); 00118 if (queues[i].empty()) { 00119 // We can't determine the next interval: nothing to do 00120 //cout << "nothing to do" << endl; 00121 return; 00122 } 00123 if (queues[i][0]->start > start) { 00124 start = queues[i][0]->start; 00125 } 00126 if (queues[i][0]->end < end) { 00127 end = queues[i][0]->end; 00128 queue_to_pop = i; 00129 } 00130 // mutexes[i] released 00131 } 00132 if (queue_to_pop < 0) { 00133 ROS_ERROR("IntervalIntersection logic error"); 00134 exit(-1); 00135 } 00136 // 2) Publish the interval 00137 //cout << "about to publish" << endl; 00138 if (start < end) { 00139 // Output the interval 00140 calibration_msgs::Interval interval; 00141 interval.start = start; 00142 interval.end = end; 00143 output_callback_(interval); 00144 } 00145 else 00146 { 00147 ROS_DEBUG("Publishing null interval"); 00148 calibration_msgs::Interval interval; 00149 interval.start = start; 00150 interval.end = start; 00151 output_callback_(interval); 00152 } 00153 // 3) Pop the input interval with earliest end time 00154 boost::mutex::scoped_lock lock(*mutexes[queue_to_pop]); 00155 queues[queue_to_pop].pop_front(); 00156 // mutexes[queue_to_pop] unlocks 00157 // processing_mutex unlocks 00158 } 00159 } 00160 00161