Commit eb573b71 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

Switch ThreadId to std::atomic

Avoid atomicops, use std::atomic instead.

R=ishell@chromium.org

Cq-Include-Trybots: luci.v8.try:v8_linux64_tsan_rel
Bug: v8:8834, v8:8926
Change-Id: I07bc7bbe079fc4a138feb4d8fda91eb57046846d
Reviewed-on: https://chromium-review.googlesource.com/c/1491609Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59949}
parent c7ebc581
...@@ -9,13 +9,13 @@ ...@@ -9,13 +9,13 @@
namespace v8 { namespace v8 {
namespace internal { namespace internal {
base::Atomic32 ThreadId::highest_thread_id_ = 0;
namespace { namespace {
DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::Thread::LocalStorageKey, GetThreadIdKey, DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::Thread::LocalStorageKey, GetThreadIdKey,
base::Thread::CreateThreadLocalKey()) base::Thread::CreateThreadLocalKey())
std::atomic<int> next_thread_id{1};
} // namespace } // namespace
// static // static
...@@ -26,10 +26,12 @@ ThreadId ThreadId::TryGetCurrent() { ...@@ -26,10 +26,12 @@ ThreadId ThreadId::TryGetCurrent() {
// static // static
int ThreadId::GetCurrentThreadId() { int ThreadId::GetCurrentThreadId() {
int thread_id = base::Thread::GetThreadLocalInt(*GetThreadIdKey()); auto key = *GetThreadIdKey();
int thread_id = base::Thread::GetThreadLocalInt(key);
if (thread_id == 0) { if (thread_id == 0) {
thread_id = AllocateThreadId(); thread_id = next_thread_id.fetch_add(1);
base::Thread::SetThreadLocalInt(*GetThreadIdKey(), thread_id); CHECK_LE(1, thread_id);
base::Thread::SetThreadLocalInt(key, thread_id);
} }
return thread_id; return thread_id;
} }
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#ifndef V8_THREAD_ID_H_ #ifndef V8_THREAD_ID_H_
#define V8_THREAD_ID_H_ #define V8_THREAD_ID_H_
#include "src/base/atomicops.h" #include <atomic>
#include "src/base/macros.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -14,10 +16,12 @@ namespace internal { ...@@ -14,10 +16,12 @@ namespace internal {
class ThreadId { class ThreadId {
public: public:
// Creates an invalid ThreadId. // Creates an invalid ThreadId.
ThreadId() { base::Relaxed_Store(&id_, kInvalidId); } ThreadId() : ThreadId(kInvalidId) {}
ThreadId(const ThreadId& other) V8_NOEXCEPT : ThreadId(other.ToInteger()) {}
ThreadId& operator=(const ThreadId& other) V8_NOEXCEPT { ThreadId& operator=(const ThreadId& other) V8_NOEXCEPT {
base::Relaxed_Store(&id_, base::Relaxed_Load(&other.id_)); id_.store(other.ToInteger(), std::memory_order_relaxed);
return *this; return *this;
} }
...@@ -34,37 +38,28 @@ class ThreadId { ...@@ -34,37 +38,28 @@ class ThreadId {
// Compares ThreadIds for equality. // Compares ThreadIds for equality.
V8_INLINE bool Equals(const ThreadId& other) const { V8_INLINE bool Equals(const ThreadId& other) const {
return base::Relaxed_Load(&id_) == base::Relaxed_Load(&other.id_); return ToInteger() == other.ToInteger();
} }
// Checks whether this ThreadId refers to any thread. // Checks whether this ThreadId refers to any thread.
V8_INLINE bool IsValid() const { V8_INLINE bool IsValid() const { return ToInteger() != kInvalidId; }
return base::Relaxed_Load(&id_) != kInvalidId;
}
// Converts ThreadId to an integer representation // Converts ThreadId to an integer representation
// (required for public API: V8::V8::GetCurrentThreadId). // (required for public API: V8::V8::GetCurrentThreadId).
int ToInteger() const { return static_cast<int>(base::Relaxed_Load(&id_)); } int ToInteger() const { return id_.load(std::memory_order_relaxed); }
// Converts ThreadId to an integer representation // Converts ThreadId to an integer representation
// (required for public API: V8::V8::TerminateExecution). // (required for public API: V8::V8::TerminateExecution).
static ThreadId FromInteger(int id) { return ThreadId(id); } static ThreadId FromInteger(int id) { return ThreadId(id); }
private: private:
static const int kInvalidId = -1; static constexpr int kInvalidId = -1;
explicit ThreadId(int id) { base::Relaxed_Store(&id_, id); } explicit ThreadId(int id) { id_.store(id, std::memory_order_relaxed); }
static int AllocateThreadId() {
int new_id = base::Relaxed_AtomicIncrement(&highest_thread_id_, 1);
return new_id;
}
static int GetCurrentThreadId(); static int GetCurrentThreadId();
base::Atomic32 id_; std::atomic<int> id_;
static base::Atomic32 highest_thread_id_;
}; };
} // namespace internal } // namespace internal
......
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