Commit 0f5ad735 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

Make the thread_id field of Isolate::thread_local_top atomic

Isolate::thread_id() may be invoked on a background thread to perform
a check if the thread is the main thread.

This also removes the copy constructor of ThreadLocalTop and clears
its fields explicitly.

Change-Id: I2207bf1ebd655805841fce9f23d23cff7be8f13b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2537693Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarSantiago Aboy Solanes <solanes@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71177}
parent dacc2fee
......@@ -650,7 +650,12 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
Context* context_address() { return &thread_local_top()->context_; }
// Access to current thread id.
THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
inline void set_thread_id(ThreadId id) {
thread_local_top()->thread_id_.store(id, std::memory_order_relaxed);
}
inline ThreadId thread_id() const {
return thread_local_top()->thread_id_.load(std::memory_order_relaxed);
}
// Interface to pending exception.
inline Object pending_exception();
......
......@@ -10,15 +10,43 @@
namespace v8 {
namespace internal {
void ThreadLocalTop::Clear() {
try_catch_handler_ = nullptr;
isolate_ = nullptr;
context_ = Context();
thread_id_ = ThreadId();
pending_handler_entrypoint_ = kNullAddress;
pending_handler_constant_pool_ = kNullAddress;
pending_handler_fp_ = kNullAddress;
pending_handler_sp_ = kNullAddress;
last_api_entry_ = kNullAddress;
pending_message_obj_ = Object();
rethrowing_message_ = false;
external_caught_exception_ = false;
c_entry_fp_ = kNullAddress;
handler_ = kNullAddress;
c_function_ = kNullAddress;
promise_on_stack_ = nullptr;
simulator_ = nullptr;
js_entry_sp_ = kNullAddress;
external_callback_scope_ = nullptr;
current_vm_state_ = EXTERNAL;
failed_access_check_callback_ = nullptr;
thread_in_wasm_flag_address_ = kNullAddress;
#ifdef V8_ENABLE_CONSERVATIVE_STACK_SCANNING
stack_ = ::heap::base::Stack(nullptr);
#endif
}
void ThreadLocalTop::Initialize(Isolate* isolate) {
*this = ThreadLocalTop();
Clear();
isolate_ = isolate;
#ifdef USE_SIMULATOR
simulator_ = Simulator::current(isolate);
#endif
thread_id_ = ThreadId::Current();
thread_in_wasm_flag_address_ = reinterpret_cast<Address>(
trap_handler::GetThreadInWasmThreadLocalAddress());
#ifdef USE_SIMULATOR
simulator_ = Simulator::current(isolate);
#endif
#ifdef V8_ENABLE_CONSERVATIVE_STACK_SCANNING
stack_ = ::heap::base::Stack(base::Stack::GetStackStart());
......
......@@ -38,7 +38,9 @@ class ThreadLocalTop {
// Does early low-level initialization that does not depend on the
// isolate being present.
ThreadLocalTop() = default;
ThreadLocalTop() { Clear(); }
void Clear();
// Initialize the thread data.
void Initialize(Isolate*);
......@@ -48,7 +50,7 @@ class ThreadLocalTop {
// This field is not guaranteed to hold an address that can be
// used for comparison with addresses into the JS stack. If such
// an address is needed, use try_catch_handler_address.
v8::TryCatch* try_catch_handler_ = nullptr;
v8::TryCatch* try_catch_handler_;
// Get the address of the top C++ try catch handler or nullptr if
// none are registered.
......@@ -92,7 +94,7 @@ class ThreadLocalTop {
void Free();
Isolate* isolate_ = nullptr;
Isolate* isolate_;
// The context where the current execution method is created and for variable
// lookups.
// TODO(3770): This field is read/written from generated code, so it would
......@@ -101,58 +103,58 @@ class ThreadLocalTop {
// meantime, assert that the memory layout is the same.
STATIC_ASSERT(sizeof(Context) == kSystemPointerSize);
Context context_;
ThreadId thread_id_ = ThreadId::Invalid();
std::atomic<ThreadId> thread_id_;
Object pending_exception_;
// Communication channel between Isolate::FindHandler and the CEntry.
Context pending_handler_context_;
Address pending_handler_entrypoint_ = kNullAddress;
Address pending_handler_constant_pool_ = kNullAddress;
Address pending_handler_fp_ = kNullAddress;
Address pending_handler_sp_ = kNullAddress;
Address pending_handler_entrypoint_;
Address pending_handler_constant_pool_;
Address pending_handler_fp_;
Address pending_handler_sp_;
Address last_api_entry_ = kNullAddress;
Address last_api_entry_;
// Communication channel between Isolate::Throw and message consumers.
Object pending_message_obj_;
bool rethrowing_message_ = false;
bool rethrowing_message_;
// Use a separate value for scheduled exceptions to preserve the
// invariants that hold about pending_exception. We may want to
// unify them later.
bool external_caught_exception_ = false;
bool external_caught_exception_;
Object scheduled_exception_;
// Stack.
// The frame pointer of the top c entry frame.
Address c_entry_fp_ = kNullAddress;
Address c_entry_fp_;
// Try-blocks are chained through the stack.
Address handler_ = kNullAddress;
Address handler_;
// C function that was called at c entry.
Address c_function_ = kNullAddress;
Address c_function_;
// Throwing an exception may cause a Promise rejection. For this purpose
// we keep track of a stack of nested promises and the corresponding
// try-catch handlers.
PromiseOnStack* promise_on_stack_ = nullptr;
PromiseOnStack* promise_on_stack_;
// Simulator field is always present to get predictable layout.
Simulator* simulator_ = nullptr;
Simulator* simulator_;
// The stack pointer of the bottom JS entry frame.
Address js_entry_sp_ = kNullAddress;
Address js_entry_sp_;
// The external callback we're currently in.
ExternalCallbackScope* external_callback_scope_ = nullptr;
StateTag current_vm_state_ = EXTERNAL;
ExternalCallbackScope* external_callback_scope_;
StateTag current_vm_state_;
// Call back function to report unsafe JS accesses.
v8::FailedAccessCheckCallback failed_access_check_callback_ = nullptr;
v8::FailedAccessCheckCallback failed_access_check_callback_;
// Address of the thread-local "thread in wasm" flag.
Address thread_in_wasm_flag_address_ = kNullAddress;
Address thread_in_wasm_flag_address_;
#ifdef V8_ENABLE_CONSERVATIVE_STACK_SCANNING
::heap::base::Stack stack_ = ::heap::base::Stack(nullptr);
::heap::base::Stack stack_;
#endif
};
......
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