Migrate rtc_base to webrtc::Mutex.

Bug: webrtc:11567
Change-Id: Ib8630e0cf1266e7c3f8ce718e1ed9f8848f42ec8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/178806
Reviewed-by: Tommi <tommi@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31682}
diff --git a/pc/srtp_session.h b/pc/srtp_session.h
index 0a26c02..e2f1b8d 100644
--- a/pc/srtp_session.h
+++ b/pc/srtp_session.h
@@ -14,6 +14,7 @@
 #include <vector>
 
 #include "api/scoped_refptr.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/thread_checker.h"
 
 // Forward declaration to avoid pulling in libsrtp headers here
diff --git a/pc/srtp_session_unittest.cc b/pc/srtp_session_unittest.cc
index d0f6ea6..e5a03d4 100644
--- a/pc/srtp_session_unittest.cc
+++ b/pc/srtp_session_unittest.cc
@@ -17,6 +17,7 @@
 #include "media/base/fake_rtp.h"
 #include "pc/test/srtp_test_util.h"
 #include "rtc_base/byte_order.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/ssl_stream_adapter.h"  // For rtc::SRTP_*
 #include "system_wrappers/include/metrics.h"
 #include "test/gmock.h"
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index fb341e2..2f0fba9 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -55,6 +55,7 @@
     ":type_traits",
     "../api:array_view",
     "../api:scoped_refptr",
+    "synchronization:mutex",
     "system:arch",
     "system:rtc_export",
     "system:unused",
@@ -325,6 +326,7 @@
   deps = [
     ":rtc_base_approved",
     "../system_wrappers",
+    "synchronization:mutex",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
 }
@@ -479,6 +481,7 @@
       ":safe_conversions",
       ":timeutils",
       "../api/task_queue",
+      "synchronization:mutex",
     ]
     absl_deps = [
       "//third_party/abseil-cpp/absl/container:inlined_vector",
@@ -501,6 +504,7 @@
       ":checks",
       ":logging",
       "../api/task_queue",
+      "synchronization:mutex",
       "system:gcd_helpers",
     ]
     absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
@@ -524,6 +528,7 @@
       ":safe_conversions",
       ":timeutils",
       "../api/task_queue",
+      "synchronization:mutex",
     ]
     absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
   }
@@ -544,6 +549,7 @@
     ":safe_conversions",
     ":timeutils",
     "../api/task_queue",
+    "synchronization:mutex",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
 }
@@ -782,6 +788,7 @@
     "../api/task_queue",
     "../system_wrappers:field_trial",
     "network:sent_packet",
+    "synchronization:mutex",
     "synchronization:sequence_checker",
     "system:file_wrapper",
     "system:inline",
@@ -1037,6 +1044,7 @@
     ":rtc_base",
     ":rtc_base_tests_utils",
     ":timeutils",
+    "synchronization:mutex",
   ]
 }
 
@@ -1085,6 +1093,7 @@
     "../api/units:time_delta",
     "../api/units:timestamp",
     "memory:fifo_buffer",
+    "synchronization:mutex",
     "third_party/sigslot",
   ]
   absl_deps = [
@@ -1122,6 +1131,7 @@
       ":rtc_base",
       ":rtc_base_tests_utils",
       "../test:test_support",
+      "synchronization:mutex",
       "third_party/sigslot",
     ]
   }
@@ -1230,6 +1240,7 @@
       "../test:test_main",
       "../test:test_support",
       "memory:unittests",
+      "synchronization:mutex",
       "task_utils:to_queued_task",
       "third_party/base64",
       "third_party/sigslot",
