Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <moveit/background_processing/background_processing.h>
00038 #include <console_bridge/console.h>
00039
00040 moveit::tools::BackgroundProcessing::BackgroundProcessing()
00041 {
00042
00043 run_processing_thread_ = true;
00044 processing_ = false;
00045 processing_thread_.reset(new boost::thread(boost::bind(&BackgroundProcessing::processingThread, this)));
00046 }
00047
00048 moveit::tools::BackgroundProcessing::~BackgroundProcessing()
00049 {
00050 run_processing_thread_ = false;
00051 new_action_condition_.notify_all();
00052 processing_thread_->join();
00053 }
00054
00055 void moveit::tools::BackgroundProcessing::processingThread()
00056 {
00057 boost::unique_lock<boost::mutex> ulock(action_lock_);
00058
00059 while (run_processing_thread_)
00060 {
00061 while (actions_.empty() && run_processing_thread_)
00062 new_action_condition_.wait(ulock);
00063
00064 while (!actions_.empty())
00065 {
00066 JobCallback fn = actions_.front();
00067 std::string action_name = action_names_.front();
00068 actions_.pop_front();
00069 action_names_.pop_front();
00070 processing_ = true;
00071
00072
00073 action_lock_.unlock();
00074 try
00075 {
00076 logDebug("moveit.background: Begin executing '%s'", action_name.c_str());
00077 fn();
00078 logDebug("moveit.background: Done executing '%s'", action_name.c_str());
00079 }
00080 catch (std::runtime_error& ex)
00081 {
00082 logError("Exception caught while processing action '%s': %s", action_name.c_str(), ex.what());
00083 }
00084 catch (...)
00085 {
00086 logError("Exception caught while processing action '%s'", action_name.c_str());
00087 }
00088 processing_ = false;
00089 if (queue_change_event_)
00090 queue_change_event_(COMPLETE, action_name);
00091 action_lock_.lock();
00092 }
00093 }
00094 }
00095
00096 void moveit::tools::BackgroundProcessing::addJob(const boost::function<void()>& job, const std::string& name)
00097 {
00098 {
00099 boost::mutex::scoped_lock _(action_lock_);
00100 actions_.push_back(job);
00101 action_names_.push_back(name);
00102 new_action_condition_.notify_all();
00103 }
00104 if (queue_change_event_)
00105 queue_change_event_(ADD, name);
00106 }
00107
00108 void moveit::tools::BackgroundProcessing::clear()
00109 {
00110 bool update = false;
00111 std::deque<std::string> removed;
00112 {
00113 boost::mutex::scoped_lock _(action_lock_);
00114 update = !actions_.empty();
00115 actions_.clear();
00116 action_names_.swap(removed);
00117 }
00118 if (update && queue_change_event_)
00119 for (std::deque<std::string>::iterator it = removed.begin(); it != removed.end(); ++it)
00120 queue_change_event_(REMOVE, *it);
00121 }
00122
00123 std::size_t moveit::tools::BackgroundProcessing::getJobCount() const
00124 {
00125 boost::mutex::scoped_lock _(action_lock_);
00126 return actions_.size() + (processing_ ? 1 : 0);
00127 }
00128
00129 void moveit::tools::BackgroundProcessing::setJobUpdateEvent(const JobUpdateCallback& event)
00130 {
00131 boost::mutex::scoped_lock _(action_lock_);
00132 queue_change_event_ = event;
00133 }
00134
00135 void moveit::tools::BackgroundProcessing::clearJobUpdateEvent()
00136 {
00137 setJobUpdateEvent(JobUpdateCallback());
00138 }