From 75c691900a692c38cfdb9947446ca438efca6ee6 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 16 Mar 2023 21:16:02 +0800 Subject: [PATCH] update --- core/components/timer/simple_timer.cpp | 63 ++++++++++++++++++++++++++++++++++ core/components/timer/simple_timer.hpp | 57 ++++++++++++++++++++++++++++++ module.cmake | 2 +- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 core/components/timer/simple_timer.cpp create mode 100644 core/components/timer/simple_timer.hpp diff --git a/core/components/timer/simple_timer.cpp b/core/components/timer/simple_timer.cpp new file mode 100644 index 0000000..188888f --- /dev/null +++ b/core/components/timer/simple_timer.cpp @@ -0,0 +1,63 @@ +#include "simple_timer.hpp" + +using namespace std; +using namespace iflytop; +using namespace core; + +void SimpleTimer::setTimeout(function function, int delay) { + lock_guard lock(m_mutex); + + if (m_thread) { + m_thread->join(); + m_thread = nullptr; + } + + m_thread.reset(new Thread(m_name, [=]() { + ThisThread thisThread; + zsteady_tp start = zsteady_clock().now(); + while (!thisThread.getExitFlag()) { + if (zsteady_clock().elapsedTimeMs(start) >= delay) { + function(); + break; + } + thisThread.sleepForMs(3); + } + })); +} + +bool SimpleTimer::isRunning() { + lock_guard lock(m_mutex); + + if (m_thread) { + return !m_thread->isWaitingForJoin(); + } + return false; +} +void SimpleTimer::stop() { + lock_guard lock(m_mutex); + if (m_thread) { + m_thread->join(); + m_thread = nullptr; + } +} + +void SimpleTimer::setInterval(function function, int interval) { + lock_guard lock(m_mutex); + + if (m_thread) { + m_thread->join(); + m_thread = nullptr; + } + + m_thread.reset(new Thread(m_name, [=]() { + ThisThread thisThread; + zsteady_tp start = zsteady_clock().now(); + while (!thisThread.getExitFlag()) { + if (zsteady_clock().elapsedTimeMs(start) >= interval) { + function(); + start = zsteady_clock().now(); + } + thisThread.sleepForMs(3); + } + })); +} \ No newline at end of file diff --git a/core/components/timer/simple_timer.hpp b/core/components/timer/simple_timer.hpp new file mode 100644 index 0000000..2fb0ab3 --- /dev/null +++ b/core/components/timer/simple_timer.hpp @@ -0,0 +1,57 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iflytopcpp/core/components/timeutils.hpp" +#include "iflytopcpp/core/spdlogfactory/logger.hpp" +#include "iflytopcpp/core/thread/thread.hpp" + +/** + * @brief + * + * service: SimpleTimer + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; +class SimpleTimer { + ENABLE_LOGGER(SimpleTimer); + + unique_ptr m_thread; + string m_name; + + mutex m_mutex; + + public: + SimpleTimer(string name) { m_name = name; }; + + void setTimeout(function func, int delay); + void setInterval(function func, int interval); + + void stop(); + + bool isRunning(); + + ~SimpleTimer() { + if (m_thread) m_thread->join(); + }; +}; +} // namespace iflytop \ No newline at end of file diff --git a/module.cmake b/module.cmake index 5d1d399..c20eb41 100644 --- a/module.cmake +++ b/module.cmake @@ -29,7 +29,7 @@ set(DEP_SRC dep/iflytopcpp/core/components/process/process_unix.cpp dep/iflytopcpp/core/components/process/process.cpp - + dep/iflytopcpp/core/components/timer/simple_timer.cpp # ) set(DEP_DEFINE ${DEP_DEFINE})