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