Commit 9230c1f3 authored by jochen@chromium.org's avatar jochen@chromium.org

Reland^3 r22105 "Remove static initializer from isolate"

This time:
When accessing the isolate from the sampler signal handler, don't try
to take the global lock. A sampler should only be active if there is
already an isolate on that thread, so we don't need to check whether
the TLS key is already created.

BUG=none
R=dcarney@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/378513002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22235 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent d6c61b8c
...@@ -46,6 +46,7 @@ int ThreadId::AllocateThreadId() { ...@@ -46,6 +46,7 @@ int ThreadId::AllocateThreadId() {
int ThreadId::GetCurrentThreadId() { int ThreadId::GetCurrentThreadId() {
Isolate::EnsureInitialized();
int thread_id = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_); int thread_id = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_);
if (thread_id == 0) { if (thread_id == 0) {
thread_id = AllocateThreadId(); thread_id = AllocateThreadId();
...@@ -104,24 +105,17 @@ base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; ...@@ -104,24 +105,17 @@ base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
#ifdef DEBUG #ifdef DEBUG
base::Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; base::Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key;
#endif // DEBUG #endif // DEBUG
base::Mutex Isolate::process_wide_mutex_; base::LazyMutex Isolate::process_wide_mutex_ = LAZY_MUTEX_INITIALIZER;
// TODO(dcarney): Remove with default isolate.
enum DefaultIsolateStatus {
kDefaultIsolateUninitialized,
kDefaultIsolateInitialized,
kDefaultIsolateCrashIfInitialized
};
static DefaultIsolateStatus default_isolate_status_ =
kDefaultIsolateUninitialized;
Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
base::Atomic32 Isolate::isolate_counter_ = 0; base::Atomic32 Isolate::isolate_counter_ = 0;
Isolate::PerIsolateThreadData* Isolate::PerIsolateThreadData*
Isolate::FindOrAllocatePerThreadDataForThisThread() { Isolate::FindOrAllocatePerThreadDataForThisThread() {
EnsureInitialized();
ThreadId thread_id = ThreadId::Current(); ThreadId thread_id = ThreadId::Current();
PerIsolateThreadData* per_thread = NULL; PerIsolateThreadData* per_thread = NULL;
{ {
base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer());
per_thread = thread_data_table_->Lookup(this, thread_id); per_thread = thread_data_table_->Lookup(this, thread_id);
if (per_thread == NULL) { if (per_thread == NULL) {
per_thread = new PerIsolateThreadData(this, thread_id); per_thread = new PerIsolateThreadData(this, thread_id);
...@@ -141,25 +135,18 @@ Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() { ...@@ -141,25 +135,18 @@ Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() {
Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThread( Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThread(
ThreadId thread_id) { ThreadId thread_id) {
EnsureInitialized();
PerIsolateThreadData* per_thread = NULL; PerIsolateThreadData* per_thread = NULL;
{ {
base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer());
per_thread = thread_data_table_->Lookup(this, thread_id); per_thread = thread_data_table_->Lookup(this, thread_id);
} }
return per_thread; return per_thread;
} }
void Isolate::SetCrashIfDefaultIsolateInitialized() { void Isolate::EnsureInitialized() {
base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer());
CHECK(default_isolate_status_ != kDefaultIsolateInitialized);
default_isolate_status_ = kDefaultIsolateCrashIfInitialized;
}
void Isolate::EnsureDefaultIsolate() {
base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_);
CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized);
if (thread_data_table_ == NULL) { if (thread_data_table_ == NULL) {
isolate_key_ = base::Thread::CreateThreadLocalKey(); isolate_key_ = base::Thread::CreateThreadLocalKey();
thread_id_key_ = base::Thread::CreateThreadLocalKey(); thread_id_key_ = base::Thread::CreateThreadLocalKey();
...@@ -172,10 +159,6 @@ void Isolate::EnsureDefaultIsolate() { ...@@ -172,10 +159,6 @@ void Isolate::EnsureDefaultIsolate() {
} }
} }
struct StaticInitializer {
StaticInitializer() { Isolate::EnsureDefaultIsolate(); }
} static_initializer;
Address Isolate::get_address_from_id(Isolate::AddressId id) { Address Isolate::get_address_from_id(Isolate::AddressId id) {
return isolate_addresses_[id]; return isolate_addresses_[id];
...@@ -1533,7 +1516,7 @@ void Isolate::TearDown() { ...@@ -1533,7 +1516,7 @@ void Isolate::TearDown() {
Deinit(); Deinit();
{ {
base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer());
thread_data_table_->RemoveAllThreads(this); thread_data_table_->RemoveAllThreads(this);
} }
...@@ -1634,6 +1617,7 @@ void Isolate::PushToPartialSnapshotCache(Object* obj) { ...@@ -1634,6 +1617,7 @@ void Isolate::PushToPartialSnapshotCache(Object* obj) {
void Isolate::SetIsolateThreadLocals(Isolate* isolate, void Isolate::SetIsolateThreadLocals(Isolate* isolate,
PerIsolateThreadData* data) { PerIsolateThreadData* data) {
EnsureInitialized();
base::Thread::SetThreadLocal(isolate_key_, isolate); base::Thread::SetThreadLocal(isolate_key_, isolate);
base::Thread::SetThreadLocal(per_isolate_thread_data_key_, data); base::Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
} }
......
...@@ -447,12 +447,14 @@ class Isolate { ...@@ -447,12 +447,14 @@ class Isolate {
// Returns the PerIsolateThreadData for the current thread (or NULL if one is // Returns the PerIsolateThreadData for the current thread (or NULL if one is
// not currently set). // not currently set).
static PerIsolateThreadData* CurrentPerIsolateThreadData() { static PerIsolateThreadData* CurrentPerIsolateThreadData() {
EnsureInitialized();
return reinterpret_cast<PerIsolateThreadData*>( return reinterpret_cast<PerIsolateThreadData*>(
base::Thread::GetThreadLocal(per_isolate_thread_data_key_)); base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
} }
// Returns the isolate inside which the current thread is running. // Returns the isolate inside which the current thread is running.
INLINE(static Isolate* Current()) { INLINE(static Isolate* Current()) {
EnsureInitialized();
Isolate* isolate = reinterpret_cast<Isolate*>( Isolate* isolate = reinterpret_cast<Isolate*>(
base::Thread::GetExistingThreadLocal(isolate_key_)); base::Thread::GetExistingThreadLocal(isolate_key_));
ASSERT(isolate != NULL); ASSERT(isolate != NULL);
...@@ -460,6 +462,14 @@ class Isolate { ...@@ -460,6 +462,14 @@ class Isolate {
} }
INLINE(static Isolate* UncheckedCurrent()) { INLINE(static Isolate* UncheckedCurrent()) {
EnsureInitialized();
return reinterpret_cast<Isolate*>(
base::Thread::GetThreadLocal(isolate_key_));
}
// Like UncheckedCurrent, but skips the check that |isolate_key_| was
// initialized. Callers have to ensure that themselves.
INLINE(static Isolate* UnsafeCurrent()) {
return reinterpret_cast<Isolate*>( return reinterpret_cast<Isolate*>(
base::Thread::GetThreadLocal(isolate_key_)); base::Thread::GetThreadLocal(isolate_key_));
} }
...@@ -485,13 +495,6 @@ class Isolate { ...@@ -485,13 +495,6 @@ class Isolate {
static void GlobalTearDown(); static void GlobalTearDown();
static void SetCrashIfDefaultIsolateInitialized();
// Ensures that process-wide resources and the default isolate have been
// allocated. It is only necessary to call this method in rare cases, for
// example if you are using V8 from within the body of a static initializer.
// Safe to call multiple times.
static void EnsureDefaultIsolate();
// Find the PerThread for this particular (isolate, thread) combination // Find the PerThread for this particular (isolate, thread) combination
// If one does not yet exist, return null. // If one does not yet exist, return null.
PerIsolateThreadData* FindPerThreadDataForThisThread(); PerIsolateThreadData* FindPerThreadDataForThisThread();
...@@ -504,11 +507,13 @@ class Isolate { ...@@ -504,11 +507,13 @@ class Isolate {
// Used internally for V8 threads that do not execute JavaScript but still // Used internally for V8 threads that do not execute JavaScript but still
// are part of the domain of an isolate (like the context switcher). // are part of the domain of an isolate (like the context switcher).
static base::Thread::LocalStorageKey isolate_key() { static base::Thread::LocalStorageKey isolate_key() {
EnsureInitialized();
return isolate_key_; return isolate_key_;
} }
// Returns the key used to store process-wide thread IDs. // Returns the key used to store process-wide thread IDs.
static base::Thread::LocalStorageKey thread_id_key() { static base::Thread::LocalStorageKey thread_id_key() {
EnsureInitialized();
return thread_id_key_; return thread_id_key_;
} }
...@@ -1089,6 +1094,8 @@ class Isolate { ...@@ -1089,6 +1094,8 @@ class Isolate {
void CountUsage(v8::Isolate::UseCounterFeature feature); void CountUsage(v8::Isolate::UseCounterFeature feature);
private: private:
static void EnsureInitialized();
Isolate(); Isolate();
friend struct GlobalState; friend struct GlobalState;
...@@ -1148,7 +1155,7 @@ class Isolate { ...@@ -1148,7 +1155,7 @@ class Isolate {
}; };
// This mutex protects highest_thread_id_ and thread_data_table_. // This mutex protects highest_thread_id_ and thread_data_table_.
static base::Mutex process_wide_mutex_; static base::LazyMutex process_wide_mutex_;
static base::Thread::LocalStorageKey per_isolate_thread_data_key_; static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
static base::Thread::LocalStorageKey isolate_key_; static base::Thread::LocalStorageKey isolate_key_;
......
...@@ -331,7 +331,7 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, ...@@ -331,7 +331,7 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
#else #else
USE(info); USE(info);
if (signal != SIGPROF) return; if (signal != SIGPROF) return;
Isolate* isolate = Isolate::UncheckedCurrent(); Isolate* isolate = Isolate::UnsafeCurrent();
if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) {
// We require a fully initialized and entered isolate. // We require a fully initialized and entered isolate.
return; return;
......
...@@ -32,8 +32,7 @@ ...@@ -32,8 +32,7 @@
# Allow: # Allow:
# - _GLOBAL__I__ZN2v810LineEditor6first_E # - _GLOBAL__I__ZN2v810LineEditor6first_E
# - _GLOBAL__I__ZN2v88internal32AtomicOps_Internalx86CPUFeaturesE # - _GLOBAL__I__ZN2v88internal32AtomicOps_Internalx86CPUFeaturesE
# - _GLOBAL__I__ZN2v88internal8ThreadId18highest_thread_id_E expected_static_init_count=2
expected_static_init_count=3
v8_root=$(readlink -f $(dirname $BASH_SOURCE)/../) v8_root=$(readlink -f $(dirname $BASH_SOURCE)/../)
......
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