-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added threadpool to laser plugin, fixed bug with plugin enabled param… #47
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
https://github.com/progschj/ThreadPool | ||
|
||
Copyright (c) 2012 Jakob Progsch, Václav Zeman | ||
|
||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
|
||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
|
||
3. This notice may not be removed or altered from any source | ||
distribution. | ||
*/ | ||
|
||
#ifndef THREAD_POOL_H | ||
#define THREAD_POOL_H | ||
|
||
#include <vector> | ||
#include <queue> | ||
#include <memory> | ||
#include <thread> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <future> | ||
#include <functional> | ||
#include <stdexcept> | ||
|
||
class ThreadPool { | ||
public: | ||
ThreadPool(size_t); | ||
template<class F, class... Args> | ||
auto enqueue(F&& f, Args&&... args) | ||
-> std::future<typename std::result_of<F(Args...)>::type>; | ||
~ThreadPool(); | ||
private: | ||
// need to keep track of threads so we can join them | ||
std::vector< std::thread > workers; | ||
// the task queue | ||
std::queue< std::function<void()> > tasks; | ||
|
||
// synchronization | ||
std::mutex queue_mutex; | ||
std::condition_variable condition; | ||
bool stop; | ||
}; | ||
|
||
// the constructor just launches some amount of workers | ||
inline ThreadPool::ThreadPool(size_t threads) | ||
: stop(false) | ||
{ | ||
for(size_t i = 0;i<threads;++i) | ||
workers.emplace_back( | ||
[this] | ||
{ | ||
for(;;) | ||
{ | ||
std::function<void()> task; | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock(this->queue_mutex); | ||
this->condition.wait(lock, | ||
[this]{ return this->stop || !this->tasks.empty(); }); | ||
if(this->stop && this->tasks.empty()) | ||
return; | ||
task = std::move(this->tasks.front()); | ||
this->tasks.pop(); | ||
} | ||
|
||
task(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
// add new work item to the pool | ||
template<class F, class... Args> | ||
auto ThreadPool::enqueue(F&& f, Args&&... args) | ||
-> std::future<typename std::result_of<F(Args...)>::type> | ||
{ | ||
using return_type = typename std::result_of<F(Args...)>::type; | ||
|
||
auto task = std::make_shared< std::packaged_task<return_type()> >( | ||
std::bind(std::forward<F>(f), std::forward<Args>(args)...) | ||
); | ||
|
||
std::future<return_type> res = task->get_future(); | ||
{ | ||
std::unique_lock<std::mutex> lock(queue_mutex); | ||
|
||
// don't allow enqueueing after stopping the pool | ||
if(stop) | ||
throw std::runtime_error("enqueue on stopped ThreadPool"); | ||
|
||
tasks.emplace([task](){ (*task)(); }); | ||
} | ||
condition.notify_one(); | ||
return res; | ||
} | ||
|
||
// the destructor joins all threads | ||
inline ThreadPool::~ThreadPool() | ||
{ | ||
{ | ||
std::unique_lock<std::mutex> lock(queue_mutex); | ||
stop = true; | ||
} | ||
condition.notify_all(); | ||
for(std::thread &worker: workers) | ||
worker.join(); | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,42 +161,55 @@ void Laser::ComputeLaserRanges() { | |
v_world_laser_origin_ = m_world_to_laser_ * v_zero_point_; | ||
|
||
// Conver to Box2D data types | ||
b2Vec2 laser_point; | ||
b2Vec2 laser_origin_point(v_world_laser_origin_(0), v_world_laser_origin_(1)); | ||
|
||
// loop through the laser points and call the Box2D world raycast | ||
// Results vector | ||
std::vector<std::future<std::pair<double, double>>> results( | ||
laser_scan_.ranges.size()); | ||
|
||
// loop through the laser points and call the Box2D world raycast by | ||
// enqueueing the callback | ||
for (unsigned int i = 0; i < laser_scan_.ranges.size(); ++i) { | ||
results[i] = | ||
pool_.enqueue([i, this, laser_origin_point] { // Lambda function | ||
b2Vec2 laser_point; | ||
laser_point.x = m_world_laser_points_(0, i); | ||
laser_point.y = m_world_laser_points_(1, i); | ||
LaserCallback cb(this); | ||
|
||
GetModel()->GetPhysicsWorld()->RayCast(&cb, laser_origin_point, | ||
laser_point); | ||
|
||
if (!cb.did_hit_) { | ||
return std::make_pair<double, double>(NAN, 0); | ||
} else { | ||
return std::make_pair<double, double>( | ||
cb.fraction_ * this->range_ + this->noise_gen_(this->rng_), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::normal_distribution might not be thread safe. Is it possible to move this noise generator to cb object so that nobody shares it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can move the noise out of the thread |
||
cb.intensity_); | ||
} | ||
}); | ||
} | ||
|
||
// Unqueue all of the future'd results | ||
for (unsigned int i = 0; i < laser_scan_.ranges.size(); ++i) { | ||
laser_point.x = m_world_laser_points_(0, i); | ||
laser_point.y = m_world_laser_points_(1, i); | ||
|
||
did_hit_ = false; | ||
intensity_ = 0.0; | ||
|
||
GetModel()->GetPhysicsWorld()->RayCast(this, laser_origin_point, | ||
laser_point); | ||
|
||
if (!did_hit_) { | ||
laser_scan_.ranges[i] = NAN; | ||
if (reflectance_layers_bits_) laser_scan_.intensities[i] = 0; | ||
} else { | ||
laser_scan_.ranges[i] = fraction_ * range_ + noise_gen_(rng_); | ||
if (reflectance_layers_bits_) laser_scan_.intensities[i] = intensity_; | ||
} | ||
auto result = results[i].get(); // Pull the result from the future | ||
laser_scan_.ranges[i] = result.first; | ||
if (reflectance_layers_bits_) laser_scan_.intensities[i] = result.second; | ||
} | ||
} | ||
|
||
float Laser::ReportFixture(b2Fixture *fixture, const b2Vec2 &point, | ||
const b2Vec2 &normal, float fraction) { | ||
float LaserCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &point, | ||
const b2Vec2 &normal, float fraction) { | ||
uint16_t category_bits = fixture->GetFilterData().categoryBits; | ||
// only register hit in the specified layers | ||
if (!(category_bits & layers_bits_)) { | ||
if (!(category_bits & parent_->layers_bits_)) { | ||
return -1.0f; // return -1 to ignore this hit | ||
} | ||
|
||
// Don't return on hitting sensors... they're not real | ||
if (fixture->IsSensor()) return -1.0f; | ||
|
||
if (category_bits & reflectance_layers_bits_) { | ||
if (category_bits & parent_->reflectance_layers_bits_) { | ||
intensity_ = 255.0; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is thread safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it and it doesn't seem to modify the physics world state at all