| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_MESSAGEQUEUE_H_ | 11 #ifndef WEBRTC_BASE_MESSAGEQUEUE_H_ |
| 12 #define WEBRTC_BASE_MESSAGEQUEUE_H_ | 12 #define WEBRTC_BASE_MESSAGEQUEUE_H_ |
| 13 | 13 |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 | 15 |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <list> | 17 #include <list> |
| 18 #include <memory> | 18 #include <memory> |
| 19 #include <queue> | 19 #include <queue> |
| 20 #include <vector> | 20 #include <vector> |
| 21 | 21 |
| 22 #include "webrtc/base/basictypes.h" | 22 #include "webrtc/base/basictypes.h" |
| 23 #include "webrtc/base/constructormagic.h" | 23 #include "webrtc/base/constructormagic.h" |
| 24 #include "webrtc/base/criticalsection.h" | 24 #include "webrtc/base/criticalsection.h" |
| 25 #include "webrtc/base/location.h" |
| 25 #include "webrtc/base/messagehandler.h" | 26 #include "webrtc/base/messagehandler.h" |
| 26 #include "webrtc/base/scoped_ref_ptr.h" | 27 #include "webrtc/base/scoped_ref_ptr.h" |
| 27 #include "webrtc/base/sharedexclusivelock.h" | 28 #include "webrtc/base/sharedexclusivelock.h" |
| 28 #include "webrtc/base/sigslot.h" | 29 #include "webrtc/base/sigslot.h" |
| 29 #include "webrtc/base/socketserver.h" | 30 #include "webrtc/base/socketserver.h" |
| 30 #include "webrtc/base/timeutils.h" | 31 #include "webrtc/base/timeutils.h" |
| 31 #include "webrtc/base/thread_annotations.h" | 32 #include "webrtc/base/thread_annotations.h" |
| 32 | 33 |
| 33 namespace rtc { | 34 namespace rtc { |
| 34 | 35 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 private: | 126 private: |
| 126 T* data_; | 127 T* data_; |
| 127 }; | 128 }; |
| 128 | 129 |
| 129 const uint32_t MQID_ANY = static_cast<uint32_t>(-1); | 130 const uint32_t MQID_ANY = static_cast<uint32_t>(-1); |
| 130 const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2); | 131 const uint32_t MQID_DISPOSE = static_cast<uint32_t>(-2); |
| 131 | 132 |
| 132 // No destructor | 133 // No destructor |
| 133 | 134 |
| 134 struct Message { | 135 struct Message { |
| 135 Message() { | 136 Message() |
| 136 memset(this, 0, sizeof(*this)); | 137 : phandler(nullptr), message_id(0), pdata(nullptr), ts_sensitive(0) {} |
| 137 } | |
| 138 inline bool Match(MessageHandler* handler, uint32_t id) const { | 138 inline bool Match(MessageHandler* handler, uint32_t id) const { |
| 139 return (handler == NULL || handler == phandler) | 139 return (handler == NULL || handler == phandler) |
| 140 && (id == MQID_ANY || id == message_id); | 140 && (id == MQID_ANY || id == message_id); |
| 141 } | 141 } |
| 142 Location posted_from; |
| 142 MessageHandler *phandler; | 143 MessageHandler *phandler; |
| 143 uint32_t message_id; | 144 uint32_t message_id; |
| 144 MessageData *pdata; | 145 MessageData *pdata; |
| 145 int64_t ts_sensitive; | 146 int64_t ts_sensitive; |
| 146 }; | 147 }; |
| 147 | 148 |
| 148 typedef std::list<Message> MessageList; | 149 typedef std::list<Message> MessageList; |
| 149 | 150 |
| 150 // DelayedMessage goes into a priority queue, sorted by trigger time. Messages | 151 // DelayedMessage goes into a priority queue, sorted by trigger time. Messages |
| 151 // with the same trigger time are processed in num_ (FIFO) order. | 152 // with the same trigger time are processed in num_ (FIFO) order. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 virtual bool IsQuitting(); | 198 virtual bool IsQuitting(); |
| 198 virtual void Restart(); | 199 virtual void Restart(); |
| 199 | 200 |
| 200 // Get() will process I/O until: | 201 // Get() will process I/O until: |
| 201 // 1) A message is available (returns true) | 202 // 1) A message is available (returns true) |
| 202 // 2) cmsWait seconds have elapsed (returns false) | 203 // 2) cmsWait seconds have elapsed (returns false) |
| 203 // 3) Stop() is called (returns false) | 204 // 3) Stop() is called (returns false) |
| 204 virtual bool Get(Message *pmsg, int cmsWait = kForever, | 205 virtual bool Get(Message *pmsg, int cmsWait = kForever, |
| 205 bool process_io = true); | 206 bool process_io = true); |
| 206 virtual bool Peek(Message *pmsg, int cmsWait = 0); | 207 virtual bool Peek(Message *pmsg, int cmsWait = 0); |
| 207 virtual void Post(MessageHandler* phandler, | 208 virtual void Post(const Location& posted_from, |
| 209 MessageHandler* phandler, |
| 208 uint32_t id = 0, | 210 uint32_t id = 0, |
| 209 MessageData* pdata = NULL, | 211 MessageData* pdata = NULL, |
| 210 bool time_sensitive = false); | 212 bool time_sensitive = false); |
| 211 virtual void PostDelayed(int cmsDelay, | 213 virtual void PostDelayed(const Location& posted_from, |
| 214 int cmsDelay, |
| 212 MessageHandler* phandler, | 215 MessageHandler* phandler, |
| 213 uint32_t id = 0, | 216 uint32_t id = 0, |
| 214 MessageData* pdata = NULL); | 217 MessageData* pdata = NULL); |
| 215 virtual void PostAt(int64_t tstamp, | 218 virtual void PostAt(const Location& posted_from, |
| 219 int64_t tstamp, |
| 216 MessageHandler* phandler, | 220 MessageHandler* phandler, |
| 217 uint32_t id = 0, | 221 uint32_t id = 0, |
| 218 MessageData* pdata = NULL); | 222 MessageData* pdata = NULL); |
| 219 // TODO(honghaiz): Remove this when all the dependencies are removed. | 223 // TODO(honghaiz): Remove this when all the dependencies are removed. |
| 220 virtual void PostAt(uint32_t tstamp, | 224 virtual void PostAt(const Location& posted_from, |
| 225 uint32_t tstamp, |
| 221 MessageHandler* phandler, | 226 MessageHandler* phandler, |
| 222 uint32_t id = 0, | 227 uint32_t id = 0, |
| 223 MessageData* pdata = NULL); | 228 MessageData* pdata = NULL); |
| 224 virtual void Clear(MessageHandler* phandler, | 229 virtual void Clear(MessageHandler* phandler, |
| 225 uint32_t id = MQID_ANY, | 230 uint32_t id = MQID_ANY, |
| 226 MessageList* removed = NULL); | 231 MessageList* removed = NULL); |
| 227 virtual void Dispatch(Message *pmsg); | 232 virtual void Dispatch(Message *pmsg); |
| 228 virtual void ReceiveSends(); | 233 virtual void ReceiveSends(); |
| 229 | 234 |
| 230 // Amount of time until the next message can be retrieved | 235 // Amount of time until the next message can be retrieved |
| 231 virtual int GetDelay(); | 236 virtual int GetDelay(); |
| 232 | 237 |
| 233 bool empty() const { return size() == 0u; } | 238 bool empty() const { return size() == 0u; } |
| 234 size_t size() const { | 239 size_t size() const { |
| 235 CritScope cs(&crit_); // msgq_.size() is not thread safe. | 240 CritScope cs(&crit_); // msgq_.size() is not thread safe. |
| 236 return msgq_.size() + dmsgq_.size() + (fPeekKeep_ ? 1u : 0u); | 241 return msgq_.size() + dmsgq_.size() + (fPeekKeep_ ? 1u : 0u); |
| 237 } | 242 } |
| 238 | 243 |
| 239 // Internally posts a message which causes the doomed object to be deleted | 244 // Internally posts a message which causes the doomed object to be deleted |
| 240 template<class T> void Dispose(T* doomed) { | 245 template<class T> void Dispose(T* doomed) { |
| 241 if (doomed) { | 246 if (doomed) { |
| 242 Post(NULL, MQID_DISPOSE, new DisposeData<T>(doomed)); | 247 Post(FROM_HERE, NULL, MQID_DISPOSE, new DisposeData<T>(doomed)); |
| 243 } | 248 } |
| 244 } | 249 } |
| 245 | 250 |
| 246 // When this signal is sent out, any references to this queue should | 251 // When this signal is sent out, any references to this queue should |
| 247 // no longer be used. | 252 // no longer be used. |
| 248 sigslot::signal0<> SignalQueueDestroyed; | 253 sigslot::signal0<> SignalQueueDestroyed; |
| 249 | 254 |
| 250 protected: | 255 protected: |
| 251 class PriorityQueue : public std::priority_queue<DelayedMessage> { | 256 class PriorityQueue : public std::priority_queue<DelayedMessage> { |
| 252 public: | 257 public: |
| 253 container_type& container() { return c; } | 258 container_type& container() { return c; } |
| 254 void reheap() { make_heap(c.begin(), c.end(), comp); } | 259 void reheap() { make_heap(c.begin(), c.end(), comp); } |
| 255 }; | 260 }; |
| 256 | 261 |
| 257 void DoDelayPost(int cmsDelay, | 262 void DoDelayPost(const Location& posted_from, |
| 263 int cmsDelay, |
| 258 int64_t tstamp, | 264 int64_t tstamp, |
| 259 MessageHandler* phandler, | 265 MessageHandler* phandler, |
| 260 uint32_t id, | 266 uint32_t id, |
| 261 MessageData* pdata); | 267 MessageData* pdata); |
| 262 | 268 |
| 263 // Perform initialization, subclasses must call this from their constructor | 269 // Perform initialization, subclasses must call this from their constructor |
| 264 // if false was passed as init_queue to the MessageQueue constructor. | 270 // if false was passed as init_queue to the MessageQueue constructor. |
| 265 void DoInit(); | 271 void DoInit(); |
| 266 | 272 |
| 267 // Perform cleanup, subclasses that override Clear must call this from the | 273 // Perform cleanup, subclasses that override Clear must call this from the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 286 // Used if SocketServer ownership lies with |this|. | 292 // Used if SocketServer ownership lies with |this|. |
| 287 std::unique_ptr<SocketServer> own_ss_; | 293 std::unique_ptr<SocketServer> own_ss_; |
| 288 SharedExclusiveLock ss_lock_; | 294 SharedExclusiveLock ss_lock_; |
| 289 | 295 |
| 290 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageQueue); | 296 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MessageQueue); |
| 291 }; | 297 }; |
| 292 | 298 |
| 293 } // namespace rtc | 299 } // namespace rtc |
| 294 | 300 |
| 295 #endif // WEBRTC_BASE_MESSAGEQUEUE_H_ | 301 #endif // WEBRTC_BASE_MESSAGEQUEUE_H_ |
| OLD | NEW |