Files
core_misc/tjp/core/time-obsolete/SchedulerManual.hpp
Timothy Prepscius fa54be052a flatten 20260225
2026-02-25 12:39:24 -05:00

122 lines
2.2 KiB
C++

// 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 <core/types/Types.h>
#include <core/ptr/Ptr.hpp>
#include <core/threads/Lock.h>
#include <core/allocator/MapAllocator.hpp>
#include <core/containers/Map.h>
#include <core/threads/Atomic.h>
#include "TimeSource.hpp"
#include <set>
#include <map>
#include <atomic>
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<EventImp> WeakEventPtr;
typedef core::MapAllocator<EventTime, WeakEventPtr> Allocator;
typedef Map<EventTime, WeakEventPtr,std::less<EventTime>,Allocator> Events;
// typedef Map<EventTime, WeakEventPtr> Events;
struct EventImp : SchedulerEvent
{
SchedulerManual *parent;
Time period;
bool repeat;
std::function<void()> f;
EventTime at;
RecursiveMutex mutex;
Events::iterator position;
EventImp(
SchedulerManual *parent,
Time period,
bool repeat,
std::function<void()> &&f
);
bool operator<(const EventImp &rhs) const
{
return at < rhs.at;
}
void cancel () override;
} ;
Atomic<u64> nextIndex;
StrongPtr<TimeSource> timeSource;
Mutex mutex;
Events events;
SchedulerManual(const StrongPtr<TimeSource> &timeSource_);
~SchedulerManual();
SchedulerEventPtr schedule (Time period, bool repeat, std::function<void()> &&f) override;
SchedulerEventPtr schedule (const StrongPtr<EventImp> &event, Time now);
SchedulerEventPtr schedule_no_lock (const StrongPtr<EventImp> &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