mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00
Remove aggressive thread checker from WeakPtrReference
This allows to create WeakPtr and dereference it on different threads. Fix test to validate it. Bug: webrtc:8517 Change-Id: Idaf0bbdcf14bffbe43cb5fb6514041e8fa746004 Reviewed-on: https://webrtc-review.googlesource.com/21700 Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Reviewed-by: Per Kjellander <perkj@webrtc.org> Cr-Commit-Position: refs/heads/master@{#20634}
This commit is contained in:
parent
0ec39e2d0f
commit
66cebbda35
3 changed files with 15 additions and 12 deletions
|
@ -52,16 +52,13 @@ bool WeakReference::is_valid() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReferenceOwner::WeakReferenceOwner() {
|
WeakReferenceOwner::WeakReferenceOwner() {
|
||||||
checker_.Detach();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReferenceOwner::~WeakReferenceOwner() {
|
WeakReferenceOwner::~WeakReferenceOwner() {
|
||||||
RTC_DCHECK(checker_.CalledSequentially());
|
|
||||||
Invalidate();
|
Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference WeakReferenceOwner::GetRef() const {
|
WeakReference WeakReferenceOwner::GetRef() const {
|
||||||
RTC_DCHECK(checker_.CalledSequentially());
|
|
||||||
// If we hold the last reference to the Flag then create a new one.
|
// If we hold the last reference to the Flag then create a new one.
|
||||||
if (!HasRefs())
|
if (!HasRefs())
|
||||||
flag_ = new RefCountedObject<WeakReference::Flag>();
|
flag_ = new RefCountedObject<WeakReference::Flag>();
|
||||||
|
@ -70,7 +67,6 @@ WeakReference WeakReferenceOwner::GetRef() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeakReferenceOwner::Invalidate() {
|
void WeakReferenceOwner::Invalidate() {
|
||||||
RTC_DCHECK(checker_.CalledSequentially());
|
|
||||||
if (flag_.get()) {
|
if (flag_.get()) {
|
||||||
flag_->Invalidate();
|
flag_->Invalidate();
|
||||||
flag_ = nullptr;
|
flag_ = nullptr;
|
||||||
|
|
|
@ -135,7 +135,6 @@ class WeakReferenceOwner {
|
||||||
void Invalidate();
|
void Invalidate();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SequencedTaskChecker checker_;
|
|
||||||
mutable scoped_refptr<RefCountedObject<WeakReference::Flag>> flag_;
|
mutable scoped_refptr<RefCountedObject<WeakReference::Flag>> flag_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -221,13 +221,21 @@ TEST(WeakPtrTest, ObjectAndWeakPtrOnDifferentThreads) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WeakPtrTest, WeakPtrInitiateAndUseOnDifferentThreads) {
|
TEST(WeakPtrTest, WeakPtrInitiateAndUseOnDifferentThreads) {
|
||||||
// Test that it is OK to create an object that has a WeakPtr member on one
|
// Test that it is OK to create a WeakPtr on one thread, but use it on
|
||||||
// thread, but use it on another. This tests that we do not trip runtime
|
// another. This tests that we do not trip runtime checks that ensure that a
|
||||||
// checks that ensure that a WeakPtr is not used by multiple threads.
|
// WeakPtr is not used by multiple threads.
|
||||||
std::unique_ptr<Arrow> arrow(NewObjectCreatedOnTaskQueue<Arrow>());
|
auto target = rtc::MakeUnique<TargetWithFactory>();
|
||||||
TargetWithFactory target;
|
// Create weak ptr on main thread
|
||||||
arrow->target = target.factory.GetWeakPtr();
|
WeakPtr<Target> weak_ptr = target->factory.GetWeakPtr();
|
||||||
EXPECT_EQ(&target, arrow->target.get());
|
rtc::TaskQueue queue("queue");
|
||||||
|
rtc::Event done(false, false);
|
||||||
|
queue.PostTask([&] {
|
||||||
|
// Dereference and invalide weak_ptr on another thread.
|
||||||
|
EXPECT_EQ(weak_ptr.get(), target.get());
|
||||||
|
target.reset();
|
||||||
|
done.Set();
|
||||||
|
});
|
||||||
|
EXPECT_TRUE(done.Wait(1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
|
Loading…
Reference in a new issue