V8 Project
v8::Unlocker Class Reference

Multiple threads in V8 are allowed, but only one thread at a time is allowed to use any given V8 isolate, see the comments in the Isolate class. More...

#include <v8.h>

+ Collaboration diagram for v8::Unlocker:

Public Member Functions

 Unlocker (Isolate *isolate)
 Initialize Unlocker for a given Isolate. More...
 
 ~Unlocker ()
 

Private Member Functions

void Initialize (Isolate *isolate)
 

Private Attributes

internal::Isolateisolate_
 

Detailed Description

Multiple threads in V8 are allowed, but only one thread at a time is allowed to use any given V8 isolate, see the comments in the Isolate class.

The definition of 'using a V8 isolate' includes accessing handles or holding onto object pointers obtained from V8 handles while in the particular V8 isolate. It is up to the user of V8 to ensure, perhaps with locking, that this constraint is not violated. In addition to any other synchronization mechanism that may be used, the v8::Locker and v8::Unlocker classes must be used to signal thead switches to V8.

v8::Locker is a scoped lock object. While it's active, i.e. between its construction and destruction, the current thread is allowed to use the locked isolate. V8 guarantees that an isolate can be locked by at most one thread at any time. In other words, the scope of a v8::Locker is a critical section.

Sample usage:

...
{
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
...
// Code using V8 and isolate goes here.
...
} // Destructor called here
Stack-allocated class which sets the isolate for all operations executed within a local scope.
Definition: v8.h:4398

If you wish to stop using V8 in a thread A you can do this either by destroying the v8::Locker object as above or by constructing a v8::Unlocker object:

{
isolate->Exit();
v8::Unlocker unlocker(isolate);
...
// Code not using V8 goes here while V8 can run in another thread.
...
} // Destructor called here.
isolate->Enter();
Multiple threads in V8 are allowed, but only one thread at a time is allowed to use any given V8 isol...
Definition: v8.h:5677

The Unlocker object is intended for use in a long-running callback from V8, where you want to release the V8 lock for other threads to use.

The v8::Locker is a recursive lock, i.e. you can lock more than once in a given thread. This can be useful if you have code that can be called either from code that holds the lock or from code that does not. The Unlocker is not recursive so you can not have several Unlockers on the stack at once, and you can not use an Unlocker in a thread that is not inside a Locker's scope.

An unlocker will unlock several lockers if it has to and reinstate the correct depth of locking on its destruction, e.g.:

// V8 not locked.
{
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
// V8 locked.
{
v8::Locker another_locker(isolate);
// V8 still locked (2 levels).
{
isolate->Exit();
v8::Unlocker unlocker(isolate);
// V8 not locked.
}
isolate->Enter();
// V8 locked again (2 levels).
}
// V8 still locked (1 level).
}
// V8 Now no longer locked.

Definition at line 5677 of file v8.h.

Constructor & Destructor Documentation

◆ Unlocker()

v8::Unlocker::Unlocker ( Isolate isolate)
inlineexplicit

Initialize Unlocker for a given Isolate.

Definition at line 5682 of file v8.h.

5682 { Initialize(isolate); }
void Initialize(Isolate *isolate)
Definition: v8threads.cc:84

◆ ~Unlocker()

v8::Unlocker::~Unlocker ( )

Definition at line 93 of file v8threads.cc.

93  {
97 }
internal::Isolate * isolate_
Definition: v8.h:5688
ThreadManager * thread_manager()
Definition: isolate.h:921
#define DCHECK(condition)
Definition: logging.h:205

References DCHECK, v8::internal::ThreadManager::IsLockedByCurrentThread(), isolate_, v8::internal::ThreadManager::Lock(), v8::internal::ThreadManager::RestoreThread(), and v8::internal::Isolate::thread_manager().

+ Here is the call graph for this function:

Member Function Documentation

◆ Initialize()

void v8::Unlocker::Initialize ( v8::Isolate isolate)
private

Definition at line 84 of file v8threads.cc.

84  {
85  DCHECK(isolate != NULL);
86  isolate_ = reinterpret_cast<i::Isolate*>(isolate);
90 }
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL

References v8::internal::ThreadManager::ArchiveThread(), DCHECK, v8::internal::ThreadManager::IsLockedByCurrentThread(), isolate_, NULL, v8::internal::Isolate::thread_manager(), and v8::internal::ThreadManager::Unlock().

+ Here is the call graph for this function:

Member Data Documentation

◆ isolate_

internal::Isolate* v8::Unlocker::isolate_
private

Definition at line 5688 of file v8.h.

Referenced by Initialize(), and ~Unlocker().


The documentation for this class was generated from the following files: