Fix race between Thread ctor/dtor and MessageQueueManager registrations.
This CL fixes a race where for Thread objects the parent MessageQueue
constructor registers the object in the MessageQueueManager even though
the Thread is not constructed completely yet. Same happens during
destruction.
BUG=webrtc:1225
Review URL: https://codereview.webrtc.org/1666863002
Cr-Commit-Position: refs/heads/master@{#11497}
diff --git a/webrtc/base/messagequeue.cc b/webrtc/base/messagequeue.cc
index 857cf12..bbdb941 100644
--- a/webrtc/base/messagequeue.cc
+++ b/webrtc/base/messagequeue.cc
@@ -116,9 +116,9 @@
//------------------------------------------------------------------
// MessageQueue
-MessageQueue::MessageQueue(SocketServer* ss)
+MessageQueue::MessageQueue(SocketServer* ss, bool init_queue)
: ss_(ss), fStop_(false), fPeekKeep_(false),
- dmsgq_next_num_(0) {
+ dmsgq_next_num_(0), fInitialized_(false), fDestroyed_(false) {
if (!ss_) {
// Currently, MessageQueue holds a socket server, and is the base class for
// Thread. It seems like it makes more sense for Thread to hold the socket
@@ -129,10 +129,30 @@
ss_ = default_ss_.get();
}
ss_->SetMessageQueue(this);
- MessageQueueManager::Add(this);
+ if (init_queue) {
+ DoInit();
+ }
}
MessageQueue::~MessageQueue() {
+ DoDestroy();
+}
+
+void MessageQueue::DoInit() {
+ if (fInitialized_) {
+ return;
+ }
+
+ fInitialized_ = true;
+ MessageQueueManager::Add(this);
+}
+
+void MessageQueue::DoDestroy() {
+ if (fDestroyed_) {
+ return;
+ }
+
+ fDestroyed_ = true;
// The signal is done from here to ensure
// that it always gets called when the queue
// is going away.