Skip to content

Commit

Permalink
Merge pull request #122 from cactusdynamics/loop-control
Browse files Browse the repository at this point in the history
Be more explicit for the return of Loop
  • Loading branch information
shuhaowu authored Aug 21, 2024
2 parents c7e8de6 + 7d5dc26 commit dc53ca0
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 39 deletions.
10 changes: 4 additions & 6 deletions examples/lockless_example/main.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <cactus_rt/experimental/lockless.h>
#include <cactus_rt/rt.h>

#include <chrono>

using cactus_rt::App;
using cactus_rt::CyclicThread;
using cactus_rt::Thread;
Expand Down Expand Up @@ -75,14 +73,14 @@ class RTThread : public CyclicThread {
RTThread(Context& ctx) : CyclicThread("RTThread", CreateThreadConfig()), ctx_(ctx) {}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
if (ctx_.done.Read()) {
return true;
return LoopControl::Stop;
}

const Pose new_pose = ctx_.target_pose.Read();
if (!new_pose.valid) {
return false;
return LoopControl::Continue;
}

if (new_pose != current_target_pose_) {
Expand All @@ -99,7 +97,7 @@ class RTThread : public CyclicThread {
);
}

return false;
return LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/logging_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class ExampleRTThread : public CyclicThread {
}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
if (loop_counter_ % 1000 == 0) {
LOG_INFO(Logger(), "Loop {}", loop_counter_);
}
LOG_INFO_LIMIT(std::chrono::milliseconds{1500}, Logger(), "Log limit: Loop {}", loop_counter_);
return false;
return LoopControl::Continue;
}
};

Expand Down
1 change: 0 additions & 1 deletion examples/message_passing_example/data_logger_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <atomic>
#include <cstdint>
#include <fstream>
#include <tuple>
#include <vector>

using moodycamel::ReaderWriterQueue;
Expand Down
4 changes: 2 additions & 2 deletions examples/message_passing_example/rt_thread.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "rt_thread.h"

