1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_CROSS_THREAD_PERSISTENT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_CROSS_THREAD_PERSISTENT_H_
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "v8/include/cppgc/cross-thread-persistent.h"
namespace blink {
// CrossThreadPersistent allows retaining objects from threads other than the
// thread that owns the heap of the corresponding object.
//
// Strongly prefer using `CrossThreadHandle` if the object must only be held
// from a different thread.
//
// Caveats:
// - Does not protect the heap owning an object from terminating. E.g., posting
// a task with a CrossThreadPersistent for `this` will result in a
// use-after-free in case the heap owning `this` is terminated before the task
// is invoked.
// - Reaching transitively through the graph is unsupported as objects may be
// moved concurrently on the thread owning the object.
template <typename T>
using CrossThreadPersistent = cppgc::subtle::CrossThreadPersistent<T>;
// CrossThreadWeakPersistent allows weakly retaining objects from threads other
// than the thread that owns the heap of the corresponding object.
//
// Strongly prefer using `CrossThreadWeakHandle` if the object must only be held
// from a different thread.
//
// Caveats:
// - Does not protect the heap owning an object from termination, as the
// reference is weak.
// - In order to access the underlying object
// `CrossThreadWeakPersistent<T>::Lock()` must be used which returns a
// `CrossThreadPersistent<T>` which in turn also does not protect the heap
// owning the object from terminating (see above).
// - Reaching transitively through the graph is unsupported as objects may be
// moved concurrently on the thread owning the object.
template <typename T>
using CrossThreadWeakPersistent = cppgc::subtle::WeakCrossThreadPersistent<T>;
template <typename T>
CrossThreadPersistent<T> WrapCrossThreadPersistent(
T* value,
const PersistentLocation& loc = PERSISTENT_LOCATION_FROM_HERE) {
return CrossThreadPersistent<T>(value, loc);
}
template <typename T>
CrossThreadWeakPersistent<T> WrapCrossThreadWeakPersistent(
T* value,
const PersistentLocation& loc = PERSISTENT_LOCATION_FROM_HERE) {
return CrossThreadWeakPersistent<T>(value, loc);
}
} // namespace blink
namespace WTF {
template <typename T>
struct HashTraits<blink::CrossThreadPersistent<T>>
: BasePersistentHashTraits<T, blink::CrossThreadPersistent<T>> {};
template <typename T>
struct HashTraits<blink::CrossThreadWeakPersistent<T>>
: BasePersistentHashTraits<T, blink::CrossThreadWeakPersistent<T>> {};
template <typename T>
struct CrossThreadCopier<blink::CrossThreadPersistent<T>>
: public CrossThreadCopierPassThrough<blink::CrossThreadPersistent<T>> {
STATIC_ONLY(CrossThreadCopier);
};
template <typename T>
struct CrossThreadCopier<blink::CrossThreadWeakPersistent<T>>
: public CrossThreadCopierPassThrough<blink::CrossThreadWeakPersistent<T>> {
STATIC_ONLY(CrossThreadCopier);
};
} // namespace WTF
namespace base {
template <typename T>
struct IsWeakReceiver<blink::CrossThreadWeakPersistent<T>> : std::true_type {};
template <typename>
struct BindUnwrapTraits;
template <typename T>
struct BindUnwrapTraits<blink::CrossThreadWeakPersistent<T>> {
static blink::CrossThreadPersistent<T> Unwrap(
const blink::CrossThreadWeakPersistent<T>& wrapped) {
return wrapped.Lock();
}
};
template <typename T>
struct MaybeValidTraits<blink::CrossThreadWeakPersistent<T>> {
static bool MaybeValid(const blink::CrossThreadWeakPersistent<T>& p) {
return true;
}
};
} // namespace base
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_CROSS_THREAD_PERSISTENT_H_
|