diff --git a/rtc_base/buffer_queue.cc b/rtc_base/buffer_queue.cc
index 445045c..adad9dd 100644
--- a/rtc_base/buffer_queue.cc
+++ b/rtc_base/buffer_queue.cc
@@ -21,7 +21,7 @@
     : capacity_(capacity), default_size_(default_size) {}
 
 BufferQueue::~BufferQueue() {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
 
   for (Buffer* buffer : queue_) {
     delete buffer;
@@ -32,12 +32,12 @@
 }
 
 size_t BufferQueue::size() const {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   return queue_.size();
 }
 
 void BufferQueue::Clear() {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   while (!queue_.empty()) {
     free_list_.push_back(queue_.front());
     queue_.pop_front();
@@ -45,7 +45,7 @@
 }
 
 bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   if (queue_.empty()) {
     return false;
   }
@@ -69,7 +69,7 @@
 bool BufferQueue::WriteBack(const void* buffer,
                             size_t bytes,
                             size_t* bytes_written) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   if (queue_.size() == capacity_) {
     return false;
   }
diff --git a/rtc_base/buffer_queue.h b/rtc_base/buffer_queue.h
index 5cb18d0..29d1a5b 100644
--- a/rtc_base/buffer_queue.h
+++ b/rtc_base/buffer_queue.h
@@ -18,7 +18,7 @@
 
 #include "rtc_base/buffer.h"
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace rtc {
@@ -52,9 +52,9 @@
  private:
   size_t capacity_;
   size_t default_size_;
-  CriticalSection crit_;
-  std::deque<Buffer*> queue_ RTC_GUARDED_BY(crit_);
-  std::vector<Buffer*> free_list_ RTC_GUARDED_BY(crit_);
+  mutable webrtc::Mutex mutex_;
+  std::deque<Buffer*> queue_ RTC_GUARDED_BY(mutex_);
+  std::vector<Buffer*> free_list_ RTC_GUARDED_BY(mutex_);
 
   RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
 };
diff --git a/rtc_base/event_tracer.cc b/rtc_base/event_tracer.cc
index d23af21..3af8183 100644
--- a/rtc_base/event_tracer.cc
+++ b/rtc_base/event_tracer.cc
@@ -19,11 +19,11 @@
 
 #include "rtc_base/atomic_ops.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/event.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/platform_thread_types.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/thread_checker.h"
 #include "rtc_base/time_utils.h"
@@ -120,7 +120,7 @@
         arg.value.as_string = str_copy;
       }
     }
-    rtc::CritScope lock(&crit_);
+    webrtc::MutexLock lock(&mutex_);
     trace_events_.push_back(
         {name, category_enabled, phase, args, timestamp, 1, thread_id});
   }
@@ -136,7 +136,7 @@
       bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs);
       std::vector<TraceEvent> events;
       {
-        rtc::CritScope lock(&crit_);
+        webrtc::MutexLock lock(&mutex_);
         trace_events_.swap(events);
       }
       std::string args_str;
@@ -196,7 +196,7 @@
     output_file_ = file;
     output_file_owned_ = owned;
     {
-      rtc::CritScope lock(&crit_);
+      webrtc::MutexLock lock(&mutex_);
       // Since the atomic fast-path for adding events to the queue can be
       // bypassed while the logging thread is shutting down there may be some
       // stale events in the queue, hence the vector needs to be cleared to not
@@ -317,8 +317,8 @@
     return output;
   }
 
-  rtc::CriticalSection crit_;
-  std::vector<TraceEvent> trace_events_ RTC_GUARDED_BY(crit_);
+  webrtc::Mutex mutex_;
+  std::vector<TraceEvent> trace_events_ RTC_GUARDED_BY(mutex_);
   rtc::PlatformThread logging_thread_;
   rtc::Event shutdown_event_;
   rtc::ThreadChecker thread_checker_;
diff --git a/rtc_base/event_tracer_unittest.cc b/rtc_base/event_tracer_unittest.cc
index 79cc9c0..f4d41e4 100644
--- a/rtc_base/event_tracer_unittest.cc
+++ b/rtc_base/event_tracer_unittest.cc
@@ -10,7 +10,7 @@
 
 #include "rtc_base/event_tracer.h"
 
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/trace_event.h"
 #include "test/gtest.h"
@@ -20,17 +20,17 @@
 class TestStatistics {
  public:
   void Reset() {
-    rtc::CritScope cs(&crit_);
+    webrtc::MutexLock lock(&mutex_);
     events_logged_ = 0;
   }
 
   void Increment() {
-    rtc::CritScope cs(&crit_);
+    webrtc::MutexLock lock(&mutex_);
     ++events_logged_;
   }
 
   int Count() const {
-    rtc::CritScope cs(&crit_);
+    webrtc::MutexLock lock(&mutex_);
     return events_logged_;
   }
 
@@ -41,8 +41,8 @@
   }
 
  private:
-  rtc::CriticalSection crit_;
-  int events_logged_ RTC_GUARDED_BY(crit_) = 0;
+  mutable webrtc::Mutex mutex_;
+  int events_logged_ RTC_GUARDED_BY(mutex_) = 0;
 };
 
 }  // namespace
diff --git a/rtc_base/fake_clock.cc b/rtc_base/fake_clock.cc
index e242e8e..652a5af 100644
--- a/rtc_base/fake_clock.cc
+++ b/rtc_base/fake_clock.cc
@@ -16,18 +16,18 @@
 namespace rtc {
 
 int64_t FakeClock::TimeNanos() const {
-  CritScope cs(&lock_);
+  webrtc::MutexLock lock(&lock_);
   return time_ns_;
 }
 
 void FakeClock::SetTime(webrtc::Timestamp new_time) {
-  CritScope cs(&lock_);
+  webrtc::MutexLock lock(&lock_);
   RTC_DCHECK(new_time.us() * 1000 >= time_ns_);
   time_ns_ = new_time.us() * 1000;
 }
 
 void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
-  CritScope cs(&lock_);
+  webrtc::MutexLock lock(&lock_);
   time_ns_ += delta.ns();
 }
 
