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);
106 : LocationPolicy(loc) {}
110 : LocationPolicy(loc) {}
119 if (!IsValid(raw))
return;
123 this->CheckPointer(raw);
129 template <
typename U,
typename OtherWeaknessPolicy,
130 typename OtherLocationPolicy,
typename OtherCheckingPolicy>
137 if (!IsValid(raw))
return;
140 this->CheckPointer(raw);
147 template <
typename U,
typename MemberBarrierPolicy,
148 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
149 typename MemberStorageType,
150 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
153 MemberCheckingPolicy, MemberStorageType>
166 template <
typename U,
typename OtherWeaknessPolicy,
167 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
168 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
170 U, OtherWeaknessPolicy, OtherLocationPolicy,
171 OtherCheckingPolicy>& other,
181 *
this = std::move(other);
187 AssignSafe(guard, other.
Get());
191 template <
typename U,
typename OtherWeaknessPolicy,
192 typename OtherLocationPolicy,
typename OtherCheckingPolicy,
193 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
197 OtherCheckingPolicy>& other) {
199 AssignSafe(guard, other.
Get());
204 if (
this == &other)
return *
this;
207 PersistentBase::operator=(std::move(other));
208 LocationPolicy::operator=(std::move(other));
209 if (!IsValid(
GetValue()))
return *
this;
213 this->CheckPointer(
Get());
228 template <
typename U,
typename MemberBarrierPolicy,
229 typename MemberWeaknessTag,
typename MemberCheckingPolicy,
230 typename MemberStorageType,
231 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
234 MemberCheckingPolicy, MemberStorageType>
256 AssignSafe(guard, s);
271 return static_cast<T*
>(
const_cast<void*
>(
GetValue()));
279 AssignSafe(guard,
nullptr);
302 explicit operator bool()
const {
return Get(); }
311 operator T*()
const {
return Get(); }
321 template <
typename U,
typename OtherWeaknessPolicy = WeaknessPolicy,
322 typename OtherLocationPolicy = LocationPolicy,
323 typename OtherCheckingPolicy = CheckingPolicy>
327 using OtherBasicCrossThreadPersistent =
329 OtherCheckingPolicy>;
331 return OtherBasicCrossThreadPersistent(
332 typename OtherBasicCrossThreadPersistent::UnsafeCtorTag(),
333 static_cast<U*
>(
Get()));
336 template <
typename U = T,
338 U, WeaknessPolicy>::IsStrongPersistent::value>>
346 static bool IsValid(
const void* ptr) {
350 static void TraceAsRoot(
RootVisitor& root_visitor,
const void* ptr) {
351 root_visitor.Trace(*
static_cast<const BasicCrossThreadPersistent*
>(ptr));
354 void AssignUnsafe(T* ptr) {
356 if (IsValid(old_value)) {
357 PersistentRegionLock guard;
361 if (IsValid(old_value)) {
362 CrossThreadPersistentRegion& region =
363 this->GetPersistentRegion(old_value);
364 if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) {
366 this->CheckPointer(ptr);
376 if (!IsValid(ptr))
return;
377 PersistentRegionLock guard;
378 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &TraceAsRoot));
379 this->CheckPointer(ptr);
382 void AssignSafe(PersistentRegionLock&, T* ptr) {
385 if (IsValid(old_value)) {
386 CrossThreadPersistentRegion& region =
387 this->GetPersistentRegion(old_value);
388 if (IsValid(ptr) && (®ion == &this->GetPersistentRegion(ptr))) {
390 this->CheckPointer(ptr);
397 if (!IsValid(ptr))
return;
398 SetNode(this->GetPersistentRegion(ptr).AllocateNode(
this, &TraceAsRoot));
399 this->CheckPointer(ptr);
402 void ClearFromGC()
const {
412 T* GetFromGC()
const {
419template <
typename T,
typename LocationPolicy,
typename CheckingPolicy>
422 LocationPolicy, CheckingPolicy>>
Definition: cross-thread-persistent.h:126
Definition: cross-thread-persistent.h:74
BasicCrossThreadPersistent & operator=(const BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > &other)
Definition: cross-thread-persistent.h:194
T * Get() const
Definition: cross-thread-persistent.h:270
T & operator*() const
Definition: cross-thread-persistent.h:319
friend class internal::RootVisitor
Definition: cross-thread-persistent.h:416
BasicCrossThreadPersistent(T *raw, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:116
BasicCrossThreadPersistent & operator=(T *other)
Definition: cross-thread-persistent.h:222
void Clear()
Definition: cross-thread-persistent.h:277
BasicCrossThreadPersistent & operator=(BasicCrossThreadPersistent &&other)
Definition: cross-thread-persistent.h:203
~BasicCrossThreadPersistent()
Definition: cross-thread-persistent.h:79
BasicCrossThreadPersistent(BasicCrossThreadPersistent &&other, SourceLocation loc=SourceLocation::Current()) noexcept
Definition: cross-thread-persistent.h:177
BasicCrossThreadPersistent< U, internal::StrongCrossThreadPersistentPolicy > Lock() const
Definition: cross-thread-persistent.h:340
T PointeeType
Definition: cross-thread-persistent.h:77
BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > To() const
Definition: cross-thread-persistent.h:326
T * operator->() const
Definition: cross-thread-persistent.h:318
BasicCrossThreadPersistent(SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:105
BasicCrossThreadPersistent(internal::BasicMember< U, MemberBarrierPolicy, MemberWeaknessTag, MemberCheckingPolicy, MemberStorageType > member, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:151
BasicCrossThreadPersistent & operator=(std::nullptr_t)
Definition: cross-thread-persistent.h:244
BasicCrossThreadPersistent(std::nullptr_t, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:108
BasicCrossThreadPersistent(T &raw, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:143
BasicCrossThreadPersistent(UnsafeCtorTag, T *raw, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:134
T * Release()
Definition: cross-thread-persistent.h:289
BasicCrossThreadPersistent & operator=(const BasicCrossThreadPersistent &other)
Definition: cross-thread-persistent.h:184
BasicCrossThreadPersistent(const BasicCrossThreadPersistent< U, OtherWeaknessPolicy, OtherLocationPolicy, OtherCheckingPolicy > &other, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:169
BasicCrossThreadPersistent(SentinelPointer s, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:112
BasicCrossThreadPersistent & operator=(internal::BasicMember< U, MemberBarrierPolicy, MemberWeaknessTag, MemberCheckingPolicy, MemberStorageType > member)
Definition: cross-thread-persistent.h:232
BasicCrossThreadPersistent(const BasicCrossThreadPersistent &other, SourceLocation loc=SourceLocation::Current())
Definition: cross-thread-persistent.h:158
BasicCrossThreadPersistent & operator=(SentinelPointer s)
Definition: cross-thread-persistent.h:254
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:470
Definition: v8-source-location.h:22
static constexpr SourceLocation Current(const std::source_location &loc=std::source_location::current())
Definition: v8-source-location.h:28
#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:30
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:824