blob: 38ceaec174e6c3edfae8b4e587f4cb82dbcda3e8 [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>
7#include "aos/common/queue.h"
8#include "aos/common/time.h"
9
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
63 protected:
64 RawSender(const RawSender &) = delete;
65 RawSender &operator=(const RawSender &) = delete;
66};
67
68// Opaque Information extracted from a particular type passed to the underlying
69// system so that it knows how much memory to allocate etc.
70struct QueueTypeInfo {
71 // Message size:
72 size_t size;
73 // This should be a globally unique identifier for the type.
74 int hash;
75 // Config parameter for how long the queue should be.
76 int queue_length;
77
78 template <typename T>
79 static QueueTypeInfo Get() {
80 QueueTypeInfo info;
81 info.size = sizeof(T);
82 info.hash = T::kHash;
83 info.queue_length = T::kQueueLength;
84 return info;
85 }
86};
87
88// Virtual base class for all event queue-types.
89class RawEventLoop {
90 public:
91 virtual ~RawEventLoop() {}
92
93 // Current time.
94 virtual monotonic_clock::time_point monotonic_now() = 0;
95
96 // The passed in function will be called when the event loop starts.
97 // Use this to run code once the thread goes into "real-time-mode",
98 virtual void OnRun(std::function<void()> on_run) = 0;
99
100 bool is_running() const { return is_running_.load(); }
101
102 // Starts receiving events.
103 virtual void Run() = 0;
104
105 // Stops receiving events.
106 virtual void Exit() = 0;
107
108 protected:
109 void set_is_running(bool value) { is_running_.store(value); }
110
111 // Will send new messages from (path, type).
112 virtual std::unique_ptr<RawSender> MakeRawSender(
113 const std::string &path, const QueueTypeInfo &type) = 0;
114
115 // Will fetch new messages from (path, type).
116 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
117 const std::string &path, const QueueTypeInfo &type) = 0;
118
119 // Will watch (path, type) for new messages
120 virtual void MakeRawWatcher(
121 const std::string &path, const QueueTypeInfo &type,
122 std::function<void(const Message *message)> watcher) = 0;
123
124 private:
125 std::atomic<bool> is_running_{false};
126};
127
128} // namespace aos
129
130#endif // _AOS_EVENTS_RAW_EVENT_LOOP_H_