blob: a903e93a2bf156c453d48f01d5151d855e70c9c9 [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#ifndef _AOS_EVENTS_RAW_EVENT_LOOP_H_
2#define _AOS_EVENTS_RAW_EVENT_LOOP_H_
3
4#include <atomic>
5#include <memory>
6#include <string>
John Park33858a32018-09-28 23:05:48 -07007#include "aos/queue.h"
8#include "aos/time/time.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08009
10// This file contains raw versions of the classes in event-loop.h.
11//
12// Users should look exclusively at event-loop.h. Only people who wish to
13// implement a new IPC layer (like a fake layer for example) may wish to use
14// these classes.
15namespace aos {
16
17// Raw version of fetcher. Contains a local variable that the fetcher will
18// update.
19// It is the job of the typed version to cast this to the appropriate type.
20class RawFetcher {
21 public:
22 class FetchValue;
23 RawFetcher() {}
24 virtual ~RawFetcher() {}
25
26 virtual bool Fetch() = 0;
27
28 const FetchValue *most_recent() { return most_recent_; }
29
30 protected:
31 RawFetcher(const RawFetcher &) = delete;
32 RawFetcher &operator=(const RawFetcher &) = delete;
33 void set_most_recent(const FetchValue *most_recent) {
34 most_recent_ = most_recent;
35 }
36
37 private:
38 const FetchValue *most_recent_ = nullptr;
39};
40
41// Raw version of sender. Sending a message is a 3 part process. Fetch an opaque
42// token, cast that token to the message type, populate and then calling one of
43// Send() or Free().
44class RawSender {
45 public:
46 class SendContext;
47
48 RawSender() {}
49 virtual ~RawSender() {}
50
51 virtual SendContext *GetContext() = 0;
52
53 virtual void Free(SendContext *context) = 0;
54
55 virtual bool Send(SendContext *context) = 0;
56
57 // Call operator that calls Free().
58 template <typename T>
59 void operator()(T *t) {
60 Free(reinterpret_cast<SendContext *>(t));
61 }
62
Austin Schuhd681bbd2019-02-02 12:03:32 -080063 virtual const char *name() const = 0;
64
Parker Schuhe4a70d62017-12-27 20:10:20 -080065 protected:
66 RawSender(const RawSender &) = delete;
67 RawSender &operator=(const RawSender &) = delete;
68};
69
70// Opaque Information extracted from a particular type passed to the underlying
71// system so that it knows how much memory to allocate etc.
72struct QueueTypeInfo {
73 // Message size:
74 size_t size;
75 // This should be a globally unique identifier for the type.
76 int hash;
77 // Config parameter for how long the queue should be.
78 int queue_length;
79
80 template <typename T>
81 static QueueTypeInfo Get() {
82 QueueTypeInfo info;
83 info.size = sizeof(T);
84 info.hash = T::kHash;
85 info.queue_length = T::kQueueLength;
86 return info;
87 }
Neil Balchc8f41ed2018-01-20 22:06:53 -080088
89 // Necessary for the comparison of QueueTypeInfo objects in the
90 // SimulatedEventLoop.
91 bool operator<(const QueueTypeInfo &other) const {
92 if (size != other.size) return size < other.size;
93 if (hash != other.hash) return hash < other.hash;
94 return queue_length < other.queue_length;
95 }
Parker Schuhe4a70d62017-12-27 20:10:20 -080096};
97
Neil Balch229001a2018-01-07 18:22:52 -080098// Interface for timers
99class TimerHandler {
100 public:
101 virtual ~TimerHandler() {}
102
103 // Timer should sleep until base, base + offset, base + offset * 2, ...
104 // If repeat_offset isn't set, the timer only expires once.
105 virtual void Setup(monotonic_clock::time_point base,
106 monotonic_clock::duration repeat_offset =
107 ::aos::monotonic_clock::zero()) = 0;
108
109 // Stop future calls to callback().
110 virtual void Disable() = 0;
111};
112
Parker Schuhe4a70d62017-12-27 20:10:20 -0800113// Virtual base class for all event queue-types.
114class RawEventLoop {
115 public:
116 virtual ~RawEventLoop() {}
117
118 // Current time.
119 virtual monotonic_clock::time_point monotonic_now() = 0;
120
121 // The passed in function will be called when the event loop starts.
122 // Use this to run code once the thread goes into "real-time-mode",
123 virtual void OnRun(std::function<void()> on_run) = 0;
124
125 bool is_running() const { return is_running_.load(); }
126
Neil Balch229001a2018-01-07 18:22:52 -0800127 // Creates a timer that executes callback when the timer expires
128 // Returns a TimerHandle for configuration of the timer
129 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
130
Parker Schuhe4a70d62017-12-27 20:10:20 -0800131 // Starts receiving events.
132 virtual void Run() = 0;
133
134 // Stops receiving events.
135 virtual void Exit() = 0;
136
137 protected:
138 void set_is_running(bool value) { is_running_.store(value); }
139
140 // Will send new messages from (path, type).
141 virtual std::unique_ptr<RawSender> MakeRawSender(
142 const std::string &path, const QueueTypeInfo &type) = 0;
143
144 // Will fetch new messages from (path, type).
145 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
146 const std::string &path, const QueueTypeInfo &type) = 0;
147
148 // Will watch (path, type) for new messages
149 virtual void MakeRawWatcher(
150 const std::string &path, const QueueTypeInfo &type,
151 std::function<void(const Message *message)> watcher) = 0;
152
153 private:
154 std::atomic<bool> is_running_{false};
155};
156
157} // namespace aos
158
159#endif // _AOS_EVENTS_RAW_EVENT_LOOP_H_