diff --git a/examples/lockless_example/main.cc b/examples/lockless_example/main.cc index af1538a..229e121 100644 --- a/examples/lockless_example/main.cc +++ b/examples/lockless_example/main.cc @@ -1,8 +1,6 @@ #include #include -#include - using cactus_rt::App; using cactus_rt::CyclicThread; using cactus_rt::Thread; @@ -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_) { @@ -99,7 +97,7 @@ class RTThread : public CyclicThread { ); } - return false; + return LoopControl::Continue; } }; diff --git a/examples/logging_example/main.cc b/examples/logging_example/main.cc index 51174be..7b6cef1 100644 --- a/examples/logging_example/main.cc +++ b/examples/logging_example/main.cc @@ -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; } }; diff --git a/examples/message_passing_example/data_logger_thread.h b/examples/message_passing_example/data_logger_thread.h index 6c36be4..d40648b 100644 --- a/examples/message_passing_example/data_logger_thread.h +++ b/examples/message_passing_example/data_logger_thread.h @@ -7,7 +7,6 @@ #include #include #include -#include #include using moodycamel::ReaderWriterQueue; diff --git a/examples/message_passing_example/rt_thread.cc b/examples/message_passing_example/rt_thread.cc index 6e7541e..6028bd8 100644 --- a/examples/message_passing_example/rt_thread.cc +++ b/examples/message_passing_example/rt_thread.cc @@ -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(ellapsed_ns) / 1'000'000.0; const double period = 1000.0; @@ -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; } diff --git a/examples/message_passing_example/rt_thread.h b/examples/message_passing_example/rt_thread.h index d83588a..c9368b8 100644 --- a/examples/message_passing_example/rt_thread.h +++ b/examples/message_passing_example/rt_thread.h @@ -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 diff --git a/examples/mutex_example/main.cc b/examples/mutex_example/main.cc index 8b14bce..cfcc43c 100644 --- a/examples/mutex_example/main.cc +++ b/examples/mutex_example/main.cc @@ -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; @@ -38,7 +38,7 @@ class RTThread : public CyclicThread { buf_.Write(d); - return false; + return LoopControl::Continue; } }; diff --git a/examples/ros2/publisher/complex_data.cc b/examples/ros2/publisher/complex_data.cc index 0db4cf6..0a14f9b 100644 --- a/examples/ros2/publisher/complex_data.cc +++ b/examples/ros2/publisher/complex_data.cc @@ -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; @@ -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; } }; diff --git a/examples/ros2/publisher/simple_data.cc b/examples/ros2/publisher/simple_data.cc index 6904634..8f896b0 100644 --- a/examples/ros2/publisher/simple_data.cc +++ b/examples/ros2/publisher/simple_data.cc @@ -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; @@ -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; } }; diff --git a/examples/ros2/subscriber/complex_data.cc b/examples/ros2/subscriber/complex_data.cc index 1c514ed..c6f51f4 100644 --- a/examples/ros2/subscriber/complex_data.cc +++ b/examples/ros2/subscriber/complex_data.cc @@ -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 msg; { const auto span = Tracer().WithSpan("Subscription::ReadLatest"); @@ -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; } }; diff --git a/examples/ros2/subscriber/simple_data.cc b/examples/ros2/subscriber/simple_data.cc index ebc8079..9fc4ea9 100644 --- a/examples/ros2/subscriber/simple_data.cc +++ b/examples/ros2/subscriber/simple_data.cc @@ -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 msg; { const auto span = Tracer().WithSpan("Subscription::ReadLatest"); @@ -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; } }; diff --git a/examples/signal_handling_example/main.cc b/examples/signal_handling_example/main.cc index a77116b..5faae95 100644 --- a/examples/signal_handling_example/main.cc +++ b/examples/signal_handling_example/main.cc @@ -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; } }; diff --git a/examples/simple_deadline_example/main.cc b/examples/simple_deadline_example/main.cc index b2e2f0a..56eb4b5 100644 --- a/examples/simple_deadline_example/main.cc +++ b/examples/simple_deadline_example/main.cc @@ -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; } }; diff --git a/examples/simple_example/main.cc b/examples/simple_example/main.cc index 7900acf..4775ccd 100644 --- a/examples/simple_example/main.cc +++ b/examples/simple_example/main.cc @@ -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: diff --git a/examples/tracing_example/main.cc b/examples/tracing_example/main.cc index 1d8e0d7..f588c6a 100644 --- a/examples/tracing_example/main.cc +++ b/examples/tracing_example/main.cc @@ -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_); @@ -51,7 +51,7 @@ class ExampleRTThread : public CyclicThread { WasteTime(std::chrono::microseconds(1200)); } - return false; + return LoopControl::Continue; } private: @@ -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; } }; diff --git a/include/cactus_rt/cyclic_thread.h b/include/cactus_rt/cyclic_thread.h index d7e3f44..9dca435 100644 --- a/include/cactus_rt/cyclic_thread.h +++ b/include/cactus_rt/cyclic_thread.h @@ -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 @@ -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. diff --git a/src/cactus_rt/cyclic_thread.cc b/src/cactus_rt/cyclic_thread.cc index bcc89c6..2421394 100644 --- a/src/cactus_rt/cyclic_thread.cc +++ b/src/cactus_rt/cyclic_thread.cc @@ -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(); @@ -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; } diff --git a/tests/tracing/helpers/mock_threads.cc b/tests/tracing/helpers/mock_threads.cc index 5bca023..9d3f2cd 100644 --- a/tests/tracing/helpers/mock_threads.cc +++ b/tests/tracing/helpers/mock_threads.cc @@ -74,7 +74,7 @@ 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 { @@ -82,5 +82,5 @@ bool MockCyclicThread::Loop(int64_t /* ellapsed_ns */) noexcept { } iterations_executed_++; - return iterations_executed_ >= num_iterations_; + return iterations_executed_ >= num_iterations_ ? LoopControl::Stop : LoopControl::Continue; } diff --git a/tests/tracing/helpers/mock_threads.h b/tests/tracing/helpers/mock_threads.h index fd0b7a9..08d4b76 100644 --- a/tests/tracing/helpers/mock_threads.h +++ b/tests/tracing/helpers/mock_threads.h @@ -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