Prevent data race in GetStaticInstance

The previous code attempted to lock instance_count and instance with a
CriticalSection, but the CriticalSection was not static, so each
function invocation got its own instance. Locking this call-specific
instance doesn't actually stop any other threads from concurrently
accessing the same function-scope globals, so this function had a data
race, which broke tsan tests (and possibly other things).

Making the CriticalSection shared among function calls will actually
synchronize access to the globals and allow our tsan tests to pass.

BUG=webrtc:3062

Review-Url: https://codereview.webrtc.org/2890213002
Cr-Commit-Position: refs/heads/master@{#18296}
This commit is contained in:
cstanfill
2017-05-29 00:01:14 -07:00
committed by Commit bot
parent 3a343ad329
commit c66f8d7d6d

View File

@ -40,7 +40,7 @@ static T* GetStaticInstance(CountOperation count_operation) {
static T* volatile instance = NULL;
CreateOperation state = kInstanceExists;
#ifndef _WIN32
rtc::CriticalSection crit_sect;
static rtc::CriticalSection crit_sect;
rtc::CritScope lock(&crit_sect);
if (count_operation ==