diff --git a/pc/srtp_session.h b/pc/srtp_session.h index 0a26c02c9f..e2f1b8d730 100644 --- a/pc/srtp_session.h +++ b/pc/srtp_session.h @@ -14,6 +14,7 @@ #include #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 d0f6ea6c84..e5a03d43ce 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 fb341e2502..2f0fba9eb2 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -55,6 +55,7 @@ rtc_library("rtc_base_approved") { ":type_traits", "../api:array_view", "../api:scoped_refptr", + "synchronization:mutex", "system:arch", "system:rtc_export", "system:unused", @@ -325,6 +326,7 @@ rtc_library("rate_limiter") { deps = [ ":rtc_base_approved", "../system_wrappers", + "synchronization:mutex", ] absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] } @@ -479,6 +481,7 @@ if (rtc_enable_libevent) { ":safe_conversions", ":timeutils", "../api/task_queue", + "synchronization:mutex", ] absl_deps = [ "//third_party/abseil-cpp/absl/container:inlined_vector", @@ -501,6 +504,7 @@ if (is_mac || is_ios) { ":checks", ":logging", "../api/task_queue", + "synchronization:mutex", "system:gcd_helpers", ] absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] @@ -524,6 +528,7 @@ if (is_win) { ":safe_conversions", ":timeutils", "../api/task_queue", + "synchronization:mutex", ] absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] } @@ -544,6 +549,7 @@ rtc_library("rtc_task_queue_stdlib") { ":safe_conversions", ":timeutils", "../api/task_queue", + "synchronization:mutex", ] absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] } @@ -782,6 +788,7 @@ rtc_library("rtc_base") { "../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_library("testclient") { ":rtc_base", ":rtc_base_tests_utils", ":timeutils", + "synchronization:mutex", ] } @@ -1085,6 +1093,7 @@ rtc_library("rtc_base_tests_utils") { "../api/units:time_delta", "../api/units:timestamp", "memory:fifo_buffer", + "synchronization:mutex", "third_party/sigslot", ] absl_deps = [ @@ -1122,6 +1131,7 @@ if (rtc_include_tests) { ":rtc_base", ":rtc_base_tests_utils", "../test:test_support", + "synchronization:mutex", "third_party/sigslot", ] } @@ -1230,6 +1240,7 @@ if (rtc_include_tests) { "../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 445045ceea..adad9dda17 100644 --- a/rtc_base/buffer_queue.cc +++ b/rtc_base/buffer_queue.cc @@ -21,7 +21,7 @@ BufferQueue::BufferQueue(size_t capacity, size_t default_size) : 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 @@ BufferQueue::~BufferQueue() { } 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 @@ void BufferQueue::Clear() { } 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::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) { 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 5cb18d0220..29d1a5b136 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 @@ class BufferQueue { private: size_t capacity_; size_t default_size_; - CriticalSection crit_; - std::deque queue_ RTC_GUARDED_BY(crit_); - std::vector free_list_ RTC_GUARDED_BY(crit_); + mutable webrtc::Mutex mutex_; + std::deque queue_ RTC_GUARDED_BY(mutex_); + std::vector 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 d23af21421..3af8183b1f 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 @@ class EventLogger final { 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 @@ class EventLogger final { bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs); std::vector events; { - rtc::CritScope lock(&crit_); + webrtc::MutexLock lock(&mutex_); trace_events_.swap(events); } std::string args_str; @@ -196,7 +196,7 @@ class EventLogger final { 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 @@ class EventLogger final { return output; } - rtc::CriticalSection crit_; - std::vector trace_events_ RTC_GUARDED_BY(crit_); + webrtc::Mutex mutex_; + std::vector 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 79cc9c0788..f4d41e4e7c 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 @@ namespace { 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 @@ class TestStatistics { } 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 e242e8e659..652a5afa3a 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 0ab9a937a8..edb507becb 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 @@ class FakeClock : public ClockInterface { 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 fc7917613c..8f44753760 100644 --- a/rtc_base/firewall_socket_server.cc +++ b/rtc_base/firewall_socket_server.cc @@ -163,19 +163,19 @@ void FirewallSocketServer::AddRule(bool allow, 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 @@ FirewallManager::~FirewallManager() { } 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 @@ void FirewallManager::AddRule(bool allow, FirewallProtocol p, FirewallDirection d, const SocketAddress& addr) { - CritScope scope(&crit_); + webrtc::MutexLock scope(&mutex_); for (std::vector::const_iterator it = servers_.begin(); it != servers_.end(); ++it) { (*it)->AddRule(allow, p, d, addr); @@ -261,7 +261,7 @@ void FirewallManager::AddRule(bool allow, } void FirewallManager::ClearRules() { - CritScope scope(&crit_); + webrtc::MutexLock scope(&mutex_); for (std::vector::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 d174033e01..23b91d6ad3 100644 --- a/rtc_base/firewall_socket_server.h +++ b/rtc_base/firewall_socket_server.h @@ -14,11 +14,11 @@ #include #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 @@ class FirewallSocketServer : public SocketServer { private: SocketServer* server_; FirewallManager* manager_; - CriticalSection crit_; + webrtc::Mutex mutex_; struct Rule { bool allow; FirewallProtocol p; @@ -123,7 +123,7 @@ class FirewallManager { void ClearRules(); private: - CriticalSection crit_; + webrtc::Mutex mutex_; std::vector servers_; }; diff --git a/rtc_base/memory/BUILD.gn b/rtc_base/memory/BUILD.gn index aa905c6f70..5c3dd0a5d1 100644 --- a/rtc_base/memory/BUILD.gn +++ b/rtc_base/memory/BUILD.gn @@ -31,7 +31,10 @@ rtc_library("fifo_buffer") { "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 44fb032e57..49e926719f 100644 --- a/rtc_base/memory/fifo_buffer.cc +++ b/rtc_base/memory/fifo_buffer.cc @@ -39,13 +39,13 @@ FifoBuffer::FifoBuffer(size_t size, Thread* owner) 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 @@ StreamResult FifoBuffer::ReadOffset(void* buffer, 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 @@ StreamResult FifoBuffer::WriteOffset(const void* buffer, 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 @@ StreamResult FifoBuffer::Read(void* buffer, 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, ©); @@ -114,7 +114,7 @@ StreamResult FifoBuffer::Write(const void* buffer, 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 @@ StreamResult FifoBuffer::Write(const void* buffer, } 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 @@ const void* FifoBuffer::GetReadData(size_t* size) { } 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::ConsumeReadData(size_t size) { } 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::GetWriteBuffer(size_t* size) { } 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 @@ void FifoBuffer::ConsumeWriteBuffer(size_t size) { } 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 f859815c70..04c4cbf33b 100644 --- a/rtc_base/memory/fifo_buffer.h +++ b/rtc_base/memory/fifo_buffer.h @@ -14,6 +14,7 @@ #include #include "rtc_base/stream.h" +#include "rtc_base/synchronization/mutex.h" namespace rtc { @@ -103,7 +104,7 @@ class FifoBuffer final : public StreamInterface { 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 @@ class FifoBuffer final : public StreamInterface { 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 buffer_ RTC_GUARDED_BY(crit_); + std::unique_ptr 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 0c458dfe2c..725a57be9f 100644 --- a/rtc_base/nat_server.cc +++ b/rtc_base/nat_server.cc @@ -243,12 +243,12 @@ NATServer::TransEntry::~TransEntry() { } 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 47a1a33ba1..5078fbb2c1 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 @@ class NATServer : public sigslot::has_slots<> { SocketAddressPair route; AsyncUDPSocket* socket; AddressSet* allowlist; - CriticalSection crit_; + webrtc::Mutex mutex_; }; typedef std::map InternalMap; diff --git a/rtc_base/one_time_event.h b/rtc_base/one_time_event.h index c5ccbf6933..d33ddbd587 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 @@ class OneTimeEvent { public: OneTimeEvent() {} bool operator()() { - rtc::CritScope cs(&critsect_); + MutexLock lock(&mutex_); if (happened_) { return false; } @@ -36,7 +36,7 @@ class OneTimeEvent { 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 7394c3eb89..0f3f343aed 100644 --- a/rtc_base/rate_limiter.cc +++ b/rtc_base/rate_limiter.cc @@ -31,7 +31,7 @@ RateLimiter::~RateLimiter() {} // 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 current_rate = current_rate_.Rate(now_ms); if (current_rate) { @@ -53,14 +53,14 @@ bool RateLimiter::TryUseRate(size_t packet_size_bytes) { } 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 1c956d788b..051ccf6aa6 100644 --- a/rtc_base/rate_limiter.h +++ b/rtc_base/rate_limiter.h @@ -15,8 +15,8 @@ #include #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 @@ class RateLimiter { 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 f6e6d0bfaa..a79a0486af 100644 --- a/rtc_base/synchronization/BUILD.gn +++ b/rtc_base/synchronization/BUILD.gn @@ -68,6 +68,7 @@ rtc_library("sequence_checker") { "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 ff433db137..1de26cf0fe 100644 --- a/rtc_base/synchronization/sequence_checker.cc +++ b/rtc_base/synchronization/sequence_checker.cc @@ -48,7 +48,7 @@ bool SequenceCheckerImpl::IsCurrent() const { 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 @@ bool SequenceCheckerImpl::IsCurrent() const { } 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 @@ std::string SequenceCheckerImpl::ExpectationToString() const { 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 fd0a69983a..ecf8490cec 100644 --- a/rtc_base/synchronization/sequence_checker.h +++ b/rtc_base/synchronization/sequence_checker.h @@ -13,8 +13,8 @@ #include #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 @@ class RTC_EXPORT SequenceCheckerImpl { 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 349a5f21fc..38660cd5a2 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 @@ class TaskQueueLibevent final : public TaskQueueBase { event_base* event_base_; event wakeup_event_; rtc::PlatformThread thread_; - rtc::CriticalSection pending_lock_; + Mutex pending_lock_; absl::InlinedVector, 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::Delete() { void TaskQueueLibevent::PostTask(std::unique_ptr 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 @@ void TaskQueueLibevent::OnWakeup(int socket, case kRunTasks: { absl::InlinedVector, 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 7052f7c6db..5de634512e 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 @@ class TaskQueueStdlib final : public TaskQueueBase { // 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 @@ void TaskQueueStdlib::Delete() { RTC_DCHECK(!IsCurrent()); { - rtc::CritScope lock(&pending_lock_); + MutexLock lock(&pending_lock_); thread_should_quit_ = true; } @@ -148,7 +148,7 @@ void TaskQueueStdlib::Delete() { void TaskQueueStdlib::PostTask(std::unique_ptr task) { { - rtc::CritScope lock(&pending_lock_); + MutexLock lock(&pending_lock_); OrderId order = thread_posting_order_++; pending_queue_.push(std::pair>( @@ -166,7 +166,7 @@ void TaskQueueStdlib::PostDelayedTask(std::unique_ptr task, 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 @@ TaskQueueStdlib::NextTask TaskQueueStdlib::GetNextTask() { 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 8c11b8764a..5eb3776cea 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 @@ class TaskQueueWin : public TaskQueueBase { timer_tasks_; UINT_PTR timer_id_ = 0; WorkerThread thread_; - rtc::CriticalSection pending_lock_; + Mutex pending_lock_; std::queue> pending_ RTC_GUARDED_BY(pending_lock_); HANDLE in_queue_; @@ -235,7 +235,7 @@ void TaskQueueWin::Delete() { } void TaskQueueWin::PostTask(std::unique_ptr task) { - rtc::CritScope lock(&pending_lock_); + MutexLock lock(&pending_lock_); pending_.push(std::move(task)); ::SetEvent(in_queue_); } @@ -262,7 +262,7 @@ void TaskQueueWin::RunPendingTasks() { while (true) { std::unique_ptr 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 e5aa9d7987..f23ac2aec0 100644 --- a/rtc_base/test_client.cc +++ b/rtc_base/test_client.cc @@ -75,7 +75,7 @@ std::unique_ptr TestClient::NextPacket(int timeout_ms) { 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 @@ std::unique_ptr TestClient::NextPacket(int timeout_ms) { // Return the first packet placed in the queue. std::unique_ptr 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 @@ void TestClient::OnPacket(AsyncPacketSocket* socket, 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(remote_addr, buf, size, packet_time_us)); } diff --git a/rtc_base/test_client.h b/rtc_base/test_client.h index b45cf005bb..6989fe1d57 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 @@ class TestClient : public sigslot::has_slots<> { void AdvanceTime(int ms); ThreadProcessingFakeClock* fake_clock_ = nullptr; - CriticalSection crit_; + webrtc::Mutex mutex_; std::unique_ptr socket_; std::vector> packets_; int ready_to_send_count_ = 0; diff --git a/rtc_base/virtual_socket_server.cc b/rtc_base/virtual_socket_server.cc index d42873e18b..454aa05315 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 f45fabf0af..dc7fe106a1 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 8a5b93a608..cfe21a3630 100644 --- a/rtc_base/win32_socket_server.cc +++ b/rtc_base/win32_socket_server.cc @@ -733,7 +733,7 @@ bool Win32SocketServer::Wait(int cms, bool process_io) { 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 @@ void Win32SocketServer::WakeUp() { 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::WakeUp() { 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 92fd68cd83..317acce0d2 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 @@ class Win32SocketServer : public SocketServer { 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 a97c81f1f2..af02c10f4c 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 #include #include @@ -51,14 +52,15 @@ Iterable::Iterator::Iterator(JNIEnv* jni, const JavaRef& iterable) 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 @@ ScopedJavaLocalRef& Iterable::Iterator::operator*() { } 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 955911c186..26fdd5a0b8 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 + #include +#include #include #include diff --git a/sdk/android/src/jni/video_decoder_wrapper.h b/sdk/android/src/jni/video_decoder_wrapper.h index a7f686872c..d31735b24b 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 + #include #include #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 f3f09e4a79..4163afe243 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"