henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_MESSAGE_QUEUE_H_ |
| 12 | #define RTC_BASE_MESSAGE_QUEUE_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <string.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <list> |
| 18 | #include <memory> |
| 19 | #include <queue> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
| 24 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 25 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "rtc_base/message_handler.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 27 | #include "rtc_base/socket_server.h" |
Artem Titov | e41c433 | 2018-07-25 13:04:28 | [diff] [blame] | 28 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 29 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
| 32 | |
| 33 | struct Message; |
| 34 | class MessageQueue; |
| 35 | |
| 36 | // MessageQueueManager does cleanup of of message queues |
| 37 | |
| 38 | class MessageQueueManager { |
| 39 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 40 | static void Add(MessageQueue* message_queue); |
| 41 | static void Remove(MessageQueue* message_queue); |
| 42 | static void Clear(MessageHandler* handler); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 43 | |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 44 | // TODO(nisse): Delete alias, as soon as downstream code is updated. |
| 45 | static void ProcessAllMessageQueues() { ProcessAllMessageQueuesForTesting(); } |
| 46 | |
| 47 | // For testing purposes, for use with a simulated clock. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 48 | // Ensures that all message queues have processed delayed messages |
| 49 | // up until the current point in time. |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 50 | static void ProcessAllMessageQueuesForTesting(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | static MessageQueueManager* Instance(); |
| 54 | |
| 55 | MessageQueueManager(); |
| 56 | ~MessageQueueManager(); |
| 57 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 58 | void AddInternal(MessageQueue* message_queue); |
| 59 | void RemoveInternal(MessageQueue* message_queue); |
| 60 | void ClearInternal(MessageHandler* handler); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 61 | void ProcessAllMessageQueuesInternal(); |
| 62 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 63 | // This list contains all live MessageQueues. |
danilchap | 3c6abd2 | 2017-09-06 12:46:29 | [diff] [blame] | 64 | std::vector<MessageQueue*> message_queues_ RTC_GUARDED_BY(crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 65 | |
jbauch | 5b36173 | 2017-07-07 06:51:37 | [diff] [blame] | 66 | // Methods that don't modify the list of message queues may be called in a |
| 67 | // re-entrant fashion. "processing_" keeps track of the depth of re-entrant |
| 68 | // calls. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 69 | CriticalSection crit_; |
danilchap | 3c6abd2 | 2017-09-06 12:46:29 | [diff] [blame] | 70 | size_t processing_ RTC_GUARDED_BY(crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Derive from this for specialized data |
| 74 | // App manages lifetime, except when messages are purged |
| 75 | |
| 76 | class MessageData { |
| 77 | public: |
| 78 | MessageData() {} |
| 79 | virtual ~MessageData() {} |
| 80 | }; |
| 81 | |
| 82 | template <class T> |
| 83 | class TypedMessageData : public MessageData { |
| 84 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 85 | explicit TypedMessageData(const T& data) : data_(data) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 86 | const T& data() const { return data_; } |
| 87 | T& data() { return data_; } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 88 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 89 | private: |
| 90 | T data_; |
| 91 | }; |
| 92 | |
| 93 | // Like TypedMessageData, but for pointers that require a delete. |
| 94 | template <class T> |
| 95 | class ScopedMessageData : public MessageData { |
| 96 | public: |
| 97 | explicit ScopedMessageData(std::unique_ptr<T> data) |
| 98 | : data_(std::move(data)) {} |
| 99 | // Deprecated. |
| 100 | // TODO(deadbeef): Remove this once downstream applications stop using it. |
| 101 | explicit ScopedMessageData(T* data) : data_(data) {} |
| 102 | // Deprecated. |
| 103 | // TODO(deadbeef): Returning a reference to a unique ptr? Why. Get rid of |
| 104 | // this once downstream applications stop using it, then rename inner_data to |
| 105 | // just data. |
| 106 | const std::unique_ptr<T>& data() const { return data_; } |
| 107 | std::unique_ptr<T>& data() { return data_; } |
| 108 | |
| 109 | const T& inner_data() const { return *data_; } |
| 110 | T& inner_data() { return *data_; } |
| 111 | |
| 112 | private: |
| 113 | std::unique_ptr<T> data_; |
| 114 | }; |
| 115 | |
| 116 | // Like ScopedMessageData, but for reference counted pointers. |
| 117 | template <class T> |
| 118 | class ScopedRefMessageData : public MessageData { |
| 119 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 120 | explicit ScopedRefMessageData(T* data) : data_(data) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 121 | const scoped_refptr<T>& data() const { return data_; } |
| 122 | scoped_refptr<T>& data() { return data_; } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 123 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 124 | private: |
| 125 | scoped_refptr<T> data_; |
| 126 | }; |
| 127 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 128 | template <class T> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 129 | inline MessageData* WrapMessageData(const T& data) { |
| 130 | return new TypedMessageData<T>(data); |
| 131 | } |
| 132 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 133 | template <class T> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 134 | inline const T& UseMessageData(MessageData* data) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 135 | return static_cast<TypedMessageData<T>*>(data)->data(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 136 | } |
| 137 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 138 | template <class T> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 139 | class DisposeData : public MessageData { |
| 140 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 141 | explicit DisposeData(T* data) : data_(data) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 142 | virtual ~DisposeData() { delete data_; } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 143 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 144 | private: |
| 145 | T* data_; |
| 146 | }; |
| 147 | |
| 148 | const uint32_t MQID_ANY = static_cast<uint32_t>(-1); |
| 149 | const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2); |
| 150 | |
| 151 | // No destructor |
| 152 | |
| 153 | struct Message { |
| 154 | Message() |
| 155 | : phandler(nullptr), message_id(0), pdata(nullptr), ts_sensitive(0) {} |
| 156 | inline bool Match(MessageHandler* handler, uint32_t id) const { |
| 157 | return (handler == nullptr || handler == phandler) && |
| 158 | (id == MQID_ANY || id == message_id); |
| 159 | } |
| 160 | Location posted_from; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 161 | MessageHandler* phandler; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 162 | uint32_t message_id; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 163 | MessageData* pdata; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 164 | int64_t ts_sensitive; |
| 165 | }; |
| 166 | |
| 167 | typedef std::list<Message> MessageList; |
| 168 | |
| 169 | // DelayedMessage goes into a priority queue, sorted by trigger time. Messages |
| 170 | // with the same trigger time are processed in num_ (FIFO) order. |
| 171 | |
| 172 | class DelayedMessage { |
| 173 | public: |
| 174 | DelayedMessage(int64_t delay, |
| 175 | int64_t trigger, |
| 176 | uint32_t num, |
| 177 | const Message& msg) |
| 178 | : cmsDelay_(delay), msTrigger_(trigger), num_(num), msg_(msg) {} |
| 179 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 180 | bool operator<(const DelayedMessage& dmsg) const { |
| 181 | return (dmsg.msTrigger_ < msTrigger_) || |
| 182 | ((dmsg.msTrigger_ == msTrigger_) && (dmsg.num_ < num_)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int64_t cmsDelay_; // for debugging |
| 186 | int64_t msTrigger_; |
| 187 | uint32_t num_; |
| 188 | Message msg_; |
| 189 | }; |
| 190 | |
| 191 | class MessageQueue { |
| 192 | public: |
| 193 | static const int kForever = -1; |
| 194 | |
| 195 | // Create a new MessageQueue and optionally assign it to the passed |
| 196 | // SocketServer. Subclasses that override Clear should pass false for |
| 197 | // init_queue and call DoInit() from their constructor to prevent races |
| 198 | // with the MessageQueueManager using the object while the vtable is still |
| 199 | // being created. |
| 200 | MessageQueue(SocketServer* ss, bool init_queue); |
| 201 | MessageQueue(std::unique_ptr<SocketServer> ss, bool init_queue); |
| 202 | |
| 203 | // NOTE: SUBCLASSES OF MessageQueue THAT OVERRIDE Clear MUST CALL |
| 204 | // DoDestroy() IN THEIR DESTRUCTORS! This is required to avoid a data race |
| 205 | // between the destructor modifying the vtable, and the MessageQueueManager |
| 206 | // calling Clear on the object from a different thread. |
| 207 | virtual ~MessageQueue(); |
| 208 | |
| 209 | SocketServer* socketserver(); |
| 210 | |
| 211 | // Note: The behavior of MessageQueue has changed. When a MQ is stopped, |
| 212 | // futher Posts and Sends will fail. However, any pending Sends and *ready* |
| 213 | // Posts (as opposed to unexpired delayed Posts) will be delivered before |
| 214 | // Get (or Peek) returns false. By guaranteeing delivery of those messages, |
| 215 | // we eliminate the race condition when an MessageHandler and MessageQueue |
| 216 | // may be destroyed independently of each other. |
| 217 | virtual void Quit(); |
| 218 | virtual bool IsQuitting(); |
| 219 | virtual void Restart(); |
| 220 | // Not all message queues actually process messages (such as SignalThread). |
| 221 | // In those cases, it's important to know, before posting, that it won't be |
| 222 | // Processed. Normally, this would be true until IsQuitting() is true. |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 223 | virtual bool IsProcessingMessagesForTesting(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 224 | |
| 225 | // Get() will process I/O until: |
| 226 | // 1) A message is available (returns true) |
| 227 | // 2) cmsWait seconds have elapsed (returns false) |
| 228 | // 3) Stop() is called (returns false) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 229 | virtual bool Get(Message* pmsg, |
| 230 | int cmsWait = kForever, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 231 | bool process_io = true); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 232 | virtual bool Peek(Message* pmsg, int cmsWait = 0); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 233 | virtual void Post(const Location& posted_from, |
| 234 | MessageHandler* phandler, |
| 235 | uint32_t id = 0, |
| 236 | MessageData* pdata = nullptr, |
| 237 | bool time_sensitive = false); |
| 238 | virtual void PostDelayed(const Location& posted_from, |
| 239 | int cmsDelay, |
| 240 | MessageHandler* phandler, |
| 241 | uint32_t id = 0, |
| 242 | MessageData* pdata = nullptr); |
| 243 | virtual void PostAt(const Location& posted_from, |
| 244 | int64_t tstamp, |
| 245 | MessageHandler* phandler, |
| 246 | uint32_t id = 0, |
| 247 | MessageData* pdata = nullptr); |
| 248 | // TODO(honghaiz): Remove this when all the dependencies are removed. |
| 249 | virtual void PostAt(const Location& posted_from, |
| 250 | uint32_t tstamp, |
| 251 | MessageHandler* phandler, |
| 252 | uint32_t id = 0, |
| 253 | MessageData* pdata = nullptr); |
| 254 | virtual void Clear(MessageHandler* phandler, |
| 255 | uint32_t id = MQID_ANY, |
| 256 | MessageList* removed = nullptr); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 257 | virtual void Dispatch(Message* pmsg); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 258 | virtual void ReceiveSends(); |
| 259 | |
| 260 | // Amount of time until the next message can be retrieved |
| 261 | virtual int GetDelay(); |
| 262 | |
| 263 | bool empty() const { return size() == 0u; } |
| 264 | size_t size() const { |
| 265 | CritScope cs(&crit_); // msgq_.size() is not thread safe. |
| 266 | return msgq_.size() + dmsgq_.size() + (fPeekKeep_ ? 1u : 0u); |
| 267 | } |
| 268 | |
| 269 | // Internally posts a message which causes the doomed object to be deleted |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 270 | template <class T> |
| 271 | void Dispose(T* doomed) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 272 | if (doomed) { |
| 273 | Post(RTC_FROM_HERE, nullptr, MQID_DISPOSE, new DisposeData<T>(doomed)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // When this signal is sent out, any references to this queue should |
| 278 | // no longer be used. |
| 279 | sigslot::signal0<> SignalQueueDestroyed; |
| 280 | |
| 281 | protected: |
| 282 | class PriorityQueue : public std::priority_queue<DelayedMessage> { |
| 283 | public: |
| 284 | container_type& container() { return c; } |
| 285 | void reheap() { make_heap(c.begin(), c.end(), comp); } |
| 286 | }; |
| 287 | |
| 288 | void DoDelayPost(const Location& posted_from, |
| 289 | int64_t cmsDelay, |
| 290 | int64_t tstamp, |
| 291 | MessageHandler* phandler, |
| 292 | uint32_t id, |
| 293 | MessageData* pdata); |
| 294 | |
| 295 | // Perform initialization, subclasses must call this from their constructor |
| 296 | // if false was passed as init_queue to the MessageQueue constructor. |
| 297 | void DoInit(); |
| 298 | |
Niels Möller | 5e007b7 | 2018-09-07 10:35:44 | [diff] [blame] | 299 | // Does not take any lock. Must be called either while holding crit_, or by |
| 300 | // the destructor (by definition, the latter has exclusive access). |
| 301 | void ClearInternal(MessageHandler* phandler, |
| 302 | uint32_t id, |
| 303 | MessageList* removed) RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_); |
| 304 | |
| 305 | // Perform cleanup; subclasses must call this from the destructor, |
| 306 | // and are not expected to actually hold the lock. |
| 307 | void DoDestroy() RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 308 | |
| 309 | void WakeUpSocketServer(); |
| 310 | |
| 311 | bool fPeekKeep_; |
| 312 | Message msgPeek_; |
danilchap | 3c6abd2 | 2017-09-06 12:46:29 | [diff] [blame] | 313 | MessageList msgq_ RTC_GUARDED_BY(crit_); |
| 314 | PriorityQueue dmsgq_ RTC_GUARDED_BY(crit_); |
| 315 | uint32_t dmsgq_next_num_ RTC_GUARDED_BY(crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 316 | CriticalSection crit_; |
| 317 | bool fInitialized_; |
| 318 | bool fDestroyed_; |
| 319 | |
| 320 | private: |
| 321 | volatile int stop_; |
| 322 | |
| 323 | // The SocketServer might not be owned by MessageQueue. |
| 324 | SocketServer* const ss_; |
| 325 | // Used if SocketServer ownership lies with |this|. |
| 326 | std::unique_ptr<SocketServer> own_ss_; |
| 327 | |
| 328 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageQueue); |
| 329 | }; |
| 330 | |
| 331 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 332 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 333 | #endif // RTC_BASE_MESSAGE_QUEUE_H_ |