19#include <dispenso/detail/timed_task_impl.h>
21#include <dispenso/timing.h>
28enum class TimedTaskType {
47 impl_ = std::move(other.impl_);
56 impl_->timesToRun.store(0, std::memory_order_release);
57 impl_->flags.fetch_or(detail::kFFlagsCancelled, std::memory_order_release);
66 impl_->flags.fetch_or(detail::kFFlagsDetached, std::memory_order_release);
75 return impl_->count.load(std::memory_order_acquire);
86 if (!impl_ || impl_->flags.load(std::memory_order_acquire) & detail::kFFlagsDetached) {
90 while (impl_->inProgress.load(std::memory_order_acquire)) {
99 template <
typename Schedulable,
typename F>
105 size_t timesToRun = 1,
106 TimedTaskType type = TimedTaskType::kNormal)
107 : impl_(detail::make_shared<detail::TimedTaskImpl>(
113 type == TimedTaskType::kSteady)) {}
115 std::shared_ptr<detail::TimedTaskImpl> impl_;
117 friend class TimedTaskScheduler;
144 std::lock_guard<std::mutex> lk(queueMutex_);
145 priority_ = priority;
162 template <
typename Schedulable,
typename F>
168 size_t timesToRun = 1,
169 TimedTaskType type = TimedTaskType::kNormal) {
170 TimedTask task(sched, std::forward<F>(func), nextRunAbs, period, timesToRun, type);
171 addTimedTask(task.impl_);
184 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
186 schedule(Schedulable& sched, F&& func,
const std::chrono::duration<Rep, Period>& timeInFuture) {
187 return schedule(sched, std::forward<F>(func), toNextRun(timeInFuture));
200 template <
typename Schedulable,
typename Clock,
typename Duration,
typename F>
204 const std::chrono::time_point<Clock, Duration>& nextRunTime) {
205 return schedule(sched, std::forward<F>(func), toNextRun(nextRunTime));
221 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
225 const std::chrono::duration<Rep, Period>& timeInFuture,
226 const std::chrono::duration<Rep, Period>& period,
227 size_t timesToRun = std::numeric_limits<size_t>::max(),
228 TimedTaskType type = TimedTaskType::kNormal) {
230 sched, std::forward<F>(func), toNextRun(timeInFuture), toPeriod(period), timesToRun, type);
248 typename Schedulable,
257 const std::chrono::time_point<Clock, Duration>& nextRunTime,
258 const std::chrono::duration<Rep, Period>& period,
259 size_t timesToRun = std::numeric_limits<size_t>::max(),
260 TimedTaskType type = TimedTaskType::kNormal) {
262 sched, std::forward<F>(func), toNextRun(nextRunTime), toPeriod(period), timesToRun, type);
266 template <
class Rep,
class Period>
267 static double toNextRun(
const std::chrono::duration<Rep, Period>& timeInFuture) {
268 return getTime() + std::chrono::duration<double>(timeInFuture).count();
271 template <
typename Clock,
typename Duration>
272 static double toNextRun(
const std::chrono::time_point<Clock, Duration>& nextRunTime) {
273 auto curTime = Clock::now();
274 return toNextRun(nextRunTime - curTime);
277 template <
class Rep,
class Period>
278 static double toPeriod(
const std::chrono::duration<Rep, Period>& period) {
279 return std::chrono::duration<double>(period).count();
281 DISPENSO_DLL_ACCESS
void addTimedTask(std::shared_ptr<detail::TimedTaskImpl> task);
282 void timeQueueRunLoop();
284 void kickOffTask(std::shared_ptr<detail::TimedTaskImpl> next,
double curTime);
288 const std::shared_ptr<detail::TimedTaskImpl>& a,
289 const std::shared_ptr<detail::TimedTaskImpl>& b)
const {
290 return a->nextAbsTime > b->nextAbsTime;
296 std::mutex queueMutex_;
298 std::shared_ptr<detail::TimedTaskImpl>,
299 std::vector<std::shared_ptr<detail::TimedTaskImpl>>,
303 detail::EpochWaiter epoch_;
313DISPENSO_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)