background_processing.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
38 #include <ros/console.h>
39 
40 namespace moveit
41 {
42 namespace tools
43 {
45 {
46  // spin a thread that will process user events
48  processing_ = false;
49  processing_thread_ = std::make_unique<boost::thread>([this] { processingThread(); });
50 }
51 
53 {
54  run_processing_thread_ = false;
55  new_action_condition_.notify_all();
56  processing_thread_->join();
57 }
58 
60 {
61  boost::unique_lock<boost::mutex> ulock(action_lock_);
62 
64  {
65  while (actions_.empty() && run_processing_thread_)
66  new_action_condition_.wait(ulock);
67 
68  while (!actions_.empty())
69  {
70  JobCallback fn = actions_.front();
71  std::string action_name = action_names_.front();
72  actions_.pop_front();
73  action_names_.pop_front();
74  processing_ = true;
75 
76  // make sure we are unlocked while we process the event
77  action_lock_.unlock();
78  try
79  {
80  ROS_DEBUG_NAMED("background_processing", "Begin executing '%s'", action_name.c_str());
81  fn();
82  ROS_DEBUG_NAMED("background_processing", "Done executing '%s'", action_name.c_str());
83  }
84  catch (std::exception& ex)
85  {
86  ROS_ERROR_NAMED("background_processing", "Exception caught while processing action '%s': %s",
87  action_name.c_str(), ex.what());
88  }
89  processing_ = false;
91  queue_change_event_(COMPLETE, action_name);
92  action_lock_.lock();
93  }
94  }
95 }
96 
97 void BackgroundProcessing::addJob(const boost::function<void()>& job, const std::string& name)
98 {
99  {
100  boost::mutex::scoped_lock _(action_lock_);
101  actions_.push_back(job);
102  action_names_.push_back(name);
103  new_action_condition_.notify_all();
104  }
106  queue_change_event_(ADD, name);
107 }
108 
110 {
111  bool update = false;
112  std::deque<std::string> removed;
113  {
114  boost::mutex::scoped_lock _(action_lock_);
115  update = !actions_.empty();
116  actions_.clear();
117  action_names_.swap(removed);
118  }
119  if (update && queue_change_event_)
120  for (const std::string& it : removed)
122 }
123 
124 std::size_t BackgroundProcessing::getJobCount() const
125 {
126  boost::mutex::scoped_lock _(action_lock_);
127  return actions_.size() + (processing_ ? 1 : 0);
128 }
129 
130 void BackgroundProcessing::setJobUpdateEvent(const JobUpdateCallback& event)
131 {
132  boost::mutex::scoped_lock _(action_lock_);
133  queue_change_event_ = event;
134 }
135 
137 {
139 }
140 
141 } // end of namespace tools
142 } // end of namespace moveit
moveit::tools::BackgroundProcessing::new_action_condition_
boost::condition_variable new_action_condition_
Definition: background_processing.h:199
moveit::tools::BackgroundProcessing::COMPLETE
@ COMPLETE
Called when a job is completed (and removed from the queue)
Definition: background_processing.h:195
moveit::tools::BackgroundProcessing::setJobUpdateEvent
void setJobUpdateEvent(const JobUpdateCallback &event)
Set the callback to be triggered when events in JobEvent take place.
Definition: background_processing.cpp:194
moveit::tools::BackgroundProcessing::processingThread
void processingThread()
Definition: background_processing.cpp:123
moveit::tools::BackgroundProcessing::queue_change_event_
JobUpdateCallback queue_change_event_
Definition: background_processing.h:203
moveit::tools::BackgroundProcessing::clear
void clear()
Clear the queue of jobs.
Definition: background_processing.cpp:173
moveit::tools::BackgroundProcessing::~BackgroundProcessing
~BackgroundProcessing()
Finishes currently executing job, clears the remaining queue.
Definition: background_processing.cpp:116
moveit::tools::BackgroundProcessing::action_lock_
boost::mutex action_lock_
Definition: background_processing.h:198
ROS_ERROR_NAMED
#define ROS_ERROR_NAMED(name,...)
console.h
moveit::tools::BackgroundProcessing::BackgroundProcessing
BackgroundProcessing()
Constructor. The background thread is activated automatically.
Definition: background_processing.cpp:108
update
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
moveit::tools::BackgroundProcessing::JobUpdateCallback
boost::function< void(JobEvent, const std::string &)> JobUpdateCallback
The signature for callback triggered when job events take place: the event that took place and the na...
Definition: background_processing.h:168
moveit::tools::BackgroundProcessing::processing_
bool processing_
Definition: background_processing.h:205
background_processing.h
moveit::tools::BackgroundProcessing::action_names_
std::deque< std::string > action_names_
Definition: background_processing.h:201
moveit::tools::BackgroundProcessing::processing_thread_
std::unique_ptr< boost::thread > processing_thread_
Definition: background_processing.h:195
moveit::tools::BackgroundProcessing::REMOVE
@ REMOVE
Called when a job is removed from the queue without execution.
Definition: background_processing.h:193
moveit::tools::BackgroundProcessing::run_processing_thread_
bool run_processing_thread_
Definition: background_processing.h:196
moveit
Main namespace for MoveIt.
Definition: background_processing.h:46
moveit::tools::BackgroundProcessing::actions_
std::deque< JobCallback > actions_
Definition: background_processing.h:200
moveit::tools::BackgroundProcessing::addJob
void addJob(const JobCallback &job, const std::string &name)
Add a job to the queue of jobs to execute. A name is also specifies for the job.
Definition: background_processing.cpp:161
moveit::tools::BackgroundProcessing::clearJobUpdateEvent
void clearJobUpdateEvent()
Clear the callback to be triggered when events in JobEvent take place.
Definition: background_processing.cpp:200
moveit::tools::BackgroundProcessing::JobCallback
boost::function< void()> JobCallback
The signature for job callbacks.
Definition: background_processing.h:171
moveit::tools::BackgroundProcessing::ADD
@ ADD
Called when a job is added to the queue.
Definition: background_processing.h:191
moveit::tools::BackgroundProcessing::getJobCount
std::size_t getJobCount() const
Get the size of the queue of jobs (includes currently processed job).
Definition: background_processing.cpp:188


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Sat Dec 21 2024 03:23:41