diff --git a/rtc_base/fake_clock.h b/rtc_base/fake_clock.h
index 0ab9a93..edb507b 100644
--- a/rtc_base/fake_clock.h
+++ b/rtc_base/fake_clock.h
@@ -15,7 +15,7 @@
 
 #include "api/units/time_delta.h"
 #include "api/units/timestamp.h"
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 
@@ -43,7 +43,7 @@
   void AdvanceTime(webrtc::TimeDelta delta);
 
  private:
-  CriticalSection lock_;
+  mutable webrtc::Mutex lock_;
   int64_t time_ns_ RTC_GUARDED_BY(lock_) = 0;
 };
 
diff --git a/rtc_base/firewall_socket_server.cc b/rtc_base/firewall_socket_server.cc
index fc79176..8f44753 100644
--- a/rtc_base/firewall_socket_server.cc
+++ b/rtc_base/firewall_socket_server.cc
@@ -163,19 +163,19 @@
   r.p = p;
   r.src = src;
   r.dst = dst;
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   rules_.push_back(r);
 }
 
 void FirewallSocketServer::ClearRules() {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   rules_.clear();
 }
 
 bool FirewallSocketServer::Check(FirewallProtocol p,
                                  const SocketAddress& src,
                                  const SocketAddress& dst) {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   for (size_t i = 0; i < rules_.size(); ++i) {
     const Rule& r = rules_[i];
     if ((r.p != p) && (r.p != FP_ANY))
@@ -239,12 +239,12 @@
 }
 
 void FirewallManager::AddServer(FirewallSocketServer* server) {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   servers_.push_back(server);
 }
 
 void FirewallManager::RemoveServer(FirewallSocketServer* server) {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   servers_.erase(std::remove(servers_.begin(), servers_.end(), server),
                  servers_.end());
 }
@@ -253,7 +253,7 @@
                               FirewallProtocol p,
                               FirewallDirection d,
                               const SocketAddress& addr) {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   for (std::vector<FirewallSocketServer*>::const_iterator it = servers_.begin();
        it != servers_.end(); ++it) {
     (*it)->AddRule(allow, p, d, addr);
@@ -261,7 +261,7 @@
 }
 
 void FirewallManager::ClearRules() {
-  CritScope scope(&crit_);
+  webrtc::MutexLock scope(&mutex_);
   for (std::vector<FirewallSocketServer*>::const_iterator it = servers_.begin();
        it != servers_.end(); ++it) {
     (*it)->ClearRules();
diff --git a/rtc_base/firewall_socket_server.h b/rtc_base/firewall_socket_server.h
index d174033..23b91d6 100644
--- a/rtc_base/firewall_socket_server.h
+++ b/rtc_base/firewall_socket_server.h
@@ -14,11 +14,11 @@
 #include <vector>
 
 #include "rtc_base/async_socket.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/ip_address.h"
 #include "rtc_base/socket.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/socket_server.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace rtc {
 
@@ -90,7 +90,7 @@
  private:
   SocketServer* server_;
   FirewallManager* manager_;
-  CriticalSection crit_;
+  webrtc::Mutex mutex_;
   struct Rule {
     bool allow;
     FirewallProtocol p;
@@ -123,7 +123,7 @@
   void ClearRules();
 
  private:
-  CriticalSection crit_;
+  webrtc::Mutex mutex_;
   std::vector<FirewallSocketServer*> servers_;
 };
 
diff --git a/rtc_base/memory/BUILD.gn b/rtc_base/memory/BUILD.gn
index aa905c6..5c3dd0a 100644
--- a/rtc_base/memory/BUILD.gn
+++ b/rtc_base/memory/BUILD.gn
@@ -31,7 +31,10 @@
     "fifo_buffer.cc",
     "fifo_buffer.h",
   ]
-  deps = [ "..:rtc_base" ]
+  deps = [
+    "..:rtc_base",
+    "../synchronization:mutex",
+  ]
 }
 
 rtc_library("unittests") {
diff --git a/rtc_base/memory/fifo_buffer.cc b/rtc_base/memory/fifo_buffer.cc
index 44fb032..49e9267 100644
--- a/rtc_base/memory/fifo_buffer.cc
+++ b/rtc_base/memory/fifo_buffer.cc
@@ -39,13 +39,13 @@
 FifoBuffer::~FifoBuffer() {}
 
 bool FifoBuffer::GetBuffered(size_t* size) const {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   *size = data_length_;
   return true;
 }
 
 bool FifoBuffer::SetCapacity(size_t size) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   if (data_length_ > size) {
     return false;
   }
@@ -67,7 +67,7 @@
                                     size_t bytes,
                                     size_t offset,
                                     size_t* bytes_read) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   return ReadOffsetLocked(buffer, bytes, offset, bytes_read);
 }
 
