mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-18 01:03:33 +08:00
Scheduler
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uavcan/timer.hpp>
|
||||
#include <uavcan/internal/linked_list.hpp>
|
||||
#include <uavcan/internal/transport/dispatcher.hpp>
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
|
||||
class Scheduler : Noncopyable
|
||||
{
|
||||
enum { DefaultTimerResolutionMs = 5 };
|
||||
enum { DefaultCleanupPeriodMs = 1000 };
|
||||
|
||||
LinkedListRoot<TimerBase> ordered_timers_; // Ordered by deadline, lowest first
|
||||
Dispatcher dispatcher_;
|
||||
uint64_t prev_cleanup_ts_;
|
||||
uint64_t timer_resolution_;
|
||||
uint64_t cleanup_period_;
|
||||
|
||||
uint64_t computeDispatcherSpinDeadline(uint64_t spin_deadline) const;
|
||||
uint64_t pollTimersAndGetMonotonicTimestamp();
|
||||
void pollCleanup(uint64_t mono_ts, uint32_t num_frames_processed_with_last_spin);
|
||||
|
||||
public:
|
||||
Scheduler(ICanDriver& can_driver, IAllocator& allocator, ISystemClock& sysclock, IOutgoingTransferRegistry& otr,
|
||||
NodeID self_node_id)
|
||||
: dispatcher_(can_driver, allocator, sysclock, otr, self_node_id)
|
||||
, prev_cleanup_ts_(sysclock.getMonotonicMicroseconds())
|
||||
, timer_resolution_(DefaultTimerResolutionMs * 1000)
|
||||
, cleanup_period_(DefaultCleanupPeriodMs * 1000)
|
||||
{ }
|
||||
|
||||
int spin(uint64_t monotonic_deadline);
|
||||
|
||||
void registerOneShotTimer(TimerBase* timer);
|
||||
void unregisterOneShotTimer(TimerBase* timer);
|
||||
bool isOneShotTimerRegistered(const TimerBase* timer) const;
|
||||
unsigned int getNumOneShotTimers() const { return ordered_timers_.getLength(); }
|
||||
|
||||
Dispatcher& getDispatcher() { return dispatcher_; }
|
||||
|
||||
ISystemClock& getSystemClock() { return dispatcher_.getSystemClock(); }
|
||||
uint64_t getMonotonicTimestamp() const { return dispatcher_.getSystemClock().getMonotonicMicroseconds(); }
|
||||
uint64_t getUtcTimestamp() const { return dispatcher_.getSystemClock().getUtcMicroseconds(); }
|
||||
|
||||
uint64_t getTimerResolution() const { return timer_resolution_; }
|
||||
void setTimerResolution(uint64_t res_usec) { timer_resolution_ = res_usec; }
|
||||
|
||||
uint64_t getCleanupPeriod() const { return cleanup_period_; }
|
||||
void setCleanupPeriod(uint64_t period_usec) { cleanup_period_ = period_usec; }
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <uavcan/internal/util.hpp>
|
||||
#include <uavcan/internal/linked_list.hpp>
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
|
||||
class Scheduler;
|
||||
class TimerBase;
|
||||
|
||||
struct TimerEvent
|
||||
{
|
||||
uint64_t scheduled_monotonic_deadline;
|
||||
uint64_t monotonic_timestamp;
|
||||
TimerBase* timer;
|
||||
|
||||
TimerEvent(uint64_t scheduled_monotonic_deadline, uint64_t monotonic_timestamp, TimerBase& timer)
|
||||
: scheduled_monotonic_deadline(scheduled_monotonic_deadline)
|
||||
, monotonic_timestamp(monotonic_timestamp)
|
||||
, timer(&timer)
|
||||
{ }
|
||||
};
|
||||
|
||||
|
||||
class TimerBase : public LinkedListNode<TimerBase>, Noncopyable
|
||||
{
|
||||
friend class Scheduler;
|
||||
|
||||
uint64_t monotonic_deadline_;
|
||||
uint64_t period_;
|
||||
Scheduler& scheduler_;
|
||||
|
||||
void handleOneShotTimeout(uint64_t ts_monotonic);
|
||||
void genericStart();
|
||||
|
||||
public:
|
||||
static const uint64_t InfinitePeriod = 0xFFFFFFFFFFFFFFFFUL;
|
||||
|
||||
TimerBase(Scheduler& scheduler)
|
||||
: monotonic_deadline_(0)
|
||||
, 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 startPeriodic(uint64_t period_usec);
|
||||
|
||||
void stop();
|
||||
bool isRunning() const;
|
||||
|
||||
uint64_t getPeriod() const { return period_; }
|
||||
|
||||
virtual void onTimerEvent(TimerEvent& event) = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename Functor>
|
||||
class Timer : public TimerBase
|
||||
{
|
||||
Functor functor_;
|
||||
|
||||
public:
|
||||
Timer(Scheduler& node, Functor functor)
|
||||
: TimerBase(node)
|
||||
, functor_(functor)
|
||||
{ }
|
||||
|
||||
const Functor& getFunctor() const { return functor_; }
|
||||
|
||||
void onTimerEvent(TimerEvent& event)
|
||||
{
|
||||
functor_(event);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user