5#ifndef INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
6#define INCLUDE_CPPGC_CROSS_THREAD_PERSISTENT_H_
43 return reinterpret_cast<std::atomic<PersistentNode*>*
>(&
node_)->load(
44 std::memory_order_acquire);
50#if defined(__has_feature)
51#if __has_feature(address_sanitizer)
57 __atomic_store(&
node_, &value, __ATOMIC_RELEASE);
61 reinterpret_cast<std::atomic<PersistentNode*>*
>(&
node_)->store(
62 value, std::memory_order_release);
69template <
typename T,
typename WeaknessPolicy,
typename LocationPolicy,
70 typename CheckingPolicy>
72 public LocationPolicy,
73 private WeaknessPolicy,
74 private CheckingPolicy {
76 using typename WeaknessPolicy::IsStrongPersistent;
91 if (IsValid(old_value)) {
93 this->GetPersistentRegion(old_value);
107 : LocationPolicy(loc) {}
111 : LocationPolicy(loc) {}
120 if (!IsValid(raw))
return;
124 this->CheckPointer(raw);
130 template <
typename U,
typename OtherWeaknessPolicy,
131 typename OtherLocationPolicy,
typename OtherCheckingPolicy>
139 if (!IsValid(raw))
return;
142 this->CheckPointer(raw);
149 template <
typename U,
typename MemberBarrierPolicy,
150 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
151 typename MemberStorageType,
152 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
155 MemberCheckingPolicy, MemberStorageType>
169 template <
typename U,
typename OtherWeaknessPolicy,
170 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
171 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
175 OtherCheckingPolicy>& other,
185 *
this = std::move(other);
191 AssignSafe(guard, other.
Get());
195 template <
typename U,
typename OtherWeaknessPolicy,
196 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
197 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
201 OtherCheckingPolicy>& other) {
203 AssignSafe(guard, other.
Get());
208 if (
this == &other)
return *
this;
211 PersistentBase::operator=(std::move(other));
212 LocationPolicy::operator=(std::move(other));
213 if (!IsValid(
GetValue()))
return *
this;
217 this->CheckPointer(
Get());
232 template <
typename U,
typename MemberBarrierPolicy,
233 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
234 typename MemberStorageType,
235 typename = std::enable_if_t<std::is_base_of<T, U>::value>>
238 MemberCheckingPolicy, MemberStorageType>
260 AssignSafe(guard, s);
275 return static_cast<T*
>(
const_cast<void*
>(
GetValue()));
283 AssignSafe(guard,
nullptr);
306 explicit operator bool()
const {
return Get(); }
315 operator T*()
const {
return Get(); }
325 template <
typename U,
typename OtherWeaknessPolicy = WeaknessPolicy,
326 typename OtherLocationPolicy = LocationPolicy,
327 typename OtherCheckingPolicy = CheckingPolicy>
331 using OtherBasicCrossThreadPersistent =
333 OtherCheckingPolicy>;
335 return OtherBasicCrossThreadPersistent(
336 typename OtherBasicCrossThreadPersistent::UnsafeCtorTag(),
337 static_cast<U*
>(
Get()));
340 template <
typename U = T,
342 U, WeaknessPolicy>::IsStrongPersistent::value>::type>
350 static bool IsValid(
const void* ptr) {
354 static void TraceAsRoot(
RootVisitor& root_visitor,
const void* ptr) {
355 root_visitor.Trace(*
static_cast<const BasicCrossThreadPersistent*
>(ptr));
358 void AssignUnsafe(T* ptr) {
360 if (IsValid(old_value)) {
361 PersistentRegionLock guard;
365 if (IsValid(old_value)) {
366 CrossThreadPersistentRegion& region =
367 this->GetPersistentRegion(old_value);
368 if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) {
370 this->CheckPointer(ptr);
380 if (!IsValid(ptr))
return;
381 PersistentRegionLock guard;
382 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &TraceAsRoot));
383 this->CheckPointer(ptr);
386 void AssignSafe(PersistentRegionLock&, T* ptr) {
389 if (IsValid(old_value)) {
390 CrossThreadPersistentRegion& region =
391 this->GetPersistentRegion(old_value);
392 if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) {
394 this->CheckPointer(ptr);
401 if (!IsValid(ptr))
return;
402 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &TraceAsRoot));
403 this->CheckPointer(ptr);
406 void ClearFromGC()
const {
416 T* GetFromGC()
const {
423template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
426 LocationPolicy, CheckingPolicy>>
Definition: cross-thread-persistent.h:127
Definition: cross-thread-persistent.h:74
BasicCrossThreadPersistent & operator=(const BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > &other)
Definition: cross-thread-persistent.h:198
T * Get() const
Definition: cross-thread-persistent.h:274
BasicCrossThreadPersistent(const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:105
T & operator*() const
Definition: cross-thread-persistent.h:323
BasicCrossThreadPersistent(const BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > &other, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:172
friend class internal::RootVisitor
Definition: cross-thread-persistent.h:420
BasicCrossThreadPersistent(const BasicCrossThreadPersistent &other, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:160
BasicCrossThreadPersistent & operator=(T *other)
Definition: cross-thread-persistent.h:226
void Clear()
Definition: cross-thread-persistent.h:281
BasicCrossThreadPersistent & operator=(BasicCrossThreadPersistent &&other)
Definition: cross-thread-persistent.h:207
~BasicCrossThreadPersistent()
Definition: cross-thread-persistent.h:79
BasicCrossThreadPersistent< U, internal::StrongCrossThreadPersistentPolicy > Lock() const
Definition: cross-thread-persistent.h:344
T PointeeType
Definition: cross-thread-persistent.h:77
BasicCrossThreadPersistent(SentinelPointer s, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:113
BasicCrossThreadPersistent(std::nullptr_t, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:109
BasicCrossThreadPersistent(T &raw, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:145
BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > To() const
Definition: cross-thread-persistent.h:330
BasicCrossThreadPersistent(BasicCrossThreadPersistent &&other, const SourceLocation &loc=SourceLocation::Current()) noexcept
Definition: cross-thread-persistent.h:181
T * operator->() const
Definition: cross-thread-persistent.h:322
BasicCrossThreadPersistent & operator=(std::nullptr_t)
Definition: cross-thread-persistent.h:248
BasicCrossThreadPersistent(UnsafeCtorTag, T *raw, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:135
T * Release()
Definition: cross-thread-persistent.h:293
BasicCrossThreadPersistent(internal::BasicMember< U, MemberBarrierPolicy, MemberWeaknessTag, MemberCheckingPolicy, MemberStorageType > member, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:153
BasicCrossThreadPersistent & operator=(const BasicCrossThreadPersistent &other)
Definition: cross-thread-persistent.h:188
BasicCrossThreadPersistent & operator=(internal::BasicMember< U, MemberBarrierPolicy, MemberWeaknessTag, MemberCheckingPolicy, MemberStorageType > member)
Definition: cross-thread-persistent.h:236
BasicCrossThreadPersistent(T *raw, const SourceLocation &loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:117
BasicCrossThreadPersistent & operator=(SentinelPointer s)
Definition: cross-thread-persistent.h:258
Definition: cross-thread-persistent.h:22
PersistentNode * GetNodeFromGC() const
Definition: cross-thread-persistent.h:32
CrossThreadPersistentBase(const void *raw)
Definition: cross-thread-persistent.h:25
const void * GetValueFromGC() const
Definition: cross-thread-persistent.h:27
void SetNodeSafe(PersistentNode *value) const
Definition: cross-thread-persistent.h:49
CrossThreadPersistentBase()=default
PersistentNode * GetNodeSafe() const
Definition: cross-thread-persistent.h:42
void ClearFromGC() const
Definition: cross-thread-persistent.h:35
Definition: persistent-node.h:185
void FreeNode(PersistentNode *node)
Definition: persistent-node.h:203
PersistentNode * AllocateNode(void *owner, TraceRootCallback trace)
Definition: persistent-node.h:195
Definition: persistent.h:23
const void * GetValue() const
Definition: persistent.h:28
void SetValue(const void *value)
Definition: persistent.h:29
PersistentNode * GetNode() const
Definition: persistent.h:31
const void * raw_
Definition: persistent.h:42
PersistentNode * node_
Definition: persistent.h:43
void SetNode(PersistentNode *node)
Definition: persistent.h:32
Definition: persistent-node.h:27
void UpdateOwner(void *owner)
Definition: persistent-node.h:45
Definition: persistent-node.h:174
static void AssertLocked()
Definition: visitor.h:441
Definition: v8-source-location.h:31
static constexpr SourceLocation Current()
Definition: v8-source-location.h:44
#define CPPGC_DCHECK(condition)
Definition: logging.h:36
Definition: allocation.h:38
constexpr internal::SentinelPointer kSentinelPointer
Definition: sentinel-pointer.h:35
Definition: type-traits.h:29
Definition: sentinel-pointer.h:17
Definition: pointer-policies.h:250
Definition: pointer-policies.h:256
#define V8_CLANG_NO_SANITIZE(what)
defined(V8_TRIVIAL_ABI)
Definition: v8config.h:754