@@ -75,12 +75,12 @@
                                      size_t bytes,
                                      size_t offset,
                                      size_t* bytes_written) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   return WriteOffsetLocked(buffer, bytes, offset, bytes_written);
 }
 
 StreamState FifoBuffer::GetState() const {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   return state_;
 }
 
@@ -88,7 +88,7 @@
                               size_t bytes,
                               size_t* bytes_read,
                               int* error) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   const bool was_writable = data_length_ < buffer_length_;
   size_t copy = 0;
   StreamResult result = ReadOffsetLocked(buffer, bytes, 0, &copy);
@@ -114,7 +114,7 @@
                                size_t bytes,
                                size_t* bytes_written,
                                int* error) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
 
   const bool was_readable = (data_length_ > 0);
   size_t copy = 0;
@@ -136,12 +136,12 @@
 }
 
 void FifoBuffer::Close() {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   state_ = SS_CLOSED;
 }
 
 const void* FifoBuffer::GetReadData(size_t* size) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   *size = (read_position_ + data_length_ <= buffer_length_)
               ? data_length_
               : buffer_length_ - read_position_;
@@ -149,7 +149,7 @@
 }
 
 void FifoBuffer::ConsumeReadData(size_t size) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   RTC_DCHECK(size <= data_length_);
   const bool was_writable = data_length_ < buffer_length_;
   read_position_ = (read_position_ + size) % buffer_length_;
@@ -160,7 +160,7 @@
 }
 
 void* FifoBuffer::GetWriteBuffer(size_t* size) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   if (state_ == SS_CLOSED) {
     return nullptr;
   }
@@ -180,7 +180,7 @@
 }
 
 void FifoBuffer::ConsumeWriteBuffer(size_t size) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   RTC_DCHECK(size <= buffer_length_ - data_length_);
   const bool was_readable = (data_length_ > 0);
   data_length_ += size;
@@ -190,7 +190,7 @@
 }
 
 bool FifoBuffer::GetWriteRemaining(size_t* size) const {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   *size = buffer_length_ - data_length_;
   return true;
 }
diff --git a/rtc_base/memory/fifo_buffer.h b/rtc_base/memory/fifo_buffer.h
index f859815..04c4cbf 100644
--- a/rtc_base/memory/fifo_buffer.h
+++ b/rtc_base/memory/fifo_buffer.h
@@ -14,6 +14,7 @@
 #include <memory>
 
 #include "rtc_base/stream.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace rtc {
 
@@ -103,7 +104,7 @@
                                 size_t bytes,
                                 size_t offset,
                                 size_t* bytes_read)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
 
   // Helper method that implements WriteOffset. Caller must acquire a lock
   // when calling this method.
@@ -111,22 +112,22 @@
                                  size_t bytes,
                                  size_t offset,
                                  size_t* bytes_written)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
 
   // keeps the opened/closed state of the stream
-  StreamState state_ RTC_GUARDED_BY(crit_);
+  StreamState state_ RTC_GUARDED_BY(mutex_);
   // the allocated buffer
-  std::unique_ptr<char[]> buffer_ RTC_GUARDED_BY(crit_);
+  std::unique_ptr<char[]> buffer_ RTC_GUARDED_BY(mutex_);
   // size of the allocated buffer
-  size_t buffer_length_ RTC_GUARDED_BY(crit_);
+  size_t buffer_length_ RTC_GUARDED_BY(mutex_);
   // amount of readable data in the buffer
-  size_t data_length_ RTC_GUARDED_BY(crit_);
+  size_t data_length_ RTC_GUARDED_BY(mutex_);
   // offset to the readable data
-  size_t read_position_ RTC_GUARDED_BY(crit_);
+  size_t read_position_ RTC_GUARDED_BY(mutex_);
   // stream callbacks are dispatched on this thread
   Thread* owner_;
   // object lock
-  CriticalSection crit_;
+  mutable webrtc::Mutex mutex_;
   RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer);
 };
 
diff --git a/rtc_base/nat_server.cc b/rtc_base/nat_server.cc
index 0c458df..725a57b 100644
--- a/rtc_base/nat_server.cc
+++ b/rtc_base/nat_server.cc
@@ -243,12 +243,12 @@
 }
 
 void NATServer::TransEntry::AllowlistInsert(const SocketAddress& addr) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   allowlist->insert(addr);
 }
 
 bool NATServer::TransEntry::AllowlistContains(const SocketAddress& ext_addr) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   return allowlist->find(ext_addr) == allowlist->end();
 }
 
