21#include <dispenso/detail/timed_task_impl.h>
49 impl_ = std::move(other.impl_);
58 impl_->timesToRun.store(0, std::memory_order_release);
59 impl_->flags.fetch_or(detail::kFFlagsCancelled, std::memory_order_release);
68 impl_->flags.fetch_or(detail::kFFlagsDetached, std::memory_order_release);
77 return impl_->count.load(std::memory_order_acquire);
88 if (!impl_ || impl_->flags.load(std::memory_order_acquire) & detail::kFFlagsDetached) {
92 while (impl_->inProgress.load(std::memory_order_acquire)) {
101 template <
typename Schedulable,
typename F>
107 size_t timesToRun = 1,
110 detail::make_shared<detail::TimedTaskImpl>(
118 std::shared_ptr<detail::TimedTaskImpl> impl_;
120 friend class TimedTaskScheduler;
147 std::lock_guard<std::mutex> lk(queueMutex_);
148 priority_ = priority;
165 template <
typename Schedulable,
typename F>
171 size_t timesToRun = 1,
173 TimedTask task(sched, std::forward<F>(func), nextRunAbs, period, timesToRun, type);
174 addTimedTask(task.impl_);
187 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
189 schedule(Schedulable& sched, F&& func,
const std::chrono::duration<Rep, Period>& timeInFuture) {
190 return schedule(sched, std::forward<F>(func), toNextRun(timeInFuture));
203 template <
typename Schedulable,
typename Clock,
typename Duration,
typename F>
207 const std::chrono::time_point<Clock, Duration>& nextRunTime) {
208 return schedule(sched, std::forward<F>(func), toNextRun(nextRunTime));
224 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
228 const std::chrono::duration<Rep, Period>& timeInFuture,
229 const std::chrono::duration<Rep, Period>& period,
230 size_t timesToRun = std::numeric_limits<size_t>::max(),
233 sched, std::forward<F>(func), toNextRun(timeInFuture), toPeriod(period), timesToRun, type);
251 typename Schedulable,
260 const std::chrono::time_point<Clock, Duration>& nextRunTime,
261 const std::chrono::duration<Rep, Period>& period,
262 size_t timesToRun = std::numeric_limits<size_t>::max(),
265 sched, std::forward<F>(func), toNextRun(nextRunTime), toPeriod(period), timesToRun, type);
269 template <
class Rep,
class Period>
270 static double toNextRun(
const std::chrono::duration<Rep, Period>& timeInFuture) {
271 return getTime() + std::chrono::duration<double>(timeInFuture).count();
274 template <
typename Clock,
typename Duration>
275 static double toNextRun(
const std::chrono::time_point<Clock, Duration>& nextRunTime) {
276 auto curTime = Clock::now();
277 return toNextRun(nextRunTime - curTime);
280 template <
class Rep,
class Period>
281 static double toPeriod(
const std::chrono::duration<Rep, Period>& period) {
282 return std::chrono::duration<double>(period).count();
284 DISPENSO_DLL_ACCESS
void addTimedTask(std::shared_ptr<detail::TimedTaskImpl> task);
285 void timeQueueRunLoop();
287 void kickOffTask(std::shared_ptr<detail::TimedTaskImpl> next,
double curTime);
291 const std::shared_ptr<detail::TimedTaskImpl>& a,
292 const std::shared_ptr<detail::TimedTaskImpl>& b)
const {
293 return a->nextAbsTime > b->nextAbsTime;
299 std::mutex queueMutex_;
301 std::shared_ptr<detail::TimedTaskImpl>,
302 std::vector<std::shared_ptr<detail::TimedTaskImpl>>,
306 detail::EpochWaiter epoch_;
316DISPENSO_DLL_ACCESS TimedTaskScheduler& globalTimedTaskScheduler();
void setPriority(ThreadPriority priority)
TimedTask schedule(Schedulable &sched, F &&func, const std::chrono::duration< Rep, Period > &timeInFuture, const std::chrono::duration< Rep, Period > &period, size_t timesToRun=std::numeric_limits< size_t >::max(), TimedTaskType type=TimedTaskType::kNormal)
TimedTask schedule(Schedulable &sched, F &&func, double nextRunAbs, double period=0.0, size_t timesToRun=1, TimedTaskType type=TimedTaskType::kNormal)
TimedTask schedule(Schedulable &sched, F &&func, const std::chrono::time_point< Clock, Duration > &nextRunTime, const std::chrono::duration< Rep, Period > &period, size_t timesToRun=std::numeric_limits< size_t >::max(), TimedTaskType type=TimedTaskType::kNormal)
TimedTask schedule(Schedulable &sched, F &&func, const std::chrono::duration< Rep, Period > &timeInFuture)
TimedTask schedule(Schedulable &sched, F &&func, const std::chrono::time_point< Clock, Duration > &nextRunTime)
DISPENSO_DLL_ACCESS TimedTaskScheduler(ThreadPriority priority=ThreadPriority::kNormal)