Commit 72e6c758 authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[cleanup] Move ThreadId to separate source file

and make it Isolate-independent.

Bug: v8:8238
Change-Id: I23faae87c302d24877ef001873f673d4a1cdd327
Reviewed-on: https://chromium-review.googlesource.com/c/1301484Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57066}
parent 0ccaa2fd
......@@ -2532,6 +2532,8 @@ v8_source_set("v8_base") {
"src/strtod.cc",
"src/strtod.h",
"src/third_party/utf8-decoder/utf8-decoder.h",
"src/thread-id.cc",
"src/thread-id.h",
"src/torque-assembler.h",
"src/tracing/trace-event.cc",
"src/tracing/trace-event.h",
......
......@@ -87,8 +87,6 @@ namespace internal {
#define TRACE_ISOLATE(tag)
#endif
base::Atomic32 ThreadId::highest_thread_id_ = 0;
extern const uint8_t* DefaultEmbeddedBlob();
extern uint32_t DefaultEmbeddedBlobSize();
......@@ -140,21 +138,6 @@ uint32_t Isolate::CurrentEmbeddedBlobSize() {
std::memory_order::memory_order_relaxed);
}
int ThreadId::AllocateThreadId() {
int new_id = base::Relaxed_AtomicIncrement(&highest_thread_id_, 1);
return new_id;
}
int ThreadId::GetCurrentThreadId() {
int thread_id = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_);
if (thread_id == 0) {
thread_id = AllocateThreadId();
base::Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id);
}
return thread_id;
}
void ThreadLocalTop::Initialize(Isolate* isolate) {
*this = ThreadLocalTop();
isolate_ = isolate;
......@@ -173,7 +156,6 @@ void ThreadLocalTop::Free() {
base::Thread::LocalStorageKey Isolate::isolate_key_;
base::Thread::LocalStorageKey Isolate::thread_id_key_;
base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
base::Atomic32 Isolate::isolate_counter_ = 0;
#if DEBUG
......@@ -198,9 +180,8 @@ Isolate::PerIsolateThreadData*
void Isolate::DiscardPerThreadDataForThisThread() {
int thread_id_int = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_);
if (thread_id_int) {
ThreadId thread_id = ThreadId(thread_id_int);
ThreadId thread_id = ThreadId::TryGetCurrent();
if (thread_id.IsValid()) {
DCHECK(!thread_manager_->mutex_owner_.Equals(thread_id));
base::MutexGuard lock_guard(&thread_data_table_mutex_);
PerIsolateThreadData* per_thread = thread_data_table_.Lookup(thread_id);
......@@ -234,7 +215,6 @@ void Isolate::InitializeOncePerProcess() {
#if DEBUG
base::Relaxed_Store(&isolate_key_created_, 1);
#endif
thread_id_key_ = base::Thread::CreateThreadLocalKey();
per_isolate_thread_data_key_ = base::Thread::CreateThreadLocalKey();
}
......
......@@ -32,6 +32,7 @@
#include "src/objects/code.h"
#include "src/objects/debug-objects.h"
#include "src/runtime/runtime.h"
#include "src/thread-id.h"
#include "src/unicode.h"
#ifdef V8_INTL_SUPPORT
......@@ -331,59 +332,6 @@ class WasmEngine;
} \
} while (false)
// Platform-independent, reliable thread identifier.
class ThreadId {
public:
// Creates an invalid ThreadId.
ThreadId() { base::Relaxed_Store(&id_, kInvalidId); }
ThreadId& operator=(const ThreadId& other) {
base::Relaxed_Store(&id_, base::Relaxed_Load(&other.id_));
return *this;
}
bool operator==(const ThreadId& other) const { return Equals(other); }
// Returns ThreadId for current thread.
static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
// Returns invalid ThreadId (guaranteed not to be equal to any thread).
static ThreadId Invalid() { return ThreadId(kInvalidId); }
// Compares ThreadIds for equality.
V8_INLINE bool Equals(const ThreadId& other) const {
return base::Relaxed_Load(&id_) == base::Relaxed_Load(&other.id_);
}
// Checks whether this ThreadId refers to any thread.
V8_INLINE bool IsValid() const {
return base::Relaxed_Load(&id_) != kInvalidId;
}
// Converts ThreadId to an integer representation
// (required for public API: V8::V8::GetCurrentThreadId).
int ToInteger() const { return static_cast<int>(base::Relaxed_Load(&id_)); }
// Converts ThreadId to an integer representation
// (required for public API: V8::V8::TerminateExecution).
static ThreadId FromInteger(int id) { return ThreadId(id); }
private:
static const int kInvalidId = -1;
explicit ThreadId(int id) { base::Relaxed_Store(&id_, id); }
static int AllocateThreadId();
V8_EXPORT_PRIVATE static int GetCurrentThreadId();
base::Atomic32 id_;
static base::Atomic32 highest_thread_id_;
friend class Isolate;
};
#define FIELD_ACCESSOR(type, name) \
inline void set_##name(type v) { name##_ = v; } \
inline type name() const { return name##_; }
......@@ -693,11 +641,6 @@ class Isolate final : private HiddenFactory {
return isolate_key_;
}
// Returns the key used to store process-wide thread IDs.
static base::Thread::LocalStorageKey thread_id_key() {
return thread_id_key_;
}
static base::Thread::LocalStorageKey per_isolate_thread_data_key();
// Mutex for serializing access to break control structures.
......@@ -1693,7 +1636,6 @@ class Isolate final : private HiddenFactory {
static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
static base::Thread::LocalStorageKey isolate_key_;
static base::Thread::LocalStorageKey thread_id_key_;
// A global counter for all generated Isolates, might overflow.
static base::Atomic32 isolate_counter_;
......@@ -1962,7 +1904,6 @@ class Isolate final : private HiddenFactory {
friend class Simulator;
friend class StackGuard;
friend class TestSerializer;
friend class ThreadId;
friend class ThreadManager;
DISALLOW_COPY_AND_ASSIGN(Isolate);
......
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/thread-id.h"
#include "src/base/lazy-instance.h"
#include "src/base/platform/platform.h"
namespace v8 {
namespace internal {
base::Atomic32 ThreadId::highest_thread_id_ = 0;
namespace {
struct LocalStorageKeyAllocator {
static void Construct(void* storage_ptr_arg) {
auto storage_ptr =
reinterpret_cast<base::Thread::LocalStorageKey*>(storage_ptr_arg);
*storage_ptr = base::Thread::CreateThreadLocalKey();
}
};
static base::LazyInstance<base::Thread::LocalStorageKey,
LocalStorageKeyAllocator>::type thread_id_key =
LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
ThreadId ThreadId::TryGetCurrent() {
int thread_id = base::Thread::GetThreadLocalInt(thread_id_key.Get());
return thread_id == 0 ? Invalid() : ThreadId(thread_id);
}
// static
int ThreadId::GetCurrentThreadId() {
int thread_id = base::Thread::GetThreadLocalInt(thread_id_key.Get());
if (thread_id == 0) {
thread_id = AllocateThreadId();
base::Thread::SetThreadLocalInt(thread_id_key.Get(), thread_id);
}
return thread_id;
}
} // namespace internal
} // namespace v8
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_THREAD_ID_H_
#define V8_THREAD_ID_H_
#include "src/base/atomicops.h"
namespace v8 {
namespace internal {
// Platform-independent, reliable thread identifier.
class ThreadId {
public:
// Creates an invalid ThreadId.
ThreadId() { base::Relaxed_Store(&id_, kInvalidId); }
ThreadId& operator=(const ThreadId& other) {
base::Relaxed_Store(&id_, base::Relaxed_Load(&other.id_));
return *this;
}
bool operator==(const ThreadId& other) const { return Equals(other); }
// Returns ThreadId for current thread if it exists or invalid id.
static ThreadId TryGetCurrent();
// Returns ThreadId for current thread.
static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
// Returns invalid ThreadId (guaranteed not to be equal to any thread).
static ThreadId Invalid() { return ThreadId(kInvalidId); }
// Compares ThreadIds for equality.
V8_INLINE bool Equals(const ThreadId& other) const {
return base::Relaxed_Load(&id_) == base::Relaxed_Load(&other.id_);
}
// Checks whether this ThreadId refers to any thread.
V8_INLINE bool IsValid() const {
return base::Relaxed_Load(&id_) != kInvalidId;
}
// Converts ThreadId to an integer representation
// (required for public API: V8::V8::GetCurrentThreadId).
int ToInteger() const { return static_cast<int>(base::Relaxed_Load(&id_)); }
// Converts ThreadId to an integer representation
// (required for public API: V8::V8::TerminateExecution).
static ThreadId FromInteger(int id) { return ThreadId(id); }
private:
static const int kInvalidId = -1;
explicit ThreadId(int id) { base::Relaxed_Store(&id_, id); }
static int AllocateThreadId() {
int new_id = base::Relaxed_AtomicIncrement(&highest_thread_id_, 1);
return new_id;
}
static int GetCurrentThreadId();
base::Atomic32 id_;
static base::Atomic32 highest_thread_id_;
};
} // namespace internal
} // namespace v8
#endif // V8_THREAD_ID_H_
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