|
From: <jl...@us...> - 2009-01-16 19:09:44
|
Revision: 9547
http://personalrobots.svn.sourceforge.net/personalrobots/?rev=9547&view=rev
Author: jleibs
Date: 2009-01-16 19:09:38 +0000 (Fri, 16 Jan 2009)
Log Message:
-----------
Moving topic_synchronizer over to boost threads.
Modified Paths:
--------------
pkg/trunk/util/topic_synchronizer/manifest.xml
pkg/trunk/util/topic_synchronizer/topic_synchronizer.h
pkg/trunk/vision/stereo_view/stereo_view.cpp
pkg/trunk/vision/stereo_view/stereo_view_pixel_info.cpp
Modified: pkg/trunk/util/topic_synchronizer/manifest.xml
===================================================================
--- pkg/trunk/util/topic_synchronizer/manifest.xml 2009-01-16 18:37:07 UTC (rev 9546)
+++ pkg/trunk/util/topic_synchronizer/manifest.xml 2009-01-16 19:09:38 UTC (rev 9547)
@@ -7,7 +7,7 @@
<depend package="std_msgs" />
<depend package="rosthread" />
<export>
- <cpp cflags="-I${prefix}"/>
+ <cpp cflags="-I${prefix} -D__STDC_LIMIT_MACROS -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG"/>
</export>
<url>http://pr.willowgarage.com/wiki/topic_synchronizer</url>
</package>
Modified: pkg/trunk/util/topic_synchronizer/topic_synchronizer.h
===================================================================
--- pkg/trunk/util/topic_synchronizer/topic_synchronizer.h 2009-01-16 18:37:07 UTC (rev 9546)
+++ pkg/trunk/util/topic_synchronizer/topic_synchronizer.h 2009-01-16 19:09:38 UTC (rev 9547)
@@ -39,8 +39,13 @@
#ifndef TOPIC_SYNCHRONIZER_HH
#define TOPIC_SYNCHRONIZER_HH
-#include "rosthread/mutex.h"
-#include "rosthread/condition.h"
+
+#include <boost/thread.hpp>
+#include "boost/date_time/posix_time/posix_time.hpp"
+
+//#include "rosthread/mutex.h"
+//#include "rosthread/condition.h"
+
#include "ros/time.h"
//! A templated class for synchronizing incoming topics
@@ -56,7 +61,8 @@
class UnsubscribeList
{
std::list<std::string> list_;
- ros::thread::mutex list_mutex_;
+ boost::mutex list_mutex_;
+ // ros::thread::mutex list_mutex_;
public:
UnsubscribeList(std::list<std::string>& l) : list_(l) { }
@@ -106,13 +112,16 @@
void (N::*callback_)(ros::Time);
//! Timeout duration
- ros::Duration timeout_;
+ boost::posix_time::time_duration timeout_;
+ // ros::Duration timeout_;
//! The callback to be called if timed out
void (N::*timeout_callback_)(ros::Time);
- //! The condition variable used for synchronization
- ros::thread::condition cond_all_;
+ //! The condition variable and mutex used for synchronization
+ boost::condition_variable cond_all_;
+ boost::mutex cond_all_mutex_;
+ // ros::thread::condition cond_all_;
//! The number of expected incoming messages
int expected_count_;
@@ -144,19 +153,21 @@
ros::Time* time = (ros::Time*)(p);
- cond_all_.lock();
+ boost::unique_lock<boost::mutex> lock(cond_all_mutex_);
+ // cond_all_mutex_.lock();
+
// If first to get message, wait for others
if (count_ == 0)
{
- wait_for_others(time);
+ wait_for_others(time, lock);
return;
}
// If behind, skip
if (*time < waiting_time_)
{
- cond_all_.unlock();
+ // cond_all_mutex_.unlock();
return;
}
@@ -166,21 +177,21 @@
count_++;
if (count_ == expected_count_)
{
- cond_all_.broadcast();
+ cond_all_.notify_all();
}
while (!done_ && *time == waiting_time_)
- cond_all_.wait();
+ cond_all_.wait(lock);
- cond_all_.unlock();
+ // cond_all_mutex_.unlock();
return;
}
// If ahead, wakeup others, and ten wait for others
if (*time > waiting_time_)
{
- cond_all_.broadcast();
- wait_for_others(time);
+ cond_all_.notify_all();
+ wait_for_others(time, lock);
}
}
@@ -200,7 +211,7 @@
/*!
* \param time The time that is being waited for
*/
- void wait_for_others(ros::Time* time)
+ void wait_for_others(ros::Time* time, boost::unique_lock<boost::mutex>& lock)
{
count_ = 1;
done_ = false;
@@ -209,7 +220,7 @@
bool timed_out = false;
while (count_ < expected_count_ && *time == waiting_time_ && !timed_out)
- if (!cond_all_.timed_wait(timeout_))
+ if (!cond_all_.timed_wait(lock, timeout_))
{
timed_out = true;
if (timeout_callback_)
@@ -223,9 +234,9 @@
{
done_ = true;
count_ = 0;
- cond_all_.broadcast();
+ cond_all_.notify_all();
}
- cond_all_.unlock();
+ // cond_all_mutex_.unlock();
}
public:
@@ -239,8 +250,10 @@
* \param timeout The duration
* \param timeout_callback A callback which is triggered when the timeout expires
*/
- TopicSynchronizer(N* node, void (N::*callback)(ros::Time), ros::Duration timeout = ros::Duration(1.0), void (N::*timeout_callback)(ros::Time) = NULL) : node_(node), callback_(callback), timeout_(timeout), timeout_callback_(timeout_callback), expected_count_(0), count_(0), done_(false)
- { }
+ TopicSynchronizer(N* node, void (N::*callback)(ros::Time), ros::Duration timeout = ros::Duration(1.0), void (N::*timeout_callback)(ros::Time) = NULL) : node_(node), callback_(callback), timeout_callback_(timeout_callback), expected_count_(0), count_(0), done_(false)
+ {
+ timeout_ = boost::posix_time::nanosec(timeout.toNSec());
+ }
//! Destructor
~TopicSynchronizer()
Modified: pkg/trunk/vision/stereo_view/stereo_view.cpp
===================================================================
--- pkg/trunk/vision/stereo_view/stereo_view.cpp 2009-01-16 18:37:07 UTC (rev 9546)
+++ pkg/trunk/vision/stereo_view/stereo_view.cpp 2009-01-16 19:09:38 UTC (rev 9547)
@@ -50,6 +50,8 @@
#include "topic_synchronizer.h"
+#include <boost/thread.hpp>
+
using namespace std;
class StereoView : public ros::node
@@ -78,7 +80,7 @@
bool calib_color_;
bool recompand_;
- ros::thread::mutex cv_mutex;
+ boost::mutex cv_mutex;
StereoView() : ros::node("stereo_view"),
lcal(this), rcal(this), lcalimage(NULL), rcalimage(NULL),
@@ -90,20 +92,20 @@
cvNamedWindow("disparity", CV_WINDOW_AUTOSIZE);
std::list<std::string> left_list;
- left_list.push_back(map_name("stereo") + std::string("/left/image_rect_color"));
- left_list.push_back(map_name("stereo") + std::string("/left/image_rect"));
+ left_list.push_back(mapName("stereo") + std::string("/left/image_rect_color"));
+ left_list.push_back(mapName("stereo") + std::string("/left/image_rect"));
std::list<std::string> right_list;
- right_list.push_back(map_name("stereo") + std::string("/right/image_rect_color"));
- right_list.push_back(map_name("stereo") + std::string("/right/image_rect"));
+ right_list.push_back(mapName("stereo") + std::string("/right/image_rect_color"));
+ right_list.push_back(mapName("stereo") + std::string("/right/image_rect"));
sync.subscribe(left_list, limage, 1);
sync.subscribe(right_list, rimage, 1);
- sync.subscribe(map_name("stereo") + "/disparity", dimage, 1);
- sync.subscribe(map_name("stereo") + "/stereo_info", stinfo, 1);
- sync.subscribe(map_name("stereo") + "/disparity_info", dinfo, 1);
+ sync.subscribe(mapName("stereo") + "/disparity", dimage, 1);
+ sync.subscribe(mapName("stereo") + "/stereo_info", stinfo, 1);
+ sync.subscribe(mapName("stereo") + "/disparity_info", dinfo, 1);
sync.ready();
}
@@ -192,8 +194,8 @@
// Fetch color calibration parameters as necessary
if (calib_color_)
{
- lcal.getFromParam(map_name("stereo") + "/left/image_rect_color");
- rcal.getFromParam(map_name("stereo") + "/right/image_rect_color");
+ lcal.getFromParam(mapName("stereo") + "/left/image_rect_color");
+ rcal.getFromParam(mapName("stereo") + "/right/image_rect_color");
}
cv_mutex.unlock();
Modified: pkg/trunk/vision/stereo_view/stereo_view_pixel_info.cpp
===================================================================
--- pkg/trunk/vision/stereo_view/stereo_view_pixel_info.cpp 2009-01-16 18:37:07 UTC (rev 9546)
+++ pkg/trunk/vision/stereo_view/stereo_view_pixel_info.cpp 2009-01-16 19:09:38 UTC (rev 9547)
@@ -61,6 +61,8 @@
#include "topic_synchronizer.h"
+#include <boost/thread.hpp>
+
using namespace std;
struct MouseCallbackParams {
@@ -72,7 +74,7 @@
};
//ros::thread::mutex cv_mutex;
-ros::thread::mutex g_cv_mutex;
+boost::mutex g_cv_mutex;
/*!
* \brief Click on a point in the left image to get 2d, color and 3d information.
@@ -170,7 +172,7 @@
MouseCallbackParams mcbparams_; /**< Parameters for the mouse callback. */
- ros::thread::mutex cv_mutex;
+ boost::mutex cv_mutex;
StereoView() : ros::node("stereo_view"),
lcal(this), rcal(this), lcalimage(NULL), rcalimage(NULL),
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|