blob: 70a0213c61391d3e8b8123d46175a6ca3ff3d4b3 [file] [log] [blame]
Kaido Kert25902c62024-06-17 17:10:28 -07001// Copyright 2011 The Chromium Authors
David Ghandehari9e5b5872016-07-28 09:50:04 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/memory/weak_ptr.h"
6
Kaido Kert25902c62024-06-17 17:10:28 -07007#if DCHECK_IS_ON()
8#include <ostream>
9
10#include "base/debug/stack_trace.h"
11#endif
12
13namespace base::internal {
David Ghandehari9e5b5872016-07-28 09:50:04 -070014
Andrew Top0d1858f2019-05-15 22:01:47 -070015WeakReference::Flag::Flag() {
16 // Flags only become bound when checked for validity, or invalidated,
17 // so that we can check that later validity/invalidation operations on
18 // the same Flag take place on the same sequenced thread.
19 DETACH_FROM_SEQUENCE(sequence_checker_);
David Ghandehari9e5b5872016-07-28 09:50:04 -070020}
21
22void WeakReference::Flag::Invalidate() {
23 // The flag being invalidated with a single ref implies that there are no
24 // weak pointers in existence. Allow deletion on other thread in this case.
Andrew Top0d1858f2019-05-15 22:01:47 -070025#if DCHECK_IS_ON()
Kaido Kert25902c62024-06-17 17:10:28 -070026 std::unique_ptr<debug::StackTrace> bound_at;
27 DCHECK(sequence_checker_.CalledOnValidSequence(&bound_at) || HasOneRef())
28 << "WeakPtrs must be invalidated on the same sequenced thread as where "
29 << "they are bound.\n"
30 << (bound_at ? "This was bound at:\n" + bound_at->ToString() : "")
31 << "Check failed at:";
Andrew Top0d1858f2019-05-15 22:01:47 -070032#endif
33 invalidated_.Set();
David Ghandehari9e5b5872016-07-28 09:50:04 -070034}
35
36bool WeakReference::Flag::IsValid() const {
Kaido Kert25902c62024-06-17 17:10:28 -070037 // WeakPtrs must be checked on the same sequenced thread.
38 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Andrew Top0d1858f2019-05-15 22:01:47 -070039 return !invalidated_.IsSet();
David Ghandehari9e5b5872016-07-28 09:50:04 -070040}
41
Andrew Top0d1858f2019-05-15 22:01:47 -070042bool WeakReference::Flag::MaybeValid() const {
43 return !invalidated_.IsSet();
David Ghandehari9e5b5872016-07-28 09:50:04 -070044}
45
Kaido Kert25902c62024-06-17 17:10:28 -070046#if DCHECK_IS_ON()
47void WeakReference::Flag::DetachFromSequence() {
48 DETACH_FROM_SEQUENCE(sequence_checker_);
49}
50#endif
51
Andrew Top0d1858f2019-05-15 22:01:47 -070052WeakReference::Flag::~Flag() = default;
David Ghandehari9e5b5872016-07-28 09:50:04 -070053
Andrew Top0d1858f2019-05-15 22:01:47 -070054WeakReference::WeakReference() = default;
Andrew Top0d1858f2019-05-15 22:01:47 -070055WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {}
Andrew Top0d1858f2019-05-15 22:01:47 -070056WeakReference::~WeakReference() = default;
57
Andrew Top0d1858f2019-05-15 22:01:47 -070058WeakReference::WeakReference(const WeakReference& other) = default;
Kaido Kert25902c62024-06-17 17:10:28 -070059WeakReference& WeakReference::operator=(const WeakReference& other) = default;
60
61WeakReference::WeakReference(WeakReference&& other) noexcept = default;
62WeakReference& WeakReference::operator=(WeakReference&& other) noexcept =
63 default;
64
65void WeakReference::Reset() {
66 flag_ = nullptr;
67}
Andrew Top0d1858f2019-05-15 22:01:47 -070068
69bool WeakReference::IsValid() const {
David Ghandehari9e5b5872016-07-28 09:50:04 -070070 return flag_ && flag_->IsValid();
71}
72
Andrew Top0d1858f2019-05-15 22:01:47 -070073bool WeakReference::MaybeValid() const {
74 return flag_ && flag_->MaybeValid();
David Ghandehari9e5b5872016-07-28 09:50:04 -070075}
76
Kaido Kert25902c62024-06-17 17:10:28 -070077WeakReferenceOwner::WeakReferenceOwner()
78 : flag_(MakeRefCounted<WeakReference::Flag>()) {}
Andrew Top0d1858f2019-05-15 22:01:47 -070079
David Ghandehari9e5b5872016-07-28 09:50:04 -070080WeakReferenceOwner::~WeakReferenceOwner() {
Kaido Kert25902c62024-06-17 17:10:28 -070081 flag_->Invalidate();
David Ghandehari9e5b5872016-07-28 09:50:04 -070082}
83
84WeakReference WeakReferenceOwner::GetRef() const {
Kaido Kert25902c62024-06-17 17:10:28 -070085#if DCHECK_IS_ON()
86 // If we hold the last reference to the Flag then detach the SequenceChecker.
David Ghandehari9e5b5872016-07-28 09:50:04 -070087 if (!HasRefs())
Kaido Kert25902c62024-06-17 17:10:28 -070088 flag_->DetachFromSequence();
89#endif
Andrew Top0d1858f2019-05-15 22:01:47 -070090
David Ghandehari9e5b5872016-07-28 09:50:04 -070091 return WeakReference(flag_);
92}
93
94void WeakReferenceOwner::Invalidate() {
Kaido Kert25902c62024-06-17 17:10:28 -070095 flag_->Invalidate();
96 flag_ = MakeRefCounted<WeakReference::Flag>();
David Ghandehari9e5b5872016-07-28 09:50:04 -070097}
98
Andrew Top0d1858f2019-05-15 22:01:47 -070099WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {
100 DCHECK(ptr_);
David Ghandehari9e5b5872016-07-28 09:50:04 -0700101}
102
Andrew Top0d1858f2019-05-15 22:01:47 -0700103WeakPtrFactoryBase::~WeakPtrFactoryBase() {
104 ptr_ = 0;
David Ghandehari9e5b5872016-07-28 09:50:04 -0700105}
106
Kaido Kert25902c62024-06-17 17:10:28 -0700107} // namespace base::internal