From b500dbfcc5833175e235b8339397c10aa2729133 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 1 May 2024 09:04:21 +0000 Subject: [PATCH] feat support void function in timer callback --- src/sled/task_queue/pending_task_safety_flag.h | 10 ---------- src/sled/timer/timer.cc | 13 ++++++++++++- src/sled/timer/timer.h | 3 +++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/sled/task_queue/pending_task_safety_flag.h b/src/sled/task_queue/pending_task_safety_flag.h index 502cdaa..ba2815f 100644 --- a/src/sled/task_queue/pending_task_safety_flag.h +++ b/src/sled/task_queue/pending_task_safety_flag.h @@ -54,16 +54,6 @@ private: scoped_refptr flag_; }; -template>::value>> -std::function(Args &&...)> -SafeTask(scoped_refptr flag, F &&f) -{ - return [flag, f](Args &&...args) mutable -> InvokeResultT { - if (flag->alive()) { return f(std::forward(args)...); } - return {}; - }; -} - template>::value>> std::function SafeTask(scoped_refptr flag, F &&f) diff --git a/src/sled/timer/timer.cc b/src/sled/timer/timer.cc index e1d6bce..0d5cbf3 100644 --- a/src/sled/timer/timer.cc +++ b/src/sled/timer/timer.cc @@ -109,7 +109,7 @@ Timer::Trigger(TimerGeneration generation) } } -std::unique_ptr +Timer::Ptr TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner) { next_id_ = TimerID(next_id_ + 1); @@ -128,6 +128,17 @@ TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired, return unique_timer; } +Timer::Ptr +TimerManager::CreateTimer(const std::string &name, Timer::OnVoidExpired on_void_expired, TaskQueueBase *owner) +{ + Timer::OnExpired on_expired = [on_void_expired]() { + on_void_expired(); + return sled::nullopt; + }; + + return CreateTimer(name, on_expired, owner); +} + void TimerManager::HandleTimeout(TimeoutID id) { diff --git a/src/sled/timer/timer.h b/src/sled/timer/timer.h index 05c6aa7..61eddc1 100644 --- a/src/sled/timer/timer.h +++ b/src/sled/timer/timer.h @@ -30,6 +30,7 @@ public: using Ptr = std::unique_ptr; using OnExpired = std::function()>; + using OnVoidExpired = std::function; Timer(const Timer &) = delete; Timer &operator=(const Timer &) = delete; ~Timer(); @@ -78,6 +79,8 @@ public: std::unique_ptr CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner = nullptr); + std::unique_ptr + CreateTimer(const std::string &name, Timer::OnVoidExpired on_void_expired, TaskQueueBase *owner = nullptr); void HandleTimeout(TimeoutID timeout_id); private: