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>
106 TimedTaskType
type = TimedTaskType::kNormal)
113 type == TimedTaskType::kSteady)) {}
115 std::shared_ptr<detail::TimedTaskImpl> impl_;
117 friend class TimedTaskScheduler;
144 std::lock_guard<std::mutex>
lk(queueMutex_);
162 template <
typename Schedulable,
typename F>
169 TimedTaskType
type = TimedTaskType::kNormal) {
171 addTimedTask(
task.impl_);
184 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
200 template <
typename Schedulable,
typename Clock,
typename Duration,
typename F>
204 const std::chrono::time_point<Clock, Duration>&
nextRunTime) {
221 template <
typename Schedulable,
typename Rep,
typename Period,
typename F>
226 const std::chrono::duration<Rep, Period>&
period,
227 size_t timesToRun = std::numeric_limits<size_t>::max(),
228 TimedTaskType
type = TimedTaskType::kNormal) {
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) {
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)
detail::OpResult< T > OpResult