diff --git a/rtc_base/nat_server.h b/rtc_base/nat_server.h
index 47a1a33..5078fbb 100644
--- a/rtc_base/nat_server.h
+++ b/rtc_base/nat_server.h
@@ -20,6 +20,7 @@
 #include "rtc_base/proxy_server.h"
 #include "rtc_base/socket_address_pair.h"
 #include "rtc_base/socket_factory.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread.h"
 
 namespace rtc {
@@ -102,7 +103,7 @@
     SocketAddressPair route;
     AsyncUDPSocket* socket;
     AddressSet* allowlist;
-    CriticalSection crit_;
+    webrtc::Mutex mutex_;
   };
 
   typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
diff --git a/rtc_base/one_time_event.h b/rtc_base/one_time_event.h
index c5ccbf6..d33ddbd 100644
--- a/rtc_base/one_time_event.h
+++ b/rtc_base/one_time_event.h
@@ -11,7 +11,7 @@
 #ifndef RTC_BASE_ONE_TIME_EVENT_H_
 #define RTC_BASE_ONE_TIME_EVENT_H_
 
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace webrtc {
 // Provides a simple way to perform an operation (such as logging) one
@@ -26,7 +26,7 @@
  public:
   OneTimeEvent() {}
   bool operator()() {
-    rtc::CritScope cs(&critsect_);
+    MutexLock lock(&mutex_);
     if (happened_) {
       return false;
     }
@@ -36,7 +36,7 @@
 
  private:
   bool happened_ = false;
-  rtc::CriticalSection critsect_;
+  Mutex mutex_;
 };
 
 // A non-thread-safe, ligher-weight version of the OneTimeEvent class.
diff --git a/rtc_base/rate_limiter.cc b/rtc_base/rate_limiter.cc
index 7394c3e..0f3f343 100644
--- a/rtc_base/rate_limiter.cc
+++ b/rtc_base/rate_limiter.cc
@@ -31,7 +31,7 @@
 // calling SetMaxRate() and a timed maintenance thread periodically updating
 // the RTT.
 bool RateLimiter::TryUseRate(size_t packet_size_bytes) {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   int64_t now_ms = clock_->TimeInMilliseconds();
   absl::optional<uint32_t> current_rate = current_rate_.Rate(now_ms);
   if (current_rate) {
@@ -53,14 +53,14 @@
 }
 
 void RateLimiter::SetMaxRate(uint32_t max_rate_bps) {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   max_rate_bps_ = max_rate_bps;
 }
 
 // Set the window size over which to measure the current bitrate.
 // For retransmissions, this is typically the RTT.
 bool RateLimiter::SetWindowSize(int64_t window_size_ms) {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   window_size_ms_ = window_size_ms;
   return current_rate_.SetWindowSize(window_size_ms,
                                      clock_->TimeInMilliseconds());
diff --git a/rtc_base/rate_limiter.h b/rtc_base/rate_limiter.h
index 1c956d7..051ccf6 100644
--- a/rtc_base/rate_limiter.h
+++ b/rtc_base/rate_limiter.h
@@ -15,8 +15,8 @@
 #include <stdint.h>
 
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/rate_statistics.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
@@ -45,7 +45,7 @@
 
  private:
   Clock* const clock_;
-  rtc::CriticalSection lock_;
+  Mutex lock_;
   RateStatistics current_rate_ RTC_GUARDED_BY(lock_);
   int64_t window_size_ms_ RTC_GUARDED_BY(lock_);
   uint32_t max_rate_bps_ RTC_GUARDED_BY(lock_);
diff --git a/rtc_base/synchronization/BUILD.gn b/rtc_base/synchronization/BUILD.gn
index f6e6d0b..a79a048 100644
--- a/rtc_base/synchronization/BUILD.gn
+++ b/rtc_base/synchronization/BUILD.gn
@@ -68,6 +68,7 @@
     "sequence_checker.h",
   ]
   deps = [
+    ":mutex",
     "..:checks",
     "..:criticalsection",
     "..:macromagic",
diff --git a/rtc_base/synchronization/sequence_checker.cc b/rtc_base/synchronization/sequence_checker.cc
index ff433db..1de26cf 100644
--- a/rtc_base/synchronization/sequence_checker.cc
+++ b/rtc_base/synchronization/sequence_checker.cc
@@ -48,7 +48,7 @@
   const TaskQueueBase* const current_queue = TaskQueueBase::Current();
   const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
   const void* const current_system_queue = GetSystemQueueRef();
-  rtc::CritScope scoped_lock(&lock_);
+  MutexLock scoped_lock(&lock_);
   if (!attached_) {  // Previously detached.
     attached_ = true;
     valid_thread_ = current_thread;
@@ -66,7 +66,7 @@
 }
 
 void SequenceCheckerImpl::Detach() {
-  rtc::CritScope scoped_lock(&lock_);
+  MutexLock scoped_lock(&lock_);
   attached_ = false;
   // We don't need to touch the other members here, they will be
   // reset on the next call to IsCurrent().
@@ -77,7 +77,7 @@
   const TaskQueueBase* const current_queue = TaskQueueBase::Current();
   const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
   const void* const current_system_queue = GetSystemQueueRef();
-  rtc::CritScope scoped_lock(&lock_);
+  MutexLock scoped_lock(&lock_);
   if (!attached_)
     return "Checker currently not attached.";
 
diff --git a/rtc_base/synchronization/sequence_checker.h b/rtc_base/synchronization/sequence_checker.h
index fd0a699..ecf8490 100644
--- a/rtc_base/synchronization/sequence_checker.h
+++ b/rtc_base/synchronization/sequence_checker.h
@@ -13,8 +13,8 @@
 #include <type_traits>
 
 #include "api/task_queue/task_queue_base.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/platform_thread_types.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/system/rtc_export.h"
 #include "rtc_base/thread_annotations.h"
 
@@ -42,7 +42,7 @@
   std::string ExpectationToString() const;
 
  private:
-  rtc::CriticalSection lock_;
+  mutable Mutex lock_;
   // These are mutable so that IsCurrent can set them.
   mutable bool attached_ RTC_GUARDED_BY(lock_);
   mutable rtc::PlatformThreadRef valid_thread_ RTC_GUARDED_BY(lock_);
diff --git a/rtc_base/task_queue_libevent.cc b/rtc_base/task_queue_libevent.cc
index 349a5f2..38660cd 100644
--- a/rtc_base/task_queue_libevent.cc
+++ b/rtc_base/task_queue_libevent.cc
@@ -29,11 +29,11 @@
 #include "api/task_queue/task_queue_base.h"
 #include "base/third_party/libevent/event.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/platform_thread_types.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 
@@ -130,7 +130,7 @@
   event_base* event_base_;
   event wakeup_event_;
   rtc::PlatformThread thread_;
-  rtc::CriticalSection pending_lock_;
+  Mutex pending_lock_;
   absl::InlinedVector<std::unique_ptr<QueuedTask>, 4> pending_
       RTC_GUARDED_BY(pending_lock_);
   // Holds a list of events pending timers for cleanup when the loop exits.
@@ -216,7 +216,7 @@
 
 void TaskQueueLibevent::PostTask(std::unique_ptr<QueuedTask> task) {
   {
-    rtc::CritScope lock(&pending_lock_);
+    MutexLock lock(&pending_lock_);
     bool had_pending_tasks = !pending_.empty();
     pending_.push_back(std::move(task));
 
@@ -282,7 +282,7 @@
     case kRunTasks: {
       absl::InlinedVector<std::unique_ptr<QueuedTask>, 4> tasks;
       {
-        rtc::CritScope lock(&me->pending_lock_);
+        MutexLock lock(&me->pending_lock_);
         tasks.swap(me->pending_);
       }
       RTC_DCHECK(!tasks.empty());
diff --git a/rtc_base/task_queue_stdlib.cc b/rtc_base/task_queue_stdlib.cc
index 7052f7c..5de6345 100644
--- a/rtc_base/task_queue_stdlib.cc
+++ b/rtc_base/task_queue_stdlib.cc
@@ -22,10 +22,10 @@
 #include "api/task_queue/queued_task.h"
 #include "api/task_queue/task_queue_base.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/event.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_thread.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 
@@ -97,7 +97,7 @@
   // tasks (including delayed tasks).
   rtc::PlatformThread thread_;
 
-  rtc::CriticalSection pending_lock_;
+  Mutex pending_lock_;
 
   // Indicates if the worker thread needs to shutdown now.
   bool thread_should_quit_ RTC_GUARDED_BY(pending_lock_){false};
@@ -135,7 +135,7 @@
   RTC_DCHECK(!IsCurrent());
 
   {
-    rtc::CritScope lock(&pending_lock_);
+    MutexLock lock(&pending_lock_);
     thread_should_quit_ = true;
   }
 
@@ -148,7 +148,7 @@
 
 void TaskQueueStdlib::PostTask(std::unique_ptr<QueuedTask> task) {
   {
-    rtc::CritScope lock(&pending_lock_);
+    MutexLock lock(&pending_lock_);
     OrderId order = thread_posting_order_++;
 
     pending_queue_.push(std::pair<OrderId, std::unique_ptr<QueuedTask>>(
@@ -166,7 +166,7 @@
   delay.next_fire_at_ms_ = fire_at;
 
   {
-    rtc::CritScope lock(&pending_lock_);
+    MutexLock lock(&pending_lock_);
     delay.order_ = ++thread_posting_order_;
     delayed_queue_[delay] = std::move(task);
   }
@@ -179,7 +179,7 @@
 
   auto tick = rtc::TimeMillis();
 
-  rtc::CritScope lock(&pending_lock_);
+  MutexLock lock(&pending_lock_);
 
   if (thread_should_quit_) {
     result.final_task_ = true;
diff --git a/rtc_base/task_queue_win.cc b/rtc_base/task_queue_win.cc
index 8c11b87..5eb3776 100644
--- a/rtc_base/task_queue_win.cc
+++ b/rtc_base/task_queue_win.cc
@@ -33,12 +33,12 @@
 #include "api/task_queue/task_queue_base.h"
 #include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/event.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/time_utils.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace webrtc {
 namespace {
@@ -205,7 +205,7 @@
       timer_tasks_;
   UINT_PTR timer_id_ = 0;
   WorkerThread thread_;
-  rtc::CriticalSection pending_lock_;
+  Mutex pending_lock_;
   std::queue<std::unique_ptr<QueuedTask>> pending_
       RTC_GUARDED_BY(pending_lock_);
   HANDLE in_queue_;
@@ -235,7 +235,7 @@
 }
 
 void TaskQueueWin::PostTask(std::unique_ptr<QueuedTask> task) {
-  rtc::CritScope lock(&pending_lock_);
+  MutexLock lock(&pending_lock_);
   pending_.push(std::move(task));
   ::SetEvent(in_queue_);
 }
@@ -262,7 +262,7 @@
   while (true) {
     std::unique_ptr<QueuedTask> task;
     {
-      rtc::CritScope lock(&pending_lock_);
+      MutexLock lock(&pending_lock_);
       if (pending_.empty())
         break;
       task = std::move(pending_.front());
diff --git a/rtc_base/test_client.cc b/rtc_base/test_client.cc
index e5aa9d7..f23ac2a 100644
--- a/rtc_base/test_client.cc
+++ b/rtc_base/test_client.cc
@@ -75,7 +75,7 @@
   int64_t end = TimeAfter(timeout_ms);
   while (TimeUntil(end) > 0) {
     {
-      CritScope cs(&crit_);
+      webrtc::MutexLock lock(&mutex_);
       if (packets_.size() != 0) {
         break;
       }
@@ -85,7 +85,7 @@
 
   // Return the first packet placed in the queue.
   std::unique_ptr<Packet> packet;
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   if (packets_.size() > 0) {
     packet = std::move(packets_.front());
     packets_.erase(packets_.begin());
@@ -149,7 +149,7 @@
                           size_t size,
                           const SocketAddress& remote_addr,
                           const int64_t& packet_time_us) {
-  CritScope cs(&crit_);
+  webrtc::MutexLock lock(&mutex_);
   packets_.push_back(
       std::make_unique<Packet>(remote_addr, buf, size, packet_time_us));
 }
diff --git a/rtc_base/test_client.h b/rtc_base/test_client.h
index b45cf00..6989fe1 100644
--- a/rtc_base/test_client.h
+++ b/rtc_base/test_client.h
@@ -16,8 +16,8 @@
 
 #include "rtc_base/async_udp_socket.h"
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/fake_clock.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace rtc {
 
@@ -105,7 +105,7 @@
   void AdvanceTime(int ms);
 
   ThreadProcessingFakeClock* fake_clock_ = nullptr;
-  CriticalSection crit_;
+  webrtc::Mutex mutex_;
   std::unique_ptr<AsyncPacketSocket> socket_;
   std::vector<std::unique_ptr<Packet>> packets_;
   int ready_to_send_count_ = 0;
diff --git a/rtc_base/virtual_socket_server.cc b/rtc_base/virtual_socket_server.cc
index d42873e..454aa05 100644
--- a/rtc_base/virtual_socket_server.cc
+++ b/rtc_base/virtual_socket_server.cc
@@ -19,6 +19,7 @@
 
 #include "absl/algorithm/container.h"
 #include "rtc_base/checks.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/fake_clock.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/physical_socket_server.h"
diff --git a/rtc_base/virtual_socket_server.h b/rtc_base/virtual_socket_server.h
index f45fabf..dc7fe10 100644
--- a/rtc_base/virtual_socket_server.h
+++ b/rtc_base/virtual_socket_server.h
@@ -17,6 +17,7 @@
 
 #include "rtc_base/checks.h"
 #include "rtc_base/constructor_magic.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/event.h"
 #include "rtc_base/fake_clock.h"
 #include "rtc_base/message_handler.h"
diff --git a/rtc_base/win32_socket_server.cc b/rtc_base/win32_socket_server.cc
index 8a5b93a..cfe21a3 100644
--- a/rtc_base/win32_socket_server.cc
+++ b/rtc_base/win32_socket_server.cc
@@ -733,7 +733,7 @@
     MSG msg;
     b = GetMessage(&msg, nullptr, s_wm_wakeup_id, s_wm_wakeup_id);
     {
-      CritScope scope(&cs_);
+      webrtc::MutexLock lock(&mutex_);
       posted_ = false;
     }
   } else {
@@ -747,7 +747,7 @@
   if (wnd_.handle()) {
     // Set the "message pending" flag, if not already set.
     {
-      CritScope scope(&cs_);
+      webrtc::MutexLock lock(&mutex_);
       if (posted_)
         return;
       posted_ = true;
@@ -760,7 +760,7 @@
 void Win32SocketServer::Pump() {
   // Clear the "message pending" flag.
   {
-    CritScope scope(&cs_);
+    webrtc::MutexLock lock(&mutex_);
     posted_ = false;
   }
 
diff --git a/rtc_base/win32_socket_server.h b/rtc_base/win32_socket_server.h
index 92fd68c..317acce 100644
--- a/rtc_base/win32_socket_server.h
+++ b/rtc_base/win32_socket_server.h
@@ -13,10 +13,10 @@
 
 #if defined(WEBRTC_WIN)
 #include "rtc_base/async_socket.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/socket.h"
 #include "rtc_base/socket_factory.h"
 #include "rtc_base/socket_server.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/win32_window.h"
 
@@ -123,7 +123,7 @@
   static const wchar_t kWindowName[];
   Thread* message_queue_;
   MessageWindow wnd_;
-  CriticalSection cs_;
+  webrtc::Mutex mutex_;
   bool posted_;
   HWND hdlg_;
 };
diff --git a/sdk/android/native_api/jni/java_types.cc b/sdk/android/native_api/jni/java_types.cc
index a97c81f..af02c10 100644
--- a/sdk/android/native_api/jni/java_types.cc
+++ b/sdk/android/native_api/jni/java_types.cc
@@ -10,6 +10,7 @@
 
 #include "sdk/android/native_api/jni/java_types.h"
 
+#include <memory>
 #include <string>
 #include <utility>
 
@@ -51,14 +52,15 @@
 Iterable::Iterator::Iterator(Iterator&& other)
     : jni_(std::move(other.jni_)),
       iterator_(std::move(other.iterator_)),
-      value_(std::move(other.value_)),
-      thread_checker_(std::move(other.thread_checker_)) {}
+      value_(std::move(other.value_)) {
+  RTC_DCHECK_RUN_ON(&thread_checker_);
+}
 
 Iterable::Iterator::~Iterator() = default;
 
 // Advances the iterator one step.
 Iterable::Iterator& Iterable::Iterator::operator++() {
-  RTC_CHECK(thread_checker_.IsCurrent());
+  RTC_DCHECK_RUN_ON(&thread_checker_);
   if (AtEnd()) {
     // Can't move past the end.
     return *this;
@@ -93,7 +95,7 @@
 }
 
 bool Iterable::Iterator::AtEnd() const {
-  RTC_CHECK(thread_checker_.IsCurrent());
+  RTC_DCHECK_RUN_ON(&thread_checker_);
   return jni_ == nullptr || IsNull(jni_, iterator_);
 }
 
diff --git a/sdk/android/native_api/jni/java_types.h b/sdk/android/native_api/jni/java_types.h
index 955911c..26fdd5a 100644
--- a/sdk/android/native_api/jni/java_types.h
+++ b/sdk/android/native_api/jni/java_types.h
@@ -18,7 +18,9 @@
 #define SDK_ANDROID_NATIVE_API_JNI_JAVA_TYPES_H_
 
 #include <jni.h>
+
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 
diff --git a/sdk/android/src/jni/video_decoder_wrapper.h b/sdk/android/src/jni/video_decoder_wrapper.h
index a7f6868..d31735b 100644
--- a/sdk/android/src/jni/video_decoder_wrapper.h
+++ b/sdk/android/src/jni/video_decoder_wrapper.h
@@ -12,11 +12,13 @@
 #define SDK_ANDROID_SRC_JNI_VIDEO_DECODER_WRAPPER_H_
 
 #include <jni.h>
+
 #include <atomic>
 #include <deque>
 
 #include "api/video_codecs/video_decoder.h"
 #include "common_video/h264/h264_bitstream_parser.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/race_checker.h"
 #include "rtc_base/thread_checker.h"
 #include "sdk/android/src/jni/jni_helpers.h"
diff --git a/video/video_stream_decoder_impl.h b/video/video_stream_decoder_impl.h
index f3f09e4..4163afe 100644
--- a/video/video_stream_decoder_impl.h
+++ b/video/video_stream_decoder_impl.h
@@ -19,6 +19,7 @@
 #include "api/video/video_stream_decoder.h"
 #include "modules/video_coding/frame_buffer2.h"
 #include "modules/video_coding/timing.h"
+#include "rtc_base/critical_section.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/task_queue.h"
 #include "rtc_base/thread_checker.h"