blob: 2098e66864b7ecb08b53efc1df1572b1025aec19 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_QUEUE_H_
2#define AOS_COMMON_QUEUE_H_
3
4#include <assert.h>
5
Brian Silverman3204dd82013-03-12 18:42:01 -07006#include "aos/common/time.h"
brians343bc112013-02-10 01:53:46 +00007#include "aos/common/macros.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -08008#include "aos/linux_code/ipc_lib/queue.h"
brians343bc112013-02-10 01:53:46 +00009#include "aos/common/time.h"
10
brians343bc112013-02-10 01:53:46 +000011namespace aos {
12
Brian Silvermanf7986142014-04-21 17:42:35 -070013struct MessageType;
Brian Silverman96395be2014-02-11 18:35:57 -080014
brians343bc112013-02-10 01:53:46 +000015// This class is a base class for all messages sent over queues.
Brian Silverman16c82972014-02-13 15:36:40 -080016// All of the methods are overloaded in (generated) subclasses to do the same
17// thing for the whole thing.
brians343bc112013-02-10 01:53:46 +000018class Message {
19 public:
20 typedef ::aos::time::Time Time;
21 // The time that the message was sent at.
22 Time sent_time;
23
24 Message() : sent_time(0, 0) {}
25
26 // Zeros out the time.
27 void Zero();
Brian Silvermanc4546d22014-02-10 18:03:09 -080028 // Returns the size of the common fields.
brians343bc112013-02-10 01:53:46 +000029 static size_t Size() { return sizeof(Time); }
30
31 // Deserializes the common fields from the buffer.
32 size_t Deserialize(const char *buffer);
33 // Serializes the common fields into the buffer.
34 size_t Serialize(char *buffer) const;
35
36 // Populates sent_time with the current time.
37 void SetTimeToNow() { sent_time = Time::Now(); }
38
39 // Writes the contents of the message to the provided buffer.
40 size_t Print(char *buffer, int length) const;
Brian Silvermanc4546d22014-02-10 18:03:09 -080041
Brian Silverman665e60c2014-02-12 13:57:10 -080042 static const MessageType *GetType();
brians343bc112013-02-10 01:53:46 +000043};
44
45template <class T> class Queue;
46template <class T> class MessageBuilder;
47template <class T> class ScopedMessagePtr;
brians343bc112013-02-10 01:53:46 +000048
49// A ScopedMessagePtr<> manages a queue message pointer.
50// It frees it properly when the ScopedMessagePtr<> goes out of scope or gets
51// sent. By design, there is no way to get the ScopedMessagePtr to release the
Brian Silverman6da04272014-05-18 18:47:48 -070052// message pointer to external code.
brians343bc112013-02-10 01:53:46 +000053template <class T>
54class ScopedMessagePtr {
55 public:
56 // Returns a pointer to the message.
57 // This stays valid until Send or the destructor have been called.
58 const T *get() const { return msg_; }
59 T *get() { return msg_; }
60
61 const T &operator*() const {
62 const T *msg = get();
63 assert(msg != NULL);
64 return *msg;
65 }
66
67 T &operator*() {
68 T *msg = get();
69 assert(msg != NULL);
70 return *msg;
71 }
72
73 const T *operator->() const {
74 const T *msg = get();
75 assert(msg != NULL);
76 return msg;
77 }
78
79 T *operator->() {
80 T *msg = get();
81 assert(msg != NULL);
82 return msg;
83 }
84
85 operator bool() {
86 return msg_ != NULL;
87 }
88
89 // Sends the message and removes our reference to it.
90 // If the queue is full, over-ride the oldest message in it with our new
91 // message.
92 // Returns true on success, and false otherwise.
93 // The message will be freed.
94 bool Send();
95
96 // Sends the message and removes our reference to it.
97 // If the queue is full, block until it is no longer full.
98 // Returns true on success, and false otherwise.
99 // The message will be freed.
100 bool SendBlocking();
101
102 // Frees the contained message.
103 ~ScopedMessagePtr() {
104 reset();
105 }
106
brians343bc112013-02-10 01:53:46 +0000107 // Implements a move constructor. This only takes rvalue references
108 // because we want to allow someone to say
109 // ScopedMessagePtr<X> ptr = queue.MakeMessage();
110 // but we don't want to allow them to then say
111 // ScopedMessagePtr<X> new_ptr = ptr;
Brian Silvermanc4546d22014-02-10 18:03:09 -0800112 // And, if they do actually want to move the pointer, then it will correctly
brians343bc112013-02-10 01:53:46 +0000113 // clear out the source so there aren't 2 pointers to the message lying
114 // around.
115 ScopedMessagePtr(ScopedMessagePtr<T> &&ptr)
Brian Silverman6da04272014-05-18 18:47:48 -0700116 : queue_(ptr.queue_), msg_(ptr.msg_) {
brians343bc112013-02-10 01:53:46 +0000117 ptr.msg_ = NULL;
118 }
brians343bc112013-02-10 01:53:46 +0000119
120 private:
121 // Provide access to set_queue and the constructor for init.
122 friend class aos::Queue<typename std::remove_const<T>::type>;
123 // Provide access to the copy constructor for MakeWithBuilder.
124 friend class aos::MessageBuilder<T>;
125
Brian Silverman6da04272014-05-18 18:47:48 -0700126 // Only Queue should be able to build a message.
Brian Silverman08661c72013-09-01 17:24:38 -0700127 ScopedMessagePtr(RawQueue *queue, T *msg)
brians343bc112013-02-10 01:53:46 +0000128 : queue_(queue), msg_(msg) {}
brians343bc112013-02-10 01:53:46 +0000129
130 // Sets the pointer to msg, freeing the old value if it was there.
131 // This is private because nobody should be able to get a pointer to a message
132 // that needs to be scoped without using the queue.
133 void reset(T *msg = NULL);
134
brians343bc112013-02-10 01:53:46 +0000135 // Sets the queue that owns this message.
Brian Silverman08661c72013-09-01 17:24:38 -0700136 void set_queue(RawQueue *queue) { queue_ = queue; }
brians343bc112013-02-10 01:53:46 +0000137
138 // The queue that the message is a part of.
Brian Silverman08661c72013-09-01 17:24:38 -0700139 RawQueue *queue_;
brians343bc112013-02-10 01:53:46 +0000140 // The message or NULL.
141 T *msg_;
142
143 // Protect evil constructors.
144 DISALLOW_COPY_AND_ASSIGN(ScopedMessagePtr<T>);
145};
146
147// Specializations for the Builders will be automatically generated in the .q.h
148// header files with all of the handy builder methods.
brians343bc112013-02-10 01:53:46 +0000149template <class T>
150class MessageBuilder {
151 public:
152 typedef T Message;
153 bool Send();
154};
155
156// TODO(aschuh): Base class
157// T must be a Message with the same format as the messages generated by
Brian Silverman6da04272014-05-18 18:47:48 -0700158// the .q files.
brians343bc112013-02-10 01:53:46 +0000159template <class T>
160class Queue {
161 public:
162 typedef T Message;
163
164 Queue(const char *queue_name)
Brian Silverman6da04272014-05-18 18:47:48 -0700165 : queue_name_(queue_name), queue_(NULL), queue_msg_(NULL, NULL) {
brians343bc112013-02-10 01:53:46 +0000166 static_assert(shm_ok<T>::value,
167 "The provided message type can't be put in shmem.");
168 }
169
170 // Initializes the queue. This may optionally be called to do any one time
Brian Silvermanc4546d22014-02-10 18:03:09 -0800171 // work before sending information, and may be be called multiple times.
brians343bc112013-02-10 01:53:46 +0000172 // Init will be called when a message is sent, but this will cause sending to
173 // take a different amount of time the first cycle.
174 void Init();
175
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800176 // Removes all internal references to shared memory so shared memory can be
177 // restarted safely. This should only be used in testing.
178 void Clear();
179
brians343bc112013-02-10 01:53:46 +0000180 // Fetches the next message from the queue.
181 // Returns true if there was a new message available and we successfully
Brian Silverman428de562014-04-10 15:59:19 -0700182 // fetched it.
brians343bc112013-02-10 01:53:46 +0000183 bool FetchNext();
Brian Silverman6da04272014-05-18 18:47:48 -0700184
185 // Fetches the next message from the queue, waiting if necessary until there
186 // is one.
Brian Silverman428de562014-04-10 15:59:19 -0700187 void FetchNextBlocking();
brians343bc112013-02-10 01:53:46 +0000188
189 // Fetches the last message from the queue.
190 // Returns true if there was a new message available and we successfully
191 // fetched it.
192 bool FetchLatest();
193
Austin Schuh867c11e2015-02-22 21:35:43 -0800194 // Fetches the latest message from the queue, or blocks if we have already
195 // fetched it until another is avilable.
Brian Silverman428de562014-04-10 15:59:19 -0700196 void FetchAnother();
197
brians343bc112013-02-10 01:53:46 +0000198 // Returns the age of the message.
Brian Silverman5d3ab7f2015-02-21 15:54:21 -0500199 const time::Time Age() const {
200 return time::Time::Now() - queue_msg_->sent_time;
201 }
brians343bc112013-02-10 01:53:46 +0000202
203 // Returns true if the latest value in the queue is newer than age mseconds.
Brian Silverman6da04272014-05-18 18:47:48 -0700204 // DEPRECATED(brians): Use IsNewerThan(const time::Time&) instead.
205 bool IsNewerThanMS(int age) const {
Brian Silvermanc4546d22014-02-10 18:03:09 -0800206 // TODO(aschuh): Log very verbosely if something is _ever_ stale.
Brian Silverman6da04272014-05-18 18:47:48 -0700207 return IsNewerThan(time::Time::InMS(age));
208 }
209
210 bool IsNewerThan(const time::Time &age) const {
211 return get() != nullptr && Age() < age;
brians343bc112013-02-10 01:53:46 +0000212 }
213
214 // Returns a pointer to the current message.
215 // This pointer will be valid until a new message is fetched.
216 const T *get() const { return queue_msg_.get(); }
217
218 // Returns a reference to the message.
219 // The message will be valid until a new message is fetched.
220 const T &operator*() const {
221 const T *msg = get();
222 assert(msg != NULL);
223 return *msg;
224 }
225
226 // Returns a pointer to the current message.
227 // This pointer will be valid until a new message is fetched.
228 const T *operator->() const {
229 const T *msg = get();
230 assert(msg != NULL);
231 return msg;
232 }
233
brians343bc112013-02-10 01:53:46 +0000234 // Returns a scoped_ptr containing a message.
235 // GCC will optimize away the copy constructor, so this is safe.
236 ScopedMessagePtr<T> MakeMessage();
237
238 // Returns a message builder that contains a pre-allocated message.
Brian Silverman42456d82014-08-19 12:43:59 -0400239 // This message will start out completely zeroed.
brians343bc112013-02-10 01:53:46 +0000240 aos::MessageBuilder<T> MakeWithBuilder();
brians343bc112013-02-10 01:53:46 +0000241
242 const char *name() const { return queue_name_; }
243
244 private:
245 const char *queue_name_;
246
brians343bc112013-02-10 01:53:46 +0000247 T *MakeRawMessage();
Brian Silverman6da04272014-05-18 18:47:48 -0700248
brians343bc112013-02-10 01:53:46 +0000249 // Pointer to the queue that this object fetches from.
Brian Silverman08661c72013-09-01 17:24:38 -0700250 RawQueue *queue_;
Austin Schuh287d98e2014-03-09 00:41:55 -0800251 int index_ = 0;
brians343bc112013-02-10 01:53:46 +0000252 // Scoped pointer holding the latest message or NULL.
253 ScopedMessagePtr<const T> queue_msg_;
Brian Silverman6da04272014-05-18 18:47:48 -0700254
brians343bc112013-02-10 01:53:46 +0000255 DISALLOW_COPY_AND_ASSIGN(Queue<T>);
256};
257
258// Base class for all queue groups.
259class QueueGroup {
260 public:
261 // Constructs a queue group given its name and a unique hash of the name and
262 // type.
263 QueueGroup(const char *name, uint32_t hash) : name_(name), hash_(hash) {}
264
265 // Returns the name of the queue group.
266 const char *name() const { return name_.c_str(); }
267 // Returns a unique hash representing this instance of the queue group.
268 uint32_t hash() const { return hash_; }
269
270 private:
271 std::string name_;
272 uint32_t hash_;
273};
274
275} // namespace aos
276
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800277#include "aos/linux_code/queue-tmpl.h"
brians343bc112013-02-10 01:53:46 +0000278
279#endif // AOS_COMMON_QUEUE_H_