Skip to content
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

Use RAII to handle mutexes #87

Merged
merged 2 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/actionlib/client/simple_action_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,10 @@ void SimpleActionClient<ActionSpec>::handleTransition(GoalHandleT gh)
switch (cur_simple_state_.state_) {
case SimpleGoalState::PENDING:
case SimpleGoalState::ACTIVE:
done_mutex_.lock();
setSimpleState(SimpleGoalState::DONE);
done_mutex_.unlock();
{
boost::mutex::scoped_lock lock(done_mutex_);
setSimpleState(SimpleGoalState::DONE);
}

if (done_cb_) {
done_cb_(getState(), gh.getResult());
Expand Down
1 change: 1 addition & 0 deletions include/actionlib/server/action_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <ros/ros.h>
#include <boost/thread.hpp>
#include <boost/thread/reverse_lock.hpp>
#include <boost/shared_ptr.hpp>
#include <actionlib_msgs/GoalID.h>
#include <actionlib_msgs/GoalStatusArray.h>
Expand Down
9 changes: 2 additions & 7 deletions include/actionlib/server/action_server_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,10 @@ void ActionServerBase<ActionSpec>::goalCallback(const boost::shared_ptr<const Ac
GoalHandle gh = GoalHandle(it, this, handle_tracker, guard_);

// make sure that we unlock before calling the users callback
lock_.unlock();
boost::reverse_lock<boost::recursive_mutex::scoped_lock> unlocker(lock);

// now, we need to create a goal handle and call the user's callback
goal_callback_(gh);

lock_.lock();
}
}

Expand Down Expand Up @@ -310,13 +308,10 @@ void ActionServerBase<ActionSpec>::cancelCallback(
GoalHandle gh(it, this, handle_tracker, guard_);
if (gh.setCancelRequested()) {
// make sure that we're unlocked before we call the users callback
lock_.unlock();
boost::reverse_lock<boost::recursive_mutex::scoped_lock> unlocker(lock);

// call the user's cancel callback on the relevant goal
cancel_callback_(gh);

// lock for further modification of the status list
lock_.lock();
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions include/actionlib/server/handle_tracker_deleter_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ void HandleTrackerDeleter<ActionSpec>::operator()(void *)
DestructionGuard::ScopedProtector protector(*guard_);
if (protector.isProtected()) {
// make sure to lock while we erase status for this goal from the list
as_->lock_.lock();
boost::recursive_mutex::scoped_lock lock(as_->lock_);
(*status_it_).handle_destruction_time_ = ros::Time::now();
// as_->status_list_.erase(status_it_);
as_->lock_.unlock();
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions include/actionlib/server/simple_action_server_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,11 @@ void SimpleActionServer<ActionSpec>::executeLoop()
ROS_FATAL_COND(!execute_callback_,
"execute_callback_ must exist. This is a bug in SimpleActionServer");

// Make sure we're not locked when we call execute
lock.unlock();
execute_callback_(goal);
lock.lock();
{
// Make sure we're not locked when we call execute
boost::reverse_lock<boost::recursive_mutex::scoped_lock> unlocker(lock);
execute_callback_(goal);
}

if (isActive()) {
ROS_WARN_NAMED("actionlib", "Your executeCallback did not set the goal to a terminal status.\n"
Expand Down