Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0f733c

Browse files
author
Davide Faconti
committed
fix issue BehaviorTree#57 (thread safety of Timeout)
1 parent a450728 commit a0f733c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

include/behaviortree_cpp/decorators/timeout_node.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ class TimeoutNode : public DecoratorNode
2727

2828
TimeoutNode(const std::string& name, const NodeConfiguration& config);
2929

30+
~TimeoutNode() override
31+
{
32+
timer_.cancelAll();
33+
}
34+
3035
static PortsList providedPorts()
3136
{
3237
return { InputPort<unsigned>("msec", "After a certain amount of time, "
3338
"halt() the child if it is still running.") };
3439
}
3540

3641
private:
37-
static TimerQueue& timer()
38-
{
39-
static TimerQueue timer_queue;
40-
return timer_queue;
41-
}
42+
TimerQueue timer_ ;
4243

4344
virtual BT::NodeStatus tick() override;
4445

@@ -48,6 +49,7 @@ class TimeoutNode : public DecoratorNode
4849
unsigned msec_;
4950
bool read_parameter_from_ports_;
5051
bool timeout_started_;
52+
std::mutex timeout_mutex_;
5153
};
5254
}
5355

src/decorators/timeout_node.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ NodeStatus TimeoutNode::tick()
5454

5555
if (msec_ > 0)
5656
{
57-
timer_id_ = timer().add(std::chrono::milliseconds(msec_),
57+
timer_id_ = timer_.add(std::chrono::milliseconds(msec_),
5858
[this](bool aborted)
5959
{
60+
std::unique_lock<std::mutex> lk( timeout_mutex_ );
6061
if (!aborted && child()->status() == NodeStatus::RUNNING)
6162
{
6263
child_halted_ = true;
@@ -67,6 +68,8 @@ NodeStatus TimeoutNode::tick()
6768
}
6869
}
6970

71+
std::unique_lock<std::mutex> lk( timeout_mutex_ );
72+
7073
if (child_halted_)
7174
{
7275
timeout_started_ = false;
@@ -77,8 +80,10 @@ NodeStatus TimeoutNode::tick()
7780
auto child_status = child()->executeTick();
7881
if (child_status != NodeStatus::RUNNING)
7982
{
80-
timer().cancel(timer_id_);
8183
timeout_started_ = false;
84+
timeout_mutex_.unlock();
85+
timer_.cancel(timer_id_);
86+
timeout_mutex_.lock();
8287
}
8388
return child_status;
8489
}

0 commit comments

Comments
 (0)