// TJP COPYRIGHT HEADER // // Timer.h // algorithm // // Created by Timothy Prepscius on 1/19/18. // Copyright © 2018 Timothy Prepscius. All rights reserved. // #pragma once #include "Scheduler.hpp" #include #include #include #include #include #include #include "TimeSource.hpp" #include #include #include namespace timprepscius { namespace core { namespace time { struct SchedulerManual : Scheduler { struct EventImp; struct EventTime { Time time; u64 index; bool operator<(const EventTime &rhs) const { if (time == rhs.time) return index < rhs.index; return time < rhs.time; } bool operator==(const EventTime &rhs) const { return time == rhs.time && index == rhs.index; } } ; typedef WeakPtr WeakEventPtr; typedef core::MapAllocator Allocator; typedef Map,Allocator> Events; // typedef Map Events; struct EventImp : SchedulerEvent { SchedulerManual *parent; Time period; bool repeat; std::function f; EventTime at; RecursiveMutex mutex; Events::iterator position; EventImp( SchedulerManual *parent, Time period, bool repeat, std::function &&f ); bool operator<(const EventImp &rhs) const { return at < rhs.at; } void cancel () override; } ; Atomic nextIndex; StrongPtr timeSource; Mutex mutex; Events events; SchedulerManual(const StrongPtr &timeSource_); ~SchedulerManual(); SchedulerEventPtr schedule (Time period, bool repeat, std::function &&f) override; SchedulerEventPtr schedule (const StrongPtr &event, Time now); SchedulerEventPtr schedule_no_lock (const StrongPtr &event, Time now); void cancel(EventImp *event); Time durationUntilNextEvent(); Event changed; void update_has_lock(); bool running, stopped; Event stopped_; void run(); void cycle(); void stop(); void clear(); }; } // namespace } // namespace } // namespace