feat support Async Future scheduler

This commit is contained in:
tqcq 2024-05-01 06:48:38 +00:00
parent 28009f189a
commit 2c1764658e

View File

@ -361,10 +361,11 @@ public:
} }
template<typename Func, typename = EnableIfT<IsInvocable<Func>::value>> template<typename Func, typename = EnableIfT<IsInvocable<Func>::value>>
static Future<T, FailureT> Async(Func &&f) noexcept static Future<T, FailureT> Async(Func &&f, TaskQueueBase *scheduler = sled::GetDefaultScheduler()) noexcept
{ {
SLED_ASSERT(scheduler != nullptr, "TaskQueue is not valid");
Future<T, FailureT> result = Future<T, FailureT>::Create(); Future<T, FailureT> result = Future<T, FailureT>::Create();
sled::GetDefaultScheduler()->PostTask([result, f]() mutable noexcept { scheduler->PostTask([result, f]() mutable noexcept {
try { try {
result.FillSuccess(f()); result.FillSuccess(f());
} catch (const std::exception &e) { } catch (const std::exception &e) {
@ -376,9 +377,10 @@ public:
return result; return result;
} }
static Future<T, FailureT> AsyncValue(const T &value) noexcept static Future<T, FailureT> AsyncValue(const T &value,
TaskQueueBase *scheduler = sled::GetDefaultScheduler()) noexcept
{ {
return Async([value]() { return value; }); return Async([value]() { return value; }, scheduler);
} }
static Future<typename std::decay<T>::type, FailureT> Successful(T &&value) noexcept static Future<typename std::decay<T>::type, FailureT> Successful(T &&value) noexcept