Commit f4c120d6 authored by dslomov@chromium.org's avatar dslomov@chromium.org

This implements per-isolate locking and unlocking, including tests

BUG=
TEST=

Review URL: http://codereview.chromium.org/6788023

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7734 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 22fdd1cd
...@@ -3355,26 +3355,20 @@ class V8EXPORT Context { ...@@ -3355,26 +3355,20 @@ class V8EXPORT Context {
* to the user of V8 to ensure (perhaps with locking) that this * to the user of V8 to ensure (perhaps with locking) that this
* constraint is not violated. * constraint is not violated.
* *
* More then one thread and multiple V8 isolates can be used * v8::Locker is a scoped lock object. While it's
* without any locking if each isolate is created and accessed * active (i.e. between its construction and destruction) the current thread is
* by a single thread only. For example, one thread can use * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
* multiple isolates or multiple threads can each create and run * by at most one thread at any time. In other words, the scope of a v8::Locker is
* their own isolate. * a critical section.
* *
* If you wish to start using V8 isolate in more then one thread * Sample usage:
* you can do this by constructing a v8::Locker object to guard * \code
* access to the isolate. After the code using V8 has completed
* for the current thread you can call the destructor. This can
* be combined with C++ scope-based construction as follows
* (assumes the default isolate that is used if not specified as
* a parameter for the Locker):
*
* \code
* ... * ...
* { * {
* v8::Locker locker; * v8::Locker locker(isolate);
* v8::Isolate::Scope isolate_scope(isolate);
* ... * ...
* // Code using V8 goes here. * // Code using V8 and isolate goes here.
* ... * ...
* } // Destructor called here * } // Destructor called here
* \endcode * \endcode
...@@ -3385,11 +3379,13 @@ class V8EXPORT Context { ...@@ -3385,11 +3379,13 @@ class V8EXPORT Context {
* *
* \code * \code
* { * {
* v8::Unlocker unlocker; * isolate->Exit();
* v8::Unlocker unlocker(isolate);
* ... * ...
* // Code not using V8 goes here while V8 can run in another thread. * // Code not using V8 goes here while V8 can run in another thread.
* ... * ...
* } // Destructor called here. * } // Destructor called here.
* isolate->Enter();
* \endcode * \endcode
* *
* The Unlocker object is intended for use in a long-running callback * The Unlocker object is intended for use in a long-running callback
...@@ -3409,32 +3405,45 @@ class V8EXPORT Context { ...@@ -3409,32 +3405,45 @@ class V8EXPORT Context {
* \code * \code
* // V8 not locked. * // V8 not locked.
* { * {
* v8::Locker locker; * v8::Locker locker(isolate);
* Isolate::Scope isolate_scope(isolate);
* // V8 locked. * // V8 locked.
* { * {
* v8::Locker another_locker; * v8::Locker another_locker(isolate);
* // V8 still locked (2 levels). * // V8 still locked (2 levels).
* { * {
* v8::Unlocker unlocker; * isolate->Exit();
* v8::Unlocker unlocker(isolate);
* // V8 not locked. * // V8 not locked.
* } * }
* isolate->Enter();
* // V8 locked again (2 levels). * // V8 locked again (2 levels).
* } * }
* // V8 still locked (1 level). * // V8 still locked (1 level).
* } * }
* // V8 Now no longer locked. * // V8 Now no longer locked.
* \endcode * \endcode
*
*
*/ */
class V8EXPORT Unlocker { class V8EXPORT Unlocker {
public: public:
Unlocker(); /**
* Initialize Unlocker for a given Isolate. NULL means default isolate.
*/
explicit Unlocker(Isolate* isolate = NULL);
~Unlocker(); ~Unlocker();
private:
internal::Isolate* isolate_;
}; };
class V8EXPORT Locker { class V8EXPORT Locker {
public: public:
Locker(); /**
* Initialize Locker for a given Isolate. NULL means default isolate.
*/
explicit Locker(Isolate* isolate = NULL);
~Locker(); ~Locker();
/** /**
...@@ -3452,9 +3461,10 @@ class V8EXPORT Locker { ...@@ -3452,9 +3461,10 @@ class V8EXPORT Locker {
static void StopPreemption(); static void StopPreemption();
/** /**
* Returns whether or not the locker is locked by the current thread. * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
* is locked by the current thread.
*/ */
static bool IsLocked(); static bool IsLocked(Isolate* isolate = NULL);
/** /**
* Returns whether v8::Locker is being used by this V8 instance. * Returns whether v8::Locker is being used by this V8 instance.
...@@ -3464,6 +3474,7 @@ class V8EXPORT Locker { ...@@ -3464,6 +3474,7 @@ class V8EXPORT Locker {
private: private:
bool has_lock_; bool has_lock_;
bool top_level_; bool top_level_;
internal::Isolate* isolate_;
static bool active_; static bool active_;
......
...@@ -53,7 +53,6 @@ ...@@ -53,7 +53,6 @@
#define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr)) #define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr))
// TODO(isolates): avoid repeated TLS reads in function prologues.
#ifdef ENABLE_VMSTATE_TRACKING #ifdef ENABLE_VMSTATE_TRACKING
#define ENTER_V8(isolate) \ #define ENTER_V8(isolate) \
ASSERT((isolate)->IsInitialized()); \ ASSERT((isolate)->IsInitialized()); \
...@@ -290,6 +289,7 @@ static inline bool EnsureInitializedForIsolate(i::Isolate* isolate, ...@@ -290,6 +289,7 @@ static inline bool EnsureInitializedForIsolate(i::Isolate* isolate,
if (isolate != NULL) { if (isolate != NULL) {
if (isolate->IsInitialized()) return true; if (isolate->IsInitialized()) return true;
} }
ASSERT(isolate == i::Isolate::Current());
return ApiCheck(InitializeHelper(), location, "Error initializing V8"); return ApiCheck(InitializeHelper(), location, "Error initializing V8");
} }
...@@ -5687,9 +5687,8 @@ void HandleScopeImplementer::FreeThreadResources() { ...@@ -5687,9 +5687,8 @@ void HandleScopeImplementer::FreeThreadResources() {
char* HandleScopeImplementer::ArchiveThread(char* storage) { char* HandleScopeImplementer::ArchiveThread(char* storage) {
Isolate* isolate = Isolate::Current();
v8::ImplementationUtilities::HandleScopeData* current = v8::ImplementationUtilities::HandleScopeData* current =
isolate->handle_scope_data(); isolate_->handle_scope_data();
handle_scope_data_ = *current; handle_scope_data_ = *current;
memcpy(storage, this, sizeof(*this)); memcpy(storage, this, sizeof(*this));
...@@ -5707,7 +5706,7 @@ int HandleScopeImplementer::ArchiveSpacePerThread() { ...@@ -5707,7 +5706,7 @@ int HandleScopeImplementer::ArchiveSpacePerThread() {
char* HandleScopeImplementer::RestoreThread(char* storage) { char* HandleScopeImplementer::RestoreThread(char* storage) {
memcpy(this, storage, sizeof(*this)); memcpy(this, storage, sizeof(*this));
*Isolate::Current()->handle_scope_data() = handle_scope_data_; *isolate_->handle_scope_data() = handle_scope_data_;
return storage + ArchiveSpacePerThread(); return storage + ArchiveSpacePerThread();
} }
...@@ -5733,7 +5732,7 @@ void HandleScopeImplementer::IterateThis(ObjectVisitor* v) { ...@@ -5733,7 +5732,7 @@ void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
void HandleScopeImplementer::Iterate(ObjectVisitor* v) { void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
v8::ImplementationUtilities::HandleScopeData* current = v8::ImplementationUtilities::HandleScopeData* current =
Isolate::Current()->handle_scope_data(); isolate_->handle_scope_data();
handle_scope_data_ = *current; handle_scope_data_ = *current;
IterateThis(v); IterateThis(v);
} }
......
...@@ -399,8 +399,9 @@ class StringTracker { ...@@ -399,8 +399,9 @@ class StringTracker {
ISOLATED_CLASS HandleScopeImplementer { ISOLATED_CLASS HandleScopeImplementer {
public: public:
HandleScopeImplementer() explicit HandleScopeImplementer(Isolate* isolate)
: blocks_(0), : isolate_(isolate),
blocks_(0),
entered_contexts_(0), entered_contexts_(0),
saved_contexts_(0), saved_contexts_(0),
spare_(NULL), spare_(NULL),
...@@ -466,6 +467,7 @@ ISOLATED_CLASS HandleScopeImplementer { ...@@ -466,6 +467,7 @@ ISOLATED_CLASS HandleScopeImplementer {
ASSERT(call_depth_ == 0); ASSERT(call_depth_ == 0);
} }
Isolate* isolate_;
List<internal::Object**> blocks_; List<internal::Object**> blocks_;
// Used as a stack to keep track of entered contexts. // Used as a stack to keep track of entered contexts.
List<Handle<Object> > entered_contexts_; List<Handle<Object> > entered_contexts_;
......
...@@ -457,8 +457,9 @@ void StackGuard::ClearThread(const ExecutionAccess& lock) { ...@@ -457,8 +457,9 @@ void StackGuard::ClearThread(const ExecutionAccess& lock) {
void StackGuard::InitThread(const ExecutionAccess& lock) { void StackGuard::InitThread(const ExecutionAccess& lock) {
if (thread_local_.Initialize()) isolate_->heap()->SetStackLimits(); if (thread_local_.Initialize()) isolate_->heap()->SetStackLimits();
uintptr_t stored_limit = Isolate::PerIsolateThreadData* per_thread =
Isolate::CurrentPerIsolateThreadData()->stack_limit(); isolate_->FindOrAllocatePerThreadDataForThisThread();
uintptr_t stored_limit = per_thread->stack_limit();
// You should hold the ExecutionAccess lock when you call this. // You should hold the ExecutionAccess lock when you call this.
if (stored_limit != 0) { if (stored_limit != 0) {
StackGuard::SetStackLimit(stored_limit); StackGuard::SetStackLimit(stored_limit);
......
...@@ -311,6 +311,17 @@ Isolate::PerIsolateThreadData* ...@@ -311,6 +311,17 @@ Isolate::PerIsolateThreadData*
} }
Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() {
ThreadId thread_id = ThreadId::Current();
PerIsolateThreadData* per_thread = NULL;
{
ScopedLock lock(process_wide_mutex_);
per_thread = thread_data_table_->Lookup(this, thread_id);
}
return per_thread;
}
void Isolate::EnsureDefaultIsolate() { void Isolate::EnsureDefaultIsolate() {
ScopedLock lock(process_wide_mutex_); ScopedLock lock(process_wide_mutex_);
if (default_isolate_ == NULL) { if (default_isolate_ == NULL) {
...@@ -322,7 +333,9 @@ void Isolate::EnsureDefaultIsolate() { ...@@ -322,7 +333,9 @@ void Isolate::EnsureDefaultIsolate() {
} }
// Can't use SetIsolateThreadLocals(default_isolate_, NULL) here // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here
// becase a non-null thread data may be already set. // becase a non-null thread data may be already set.
if (Thread::GetThreadLocal(isolate_key_) == NULL) {
Thread::SetThreadLocal(isolate_key_, default_isolate_); Thread::SetThreadLocal(isolate_key_, default_isolate_);
}
CHECK(default_isolate_->PreInit()); CHECK(default_isolate_->PreInit());
} }
...@@ -458,6 +471,11 @@ Isolate::Isolate() ...@@ -458,6 +471,11 @@ Isolate::Isolate()
zone_.isolate_ = this; zone_.isolate_ = this;
stack_guard_.isolate_ = this; stack_guard_.isolate_ = this;
// ThreadManager is initialized early to support locking an isolate
// before it is entered.
thread_manager_ = new ThreadManager();
thread_manager_->isolate_ = this;
#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \ #if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__) defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
simulator_initialized_ = false; simulator_initialized_ = false;
...@@ -643,7 +661,6 @@ bool Isolate::PreInit() { ...@@ -643,7 +661,6 @@ bool Isolate::PreInit() {
TRACE_ISOLATE(preinit); TRACE_ISOLATE(preinit);
ASSERT(Isolate::Current() == this); ASSERT(Isolate::Current() == this);
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
debug_ = new Debug(this); debug_ = new Debug(this);
debugger_ = new Debugger(); debugger_ = new Debugger();
...@@ -671,8 +688,6 @@ bool Isolate::PreInit() { ...@@ -671,8 +688,6 @@ bool Isolate::PreInit() {
string_tracker_ = new StringTracker(); string_tracker_ = new StringTracker();
string_tracker_->isolate_ = this; string_tracker_->isolate_ = this;
thread_manager_ = new ThreadManager();
thread_manager_->isolate_ = this;
compilation_cache_ = new CompilationCache(this); compilation_cache_ = new CompilationCache(this);
transcendental_cache_ = new TranscendentalCache(); transcendental_cache_ = new TranscendentalCache();
keyed_lookup_cache_ = new KeyedLookupCache(); keyed_lookup_cache_ = new KeyedLookupCache();
...@@ -683,7 +698,7 @@ bool Isolate::PreInit() { ...@@ -683,7 +698,7 @@ bool Isolate::PreInit() {
write_input_buffer_ = new StringInputBuffer(); write_input_buffer_ = new StringInputBuffer();
global_handles_ = new GlobalHandles(this); global_handles_ = new GlobalHandles(this);
bootstrapper_ = new Bootstrapper(); bootstrapper_ = new Bootstrapper();
handle_scope_implementer_ = new HandleScopeImplementer(); handle_scope_implementer_ = new HandleScopeImplementer(this);
stub_cache_ = new StubCache(this); stub_cache_ = new StubCache(this);
ast_sentinels_ = new AstSentinels(); ast_sentinels_ = new AstSentinels();
regexp_stack_ = new RegExpStack(); regexp_stack_ = new RegExpStack();
...@@ -700,6 +715,7 @@ bool Isolate::PreInit() { ...@@ -700,6 +715,7 @@ bool Isolate::PreInit() {
void Isolate::InitializeThreadLocal() { void Isolate::InitializeThreadLocal() {
thread_local_top_.isolate_ = this;
thread_local_top_.Initialize(); thread_local_top_.Initialize();
clear_pending_exception(); clear_pending_exception();
clear_pending_message(); clear_pending_message();
......
...@@ -224,6 +224,7 @@ class ThreadLocalTop BASE_EMBEDDED { ...@@ -224,6 +224,7 @@ class ThreadLocalTop BASE_EMBEDDED {
ASSERT(try_catch_handler_address_ == NULL); ASSERT(try_catch_handler_address_ == NULL);
} }
Isolate* isolate_;
// The context where the current execution method is created and for variable // The context where the current execution method is created and for variable
// lookups. // lookups.
Context* context_; Context* context_;
...@@ -486,6 +487,10 @@ class Isolate { ...@@ -486,6 +487,10 @@ class Isolate {
// Safe to call multiple times. // Safe to call multiple times.
static void EnsureDefaultIsolate(); static void EnsureDefaultIsolate();
// Find the PerThread for this particular (isolate, thread) combination
// If one does not yet exist, return null.
PerIsolateThreadData* FindPerThreadDataForThisThread();
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
// Get the debugger from the default isolate. Preinitializes the // Get the debugger from the default isolate. Preinitializes the
// default isolate if needed. // default isolate if needed.
...@@ -1065,7 +1070,7 @@ class Isolate { ...@@ -1065,7 +1070,7 @@ class Isolate {
// If one does not yet exist, allocate a new one. // If one does not yet exist, allocate a new one.
PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread(); PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
// PreInits and returns a default isolate. Needed when a new thread tries // PreInits and returns a default isolate. Needed when a new thread tries
// to create a Locker for the first time (the lock itself is in the isolate). // to create a Locker for the first time (the lock itself is in the isolate).
static Isolate* GetDefaultIsolateForLocking(); static Isolate* GetDefaultIsolateForLocking();
...@@ -1197,9 +1202,12 @@ class Isolate { ...@@ -1197,9 +1202,12 @@ class Isolate {
friend class ExecutionAccess; friend class ExecutionAccess;
friend class IsolateInitializer; friend class IsolateInitializer;
friend class ThreadManager;
friend class StackGuard;
friend class ThreadId; friend class ThreadId;
friend class v8::Isolate; friend class v8::Isolate;
friend class v8::Locker; friend class v8::Locker;
friend class v8::Unlocker;
DISALLOW_COPY_AND_ASSIGN(Isolate); DISALLOW_COPY_AND_ASSIGN(Isolate);
}; };
......
...@@ -4958,8 +4958,7 @@ int Relocatable::ArchiveSpacePerThread() { ...@@ -4958,8 +4958,7 @@ int Relocatable::ArchiveSpacePerThread() {
// Archive statics that are thread local. // Archive statics that are thread local.
char* Relocatable::ArchiveState(char* to) { char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
Isolate* isolate = Isolate::Current();
*reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
isolate->set_relocatable_top(NULL); isolate->set_relocatable_top(NULL);
return to + ArchiveSpacePerThread(); return to + ArchiveSpacePerThread();
...@@ -4967,8 +4966,7 @@ char* Relocatable::ArchiveState(char* to) { ...@@ -4967,8 +4966,7 @@ char* Relocatable::ArchiveState(char* to) {
// Restore statics that are thread local. // Restore statics that are thread local.
char* Relocatable::RestoreState(char* from) { char* Relocatable::RestoreState(Isolate* isolate, char* from) {
Isolate* isolate = Isolate::Current();
isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
return from + ArchiveSpacePerThread(); return from + ArchiveSpacePerThread();
} }
......
...@@ -5924,8 +5924,8 @@ class Relocatable BASE_EMBEDDED { ...@@ -5924,8 +5924,8 @@ class Relocatable BASE_EMBEDDED {
static void PostGarbageCollectionProcessing(); static void PostGarbageCollectionProcessing();
static int ArchiveSpacePerThread(); static int ArchiveSpacePerThread();
static char* ArchiveState(char* to); static char* ArchiveState(Isolate* isolate, char* to);
static char* RestoreState(char* from); static char* RestoreState(Isolate* isolate, char* from);
static void Iterate(ObjectVisitor* v); static void Iterate(ObjectVisitor* v);
static void Iterate(ObjectVisitor* v, Relocatable* top); static void Iterate(ObjectVisitor* v, Relocatable* top);
static char* Iterate(ObjectVisitor* v, char* t); static char* Iterate(ObjectVisitor* v, char* t);
......
...@@ -77,9 +77,9 @@ void ThreadLocalTop::Initialize() { ...@@ -77,9 +77,9 @@ void ThreadLocalTop::Initialize() {
InitializeInternal(); InitializeInternal();
#ifdef USE_SIMULATOR #ifdef USE_SIMULATOR
#ifdef V8_TARGET_ARCH_ARM #ifdef V8_TARGET_ARCH_ARM
simulator_ = Simulator::current(Isolate::Current()); simulator_ = Simulator::current(isolate_);
#elif V8_TARGET_ARCH_MIPS #elif V8_TARGET_ARCH_MIPS
simulator_ = Simulator::current(Isolate::Current()); simulator_ = Simulator::current(isolate_);
#endif #endif
#endif #endif
thread_id_ = ThreadId::Current(); thread_id_ = ThreadId::Current();
......
...@@ -43,90 +43,85 @@ bool Locker::active_ = false; ...@@ -43,90 +43,85 @@ bool Locker::active_ = false;
// Constructor for the Locker object. Once the Locker is constructed the // Constructor for the Locker object. Once the Locker is constructed the
// current thread will be guaranteed to have the big V8 lock. // current thread will be guaranteed to have the lock for a given isolate.
Locker::Locker() : has_lock_(false), top_level_(true) { Locker::Locker(v8::Isolate* isolate)
// TODO(isolates): When Locker has Isolate parameter and it is provided, grab : has_lock_(false),
// that one instead of using the current one. top_level_(false),
// We pull default isolate for Locker constructor w/o p[arameter. isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
// A thread should not enter an isolate before acquiring a lock, if (isolate_ == NULL) {
// in cases which mandate using Lockers. isolate_ = i::Isolate::GetDefaultIsolateForLocking();
// So getting a lock is the first thing threads do in a scenario where }
// multple threads share an isolate. Hence, we need to access
// 'locking isolate' before we can actually enter into default isolate.
internal::Isolate* isolate = internal::Isolate::GetDefaultIsolateForLocking();
ASSERT(isolate != NULL);
// Record that the Locker has been used at least once. // Record that the Locker has been used at least once.
active_ = true; active_ = true;
// Get the big lock if necessary. // Get the big lock if necessary.
if (!isolate->thread_manager()->IsLockedByCurrentThread()) { if (!isolate_->thread_manager()->IsLockedByCurrentThread()) {
isolate->thread_manager()->Lock(); isolate_->thread_manager()->Lock();
has_lock_ = true; has_lock_ = true;
if (isolate->IsDefaultIsolate()) {
// This only enters if not yet entered.
internal::Isolate::EnterDefaultIsolate();
}
ASSERT(internal::Thread::HasThreadLocal(
internal::Isolate::thread_id_key()));
// Make sure that V8 is initialized. Archiving of threads interferes // Make sure that V8 is initialized. Archiving of threads interferes
// with deserialization by adding additional root pointers, so we must // with deserialization by adding additional root pointers, so we must
// initialize here, before anyone can call ~Locker() or Unlocker(). // initialize here, before anyone can call ~Locker() or Unlocker().
if (!isolate->IsInitialized()) { if (isolate_->IsDefaultIsolate()) {
// This only enters if not yet entered.
internal::Isolate::EnterDefaultIsolate();
} else if (!isolate_->IsInitialized()) {
isolate_->Enter();
V8::Initialize(); V8::Initialize();
isolate_->Exit();
} }
// This may be a locker within an unlocker in which case we have to // This may be a locker within an unlocker in which case we have to
// get the saved state for this thread and restore it. // get the saved state for this thread and restore it.
if (isolate->thread_manager()->RestoreThread()) { if (isolate_->thread_manager()->RestoreThread()) {
top_level_ = false; top_level_ = false;
} else { } else {
internal::ExecutionAccess access(isolate); internal::ExecutionAccess access(isolate_);
isolate->stack_guard()->ClearThread(access); isolate_->stack_guard()->ClearThread(access);
isolate->stack_guard()->InitThread(access); isolate_->stack_guard()->InitThread(access);
} }
} }
ASSERT(isolate->thread_manager()->IsLockedByCurrentThread()); ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
} }
bool Locker::IsLocked() { bool Locker::IsLocked(v8::Isolate* isolate) {
return internal::Isolate::Current()->thread_manager()-> i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
IsLockedByCurrentThread(); if (internal_isolate == NULL) {
internal_isolate = i::Isolate::GetDefaultIsolateForLocking();
}
return internal_isolate->thread_manager()->IsLockedByCurrentThread();
} }
Locker::~Locker() { Locker::~Locker() {
// TODO(isolate): this should use a field storing the isolate it ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
// locked instead.
internal::Isolate* isolate = internal::Isolate::Current();
ASSERT(isolate->thread_manager()->IsLockedByCurrentThread());
if (has_lock_) { if (has_lock_) {
if (top_level_) { if (top_level_) {
isolate->thread_manager()->FreeThreadResources(); isolate_->thread_manager()->FreeThreadResources();
} else { } else {
isolate->thread_manager()->ArchiveThread(); isolate_->thread_manager()->ArchiveThread();
} }
isolate->thread_manager()->Unlock(); isolate_->thread_manager()->Unlock();
} }
} }
Unlocker::Unlocker() { Unlocker::Unlocker(v8::Isolate* isolate)
internal::Isolate* isolate = internal::Isolate::Current(); : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
ASSERT(isolate->thread_manager()->IsLockedByCurrentThread()); if (isolate_ == NULL) {
isolate->thread_manager()->ArchiveThread(); isolate_ = i::Isolate::GetDefaultIsolateForLocking();
isolate->thread_manager()->Unlock(); }
ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
isolate_->thread_manager()->ArchiveThread();
isolate_->thread_manager()->Unlock();
} }
Unlocker::~Unlocker() { Unlocker::~Unlocker() {
// TODO(isolates): check it's the isolate we unlocked. ASSERT(!isolate_->thread_manager()->IsLockedByCurrentThread());
internal::Isolate* isolate = internal::Isolate::Current(); isolate_->thread_manager()->Lock();
ASSERT(!isolate->thread_manager()->IsLockedByCurrentThread()); isolate_->thread_manager()->RestoreThread();
isolate->thread_manager()->Lock();
isolate->thread_manager()->RestoreThread();
} }
...@@ -144,17 +139,20 @@ namespace internal { ...@@ -144,17 +139,20 @@ namespace internal {
bool ThreadManager::RestoreThread() { bool ThreadManager::RestoreThread() {
ASSERT(IsLockedByCurrentThread());
// First check whether the current thread has been 'lazily archived', ie // First check whether the current thread has been 'lazily archived', ie
// not archived at all. If that is the case we put the state storage we // not archived at all. If that is the case we put the state storage we
// had prepared back in the free list, since we didn't need it after all. // had prepared back in the free list, since we didn't need it after all.
if (lazily_archived_thread_.Equals(ThreadId::Current())) { if (lazily_archived_thread_.Equals(ThreadId::Current())) {
lazily_archived_thread_ = ThreadId::Invalid(); lazily_archived_thread_ = ThreadId::Invalid();
ASSERT(Isolate::CurrentPerIsolateThreadData()->thread_state() == Isolate::PerIsolateThreadData* per_thread =
lazily_archived_thread_state_); isolate_->FindPerThreadDataForThisThread();
ASSERT(per_thread != NULL);
ASSERT(per_thread->thread_state() == lazily_archived_thread_state_);
lazily_archived_thread_state_->set_id(ThreadId::Invalid()); lazily_archived_thread_state_->set_id(ThreadId::Invalid());
lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST); lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST);
lazily_archived_thread_state_ = NULL; lazily_archived_thread_state_ = NULL;
Isolate::CurrentPerIsolateThreadData()->set_thread_state(NULL); per_thread->set_thread_state(NULL);
return true; return true;
} }
...@@ -168,7 +166,7 @@ bool ThreadManager::RestoreThread() { ...@@ -168,7 +166,7 @@ bool ThreadManager::RestoreThread() {
EagerlyArchiveThread(); EagerlyArchiveThread();
} }
Isolate::PerIsolateThreadData* per_thread = Isolate::PerIsolateThreadData* per_thread =
Isolate::CurrentPerIsolateThreadData(); isolate_->FindPerThreadDataForThisThread();
if (per_thread == NULL || per_thread->thread_state() == NULL) { if (per_thread == NULL || per_thread->thread_state() == NULL) {
// This is a new thread. // This is a new thread.
isolate_->stack_guard()->InitThread(access); isolate_->stack_guard()->InitThread(access);
...@@ -178,7 +176,7 @@ bool ThreadManager::RestoreThread() { ...@@ -178,7 +176,7 @@ bool ThreadManager::RestoreThread() {
char* from = state->data(); char* from = state->data();
from = isolate_->handle_scope_implementer()->RestoreThread(from); from = isolate_->handle_scope_implementer()->RestoreThread(from);
from = isolate_->RestoreThread(from); from = isolate_->RestoreThread(from);
from = Relocatable::RestoreState(from); from = Relocatable::RestoreState(isolate_, from);
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
from = isolate_->debug()->RestoreDebug(from); from = isolate_->debug()->RestoreDebug(from);
#endif #endif
...@@ -300,9 +298,12 @@ ThreadManager::~ThreadManager() { ...@@ -300,9 +298,12 @@ ThreadManager::~ThreadManager() {
void ThreadManager::ArchiveThread() { void ThreadManager::ArchiveThread() {
ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid())); ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid()));
ASSERT(!IsArchived()); ASSERT(!IsArchived());
ASSERT(IsLockedByCurrentThread());
ThreadState* state = GetFreeThreadState(); ThreadState* state = GetFreeThreadState();
state->Unlink(); state->Unlink();
Isolate::CurrentPerIsolateThreadData()->set_thread_state(state); Isolate::PerIsolateThreadData* per_thread =
isolate_->FindOrAllocatePerThreadDataForThisThread();
per_thread->set_thread_state(state);
lazily_archived_thread_ = ThreadId::Current(); lazily_archived_thread_ = ThreadId::Current();
lazily_archived_thread_state_ = state; lazily_archived_thread_state_ = state;
ASSERT(state->id().Equals(ThreadId::Invalid())); ASSERT(state->id().Equals(ThreadId::Invalid()));
...@@ -312,6 +313,7 @@ void ThreadManager::ArchiveThread() { ...@@ -312,6 +313,7 @@ void ThreadManager::ArchiveThread() {
void ThreadManager::EagerlyArchiveThread() { void ThreadManager::EagerlyArchiveThread() {
ASSERT(IsLockedByCurrentThread());
ThreadState* state = lazily_archived_thread_state_; ThreadState* state = lazily_archived_thread_state_;
state->LinkInto(ThreadState::IN_USE_LIST); state->LinkInto(ThreadState::IN_USE_LIST);
char* to = state->data(); char* to = state->data();
...@@ -319,7 +321,7 @@ void ThreadManager::EagerlyArchiveThread() { ...@@ -319,7 +321,7 @@ void ThreadManager::EagerlyArchiveThread() {
// in ThreadManager::Iterate(ObjectVisitor*). // in ThreadManager::Iterate(ObjectVisitor*).
to = isolate_->handle_scope_implementer()->ArchiveThread(to); to = isolate_->handle_scope_implementer()->ArchiveThread(to);
to = isolate_->ArchiveThread(to); to = isolate_->ArchiveThread(to);
to = Relocatable::ArchiveState(to); to = Relocatable::ArchiveState(isolate_, to);
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
to = isolate_->debug()->ArchiveDebug(to); to = isolate_->debug()->ArchiveDebug(to);
#endif #endif
...@@ -344,11 +346,11 @@ void ThreadManager::FreeThreadResources() { ...@@ -344,11 +346,11 @@ void ThreadManager::FreeThreadResources() {
bool ThreadManager::IsArchived() { bool ThreadManager::IsArchived() {
Isolate::PerIsolateThreadData* data = Isolate::CurrentPerIsolateThreadData(); Isolate::PerIsolateThreadData* data =
isolate_->FindPerThreadDataForThisThread();
return data != NULL && data->thread_state() != NULL; return data != NULL && data->thread_state() != NULL;
} }
void ThreadManager::Iterate(ObjectVisitor* v) { void ThreadManager::Iterate(ObjectVisitor* v) {
// Expecting no threads during serialization/deserialization // Expecting no threads during serialization/deserialization
for (ThreadState* state = FirstThreadStateInUse(); for (ThreadState* state = FirstThreadStateInUse();
......
...@@ -64,6 +64,7 @@ SOURCES = { ...@@ -64,6 +64,7 @@ SOURCES = {
'test-list.cc', 'test-list.cc',
'test-liveedit.cc', 'test-liveedit.cc',
'test-lock.cc', 'test-lock.cc',
'test-lockers.cc',
'test-log-utils.cc', 'test-log-utils.cc',
'test-log.cc', 'test-log.cc',
'test-mark-compact.cc', 'test-mark-compact.cc',
......
...@@ -93,6 +93,7 @@ ...@@ -93,6 +93,7 @@
'test-list.cc', 'test-list.cc',
'test-liveedit.cc', 'test-liveedit.cc',
'test-lock.cc', 'test-lock.cc',
'test-lockers.cc',
'test-log.cc', 'test-log.cc',
'test-log-utils.cc', 'test-log-utils.cc',
'test-mark-compact.cc', 'test-mark-compact.cc',
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "v8.h" #include "v8.h"
#include "api.h" #include "api.h"
#include "isolate.h"
#include "compilation-cache.h" #include "compilation-cache.h"
#include "execution.h" #include "execution.h"
#include "snapshot.h" #include "snapshot.h"
...@@ -13386,6 +13387,28 @@ TEST(MultipleIsolatesOnIndividualThreads) { ...@@ -13386,6 +13387,28 @@ TEST(MultipleIsolatesOnIndividualThreads) {
isolate2->Dispose(); isolate2->Dispose();
} }
TEST(IsolateDifferentContexts) {
v8::Isolate* isolate = v8::Isolate::New();
Persistent<v8::Context> context;
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope;
context = v8::Context::New();
v8::Context::Scope context_scope(context);
Local<Value> v = CompileRun("2");
CHECK(v->IsNumber());
CHECK_EQ(2, static_cast<int>(v->NumberValue()));
}
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope;
context = v8::Context::New();
v8::Context::Scope context_scope(context);
Local<Value> v = CompileRun("22");
CHECK(v->IsNumber());
CHECK_EQ(22, static_cast<int>(v->NumberValue()));
}
}
class InitDefaultIsolateThread : public v8::internal::Thread { class InitDefaultIsolateThread : public v8::internal::Thread {
public: public:
......
This diff is collapsed.
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