Reland: Prevent data race in MessageQueue.

The CL prevents a data race in MessageQueue where the variable "ss_" is
modified without a lock while sometimes read inside a lock.

Also thread annotations have been added to the MessageQueue class.

This was already reviewed and landed in https://codereview.webrtc.org/1675923002/
but failed in Chromium GN builds due to sharedexclusivelock.cc not being
compiled in these builds. This changed in https://codereview.webrtc.org/1712773003/
so the reland should work fine now.

BUG=webrtc:5496

Review URL: https://codereview.webrtc.org/1729893002

Cr-Commit-Position: refs/heads/master@{#11758}
diff --git a/webrtc/base/messagequeue.h b/webrtc/base/messagequeue.h
index a7991a8..efc479c 100644
--- a/webrtc/base/messagequeue.h
+++ b/webrtc/base/messagequeue.h
@@ -24,9 +24,11 @@
 #include "webrtc/base/messagehandler.h"
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/base/scoped_ref_ptr.h"
+#include "webrtc/base/sharedexclusivelock.h"
 #include "webrtc/base/sigslot.h"
 #include "webrtc/base/socketserver.h"
 #include "webrtc/base/timeutils.h"
+#include "webrtc/base/thread_annotations.h"
 
 namespace rtc {
 
@@ -181,7 +183,7 @@
   // calling Clear on the object from a different thread.
   virtual ~MessageQueue();
 
-  SocketServer* socketserver() { return ss_; }
+  SocketServer* socketserver();
   void set_socketserver(SocketServer* ss);
 
   // Note: The behavior of MessageQueue has changed.  When a MQ is stopped,
@@ -260,21 +262,25 @@
   // destructor.
   void DoDestroy();
 
-  // The SocketServer is not owned by MessageQueue.
-  SocketServer* ss_;
-  // If a server isn't supplied in the constructor, use this one.
-  scoped_ptr<SocketServer> default_ss_;
+  void WakeUpSocketServer();
+
   bool fStop_;
   bool fPeekKeep_;
   Message msgPeek_;
-  MessageList msgq_;
-  PriorityQueue dmsgq_;
-  uint32_t dmsgq_next_num_;
+  MessageList msgq_ GUARDED_BY(crit_);
+  PriorityQueue dmsgq_ GUARDED_BY(crit_);
+  uint32_t dmsgq_next_num_ GUARDED_BY(crit_);
   CriticalSection crit_;
   bool fInitialized_;
   bool fDestroyed_;
 
  private:
+  // The SocketServer is not owned by MessageQueue.
+  SocketServer* ss_ GUARDED_BY(ss_lock_);
+  // If a server isn't supplied in the constructor, use this one.
+  scoped_ptr<SocketServer> default_ss_;
+  SharedExclusiveLock ss_lock_;
+
   RTC_DISALLOW_COPY_AND_ASSIGN(MessageQueue);
 };