21#include <dispenso/detail/timed_task_impl.h>
50 impl_ = std::move(other.impl_);
59 impl_->timesToRun.store(0, std::memory_order_release);
60 impl_->flags.fetch_or(detail::kFFlagsCancelled, std::memory_order_release);
69 impl_->flags.fetch_or(detail::kFFlagsDetached, std::memory_order_release);
78 return impl_->count.load(std::memory_order_acquire);
89 if (!impl_ || impl_->flags.load(std::memory_order_acquire) & detail::kFFlagsDetached) {
93 while (impl_->inProgress.load(std::memory_order_acquire)) {
102 template <
typename Schedulable,
typename F>
108 size_t timesToRun = 1,
111 detail::make_shared<detail::TimedTaskImpl>(
119 std::shared_ptr<detail::TimedTaskImpl> impl_;
121 friend class TimedTaskScheduler;
148 std::lock_guard<std::mutex> lk(queueMutex_);
149 priority_ = priority;
166 template <
typename Schedulable,
typename F>
172 size_t timesToRun = 1,
174 TimedTask task(sched, std::forward<F>(func), nextRunAbs, period, timesToRun, type);
175 addTimedTask(task.impl_);
188 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
190 schedule(Schedulable& sched, F&& func,
const std::chrono::duration<Rep, Period>& timeInFuture) {
191 return schedule(sched, std::forward<F>(func), toNextRun(timeInFuture));
204 template <
typename Schedulable,
typename Clock,
typename Duration,
typename F>
208 const std::chrono::time_point<Clock, Duration>& nextRunTime) {
209 return schedule(sched, std::forward<F>(func), toNextRun(nextRunTime));
225 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
229 const std::chrono::duration<Rep, Period>& timeInFuture,
230 const std::chrono::duration<Rep, Period>& period,
231 size_t timesToRun = std::numeric_limits<size_t>::max(),
234 sched, std::forward<F>(func), toNextRun(timeInFuture), toPeriod(period), timesToRun, type);
252 typename Schedulable,
261 const std::chrono::time_point<Clock, Duration>& nextRunTime,
262 const std::chrono::duration<Rep, Period>& period,
263 size_t timesToRun = std::numeric_limits<size_t>::max(),
266 sched, std::forward<F>(func), toNextRun(nextRunTime), toPeriod(period), timesToRun, type);
270 template <
class Rep,
class Period>
271 static double toNextRun(
const std::chrono::duration<Rep, Period>& timeInFuture) {
272 return getTime() + std::chrono::duration<double>(timeInFuture).count();
275 template <
typename Clock,
typename Duration>
276 static double toNextRun(
const std::chrono::time_point<Clock, Duration>& nextRunTime) {
277 auto curTime = Clock::now();
278 return toNextRun(nextRunTime - curTime);
281 template <
class Rep,
class Period>
282 static double toPeriod(
const std::chrono::duration<Rep, Period>& period) {
283 return std::chrono::duration<double>(period).count();
285 DISPENSO_DLL_ACCESS
void addTimedTask(std::shared_ptr<detail::TimedTaskImpl> task);
286 void timeQueueRunLoop();
288 void kickOffTask(std::shared_ptr<detail::TimedTaskImpl> next,
double curTime);
292 const std::shared_ptr<detail::TimedTaskImpl>& a,
293 const std::shared_ptr<detail::TimedTaskImpl>& b)
const {
294 return a->nextAbsTime > b->nextAbsTime;
300 std::mutex queueMutex_;
302 std::shared_ptr<detail::TimedTaskImpl>,
303 std::vector<std::shared_ptr<detail::TimedTaskImpl>>,
307 detail::EpochWaiter epoch_;
TimedTask(TimedTask &&other)
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)
DISPENSO_DLL_ACCESS TimedTaskScheduler(ThreadPriority priority=ThreadPriority::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 & globalTimedTaskScheduler()
DISPENSO_DLL_ACCESS double getTime()