[tsan] Guard TestStatictics against concurrent access.

This CL quiets ThreadSanitizer for EventTracerTest.ScopedTraceEvent unit test.

Bug: webrtc:10943
Change-Id: Ifaec1ab37b85f091872f55e630189387f8df4401
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151309
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Yves Gerey <yvesg@google.com>
Cr-Commit-Position: refs/heads/master@{#29090}
This commit is contained in:
Yves Gerey
2019-09-06 10:03:54 +02:00
committed by Commit Bot
parent 75e2290af2
commit 50b0baf510

View File

@ -10,6 +10,8 @@
#include "rtc_base/event_tracer.h" #include "rtc_base/event_tracer.h"
#include "rtc_base/critical_section.h"
#include "rtc_base/thread_annotations.h"
#include "rtc_base/trace_event.h" #include "rtc_base/trace_event.h"
#include "test/gtest.h" #include "test/gtest.h"
@ -17,38 +19,45 @@ namespace {
class TestStatistics { class TestStatistics {
public: public:
TestStatistics() : events_logged_(0) {} void Reset() {
rtc::CritScope cs(&crit_);
events_logged_ = 0;
}
void Reset() { events_logged_ = 0; } void Increment() {
rtc::CritScope cs(&crit_);
++events_logged_;
}
void Increment() { ++events_logged_; } int Count() const {
rtc::CritScope cs(&crit_);
int Count() const { return events_logged_; } return events_logged_;
}
static TestStatistics* Get() { static TestStatistics* Get() {
static TestStatistics* test_stats = nullptr; // google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
if (!test_stats) static auto& test_stats = *new TestStatistics();
test_stats = new TestStatistics(); return &test_stats;
return test_stats;
} }
private: private:
int events_logged_; rtc::CriticalSection crit_;
int events_logged_ RTC_GUARDED_BY(crit_) = 0;
}; };
static const unsigned char* GetCategoryEnabledHandler(const char* name) { const unsigned char* GetCategoryEnabledHandler(const char* /*name*/) {
return reinterpret_cast<const unsigned char*>("test"); return reinterpret_cast<const unsigned char*>("test");
} }
static void AddTraceEventHandler(char phase, void TraceEventHandler(char /*phase*/,
const unsigned char* category_enabled, const unsigned char* /*category_enabled*/,
const char* name, const char* /*name*/,
unsigned long long id, unsigned long long /*id*/,
int num_args, int /*num_args*/,
const char** arg_names, const char** /*arg_names*/,
const unsigned char* arg_types, const unsigned char* /*arg_types*/,
const unsigned long long* arg_values, const unsigned long long* /*arg_values*/,
unsigned char flags) { unsigned char /*flags*/) {
TestStatistics::Get()->Increment(); TestStatistics::Get()->Increment();
} }
@ -63,7 +72,7 @@ TEST(EventTracerTest, EventTracerDisabled) {
} }
TEST(EventTracerTest, ScopedTraceEvent) { TEST(EventTracerTest, ScopedTraceEvent) {
SetupEventTracer(&GetCategoryEnabledHandler, &AddTraceEventHandler); SetupEventTracer(&GetCategoryEnabledHandler, &TraceEventHandler);
{ TRACE_EVENT0("test", "ScopedTraceEvent"); } { TRACE_EVENT0("test", "ScopedTraceEvent"); }
EXPECT_EQ(2, TestStatistics::Get()->Count()); EXPECT_EQ(2, TestStatistics::Get()->Count());
TestStatistics::Get()->Reset(); TestStatistics::Get()->Reset();