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 <console_bridge/console.h>
39 
41 {
42  // spin a thread that will process user events
44  processing_ = false;
45  processing_thread_.reset(new boost::thread(boost::bind(&BackgroundProcessing::processingThread, this)));
46 }
47 
49 {
50  run_processing_thread_ = false;
52  processing_thread_->join();
53 }
54 
56 {
57  boost::unique_lock<boost::mutex> ulock(action_lock_);
58 
60  {
61  while (actions_.empty() && run_processing_thread_)
63 
64  while (!actions_.empty())
65  {
66  JobCallback fn = actions_.front();
67  std::string action_name = action_names_.front();
68  actions_.pop_front();
69  action_names_.pop_front();
70  processing_ = true;
71 
72  // make sure we are unlocked while we process the event
73  action_lock_.unlock();
74  try
75  {
76  logDebug("moveit.background: Begin executing '%s'", action_name.c_str());
77  fn();
78  logDebug("moveit.background: Done executing '%s'", action_name.c_str());
79  }
80  catch (std::exception& ex)
81  {
82  logError("Exception caught while processing action '%s': %s", action_name.c_str(), ex.what());
83  }
84  processing_ = false;
86  queue_change_event_(COMPLETE, action_name);
87  action_lock_.lock();
88  }
89  }
90 }
91 
92 void moveit::tools::BackgroundProcessing::addJob(const boost::function<void()>& job, const std::string& name)
93 {
94  {
95  boost::mutex::scoped_lock _(action_lock_);
96  actions_.push_back(job);
97  action_names_.push_back(name);
99  }
101  queue_change_event_(ADD, name);
102 }
103 
105 {
106  bool update = false;
107  std::deque<std::string> removed;
108  {
109  boost::mutex::scoped_lock _(action_lock_);
110  update = !actions_.empty();
111  actions_.clear();
112  action_names_.swap(removed);
113  }
114  if (update && queue_change_event_)
115  for (std::deque<std::string>::iterator it = removed.begin(); it != removed.end(); ++it)
117 }
118 
120 {
121  boost::mutex::scoped_lock _(action_lock_);
122  return actions_.size() + (processing_ ? 1 : 0);
123 }
124 
126 {
127  boost::mutex::scoped_lock _(action_lock_);
128  queue_change_event_ = event;
129 }
130 
132 {
134 }
void notify_all() BOOST_NOEXCEPT
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.
void wait(unique_lock< mutex > &m)
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.
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...
Called when a job is added to the queue.
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 Mon Jan 15 2018 03:50:44