diff --git a/pc/sdp_offer_answer.cc b/pc/sdp_offer_answer.cc index 53770eaa50..385d052366 100644 --- a/pc/sdp_offer_answer.cc +++ b/pc/sdp_offer_answer.cc @@ -4725,7 +4725,6 @@ void SdpOfferAnswerHandler::DestroyTransceiverChannel( // worker thread. Can DestroyTransceiverChannel be purely posted to the // worker? DestroyChannelInterface(channel); - RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(3); } } diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc index 039f82ad92..b0b0e52a07 100644 --- a/rtc_base/thread.cc +++ b/rtc_base/thread.cc @@ -362,7 +362,9 @@ Thread::ScopedCountBlockingCalls::ScopedCountBlockingCalls( result_callback_(std::move(callback)) {} Thread::ScopedCountBlockingCalls::~ScopedCountBlockingCalls() { - result_callback_(GetBlockingCallCount(), GetCouldBeBlockingCallCount()); + if (GetTotalBlockedCallCount() >= min_blocking_calls_for_callback_) { + result_callback_(GetBlockingCallCount(), GetCouldBeBlockingCallCount()); + } } uint32_t Thread::ScopedCountBlockingCalls::GetBlockingCallCount() const { diff --git a/rtc_base/thread.h b/rtc_base/thread.h index 6d3c39b8ac..e031677f1b 100644 --- a/rtc_base/thread.h +++ b/rtc_base/thread.h @@ -61,8 +61,11 @@ // number of blocking thread calls. // Note: Use of this macro, requires RTC_LOG_THREAD_BLOCK_COUNT() to be called // first. -#define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) \ - RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x) +#define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) \ + do { \ + blocked_call_count_printer.set_minimum_call_count_for_callback(x + 1); \ + RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x); \ + } while (0) #else #define RTC_LOG_THREAD_BLOCK_COUNT() #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) @@ -251,10 +254,19 @@ class RTC_LOCKABLE RTC_EXPORT Thread : public webrtc::TaskQueueBase { uint32_t GetCouldBeBlockingCallCount() const; uint32_t GetTotalBlockedCallCount() const; + void set_minimum_call_count_for_callback(uint32_t minimum) { + min_blocking_calls_for_callback_ = minimum; + } + private: Thread* const thread_; const uint32_t base_blocking_call_count_; const uint32_t base_could_be_blocking_call_count_; + // The minimum number of blocking calls required in order to issue the + // result_callback_. This is used by RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN to + // tame log spam. + // By default we always issue the callback, regardless of callback count. + uint32_t min_blocking_calls_for_callback_ = 0; std::function result_callback_; }; diff --git a/rtc_base/thread_unittest.cc b/rtc_base/thread_unittest.cc index 86e429e72f..2a24d6ca37 100644 --- a/rtc_base/thread_unittest.cc +++ b/rtc_base/thread_unittest.cc @@ -297,6 +297,40 @@ TEST(ThreadTest, CountBlockingCalls) { #endif } +#if RTC_DCHECK_IS_ON +TEST(ThreadTest, CountBlockingCallsOneCallback) { + rtc::Thread* current = rtc::Thread::Current(); + ASSERT_TRUE(current); + bool was_called_back = false; + { + rtc::Thread::ScopedCountBlockingCalls blocked_calls( + [&](uint32_t actual_block, uint32_t could_block) { + was_called_back = true; + }); + current->Invoke(RTC_FROM_HERE, []() {}); + } + EXPECT_TRUE(was_called_back); +} + +TEST(ThreadTest, CountBlockingCallsSkipCallback) { + rtc::Thread* current = rtc::Thread::Current(); + ASSERT_TRUE(current); + bool was_called_back = false; + { + rtc::Thread::ScopedCountBlockingCalls blocked_calls( + [&](uint32_t actual_block, uint32_t could_block) { + was_called_back = true; + }); + // Changed `blocked_calls` to not issue the callback if there are 1 or + // fewer blocking calls (i.e. we set the minimum required number to 2). + blocked_calls.set_minimum_call_count_for_callback(2); + current->Invoke(RTC_FROM_HERE, []() {}); + } + // We should not have gotten a call back. + EXPECT_FALSE(was_called_back); +} +#endif + // Test that setting thread names doesn't cause a malfunction. // There's no easy way to verify the name was set properly at this time. TEST(ThreadTest, Names) {