bool RtThread::Loop(int64_t ellapsed_ns) noexcept {
cactus_rt::CyclicThread::LoopControl RtThread::Loop(int64_t ellapsed_ns) noexcept {
const double ellapsed_ms = static_cast<double>(ellapsed_ns) / 1'000'000.0;

const double period = 1000.0;
Expand All @@ -9,5 +9,5 @@ bool RtThread::Loop(int64_t ellapsed_ns) noexcept {
data_logger_->EmplaceData(ellapsed_ms, amplitude * cos(2 * M_PI / period * ellapsed_ms));

++iterations_;
return iterations_ >= max_iterations_;
return iterations_ >= max_iterations_ ? LoopControl::Stop : LoopControl::Continue;
}
2 changes: 1 addition & 1 deletion examples/message_passing_example/rt_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RtThread : public CyclicThread {
}

protected:
bool Loop(int64_t ellapsed_ns) noexcept final;
LoopControl Loop(int64_t ellapsed_ns) noexcept final;
};

#endif
4 changes: 2 additions & 2 deletions examples/mutex_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RTThread : public CyclicThread {
buf_(buf) {}

protected:
bool Loop(int64_t ellapsed_ns) noexcept final {
LoopControl Loop(int64_t ellapsed_ns) noexcept final {
constexpr double period = 5'000'000'000.0; // 5 seconds period
constexpr double amplitude = 1.0;

Expand All @@ -38,7 +38,7 @@ class RTThread : public CyclicThread {

buf_.Write(d);

return false;
return LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/ros2/publisher/complex_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RTROS2PublisherThread : public cactus_rt::CyclicThread, public cactus_rt::
}

protected:
bool Loop(int64_t elapsed_ns) noexcept override {
LoopControl Loop(int64_t elapsed_ns) noexcept override {
if (elapsed_ns - last_published_at_ > 10'000'000) {
last_published_at_ = elapsed_ns;

Expand All @@ -89,7 +89,7 @@ class RTROS2PublisherThread : public cactus_rt::CyclicThread, public cactus_rt::
);
}

return elapsed_ns > run_duration_;
return elapsed_ns > run_duration_ ? LoopControl::Stop : LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/ros2/publisher/simple_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RTROS2PublisherThread : public cactus_rt::CyclicThread, public cactus_rt::
}

protected:
bool Loop(int64_t elapsed_ns) noexcept override {
LoopControl Loop(int64_t elapsed_ns) noexcept override {
if (elapsed_ns - last_published_at_ > 10'000'000) {
last_published_at_ = elapsed_ns;

Expand All @@ -49,7 +49,7 @@ class RTROS2PublisherThread : public cactus_rt::CyclicThread, public cactus_rt::
LOG_INFO(Logger(), "{} integer {}", success ? "Published" : "Did not publish", msg.data);
}

return elapsed_ns > run_duration_;
return elapsed_ns > run_duration_ ? LoopControl::Stop : LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/ros2/subscriber/complex_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RTROS2SubscriberThread : public cactus_rt::CyclicThread, public cactus_rt:
}

protected:
bool Loop(int64_t elapsed_ns) noexcept override {
LoopControl Loop(int64_t elapsed_ns) noexcept override {
cactus_rt::ros2::StampedValue<MyCoefficientData> msg;
{
const auto span = Tracer().WithSpan("Subscription::ReadLatest");
Expand All @@ -74,7 +74,7 @@ class RTROS2SubscriberThread : public cactus_rt::CyclicThread, public cactus_rt:
last_msg_id_ = msg.id;
}

return elapsed_ns > run_duration_;
return elapsed_ns > run_duration_ ? LoopControl::Stop : LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/ros2/subscriber/simple_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RTROS2SubscriberThread : public cactus_rt::CyclicThread, public cactus_rt:
}

protected:
bool Loop(int64_t elapsed_ns) noexcept override {
LoopControl Loop(int64_t elapsed_ns) noexcept override {
cactus_rt::ros2::StampedValue<std_msgs::msg::Int64> msg;
{
const auto span = Tracer().WithSpan("Subscription::ReadLatest");
Expand All @@ -49,7 +49,7 @@ class RTROS2SubscriberThread : public cactus_rt::CyclicThread, public cactus_rt:
last_msg_id_ = msg.id;
}

return elapsed_ns > run_duration_;
return elapsed_ns > run_duration_ ? LoopControl::Stop : LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/signal_handling_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ExampleRTThread : public CyclicThread {
}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
return false;
return LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/simple_deadline_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class ExampleDeadlineThread : public CyclicThread {
}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
return false;
return LoopControl::Continue;
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/simple_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class ExampleRTThread : public CyclicThread {
* @return true if you want the thread to stop
* @return false if you want to thread to continue
*/
bool Loop(int64_t elapsed_ns) noexcept final {
LoopControl Loop(int64_t elapsed_ns) noexcept final {
// Code written in this function executes every 1 ms.

// This demonstrates the usage of the quill logger. This emits a log message every 1s.
LOG_INFO_LIMIT(std::chrono::seconds(1), Logger(), "Looping for {}", std::chrono::nanoseconds(elapsed_ns));
return false;
return LoopControl::Continue;
}

private:
Expand Down
8 changes: 4 additions & 4 deletions examples/tracing_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ExampleRTThread : public CyclicThread {
}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
if (loop_counter_ % 1000 == 0) {
LOG_INFO(Logger(), "Loop {}", loop_counter_);
Expand All @@ -51,7 +51,7 @@ class ExampleRTThread : public CyclicThread {
WasteTime(std::chrono::microseconds(1200));
}

return false;
return LoopControl::Continue;
}

private:
Expand Down Expand Up @@ -87,10 +87,10 @@ class SecondRTThread : public CyclicThread {
SecondRTThread() : CyclicThread("SecondRTThread", CreateThreadConfig()) {}

protected:
bool Loop(int64_t /*now*/) noexcept final {
LoopControl Loop(int64_t /*now*/) noexcept final {
const auto span = Tracer().WithSpan("Sense");
WasteTime(std::chrono::microseconds(2000));
return false;
return LoopControl::Continue;
}
};

Expand Down
9 changes: 7 additions & 2 deletions include/cactus_rt/cyclic_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class CyclicThread : public Thread {
struct timespec next_wakeup_time_;

public:
enum class LoopControl {
Continue = 0,
Stop,
};

/**
* @brief Create a cyclic thread
* @param name The thread name
Expand All @@ -24,9 +29,9 @@ class CyclicThread : public Thread {
/**
* @brief The custom loop function that executes one iteration of the code.
*
* @returns true if the loop should break, false if it should not
* @returns LoopControl::Stop if the loop should break, LoopControl::Continue if it should not
*/
virtual bool Loop(int64_t ellapsed_ns) noexcept = 0;
virtual LoopControl Loop(int64_t ellapsed_ns) noexcept = 0;

/**
* @brief Track the latency wakeup and loop latency.
Expand Down
4 changes: 2 additions & 2 deletions src/cactus_rt/cyclic_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void CyclicThread::Run() noexcept {
Tracer().StartSpan("Loop", "cactusrt", loop_start);
}

const bool should_stop = Loop(loop_start - Thread::StartMonotonicTimeNs());
const LoopControl loop_control = Loop(loop_start - Thread::StartMonotonicTimeNs());

loop_end = NowNs();

Expand Down Expand Up @@ -58,7 +58,7 @@ void CyclicThread::Run() noexcept {
next_wakeup_time_ns = next_wakeup_time_.tv_sec * 1'000'000'000 + next_wakeup_time_.tv_nsec;
}

if (should_stop) {
if (loop_control == LoopControl::Stop) {
break;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/tracing/helpers/mock_threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ MockCyclicThread::MockCyclicThread(
num_iterations_(num_iterations) {
}

bool MockCyclicThread::Loop(int64_t /* ellapsed_ns */) noexcept {
cactus_rt::CyclicThread::LoopControl MockCyclicThread::Loop(int64_t /* ellapsed_ns */) noexcept {
if (custom_loop_func_) {
custom_loop_func_(iterations_executed_);
} else {
WasteTime(20us);
}

iterations_executed_++;
return iterations_executed_ >= num_iterations_;
return iterations_executed_ >= num_iterations_ ? LoopControl::Stop : LoopControl::Continue;
}
2 changes: 1 addition & 1 deletion tests/tracing/helpers/mock_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MockCyclicThread : public cactus_rt::CyclicThread {
);

protected:
bool Loop(int64_t /* ellapsed_ns */) noexcept final;
LoopControl Loop(int64_t /* ellapsed_ns */) noexcept final;
};

#endif

0 comments on commit dc53ca0

Please sign in to comment.