John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_QUEUE_H_ |
| 2 | #define AOS_QUEUE_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
| 4 | #include <assert.h> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/macros.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 7 | #include "aos/ipc_lib/queue.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 8 | #include "aos/messages/message.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | namespace aos { |
| 11 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | template <class T> class Queue; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
| 14 | // A ScopedMessagePtr<> manages a queue message pointer. |
| 15 | // It frees it properly when the ScopedMessagePtr<> goes out of scope or gets |
| 16 | // 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] | 17 | // message pointer to external code. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | template <class T> |
| 19 | class ScopedMessagePtr { |
| 20 | public: |
| 21 | // Returns a pointer to the message. |
| 22 | // This stays valid until Send or the destructor have been called. |
| 23 | const T *get() const { return msg_; } |
| 24 | T *get() { return msg_; } |
| 25 | |
| 26 | const T &operator*() const { |
| 27 | const T *msg = get(); |
| 28 | assert(msg != NULL); |
| 29 | return *msg; |
| 30 | } |
| 31 | |
| 32 | T &operator*() { |
| 33 | T *msg = get(); |
| 34 | assert(msg != NULL); |
| 35 | return *msg; |
| 36 | } |
| 37 | |
| 38 | const T *operator->() const { |
| 39 | const T *msg = get(); |
| 40 | assert(msg != NULL); |
| 41 | return msg; |
| 42 | } |
| 43 | |
| 44 | T *operator->() { |
| 45 | T *msg = get(); |
| 46 | assert(msg != NULL); |
| 47 | return msg; |
| 48 | } |
| 49 | |
| 50 | operator bool() { |
| 51 | return msg_ != NULL; |
| 52 | } |
| 53 | |
| 54 | // Sends the message and removes our reference to it. |
| 55 | // If the queue is full, over-ride the oldest message in it with our new |
| 56 | // message. |
| 57 | // Returns true on success, and false otherwise. |
| 58 | // The message will be freed. |
| 59 | bool Send(); |
| 60 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 61 | // Frees the contained message. |
| 62 | ~ScopedMessagePtr() { |
| 63 | reset(); |
| 64 | } |
| 65 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | // Implements a move constructor. This only takes rvalue references |
| 67 | // because we want to allow someone to say |
| 68 | // ScopedMessagePtr<X> ptr = queue.MakeMessage(); |
| 69 | // but we don't want to allow them to then say |
| 70 | // ScopedMessagePtr<X> new_ptr = ptr; |
Brian Silverman | c4546d2 | 2014-02-10 18:03:09 -0800 | [diff] [blame] | 71 | // 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] | 72 | // clear out the source so there aren't 2 pointers to the message lying |
| 73 | // around. |
| 74 | ScopedMessagePtr(ScopedMessagePtr<T> &&ptr) |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 75 | : queue_(ptr.queue_), msg_(ptr.msg_) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 76 | ptr.msg_ = NULL; |
| 77 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | // Provide access to set_queue and the constructor for init. |
| 81 | friend class aos::Queue<typename std::remove_const<T>::type>; |
| 82 | // Provide access to the copy constructor for MakeWithBuilder. |
| 83 | friend class aos::MessageBuilder<T>; |
| 84 | |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 85 | // Only Queue should be able to build a message. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 86 | ScopedMessagePtr(RawQueue *queue, T *msg) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | : queue_(queue), msg_(msg) {} |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | |
| 89 | // Sets the pointer to msg, freeing the old value if it was there. |
| 90 | // This is private because nobody should be able to get a pointer to a message |
| 91 | // that needs to be scoped without using the queue. |
| 92 | void reset(T *msg = NULL); |
| 93 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 94 | // Sets the queue that owns this message. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 95 | void set_queue(RawQueue *queue) { queue_ = queue; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 96 | |
| 97 | // The queue that the message is a part of. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 98 | RawQueue *queue_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 99 | // The message or NULL. |
| 100 | T *msg_; |
| 101 | |
| 102 | // Protect evil constructors. |
| 103 | DISALLOW_COPY_AND_ASSIGN(ScopedMessagePtr<T>); |
| 104 | }; |
| 105 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | // TODO(aschuh): Base class |
| 107 | // 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] | 108 | // the .q files. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | template <class T> |
| 110 | class Queue { |
| 111 | public: |
| 112 | typedef T Message; |
| 113 | |
Brian Silverman | 92c3f1e | 2015-12-08 20:21:31 -0500 | [diff] [blame] | 114 | explicit Queue(const char *queue_name) |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 115 | : queue_name_(queue_name), queue_(NULL), queue_msg_(NULL, NULL) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | static_assert(shm_ok<T>::value, |
| 117 | "The provided message type can't be put in shmem."); |
| 118 | } |
| 119 | |
| 120 | // 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] | 121 | // work before sending information, and may be be called multiple times. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 122 | // Init will be called when a message is sent, but this will cause sending to |
| 123 | // take a different amount of time the first cycle. |
| 124 | void Init(); |
| 125 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 126 | // Removes all internal references to shared memory so shared memory can be |
| 127 | // restarted safely. This should only be used in testing. |
| 128 | void Clear(); |
| 129 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | // Fetches the next message from the queue. |
| 131 | // Returns true if there was a new message available and we successfully |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 132 | // fetched it. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | bool FetchNext(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 134 | |
| 135 | // Fetches the next message from the queue, waiting if necessary until there |
| 136 | // is one. |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 137 | void FetchNextBlocking(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | |
| 139 | // Fetches the last message from the queue. |
| 140 | // Returns true if there was a new message available and we successfully |
| 141 | // fetched it. |
| 142 | bool FetchLatest(); |
| 143 | |
Austin Schuh | 867c11e | 2015-02-22 21:35:43 -0800 | [diff] [blame] | 144 | // Fetches the latest message from the queue, or blocks if we have already |
| 145 | // fetched it until another is avilable. |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 146 | void FetchAnother(); |
| 147 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 148 | // Returns the age of the message. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 149 | const monotonic_clock::duration Age() const { |
| 150 | return monotonic_clock::now() - queue_msg_->sent_time; |
Brian Silverman | 5d3ab7f | 2015-02-21 15:54:21 -0500 | [diff] [blame] | 151 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 153 | bool IsNewerThan(const monotonic_clock::duration age) const { |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 154 | return get() != nullptr && Age() < age; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Returns a pointer to the current message. |
| 158 | // This pointer will be valid until a new message is fetched. |
| 159 | const T *get() const { return queue_msg_.get(); } |
| 160 | |
| 161 | // Returns a reference to the message. |
| 162 | // The message will be valid until a new message is fetched. |
| 163 | const T &operator*() const { |
| 164 | const T *msg = get(); |
| 165 | assert(msg != NULL); |
| 166 | return *msg; |
| 167 | } |
| 168 | |
| 169 | // Returns a pointer to the current message. |
| 170 | // This pointer will be valid until a new message is fetched. |
| 171 | const T *operator->() const { |
| 172 | const T *msg = get(); |
| 173 | assert(msg != NULL); |
| 174 | return msg; |
| 175 | } |
| 176 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 177 | // Returns a scoped_ptr containing a message. |
| 178 | // GCC will optimize away the copy constructor, so this is safe. |
| 179 | ScopedMessagePtr<T> MakeMessage(); |
| 180 | |
| 181 | // Returns a message builder that contains a pre-allocated message. |
Brian Silverman | 42456d8 | 2014-08-19 12:43:59 -0400 | [diff] [blame] | 182 | // This message will start out completely zeroed. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 183 | aos::MessageBuilder<T> MakeWithBuilder(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 184 | |
| 185 | const char *name() const { return queue_name_; } |
| 186 | |
| 187 | private: |
| 188 | const char *queue_name_; |
| 189 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 190 | T *MakeRawMessage(); |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 191 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 192 | // Pointer to the queue that this object fetches from. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 193 | RawQueue *queue_; |
Austin Schuh | 287d98e | 2014-03-09 00:41:55 -0800 | [diff] [blame] | 194 | int index_ = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 195 | // Scoped pointer holding the latest message or NULL. |
| 196 | ScopedMessagePtr<const T> queue_msg_; |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 197 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(Queue<T>); |
| 199 | }; |
| 200 | |
| 201 | // Base class for all queue groups. |
| 202 | class QueueGroup { |
| 203 | public: |
| 204 | // Constructs a queue group given its name and a unique hash of the name and |
| 205 | // type. |
Austin Schuh | 7660fcd | 2019-01-27 13:07:52 -0800 | [diff] [blame] | 206 | QueueGroup(const char *name) : name_(name) {} |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 207 | |
| 208 | // Returns the name of the queue group. |
| 209 | const char *name() const { return name_.c_str(); } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 210 | |
| 211 | private: |
| 212 | std::string name_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } // namespace aos |
| 216 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 217 | #include "aos/queue-tmpl.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 218 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 219 | #endif // AOS_QUEUE_H_ |