Commit 7271e97f authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[platform] Switch to std::atomic

We want to get rid of the atomicops library, hence switch all uses to
std::atomic.

R=mlippautz@chromium.org

Bug: v8:8926, v8:8834

Cq-Include-Trybots: luci.v8.try:v8_linux64_tsan_rel
Change-Id: I9b7cca83703775b1ddee4f16f51b7ad6535bb67c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1520717Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60233}
parent d82c9afb
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include "src/base/utils/random-number-generator.h" #include "src/base/utils/random-number-generator.h"
#ifdef V8_FAST_TLS_SUPPORTED #ifdef V8_FAST_TLS_SUPPORTED
#include "src/base/atomicops.h" #include <atomic>
#endif #endif
#if V8_OS_MACOSX #if V8_OS_MACOSX
...@@ -815,7 +815,7 @@ static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) { ...@@ -815,7 +815,7 @@ static pthread_key_t LocalKeyToPthreadKey(Thread::LocalStorageKey local_key) {
#ifdef V8_FAST_TLS_SUPPORTED #ifdef V8_FAST_TLS_SUPPORTED
static Atomic32 tls_base_offset_initialized = 0; static std::atomic<bool> tls_base_offset_initialized{false};
intptr_t kMacTlsBaseOffset = 0; intptr_t kMacTlsBaseOffset = 0;
// It's safe to do the initialization more that once, but it has to be // It's safe to do the initialization more that once, but it has to be
...@@ -851,7 +851,7 @@ static void InitializeTlsBaseOffset() { ...@@ -851,7 +851,7 @@ static void InitializeTlsBaseOffset() {
kMacTlsBaseOffset = 0; kMacTlsBaseOffset = 0;
} }
Release_Store(&tls_base_offset_initialized, 1); tls_base_offset_initialized.store(true, std::memory_order_release);
} }
...@@ -871,7 +871,7 @@ static void CheckFastTls(Thread::LocalStorageKey key) { ...@@ -871,7 +871,7 @@ static void CheckFastTls(Thread::LocalStorageKey key) {
Thread::LocalStorageKey Thread::CreateThreadLocalKey() { Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
#ifdef V8_FAST_TLS_SUPPORTED #ifdef V8_FAST_TLS_SUPPORTED
bool check_fast_tls = false; bool check_fast_tls = false;
if (tls_base_offset_initialized == 0) { if (!tls_base_offset_initialized.load(std::memory_order_acquire)) {
check_fast_tls = true; check_fast_tls = true;
InitializeTlsBaseOffset(); InitializeTlsBaseOffset();
} }
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <ostream> #include <ostream>
#if V8_OS_WIN #if V8_OS_WIN
#include "src/base/atomicops.h"
#include "src/base/lazy-instance.h" #include "src/base/lazy-instance.h"
#include "src/base/win32-headers.h" #include "src/base/win32-headers.h"
#endif #endif
...@@ -493,7 +492,7 @@ DWORD (*g_tick_function)(void) = &timeGetTimeWrapper; ...@@ -493,7 +492,7 @@ DWORD (*g_tick_function)(void) = &timeGetTimeWrapper;
// "rollover" counter. // "rollover" counter.
union LastTimeAndRolloversState { union LastTimeAndRolloversState {
// The state as a single 32-bit opaque value. // The state as a single 32-bit opaque value.
base::Atomic32 as_opaque_32; int32_t as_opaque_32;
// The state as usable values. // The state as usable values.
struct { struct {
...@@ -509,7 +508,7 @@ union LastTimeAndRolloversState { ...@@ -509,7 +508,7 @@ union LastTimeAndRolloversState {
uint16_t rollovers; uint16_t rollovers;
} as_values; } as_values;
}; };
base::Atomic32 g_last_time_and_rollovers = 0; std::atomic<int32_t> g_last_time_and_rollovers{0};
static_assert(sizeof(LastTimeAndRolloversState) <= static_assert(sizeof(LastTimeAndRolloversState) <=
sizeof(g_last_time_and_rollovers), sizeof(g_last_time_and_rollovers),
"LastTimeAndRolloversState does not fit in a single atomic word"); "LastTimeAndRolloversState does not fit in a single atomic word");
...@@ -523,12 +522,12 @@ TimeTicks RolloverProtectedNow() { ...@@ -523,12 +522,12 @@ TimeTicks RolloverProtectedNow() {
LastTimeAndRolloversState state; LastTimeAndRolloversState state;
DWORD now; // DWORD is always unsigned 32 bits. DWORD now; // DWORD is always unsigned 32 bits.
// Fetch the "now" and "last" tick values, updating "last" with "now" and
// incrementing the "rollovers" counter if the tick-value has wrapped back
// around. Atomic operations ensure that both "last" and "rollovers" are
// always updated together.
int32_t original = g_last_time_and_rollovers.load(std::memory_order_acquire);
while (true) { while (true) {
// Fetch the "now" and "last" tick values, updating "last" with "now" and
// incrementing the "rollovers" counter if the tick-value has wrapped back
// around. Atomic operations ensure that both "last" and "rollovers" are
// always updated together.
int32_t original = base::Acquire_Load(&g_last_time_and_rollovers);
state.as_opaque_32 = original; state.as_opaque_32 = original;
now = g_tick_function(); now = g_tick_function();
uint8_t now_8 = static_cast<uint8_t>(now >> 24); uint8_t now_8 = static_cast<uint8_t>(now >> 24);
...@@ -540,11 +539,13 @@ TimeTicks RolloverProtectedNow() { ...@@ -540,11 +539,13 @@ TimeTicks RolloverProtectedNow() {
// Save the changed state. If the existing value is unchanged from the // Save the changed state. If the existing value is unchanged from the
// original, exit the loop. // original, exit the loop.
int32_t check = base::Release_CompareAndSwap(&g_last_time_and_rollovers, if (g_last_time_and_rollovers.compare_exchange_weak(
original, state.as_opaque_32); original, state.as_opaque_32, std::memory_order_acq_rel)) {
if (check == original) break; break;
}
// Another thread has done something in between so retry from the top. // Another thread has done something in between so retry from the top.
// {original} has been updated by the {compare_exchange_weak}.
} }
return TimeTicks() + return TimeTicks() +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment