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