[tsan] Guard OwnerThread against concurrent access.

Prevent potential concurrent access of OwnerThread::has_run_ from:
 * Main thread via HasRun()
 * Signal thread via Run()
when running SignalThreadTest.OwnerThreadGoesAway.

Bug: webrtc:9855
Change-Id: I8951026a64794c085b4ac799ed4b74521b97f42a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151760
Commit-Queue: Yves Gerey <yvesg@google.com>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29087}
This commit is contained in:
Yves Gerey 2019-09-06 01:26:09 +02:00 committed by Commit Bot
parent eea6cedd8d
commit c2accf2de5

View File

@ -14,9 +14,11 @@
#include "absl/memory/memory.h"
#include "rtc_base/constructor_magic.h"
#include "rtc_base/critical_section.h"
#include "rtc_base/gunit.h"
#include "rtc_base/null_socket_server.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_annotations.h"
#include "test/gtest.h"
namespace rtc {
@ -146,17 +148,24 @@ class OwnerThread : public Thread, public sigslot::has_slots<> {
signal_thread->Release();
// Delete |signal_thread|.
signal_thread->Destroy(true);
has_run_ = true;
{
rtc::CritScope cs(&crit_);
has_run_ = true;
}
}
bool has_run() { return has_run_; }
void OnWorkDone(SignalThread* signal_thread) {
bool has_run() {
rtc::CritScope cs(&crit_);
return has_run_;
}
void OnWorkDone(SignalThread* /*signal_thread*/) {
FAIL() << " This shouldn't get called.";
}
private:
rtc::CriticalSection crit_;
SignalThreadTest* harness_;
bool has_run_;
bool has_run_ RTC_GUARDED_BY(crit_);
RTC_DISALLOW_COPY_AND_ASSIGN(OwnerThread);
};