brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_QUEUE_H_ |
| 2 | #define AOS_COMMON_QUEUE_H_ |
| 3 | |
| 4 | #include <assert.h> |
| 5 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 6 | #include "aos/common/time.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | #include "aos/common/macros.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 8 | #include "aos/linux_code/ipc_lib/queue.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include "aos/common/time.h" |
| 10 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 11 | namespace aos { |
| 12 | |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 13 | struct MessageType; |
Brian Silverman | 96395be | 2014-02-11 18:35:57 -0800 | [diff] [blame] | 14 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | // This class is a base class for all messages sent over queues. |
Brian Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 16 | // All of the methods are overloaded in (generated) subclasses to do the same |
| 17 | // thing for the whole thing. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | class 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 Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 28 | // Returns the size of the common fields. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | 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 Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 41 | |
Brian Silverman | 665e60c | 2014-02-12 13:57:10 -0800 | [diff] [blame] | 42 | static const MessageType *GetType(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | template <class T> class Queue; |
| 46 | template <class T> class MessageBuilder; |
| 47 | template <class T> class ScopedMessagePtr; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | |
| 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 Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 52 | // message pointer to external code. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | template <class T> |
| 54 | class 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 107 | // 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 Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 112 | // And, if they do actually want to move the pointer, then it will correctly |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 113 | // clear out the source so there aren't 2 pointers to the message lying |
| 114 | // around. |
| 115 | ScopedMessagePtr(ScopedMessagePtr<T> &&ptr) |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 116 | : queue_(ptr.queue_), msg_(ptr.msg_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | ptr.msg_ = NULL; |
| 118 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | |
| 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 Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 126 | // Only Queue should be able to build a message. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 127 | ScopedMessagePtr(RawQueue *queue, T *msg) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 128 | : queue_(queue), msg_(msg) {} |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 129 | |
| 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 135 | // Sets the queue that owns this message. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 136 | void set_queue(RawQueue *queue) { queue_ = queue; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 137 | |
| 138 | // The queue that the message is a part of. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 139 | RawQueue *queue_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 140 | // 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. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 149 | template <class T> |
| 150 | class 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 Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 158 | // the .q files. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 159 | template <class T> |
| 160 | class Queue { |
| 161 | public: |
| 162 | typedef T Message; |
| 163 | |
| 164 | Queue(const char *queue_name) |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 165 | : queue_name_(queue_name), queue_(NULL), queue_msg_(NULL, NULL) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 166 | 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 Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 171 | // work before sending information, and may be be called multiple times. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 172 | // 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 Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 176 | // 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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 180 | // Fetches the next message from the queue. |
| 181 | // Returns true if there was a new message available and we successfully |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 182 | // fetched it. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 183 | bool FetchNext(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 184 | |
| 185 | // Fetches the next message from the queue, waiting if necessary until there |
| 186 | // is one. |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 187 | void FetchNextBlocking(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 188 | |
| 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 | |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 194 | // Fetches another message from the queue. Blocks until there is one if the |
| 195 | // latest was already Fetched. |
| 196 | void FetchAnother(); |
| 197 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 198 | // Returns the age of the message. |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 199 | const time::Time Age() const { return time::Time::Now() - queue_msg_->sent_time; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 200 | |
| 201 | // Returns true if the latest value in the queue is newer than age mseconds. |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 202 | // DEPRECATED(brians): Use IsNewerThan(const time::Time&) instead. |
| 203 | bool IsNewerThanMS(int age) const { |
Brian Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 204 | // TODO(aschuh): Log very verbosely if something is _ever_ stale. |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 205 | return IsNewerThan(time::Time::InMS(age)); |
| 206 | } |
| 207 | |
| 208 | bool IsNewerThan(const time::Time &age) const { |
| 209 | return get() != nullptr && Age() < age; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Returns a pointer to the current message. |
| 213 | // This pointer will be valid until a new message is fetched. |
| 214 | const T *get() const { return queue_msg_.get(); } |
| 215 | |
| 216 | // Returns a reference to the message. |
| 217 | // The message will be valid until a new message is fetched. |
| 218 | const T &operator*() const { |
| 219 | const T *msg = get(); |
| 220 | assert(msg != NULL); |
| 221 | return *msg; |
| 222 | } |
| 223 | |
| 224 | // Returns a pointer to the current message. |
| 225 | // This pointer will be valid until a new message is fetched. |
| 226 | const T *operator->() const { |
| 227 | const T *msg = get(); |
| 228 | assert(msg != NULL); |
| 229 | return msg; |
| 230 | } |
| 231 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 232 | // Returns a scoped_ptr containing a message. |
| 233 | // GCC will optimize away the copy constructor, so this is safe. |
| 234 | ScopedMessagePtr<T> MakeMessage(); |
| 235 | |
| 236 | // Returns a message builder that contains a pre-allocated message. |
| 237 | aos::MessageBuilder<T> MakeWithBuilder(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 238 | |
| 239 | const char *name() const { return queue_name_; } |
| 240 | |
| 241 | private: |
| 242 | const char *queue_name_; |
| 243 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 244 | T *MakeRawMessage(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 245 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 246 | // Pointer to the queue that this object fetches from. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 247 | RawQueue *queue_; |
Austin Schuh | 287d98e | 2014-03-09 00:41:55 -0800 | [diff] [blame] | 248 | int index_ = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 249 | // Scoped pointer holding the latest message or NULL. |
| 250 | ScopedMessagePtr<const T> queue_msg_; |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame^] | 251 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 252 | DISALLOW_COPY_AND_ASSIGN(Queue<T>); |
| 253 | }; |
| 254 | |
| 255 | // Base class for all queue groups. |
| 256 | class QueueGroup { |
| 257 | public: |
| 258 | // Constructs a queue group given its name and a unique hash of the name and |
| 259 | // type. |
| 260 | QueueGroup(const char *name, uint32_t hash) : name_(name), hash_(hash) {} |
| 261 | |
| 262 | // Returns the name of the queue group. |
| 263 | const char *name() const { return name_.c_str(); } |
| 264 | // Returns a unique hash representing this instance of the queue group. |
| 265 | uint32_t hash() const { return hash_; } |
| 266 | |
| 267 | private: |
| 268 | std::string name_; |
| 269 | uint32_t hash_; |
| 270 | }; |
| 271 | |
| 272 | } // namespace aos |
| 273 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 274 | #include "aos/linux_code/queue-tmpl.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 275 | |
| 276 | #endif // AOS_COMMON_QUEUE_H_ |