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_.reset(new boost::thread(boost::bind(&BackgroundProcessing::processingThread, this)));
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 (std::deque<std::string>::iterator it = removed.begin(); it != removed.end(); ++it)
122 }
123 
125 {
126  boost::mutex::scoped_lock _(action_lock_);
127  return actions_.size() + (processing_ ? 1 : 0);
128 }
129 
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
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.
BackgroundProcessing()
Constructor. The background thread is activated automatically.
Called when a job is removed from the queue without execution.
Called when a job is completed (and removed from the queue)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
~BackgroundProcessing()
Finishes currently executing job, clears the remaining queue.
#define ROS_DEBUG_NAMED(name,...)
std::unique_ptr< boost::thread > processing_thread_
std::size_t getJobCount() const
Get the size of the queue of jobs (includes currently processed job).
void clear()
Clear the queue of jobs.
boost::function< void()> JobCallback
The signature for job callbacks.
void clearJobUpdateEvent()
Clear the callback to be triggered when events in JobEvent take place.
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...
#define ROS_ERROR_NAMED(name,...)
Called when a job is added to the queue.
Main namespace for MoveIt!
boost::condition_variable new_action_condition_
void setJobUpdateEvent(const JobUpdateCallback &event)
Set the callback to be triggered when events in JobEvent take place.


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Sun Oct 18 2020 13:16:33