Skip to content

Commit 000d3be

Browse files
committed
[impl] don't compute stage if preempted
Checked at the start of the StagePrivate::runCompute(). A callback must be issued, otherwise the stage/s cannot be preempted.
1 parent ec69724 commit 000d3be

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

core/include/moveit/task_constructor/stage_p.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
namespace moveit {
5858
namespace task_constructor {
5959

60+
/// exception thrown by StagePrivate::runCompute()
61+
class PreemptStageException : public std::exception
62+
{
63+
public:
64+
explicit PreemptStageException() {}
65+
const char* what() const noexcept override {
66+
static const char* msg = "";
67+
return msg;
68+
}
69+
};
70+
6071
class ContainerBase;
6172
class StagePrivate
6273
{
@@ -146,6 +157,10 @@ class StagePrivate
146157
bool storeFailures() const { return introspection_ != nullptr; }
147158
void runCompute() {
148159
ROS_DEBUG_STREAM_NAMED("Stage", fmt::format("Computing stage '{}'", name()));
160+
161+
if (preempted())
162+
throw PreemptStageException();
163+
149164
auto compute_start_time = std::chrono::steady_clock::now();
150165
try {
151166
compute();
@@ -159,6 +174,10 @@ class StagePrivate
159174
/** compute cost for solution through configured CostTerm */
160175
void computeCost(const InterfaceState& from, const InterfaceState& to, SolutionBase& solution);
161176

177+
void setPreemptedCheck(const std::atomic<bool>* preempt_requested);
178+
/// is the stage preempted ? defaults to false
179+
bool preempted() const;
180+
162181
protected:
163182
StagePrivate& operator=(StagePrivate&& other);
164183

@@ -197,6 +216,8 @@ class StagePrivate
197216
InterfaceWeakPtr next_starts_; // interface to be used for sendForward()
198217

199218
Introspection* introspection_; // task's introspection instance
219+
220+
std::atomic<bool> const* preempt_requested_;
200221
};
201222
PIMPL_FUNCTIONS(Stage)
202223
std::ostream& operator<<(std::ostream& os, const StagePrivate& stage);

core/src/stage.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ StagePrivate::StagePrivate(Stage* me, const std::string& name)
102102
, cost_term_{ std::make_unique<CostTerm>() }
103103
, total_compute_time_{}
104104
, parent_{ nullptr }
105-
, introspection_{ nullptr } {}
105+
, introspection_{ nullptr }
106+
, preempt_requested_{ nullptr } {}
106107

107108
StagePrivate& StagePrivate::operator=(StagePrivate&& other) {
108109
assert(typeid(*this) == typeid(other));
@@ -305,6 +306,17 @@ void StagePrivate::computeCost(const InterfaceState& from, const InterfaceState&
305306
}
306307
}
307308

309+
void StagePrivate::setPreemptedCheck(std::atomic<bool> const* preempt_requested) {
310+
preempt_requested_ = preempt_requested;
311+
}
312+
313+
bool StagePrivate::preempted() const {
314+
if (preempt_requested_)
315+
return *preempt_requested_;
316+
317+
return false;
318+
}
319+
308320
Stage::Stage(StagePrivate* impl) : pimpl_(impl) {
309321
assert(impl);
310322
auto& p = properties();

0 commit comments

Comments
 (0)