15 #ifndef SHAKA_EMBEDDED_CORE_TASK_RUNNER_H_ 16 #define SHAKA_EMBEDDED_CORE_TASK_RUNNER_H_ 18 #include <glog/logging.h> 25 #include <type_traits> 46 template <
typename Func>
53 uint64_t delay_ms,
int id,
bool loop);
57 virtual void Call() = 0;
81 template <
typename Func>
86 "Cannot pass Traceable objects to TaskRunner");
93 callback(
std::forward<Func>(callback)),
106 SetHelper<Func, Ret>::Set(&callback, &event);
110 std::shared_ptr<ThreadEvent<Ret>>
event;
113 template <
typename F,
typename R>
117 (*event)->SignalAllIfNotSet((*callback)());
120 template <
typename F>
121 struct SetHelper<F, void> {
125 (*event)->SignalAllIfNotSet();
130 template <
typename T>
132 template <
typename Func>
134 promise->set_value(callback());
139 template <
typename Func>
142 promise->set_value();
168 bool HasPendingWork()
const;
171 bool BelongsToCurrentThread()
const;
181 void WaitUntilFinished();
191 template <
typename Func>
194 if (BelongsToCurrentThread()) {
195 std::promise<Ret> promise;
198 return promise.get_future().share();
201 std::forward<Func>(callback))
218 template <
typename Func>
223 std::unique_lock<Mutex> lock(mutex_);
224 const int id = ++next_id_;
227 priority, 0,
id,
false);
228 tasks_.emplace_back(pending_task);
229 pending_task->event->SetProvider(&worker_);
231 return pending_task->event;
242 template <
typename Func>
244 std::unique_lock<Mutex> lock(mutex_);
245 const int id = ++next_id_;
266 template <
typename Func>
268 std::unique_lock<Mutex> lock(mutex_);
269 const int id = ++next_id_;
280 void CancelTimer(
int id);
292 void Run(std::function<
void(
RunLoop)> wrapper);
308 std::list<std::unique_ptr<impl::PendingTaskBase>> tasks_;
310 mutable Mutex mutex_;
313 std::atomic<bool> running_;
322 #endif // SHAKA_EMBEDDED_CORE_TASK_RUNNER_H_
const TaskPriority priority
static void CallAndResolve(Func &&callback, std::promise< T > *promise)
PendingTask(const util::Clock *clock, Func &&callback, const std::string &name, TaskPriority priority, uint64_t delay_ms, int id, bool loop)
static void CallAndResolve(Func &&callback, std::promise< void > *promise)
std::function< void()> RunLoop
typename std::result_of< Func()>::type Ret
std::shared_future< impl::RetOf< Func > > InvokeOrSchedule(Func &&callback)
typename std::result_of< Func()>::type RetOf
std::decay< Func >::type callback
int AddTimer(uint64_t delay_ms, Func &&callback)
int AddRepeatedTimer(uint64_t delay_ms, Func &&callback)
std::shared_ptr< ThreadEvent< impl::RetOf< Func > > > AddInternalTask(TaskPriority priority, const std::string &name, Func &&callback)
std::atomic< bool > should_remove
std::shared_ptr< ThreadEvent< Ret > > event