henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
| 11 | #ifndef WEBRTC_BASE_MESSAGEQUEUE_H_ |
| 12 | #define WEBRTC_BASE_MESSAGEQUEUE_H_ |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <list> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 18 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include <queue> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "webrtc/base/basictypes.h" |
| 23 | #include "webrtc/base/constructormagic.h" |
| 24 | #include "webrtc/base/criticalsection.h" |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 25 | #include "webrtc/base/location.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | #include "webrtc/base/messagehandler.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | #include "webrtc/base/scoped_ref_ptr.h" |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 28 | #include "webrtc/base/sharedexclusivelock.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #include "webrtc/base/sigslot.h" |
| 30 | #include "webrtc/base/socketserver.h" |
| 31 | #include "webrtc/base/timeutils.h" |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 32 | #include "webrtc/base/thread_annotations.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
| 34 | namespace rtc { |
| 35 | |
| 36 | struct Message; |
| 37 | class MessageQueue; |
| 38 | |
| 39 | // MessageQueueManager does cleanup of of message queues |
| 40 | |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 41 | class MessageQueueManager { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | public: |
| 43 | static void Add(MessageQueue *message_queue); |
| 44 | static void Remove(MessageQueue *message_queue); |
| 45 | static void Clear(MessageHandler *handler); |
| 46 | |
| 47 | // For testing purposes, we expose whether or not the MessageQueueManager |
| 48 | // instance has been initialized. It has no other use relative to the rest of |
| 49 | // the functions of this class, which auto-initialize the underlying |
| 50 | // MessageQueueManager instance when necessary. |
| 51 | static bool IsInitialized(); |
| 52 | |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 53 | // Mainly for testing purposes, for use with a simulated clock. |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 54 | // Ensures that all message queues have processed delayed messages |
| 55 | // up until the current point in time. |
| 56 | static void ProcessAllMessageQueues(); |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 57 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | private: |
| 59 | static MessageQueueManager* Instance(); |
| 60 | |
| 61 | MessageQueueManager(); |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 62 | ~MessageQueueManager(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | |
| 64 | void AddInternal(MessageQueue *message_queue); |
| 65 | void RemoveInternal(MessageQueue *message_queue); |
| 66 | void ClearInternal(MessageHandler *handler); |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 67 | void ProcessAllMessageQueuesInternal(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | static MessageQueueManager* instance_; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 70 | // This list contains all live MessageQueues. |
andresp | cdf6172 | 2016-07-08 02:45:40 -0700 | [diff] [blame^] | 71 | std::vector<MessageQueue*> message_queues_ GUARDED_BY(crit_); |
| 72 | |
| 73 | // Acquire this with DebugNonReentrantCritScope. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | CriticalSection crit_; |
andresp | cdf6172 | 2016-07-08 02:45:40 -0700 | [diff] [blame^] | 75 | bool locked_ GUARDED_BY(crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Derive from this for specialized data |
| 79 | // App manages lifetime, except when messages are purged |
| 80 | |
| 81 | class MessageData { |
| 82 | public: |
| 83 | MessageData() {} |
| 84 | virtual ~MessageData() {} |
| 85 | }; |
| 86 | |
| 87 | template <class T> |
| 88 | class TypedMessageData : public MessageData { |
| 89 | public: |
| 90 | explicit TypedMessageData(const T& data) : data_(data) { } |
| 91 | const T& data() const { return data_; } |
| 92 | T& data() { return data_; } |
| 93 | private: |
| 94 | T data_; |
| 95 | }; |
| 96 | |
| 97 | // Like TypedMessageData, but for pointers that require a delete. |
| 98 | template <class T> |
| 99 | class ScopedMessageData : public MessageData { |
| 100 | public: |
| 101 | explicit ScopedMessageData(T* data) : data_(data) { } |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 102 | const std::unique_ptr<T>& data() const { return data_; } |
| 103 | std::unique_ptr<T>& data() { return data_; } |
| 104 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | private: |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 106 | std::unique_ptr<T> data_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // Like ScopedMessageData, but for reference counted pointers. |
| 110 | template <class T> |
| 111 | class ScopedRefMessageData : public MessageData { |
| 112 | public: |
| 113 | explicit ScopedRefMessageData(T* data) : data_(data) { } |
| 114 | const scoped_refptr<T>& data() const { return data_; } |
| 115 | scoped_refptr<T>& data() { return data_; } |
| 116 | private: |
| 117 | scoped_refptr<T> data_; |
| 118 | }; |
| 119 | |
| 120 | template<class T> |
| 121 | inline MessageData* WrapMessageData(const T& data) { |
| 122 | return new TypedMessageData<T>(data); |
| 123 | } |
| 124 | |
| 125 | template<class T> |
| 126 | inline const T& UseMessageData(MessageData* data) { |
| 127 | return static_cast< TypedMessageData<T>* >(data)->data(); |
| 128 | } |
| 129 | |
| 130 | template<class T> |
| 131 | class DisposeData : public MessageData { |
| 132 | public: |
| 133 | explicit DisposeData(T* data) : data_(data) { } |
| 134 | virtual ~DisposeData() { delete data_; } |
| 135 | private: |
| 136 | T* data_; |
| 137 | }; |
| 138 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 139 | const uint32_t MQID_ANY = static_cast<uint32_t>(-1); |
| 140 | const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | |
| 142 | // No destructor |
| 143 | |
| 144 | struct Message { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 145 | Message() |
| 146 | : phandler(nullptr), message_id(0), pdata(nullptr), ts_sensitive(0) {} |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 147 | inline bool Match(MessageHandler* handler, uint32_t id) const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 148 | return (handler == NULL || handler == phandler) |
| 149 | && (id == MQID_ANY || id == message_id); |
| 150 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 151 | Location posted_from; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | MessageHandler *phandler; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 153 | uint32_t message_id; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | MessageData *pdata; |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 155 | int64_t ts_sensitive; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | typedef std::list<Message> MessageList; |
| 159 | |
| 160 | // DelayedMessage goes into a priority queue, sorted by trigger time. Messages |
| 161 | // with the same trigger time are processed in num_ (FIFO) order. |
| 162 | |
| 163 | class DelayedMessage { |
| 164 | public: |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 165 | DelayedMessage(int64_t delay, |
| 166 | int64_t trigger, |
| 167 | uint32_t num, |
| 168 | const Message& msg) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 169 | : cmsDelay_(delay), msTrigger_(trigger), num_(num), msg_(msg) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | |
| 171 | bool operator< (const DelayedMessage& dmsg) const { |
| 172 | return (dmsg.msTrigger_ < msTrigger_) |
| 173 | || ((dmsg.msTrigger_ == msTrigger_) && (dmsg.num_ < num_)); |
| 174 | } |
| 175 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 176 | int64_t cmsDelay_; // for debugging |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 177 | int64_t msTrigger_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | uint32_t num_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | Message msg_; |
| 180 | }; |
| 181 | |
| 182 | class MessageQueue { |
| 183 | public: |
andresp@webrtc.org | 53d9012 | 2015-02-09 14:19:09 +0000 | [diff] [blame] | 184 | static const int kForever = -1; |
| 185 | |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 186 | // Create a new MessageQueue and optionally assign it to the passed |
| 187 | // SocketServer. Subclasses that override Clear should pass false for |
| 188 | // init_queue and call DoInit() from their constructor to prevent races |
| 189 | // with the MessageQueueManager using the object while the vtable is still |
| 190 | // being created. |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 191 | MessageQueue(SocketServer* ss, bool init_queue); |
| 192 | MessageQueue(std::unique_ptr<SocketServer> ss, bool init_queue); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 193 | |
| 194 | // NOTE: SUBCLASSES OF MessageQueue THAT OVERRIDE Clear MUST CALL |
| 195 | // DoDestroy() IN THEIR DESTRUCTORS! This is required to avoid a data race |
| 196 | // between the destructor modifying the vtable, and the MessageQueueManager |
| 197 | // calling Clear on the object from a different thread. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | virtual ~MessageQueue(); |
| 199 | |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 200 | SocketServer* socketserver(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | void set_socketserver(SocketServer* ss); |
| 202 | |
| 203 | // Note: The behavior of MessageQueue has changed. When a MQ is stopped, |
| 204 | // futher Posts and Sends will fail. However, any pending Sends and *ready* |
| 205 | // Posts (as opposed to unexpired delayed Posts) will be delivered before |
| 206 | // Get (or Peek) returns false. By guaranteeing delivery of those messages, |
| 207 | // we eliminate the race condition when an MessageHandler and MessageQueue |
| 208 | // may be destroyed independently of each other. |
| 209 | virtual void Quit(); |
| 210 | virtual bool IsQuitting(); |
| 211 | virtual void Restart(); |
| 212 | |
| 213 | // Get() will process I/O until: |
| 214 | // 1) A message is available (returns true) |
| 215 | // 2) cmsWait seconds have elapsed (returns false) |
| 216 | // 3) Stop() is called (returns false) |
| 217 | virtual bool Get(Message *pmsg, int cmsWait = kForever, |
| 218 | bool process_io = true); |
| 219 | virtual bool Peek(Message *pmsg, int cmsWait = 0); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 220 | virtual void Post(const Location& posted_from, |
| 221 | MessageHandler* phandler, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 222 | uint32_t id = 0, |
| 223 | MessageData* pdata = NULL, |
| 224 | bool time_sensitive = false); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 225 | virtual void PostDelayed(const Location& posted_from, |
| 226 | int cmsDelay, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 227 | MessageHandler* phandler, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 228 | uint32_t id = 0, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 229 | MessageData* pdata = NULL); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 230 | virtual void PostAt(const Location& posted_from, |
| 231 | int64_t tstamp, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 232 | MessageHandler* phandler, |
| 233 | uint32_t id = 0, |
| 234 | MessageData* pdata = NULL); |
| 235 | // TODO(honghaiz): Remove this when all the dependencies are removed. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 236 | virtual void PostAt(const Location& posted_from, |
| 237 | uint32_t tstamp, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 238 | MessageHandler* phandler, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 239 | uint32_t id = 0, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 240 | MessageData* pdata = NULL); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 241 | virtual void Clear(MessageHandler* phandler, |
| 242 | uint32_t id = MQID_ANY, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | MessageList* removed = NULL); |
| 244 | virtual void Dispatch(Message *pmsg); |
| 245 | virtual void ReceiveSends(); |
| 246 | |
| 247 | // Amount of time until the next message can be retrieved |
| 248 | virtual int GetDelay(); |
| 249 | |
| 250 | bool empty() const { return size() == 0u; } |
| 251 | size_t size() const { |
| 252 | CritScope cs(&crit_); // msgq_.size() is not thread safe. |
| 253 | return msgq_.size() + dmsgq_.size() + (fPeekKeep_ ? 1u : 0u); |
| 254 | } |
| 255 | |
| 256 | // Internally posts a message which causes the doomed object to be deleted |
| 257 | template<class T> void Dispose(T* doomed) { |
| 258 | if (doomed) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 259 | Post(RTC_FROM_HERE, NULL, MQID_DISPOSE, new DisposeData<T>(doomed)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | // When this signal is sent out, any references to this queue should |
| 264 | // no longer be used. |
| 265 | sigslot::signal0<> SignalQueueDestroyed; |
| 266 | |
| 267 | protected: |
| 268 | class PriorityQueue : public std::priority_queue<DelayedMessage> { |
| 269 | public: |
| 270 | container_type& container() { return c; } |
| 271 | void reheap() { make_heap(c.begin(), c.end(), comp); } |
| 272 | }; |
| 273 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 274 | void DoDelayPost(const Location& posted_from, |
| 275 | int64_t cmsDelay, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 276 | int64_t tstamp, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 277 | MessageHandler* phandler, |
| 278 | uint32_t id, |
| 279 | MessageData* pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 280 | |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 281 | // Perform initialization, subclasses must call this from their constructor |
| 282 | // if false was passed as init_queue to the MessageQueue constructor. |
| 283 | void DoInit(); |
| 284 | |
| 285 | // Perform cleanup, subclasses that override Clear must call this from the |
| 286 | // destructor. |
| 287 | void DoDestroy(); |
| 288 | |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 289 | void WakeUpSocketServer(); |
| 290 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 291 | bool fStop_; |
| 292 | bool fPeekKeep_; |
| 293 | Message msgPeek_; |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 294 | MessageList msgq_ GUARDED_BY(crit_); |
| 295 | PriorityQueue dmsgq_ GUARDED_BY(crit_); |
| 296 | uint32_t dmsgq_next_num_ GUARDED_BY(crit_); |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 297 | CriticalSection crit_; |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 298 | bool fInitialized_; |
| 299 | bool fDestroyed_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 300 | |
| 301 | private: |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 302 | // The SocketServer might not be owned by MessageQueue. |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 303 | SocketServer* ss_ GUARDED_BY(ss_lock_); |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 304 | // Used if SocketServer ownership lies with |this|. |
| 305 | std::unique_ptr<SocketServer> own_ss_; |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 306 | SharedExclusiveLock ss_lock_; |
| 307 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 308 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageQueue); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | } // namespace rtc |
| 312 | |
| 313 | #endif // WEBRTC_BASE_MESSAGEQUEUE_H_ |