mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-22 00:23:33 +08:00
Refactored scheduler
This commit is contained in:
@@ -5,80 +5,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <uavcan/scheduler.hpp>
|
||||
#include <uavcan/internal/util.hpp>
|
||||
#include <uavcan/internal/linked_list.hpp>
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
|
||||
class Scheduler;
|
||||
class TimerBase;
|
||||
class Timer;
|
||||
|
||||
struct TimerEvent
|
||||
{
|
||||
uint64_t scheduled_monotonic_deadline;
|
||||
uint64_t monotonic_timestamp;
|
||||
TimerBase* timer;
|
||||
Timer* timer;
|
||||
|
||||
TimerEvent(uint64_t scheduled_monotonic_deadline, uint64_t monotonic_timestamp, TimerBase& timer)
|
||||
TimerEvent(Timer* timer, uint64_t scheduled_monotonic_deadline, uint64_t monotonic_timestamp)
|
||||
: scheduled_monotonic_deadline(scheduled_monotonic_deadline)
|
||||
, monotonic_timestamp(monotonic_timestamp)
|
||||
, timer(&timer)
|
||||
, timer(timer)
|
||||
{ }
|
||||
};
|
||||
|
||||
|
||||
class TimerBase : public LinkedListNode<TimerBase>, Noncopyable
|
||||
class Timer : private MonotonicDeadlineHandler
|
||||
{
|
||||
friend class Scheduler;
|
||||
|
||||
uint64_t monotonic_deadline_;
|
||||
uint64_t period_;
|
||||
Scheduler& scheduler_;
|
||||
|
||||
void handleOneShotTimeout(uint64_t ts_monotonic);
|
||||
void genericStart();
|
||||
void onMonotonicDeadline(uint64_t monotonic_timestamp);
|
||||
|
||||
public:
|
||||
static const uint64_t InfinitePeriod = 0xFFFFFFFFFFFFFFFFUL;
|
||||
|
||||
TimerBase(Scheduler& scheduler)
|
||||
: monotonic_deadline_(0)
|
||||
using MonotonicDeadlineHandler::stop;
|
||||
using MonotonicDeadlineHandler::isRunning;
|
||||
using MonotonicDeadlineHandler::getMonotonicDeadline;
|
||||
|
||||
Timer(Scheduler& scheduler)
|
||||
: MonotonicDeadlineHandler(scheduler)
|
||||
, period_(InfinitePeriod)
|
||||
, scheduler_(scheduler)
|
||||
{ }
|
||||
|
||||
virtual ~TimerBase() { stop(); }
|
||||
|
||||
uint64_t getMonotonicDeadline() const { return monotonic_deadline_; }
|
||||
|
||||
void startOneShotDeadline(uint64_t monotonic_deadline_usec);
|
||||
void startOneShotDelay(uint64_t delay_usec);
|
||||
void startOneShotWithDeadline(uint64_t monotonic_deadline);
|
||||
void startOneShotWithDelay(uint64_t delay_usec);
|
||||
void startPeriodic(uint64_t period_usec);
|
||||
|
||||
void stop();
|
||||
bool isRunning() const;
|
||||
|
||||
uint64_t getPeriod() const { return period_; }
|
||||
|
||||
virtual void onTimerEvent(TimerEvent& event) = 0;
|
||||
virtual void onTimerEvent(const TimerEvent& event) = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename Functor>
|
||||
class Timer : public TimerBase
|
||||
class TimerEventForwarder : public Timer
|
||||
{
|
||||
Functor functor_;
|
||||
|
||||
public:
|
||||
Timer(Scheduler& node, Functor functor)
|
||||
: TimerBase(node)
|
||||
TimerEventForwarder(Scheduler& node, Functor functor)
|
||||
: Timer(node)
|
||||
, functor_(functor)
|
||||
{ }
|
||||
|
||||
const Functor& getFunctor() const { return functor_; }
|
||||
|
||||
void onTimerEvent(TimerEvent& event)
|
||||
void onTimerEvent(const TimerEvent& event)
|
||||
{
|
||||
functor_(event);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user