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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_ATOMIC_OPERATIONS_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_ATOMIC_OPERATIONS_H_
#include <atomic>
#include <cstddef>
#include <type_traits>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/wtf_export.h"
namespace WTF {
// TOOD(omerkatz): Replace these casts with std::atomic_ref (C++20) once it
// becomes available.
template <typename T>
ALWAYS_INLINE std::atomic<T>* AsAtomicPtr(T* t) {
return reinterpret_cast<std::atomic<T>*>(t);
}
template <typename T>
ALWAYS_INLINE const std::atomic<T>* AsAtomicPtr(const T* t) {
return reinterpret_cast<const std::atomic<T>*>(t);
}
// Copies |bytes| bytes from |from| to |to| using atomic reads. Assumes |to|
// and |from| are size_t-aligned (or halfword-aligned for 64-bit platforms) and
// point to buffers of size |bytes|. Note that atomicity is guaranteed only per
// word/halfword, not for the entire |bytes| bytes as a whole. The function
// copies elements one by one, so overlapping regions are not supported.
WTF_EXPORT void AtomicReadMemcpy(void* to, const void* from, size_t bytes);
namespace internal {
template <size_t bytes, typename AlignmentType>
ALWAYS_INLINE void AtomicReadMemcpyAligned(void* to, const void* from) {
static constexpr size_t kAlignment = sizeof(AlignmentType);
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(to) & (kAlignment - 1));
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(from) & (kAlignment - 1));
#if defined(ARCH_CPU_64_BITS)
if constexpr (bytes == sizeof(uint32_t)) {
*reinterpret_cast<uint32_t*>(to) =
AsAtomicPtr(reinterpret_cast<const uint32_t*>(from))
->load(std::memory_order_relaxed);
return;
}
#endif // defined(ARCH_CPU_64_BITS)
if constexpr (bytes % kAlignment == 0 && bytes >= kAlignment &&
bytes <= 3 * kAlignment) {
*reinterpret_cast<AlignmentType*>(to) =
AsAtomicPtr(reinterpret_cast<const AlignmentType*>(from))
->load(std::memory_order_relaxed);
if constexpr (bytes >= 2 * kAlignment) {
*(reinterpret_cast<AlignmentType*>(to) + 1) =
AsAtomicPtr(reinterpret_cast<const AlignmentType*>(from) + 1)
->load(std::memory_order_relaxed);
}
if constexpr (bytes == 3 * kAlignment) {
*(reinterpret_cast<AlignmentType*>(to) + 2) =
AsAtomicPtr(reinterpret_cast<const AlignmentType*>(from) + 2)
->load(std::memory_order_relaxed);
}
} else {
AtomicReadMemcpy(to, from, bytes);
}
}
} // namespace internal
template <size_t bytes, size_t alignment>
ALWAYS_INLINE void AtomicReadMemcpy(void* to, const void* from) {
static_assert(bytes > 0, "Number of copied bytes should be greater than 0");
if constexpr (alignment == sizeof(size_t)) {
internal::AtomicReadMemcpyAligned<bytes, size_t>(to, from);
} else if constexpr (alignment == sizeof(uint32_t)) {
internal::AtomicReadMemcpyAligned<bytes, uint32_t>(to, from);
} else {
AtomicReadMemcpy(to, from, bytes);
}
}
// Copies |bytes| bytes from |from| to |to| using atomic writes. Assumes |to|
// and |from| are size_t-aligned (or halfword-aligned for 64-bit platforms) and
// point to buffers of size |bytes|. Note that atomicity is guaranteed only per
// word/halfword, not for the entire |bytes| bytes as a whole. The function
// copies elements one by one, so overlapping regions are not supported.
WTF_EXPORT void AtomicWriteMemcpy(void* to, const void* from, size_t bytes);
namespace internal {
template <size_t bytes, typename AlignmentType>
ALWAYS_INLINE void AtomicWriteMemcpyAligned(void* to, const void* from) {
static constexpr size_t kAlignment = sizeof(AlignmentType);
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(to) & (kAlignment - 1));
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(from) & (kAlignment - 1));
#if defined(ARCH_CPU_64_BITS)
if constexpr (bytes == sizeof(uint32_t)) {
AsAtomicPtr(reinterpret_cast<uint32_t*>(to))
->store(*reinterpret_cast<const uint32_t*>(from),
std::memory_order_relaxed);
return;
}
#endif // defined(ARCH_CPU_64_BITS)
if constexpr (bytes % kAlignment == 0 && bytes >= kAlignment &&
bytes <= 3 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(to))
->store(*reinterpret_cast<const AlignmentType*>(from),
std::memory_order_relaxed);
if constexpr (bytes >= 2 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(to) + 1)
->store(*(reinterpret_cast<const AlignmentType*>(from) + 1),
std::memory_order_relaxed);
}
if constexpr (bytes == 3 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(to) + 2)
->store(*(reinterpret_cast<const AlignmentType*>(from) + 2),
std::memory_order_relaxed);
}
} else {
AtomicWriteMemcpy(to, from, bytes);
}
}
} // namespace internal
template <size_t bytes, size_t alignment>
ALWAYS_INLINE void AtomicWriteMemcpy(void* to, const void* from) {
static_assert(bytes > 0, "Number of copied bytes should be greater than 0");
if constexpr (alignment == sizeof(size_t)) {
internal::AtomicWriteMemcpyAligned<bytes, size_t>(to, from);
} else if constexpr (alignment == sizeof(uint32_t)) {
internal::AtomicWriteMemcpyAligned<bytes, uint32_t>(to, from);
} else {
AtomicWriteMemcpy(to, from, bytes);
}
}
// Set the first |bytes| bytes of |buf| to 0 using atomic writes. Assumes |buf|
// is size_t-aligned (or halfword-aligned for 64-bit platforms) and points to a
// buffer of size at least |bytes|. Note that atomicity is guaranteed only per
// word/halfword, not for the entire |bytes| bytes as a whole.
WTF_EXPORT void AtomicMemzero(void* buf, size_t bytes);
namespace internal {
template <size_t bytes, typename AlignmentType>
ALWAYS_INLINE void AtomicMemzeroAligned(void* buf) {
static constexpr size_t kAlignment = sizeof(AlignmentType);
DCHECK_EQ(0u, reinterpret_cast<size_t>(buf) & (kAlignment - 1));
#if defined(ARCH_CPU_64_BITS)
if constexpr (bytes == sizeof(uint32_t)) {
AsAtomicPtr(reinterpret_cast<uint32_t*>(buf))
->store(0, std::memory_order_relaxed);
return;
}
#endif // defined(ARCH_CPU_64_BITS)
if constexpr (bytes % kAlignment == 0 && bytes >= kAlignment &&
bytes <= 3 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(buf))
->store(0, std::memory_order_relaxed);
if constexpr (bytes >= 2 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(buf) + 1)
->store(0, std::memory_order_relaxed);
}
if constexpr (bytes == 3 * kAlignment) {
AsAtomicPtr(reinterpret_cast<AlignmentType*>(buf) + 2)
->store(0, std::memory_order_relaxed);
}
} else {
AtomicMemzero(buf, bytes);
}
}
} // namespace internal
template <size_t bytes, size_t alignment>
ALWAYS_INLINE void AtomicMemzero(void* buf) {
static_assert(bytes > 0, "Number of copied bytes should be greater than 0");
if constexpr (alignment == sizeof(size_t)) {
internal::AtomicMemzeroAligned<bytes, size_t>(buf);
} else if constexpr (alignment == sizeof(uint32_t)) {
internal::AtomicMemzeroAligned<bytes, uint32_t>(buf);
} else {
AtomicMemzero(buf, bytes);
}
}
// Swaps values using atomic writes.
template <typename T>
ALWAYS_INLINE void AtomicWriteSwap(T& lhs, T& rhs) {
T tmp_val = rhs;
AsAtomicPtr(&rhs)->store(lhs, std::memory_order_relaxed);
AsAtomicPtr(&lhs)->store(tmp_val, std::memory_order_relaxed);
}
} // namespace WTF
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_ATOMIC_OPERATIONS_H_
|