scheduler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_NODE_SCHEDULER_HPP_INCLUDED
6 #define UAVCAN_NODE_SCHEDULER_HPP_INCLUDED
7 
8 #include <uavcan/error.hpp>
11 
12 namespace uavcan
13 {
14 
16 
17 class UAVCAN_EXPORT DeadlineHandler : public LinkedListNode<DeadlineHandler>
18 {
20 
21 protected:
23 
24  explicit DeadlineHandler(Scheduler& scheduler)
25  : scheduler_(scheduler)
26  { }
27 
28  virtual ~DeadlineHandler() { stop(); }
29 
30 public:
31  virtual void handleDeadline(MonotonicTime current) = 0;
32 
33  void startWithDeadline(MonotonicTime deadline);
34  void startWithDelay(MonotonicDuration delay);
35  void generateDeadlineImmediately() { startWithDeadline(MonotonicTime::fromUSec(1)); }
36 
37  void stop();
38 
39  bool isRunning() const;
40 
41  MonotonicTime getDeadline() const { return deadline_; }
42  Scheduler& getScheduler() const { return scheduler_; }
43 };
44 
45 
47 {
48  LinkedListRoot<DeadlineHandler> handlers_; // Ordered by deadline, lowest first
49 
50 public:
51  void add(DeadlineHandler* mdh);
52  void remove(DeadlineHandler* mdh);
53  bool doesExist(const DeadlineHandler* mdh) const;
54  unsigned getNumHandlers() const { return handlers_.getLength(); }
55 
56  MonotonicTime pollAndGetMonotonicTime(ISystemClock& sysclock);
57  MonotonicTime getEarliestDeadline() const;
58 };
59 
64 {
65  enum { DefaultDeadlineResolutionMs = 5 };
66  enum { MinDeadlineResolutionMs = 1 };
67  enum { MaxDeadlineResolutionMs = 100 };
68 
69  enum { DefaultCleanupPeriodMs = 1000 };
70  enum { MinCleanupPeriodMs = 10 };
71  enum { MaxCleanupPeriodMs = 10000 };
72 
79 
81  {
84  : owner(o)
85  {
86  owner.inside_spin_ = true;
87  }
88  ~InsideSpinSetter() { owner.inside_spin_ = false; }
89  };
90 
91  MonotonicTime computeDispatcherSpinDeadline(MonotonicTime spin_deadline) const;
92  void pollCleanup(MonotonicTime mono_ts, uint32_t num_frames_processed_with_last_spin);
93 
94 public:
95  Scheduler(ICanDriver& can_driver, IPoolAllocator& allocator, ISystemClock& sysclock)
96  : dispatcher_(can_driver, allocator, sysclock)
97  , prev_cleanup_ts_(sysclock.getMonotonic())
98  , deadline_resolution_(MonotonicDuration::fromMSec(DefaultDeadlineResolutionMs))
99  , cleanup_period_(MonotonicDuration::fromMSec(DefaultCleanupPeriodMs))
100  , inside_spin_(false)
101  { }
102 
108  int spin(MonotonicTime deadline);
109 
115  int spinOnce();
116 
117  DeadlineScheduler& getDeadlineScheduler() { return deadline_scheduler_; }
118 
119  Dispatcher& getDispatcher() { return dispatcher_; }
120  const Dispatcher& getDispatcher() const { return dispatcher_; }
121 
122  ISystemClock& getSystemClock() { return dispatcher_.getSystemClock(); }
123  MonotonicTime getMonotonicTime() const { return dispatcher_.getSystemClock().getMonotonic(); }
124  UtcTime getUtcTime() const { return dispatcher_.getSystemClock().getUtc(); }
125 
130  MonotonicDuration getDeadlineResolution() const { return deadline_resolution_; }
132  {
133  res = min(res, MonotonicDuration::fromMSec(MaxDeadlineResolutionMs));
134  res = max(res, MonotonicDuration::fromMSec(MinDeadlineResolutionMs));
135  deadline_resolution_ = res;
136  }
137 
144  MonotonicDuration getCleanupPeriod() const { return cleanup_period_; }
146  {
147  period = min(period, MonotonicDuration::fromMSec(MaxCleanupPeriodMs));
148  period = max(period, MonotonicDuration::fromMSec(MinCleanupPeriodMs));
149  cleanup_period_ = period;
150  }
151 };
152 
153 }
154 
155 #endif // UAVCAN_NODE_SCHEDULER_HPP_INCLUDED
MonotonicTime prev_cleanup_ts_
Definition: scheduler.hpp:75
virtual MonotonicTime getMonotonic() const =0
virtual UtcTime getUtc() const =0
const Dispatcher & getDispatcher() const
Definition: scheduler.hpp:120
MonotonicTime getMonotonicTime() const
Definition: scheduler.hpp:123
void setCleanupPeriod(MonotonicDuration period)
Definition: scheduler.hpp:145
ISystemClock & getSystemClock()
Definition: scheduler.hpp:122
static MonotonicTime fromUSec(uint64_t us)
Definition: time.hpp:112
unsigned getNumHandlers() const
Definition: scheduler.hpp:54
MonotonicDuration getDeadlineResolution() const
Definition: scheduler.hpp:130
UtcTime getUtcTime() const
Definition: scheduler.hpp:124
Scheduler & getScheduler() const
Definition: scheduler.hpp:42
Scheduler & scheduler_
Definition: scheduler.hpp:22
void setDeadlineResolution(MonotonicDuration res)
Definition: scheduler.hpp:131
uavcan::MonotonicTime getMonotonic()
Definition: clock.cpp:83
void generateDeadlineImmediately()
Definition: scheduler.hpp:35
Implicitly convertible to/from uavcan.Timestamp.
Definition: time.hpp:191
MonotonicDuration getCleanupPeriod() const
Definition: scheduler.hpp:144
LinkedListRoot< DeadlineHandler > handlers_
Definition: scheduler.hpp:48
UAVCAN_EXPORT const T & max(const T &a, const T &b)
Definition: templates.hpp:291
Scheduler(ICanDriver &can_driver, IPoolAllocator &allocator, ISystemClock &sysclock)
Definition: scheduler.hpp:95
Dispatcher dispatcher_
Definition: scheduler.hpp:74
MonotonicDuration cleanup_period_
Definition: scheduler.hpp:77
std::uint32_t uint32_t
Definition: std.hpp:26
MonotonicTime getDeadline() const
Definition: scheduler.hpp:41
DeadlineScheduler deadline_scheduler_
Definition: scheduler.hpp:73
DeadlineHandler(Scheduler &scheduler)
Definition: scheduler.hpp:24
UAVCAN_EXPORT const T & min(const T &a, const T &b)
Definition: templates.hpp:281
ROSCPP_DECL void spin()
unsigned getLength() const
Definition: linked_list.hpp:89
Dispatcher & getDispatcher()
Definition: scheduler.hpp:119
virtual ~DeadlineHandler()
Definition: scheduler.hpp:28
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
MonotonicDuration deadline_resolution_
Definition: scheduler.hpp:76
ROSCPP_DECL void spinOnce()
MonotonicTime deadline_
Definition: scheduler.hpp:19
const ISystemClock & getSystemClock() const
Definition: dispatcher.hpp:230
DeadlineScheduler & getDeadlineScheduler()
Definition: scheduler.hpp:117


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39