Skip to content

Commit

Permalink
[feat] optimize GDaemon, can change wait loop ts when running.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Nov 15, 2024
1 parent 4ce91a2 commit 8fe83a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/GraphCtrl/GraphDaemon/GDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ CGRAPH_NAMESPACE_BEGIN
CStatus GDaemon::init() {
CGRAPH_FUNCTION_BEGIN
timer_.start(interval_, [this] {
this->daemonTask(param_);
daemonTask(param_);
}, [this] {
return modify(param_);
});
CGRAPH_FUNCTION_END
}
Expand All @@ -30,4 +32,9 @@ CMSec GDaemon::getInterval() const {
return interval_;
}


CMSec GDaemon::modify(GDaemonParamPtr param) {
return 0;
}

CGRAPH_NAMESPACE_END
8 changes: 8 additions & 0 deletions src/GraphCtrl/GraphDaemon/GDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class GDaemon : public GDaemonObject {
*/
virtual CVoid daemonTask(GDaemonParamPtr param) = 0;

/**
* 修改下一次休眠的时间
* @param param
* @return
* @notice 返回值 <=0 的时候,不生效。 > 0 的时候,仅针对下一次生效
*/
virtual CMSec modify(GDaemonParamPtr param);

/**
* 获取设置的延时信息
* @return
Expand Down
37 changes: 25 additions & 12 deletions src/UtilsCtrl/Timer/UTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ class UTimer : public UtilsObject {

/**
* 开始执行定时器
* @param interval 间隔时间,单位ms
* @tparam TTask
* @tparam TMod
* @param interval
* @param task
* @param modify
* @return
*/
template<typename FunctionType>
CVoid start(CMSec interval, const FunctionType& task) {
template<typename TTask, typename TMod>
CVoid start(CMSec interval, const TTask& task, const TMod& modify) {
if (!is_stop_.exchange(false)) {
return; // 如果正在执行中,则无法继续执行
}
Expand All @@ -38,21 +42,30 @@ class UTimer : public UtilsObject {
* std::launch::async:在调用async就开始创建线程。
* std::launch::deferred:延迟加载方式创建线程。调用async时不创建线程,直到调用了future的get或者wait时才创建线程。
*/
future_ = std::async(std::launch::async, [this, task]() {
future_ = std::async(std::launch::async, [this, task, modify]() {
while (!is_stop_) {
CGRAPH_UNIQUE_LOCK lk(mutex_);
auto result = cv_.wait_for(lk, std::chrono::milliseconds(left_interval_));
if (std::cv_status::timeout == result && !is_stop_) {
CMSec start = CGRAPH_GET_CURRENT_MS();
task();
CMSec span = CGRAPH_GET_CURRENT_MS() - start;
/**
* 如果任务执行时间 < 设定的时间,则消除任务耗时影响
* 如果任务执行时间 > 设定的时间,则继续sleep设定时长
*/
left_interval_ = (origin_interval_ > span)
? (origin_interval_ - span)
: (origin_interval_);
CMSec ms = modify();
if (ms > 0) {
/**
* 如果有时间修改函数,并且修改了时间
* 则修改下一次的休眠时间
*/
left_interval_ = ms;
} else {
CMSec span = CGRAPH_GET_CURRENT_MS() - start;
/**
* 如果任务执行时间 < 设定的时间,则消除任务耗时影响
* 如果任务执行时间 > 设定的时间,则继续sleep设定时长
*/
left_interval_ = (origin_interval_ > span)
? (origin_interval_ - span)
: (origin_interval_);
}
}
}
});
Expand Down

0 comments on commit 8fe83a1

Please sign in to comment.