blob: d12187ac74889989b4afd35374bf655753c35b02 [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
James Kuszmaulc79768b2019-02-18 15:08:44 -080026 // Non-blocking fetch of the next message in the queue. Returns true if there
27 // was a new message and we got it.
28 virtual bool FetchNext() = 0;
29 // Non-blocking fetch of the latest message:
Parker Schuhe4a70d62017-12-27 20:10:20 -080030 virtual bool Fetch() = 0;
31
32 const FetchValue *most_recent() { return most_recent_; }
33
34 protected:
35 RawFetcher(const RawFetcher &) = delete;
36 RawFetcher &operator=(const RawFetcher &) = delete;
37 void set_most_recent(const FetchValue *most_recent) {
38 most_recent_ = most_recent;
39 }
40
41 private:
42 const FetchValue *most_recent_ = nullptr;
43};
44
45// Raw version of sender. Sending a message is a 3 part process. Fetch an opaque
46// token, cast that token to the message type, populate and then calling one of
47// Send() or Free().
48class RawSender {
49 public:
50 class SendContext;
51
52 RawSender() {}
53 virtual ~RawSender() {}
54
55 virtual SendContext *GetContext() = 0;
56
57 virtual void Free(SendContext *context) = 0;
58
59 virtual bool Send(SendContext *context) = 0;
60
61 // Call operator that calls Free().
62 template <typename T>
63 void operator()(T *t) {
64 Free(reinterpret_cast<SendContext *>(t));
65 }
66
Austin Schuhd681bbd2019-02-02 12:03:32 -080067 virtual const char *name() const = 0;
68
Parker Schuhe4a70d62017-12-27 20:10:20 -080069 protected:
70 RawSender(const RawSender &) = delete;
71 RawSender &operator=(const RawSender &) = delete;
72};
73
74// Opaque Information extracted from a particular type passed to the underlying
75// system so that it knows how much memory to allocate etc.
76struct QueueTypeInfo {
77 // Message size:
78 size_t size;
79 // This should be a globally unique identifier for the type.
80 int hash;
81 // Config parameter for how long the queue should be.
82 int queue_length;
83
84 template <typename T>
85 static QueueTypeInfo Get() {
86 QueueTypeInfo info;
87 info.size = sizeof(T);
88 info.hash = T::kHash;
89 info.queue_length = T::kQueueLength;
90 return info;
91 }
Neil Balchc8f41ed2018-01-20 22:06:53 -080092
93 // Necessary for the comparison of QueueTypeInfo objects in the
94 // SimulatedEventLoop.
95 bool operator<(const QueueTypeInfo &other) const {
96 if (size != other.size) return size < other.size;
97 if (hash != other.hash) return hash < other.hash;
98 return queue_length < other.queue_length;
99 }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800100};
101
Neil Balch229001a2018-01-07 18:22:52 -0800102// Interface for timers
103class TimerHandler {
104 public:
105 virtual ~TimerHandler() {}
106
107 // Timer should sleep until base, base + offset, base + offset * 2, ...
108 // If repeat_offset isn't set, the timer only expires once.
109 virtual void Setup(monotonic_clock::time_point base,
110 monotonic_clock::duration repeat_offset =
111 ::aos::monotonic_clock::zero()) = 0;
112
113 // Stop future calls to callback().
114 virtual void Disable() = 0;
115};
116
Parker Schuhe4a70d62017-12-27 20:10:20 -0800117// Virtual base class for all event queue-types.
118class RawEventLoop {
119 public:
120 virtual ~RawEventLoop() {}
121
122 // Current time.
123 virtual monotonic_clock::time_point monotonic_now() = 0;
124
125 // The passed in function will be called when the event loop starts.
126 // Use this to run code once the thread goes into "real-time-mode",
127 virtual void OnRun(std::function<void()> on_run) = 0;
128
129 bool is_running() const { return is_running_.load(); }
130
Neil Balch229001a2018-01-07 18:22:52 -0800131 // Creates a timer that executes callback when the timer expires
132 // Returns a TimerHandle for configuration of the timer
133 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
134
Parker Schuhe4a70d62017-12-27 20:10:20 -0800135 // Starts receiving events.
136 virtual void Run() = 0;
137
138 // Stops receiving events.
139 virtual void Exit() = 0;
140
141 protected:
142 void set_is_running(bool value) { is_running_.store(value); }
143
144 // Will send new messages from (path, type).
145 virtual std::unique_ptr<RawSender> MakeRawSender(
146 const std::string &path, const QueueTypeInfo &type) = 0;
147
148 // Will fetch new messages from (path, type).
149 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
150 const std::string &path, const QueueTypeInfo &type) = 0;
151
152 // Will watch (path, type) for new messages
153 virtual void MakeRawWatcher(
154 const std::string &path, const QueueTypeInfo &type,
155 std::function<void(const Message *message)> watcher) = 0;
156
157 private:
158 std::atomic<bool> is_running_{false};
159};
160
161} // namespace aos
162
163#endif // _AOS_EVENTS_RAW_EVENT_LOOP_H_