From 75cfd93ad1e4fc1e565fe92b464202bd3f48c9ec Mon Sep 17 00:00:00 2001 From: Levi Armstrong Date: Fri, 13 Sep 2024 14:07:05 -0500 Subject: [PATCH] Add timer class with callback --- tesseract_common/CMakeLists.txt | 3 +- .../include/tesseract_common/timer.h | 67 +++++++++++++++++++ tesseract_common/src/timer.cpp | 53 +++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tesseract_common/include/tesseract_common/timer.h create mode 100644 tesseract_common/src/timer.cpp diff --git a/tesseract_common/CMakeLists.txt b/tesseract_common/CMakeLists.txt index 4908038e196..f884bfbde5c 100644 --- a/tesseract_common/CMakeLists.txt +++ b/tesseract_common/CMakeLists.txt @@ -82,7 +82,8 @@ add_library( src/plugin_info.cpp src/resource_locator.cpp src/types.cpp - src/stopwatch.cpp) + src/stopwatch.cpp + src/timer.cpp) target_link_libraries( ${PROJECT_NAME} PUBLIC Eigen3::Eigen diff --git a/tesseract_common/include/tesseract_common/timer.h b/tesseract_common/include/tesseract_common/timer.h new file mode 100644 index 00000000000..74272666cf4 --- /dev/null +++ b/tesseract_common/include/tesseract_common/timer.h @@ -0,0 +1,67 @@ +/** + * @file timer.h + * @brief Simple timer class using chrono and thread + * + * @author Levi Armstrong + * @date February 2, 2021 + * @version TODO + * @bug No known bugs + * + * @copyright Copyright (c) 2021, Southwest Research Institute + * + * @par License + * Software License Agreement (Apache License) + * @par + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * @par + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TESSERACT_COMMON_TIMER_H +#define TESSERACT_COMMON_TIMER_H + +#include +TESSERACT_COMMON_IGNORE_WARNINGS_PUSH +#include +#include +#include +#include +#include +TESSERACT_COMMON_IGNORE_WARNINGS_POP + +namespace tesseract_common +{ +/** @brief A timer which calls a callback every interval on a separate thread */ +class Timer +{ +public: + Timer() = default; + ~Timer(); + Timer(const Timer&) = delete; + Timer& operator=(const Timer&) = delete; + Timer(Timer&&) = delete; + Timer& operator=(Timer&&) = delete; + + /** + * @brief Start the timer with a callback function and a std::chrono::duration interval + * @param callback The callback called every time the timer expires + * @param interval The interval at which the timer triggers + */ + void start(const std::function& callback, std::chrono::steady_clock::duration interval); + + /** @brief Stop the timer */ + void stop(); + +private: + std::atomic running_{ false }; + std::thread timer_thread_; +}; + +} // namespace tesseract_common +#endif // TESSERACT_COMMON_TIMER_H diff --git a/tesseract_common/src/timer.cpp b/tesseract_common/src/timer.cpp new file mode 100644 index 00000000000..24532e7fed3 --- /dev/null +++ b/tesseract_common/src/timer.cpp @@ -0,0 +1,53 @@ +/** + * @file timer.cpp + * @brief Simple timer class using chrono and thread + * + * @author Levi Armstrong + * @date February 2, 2021 + * @version TODO + * @bug No known bugs + * + * @copyright Copyright (c) 2021, Southwest Research Institute + * + * @par License + * Software License Agreement (Apache License) + * @par + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * @par + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace tesseract_common +{ +Timer::~Timer() { stop(); } + +void Timer::start(const std::function& callback, std::chrono::steady_clock::duration interval) +{ + running_ = true; + timer_thread_ = std::thread([this, callback, interval]() { + while (running_) + { + auto next_time = std::chrono::steady_clock::now() + interval; + callback(); // Call the provided function + std::this_thread::sleep_until(next_time); + } + }); +} + +void Timer::stop() +{ + running_ = false; + if (timer_thread_.joinable()) + timer_thread_.join(); +} + +} // namespace tesseract_common