feat support void function in timer callback

This commit is contained in:
tqcq 2024-05-01 09:04:21 +00:00
parent 4b87ce5219
commit b500dbfcc5
3 changed files with 15 additions and 11 deletions

View File

@ -54,16 +54,6 @@ private:
scoped_refptr<PendingTaskSafetyFlag> flag_;
};
template<typename F, typename... Args, typename = EnableIfT<!std::is_void<InvokeResultT<F, Args...>>::value>>
std::function<InvokeResultT<F, Args...>(Args &&...)>
SafeTask(scoped_refptr<PendingTaskSafetyFlag> flag, F &&f)
{
return [flag, f](Args &&...args) mutable -> InvokeResultT<F, Args...> {
if (flag->alive()) { return f(std::forward<Args>(args)...); }
return {};
};
}
template<typename F, typename... Args, typename = EnableIfT<std::is_void<InvokeResultT<F, Args...>>::value>>
std::function<void(Args &&...)>
SafeTask(scoped_refptr<PendingTaskSafetyFlag> flag, F &&f)

View File

@ -109,7 +109,7 @@ Timer::Trigger(TimerGeneration generation)
}
}
std::unique_ptr<Timer, TimerThreadDeleter>
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)
{

View File

@ -30,6 +30,7 @@ public:
using Ptr = std::unique_ptr<Timer, TimerThreadDeleter>;
using OnExpired = std::function<sled::optional<DurationMs>()>;
using OnVoidExpired = std::function<void()>;
Timer(const Timer &) = delete;
Timer &operator=(const Timer &) = delete;
~Timer();
@ -78,6 +79,8 @@ public:
std::unique_ptr<Timer, TimerThreadDeleter>
CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner = nullptr);
std::unique_ptr<Timer, TimerThreadDeleter>
CreateTimer(const std::string &name, Timer::OnVoidExpired on_void_expired, TaskQueueBase *owner = nullptr);
void HandleTimeout(TimeoutID timeout_id);
private: