v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
cppgc::DefaultPlatform Class Reference

#include <default-platform.h>

Inheritance diagram for cppgc::DefaultPlatform:
Collaboration diagram for cppgc::DefaultPlatform:

Public Types

using IdleTaskSupport = v8::platform::IdleTaskSupport
 

Public Member Functions

 DefaultPlatform (int thread_pool_size=0, IdleTaskSupport idle_task_support=IdleTaskSupport::kDisabled, std::unique_ptr< TracingController > tracing_controller={})
 
cppgc::PageAllocatorGetPageAllocator () override
 
double MonotonicallyIncreasingTime () override
 
std::shared_ptr< cppgc::TaskRunnerGetForegroundTaskRunner (TaskPriority priority) override
 
std::unique_ptr< cppgc::JobHandlePostJob (cppgc::TaskPriority priority, std::unique_ptr< cppgc::JobTask > job_task) override
 
TracingControllerGetTracingController () override
 
v8::PlatformGetV8Platform () const
 
- Public Member Functions inherited from cppgc::Platform
virtual ~Platform ()=default
 
virtual std::shared_ptr< TaskRunnerGetForegroundTaskRunner ()
 

Protected Attributes

std::unique_ptr< v8::Platformv8_platform_
 

Static Protected Attributes

static constexpr v8::IsolatekNoIsolate = nullptr
 

Detailed Description

Platform provided by cppgc. Uses V8's DefaultPlatform provided by libplatform internally. Exception: GetForegroundTaskRunner(), see below.

Definition at line 20 of file default-platform.h.

Member Typedef Documentation

◆ IdleTaskSupport

Constructor & Destructor Documentation

◆ DefaultPlatform()

cppgc::DefaultPlatform::DefaultPlatform ( int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
std::unique_ptr< TracingController > tracing_controller = {} )
inlineexplicit

Definition at line 23 of file default-platform.h.

Member Function Documentation

◆ GetForegroundTaskRunner()

std::shared_ptr< cppgc::TaskRunner > cppgc::DefaultPlatform::GetForegroundTaskRunner ( TaskPriority priority)
inlineoverridevirtual

Returns a TaskRunner with a specific |priority| which can be used to post a task on the foreground thread.

Reimplemented from cppgc::Platform.

Definition at line 40 of file default-platform.h.

◆ GetPageAllocator()

cppgc::PageAllocator * cppgc::DefaultPlatform::GetPageAllocator ( )
inlineoverridevirtual
Returns
the allocator used by cppgc to allocate its heap and various support structures. Returning nullptr results in using the PageAllocator provided by cppgc::InitializeProcess() instead.

Implements cppgc::Platform.

Definition at line 32 of file default-platform.h.

◆ GetTracingController()

TracingController * cppgc::DefaultPlatform::GetTracingController ( )
inlineoverridevirtual

Returns an instance of a TracingController. This must be non-nullptr. The default implementation returns an empty TracingController that consumes trace data without effect.

Reimplemented from cppgc::Platform.

Definition at line 54 of file default-platform.h.

◆ GetV8Platform()

v8::Platform * cppgc::DefaultPlatform::GetV8Platform ( ) const
inline

Definition at line 58 of file default-platform.h.

◆ MonotonicallyIncreasingTime()

double cppgc::DefaultPlatform::MonotonicallyIncreasingTime ( )
inlineoverridevirtual

Monotonically increasing time in seconds from an arbitrary fixed point in the past. This function is expected to return at least millisecond-precision values. For this reason, it is recommended that the fixed point be no further in the past than the epoch.

Implements cppgc::Platform.

Definition at line 36 of file default-platform.h.

◆ PostJob()

std::unique_ptr< cppgc::JobHandle > cppgc::DefaultPlatform::PostJob ( cppgc::TaskPriority priority,
std::unique_ptr< cppgc::JobTask > job_task )
inlineoverridevirtual

Posts job_task to run in parallel. Returns a JobHandle associated with the Job, which can be joined or canceled. This avoids degenerate cases:

  • Calling CallOnWorkerThread() for each work item, causing significant overhead.
  • Fixed number of CallOnWorkerThread() calls that split the work and might run for a long time. This is problematic when many components post "num cores" tasks and all expect to use all the cores. In these cases, the scheduler lacks context to be fair to multiple same-priority requests and/or ability to request lower priority work to yield when high priority work comes in. A canonical implementation of job_task looks like:
    class MyJobTask : public JobTask {
    public:
    MyJobTask(...) : worker_queue_(...) {}
    // JobTask implementation.
    void Run(JobDelegate* delegate) override {
    while (!delegate->ShouldYield()) {
    // Smallest unit of work.
    auto work_item = worker_queue_.TakeWorkItem(); // Thread safe.
    if (!work_item) return;
    ProcessWork(work_item);
    }
    }
    size_t GetMaxConcurrency() const override {
    return worker_queue_.GetSize(); // Thread safe.
    }
    };
    // ...
    auto handle = PostJob(TaskPriority::kUserVisible,
    std::make_unique<MyJobTask>(...));
    handle->Join();
    std::unique_ptr< cppgc::JobHandle > PostJob(cppgc::TaskPriority priority, std::unique_ptr< cppgc::JobTask > job_task) override
    virtual bool ShouldYield()=0
    virtual void Run(JobDelegate *delegate)=0
    virtual size_t GetMaxConcurrency(size_t worker_count) const =0
    V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
    Definition handles-inl.h:72

PostJob() and methods of the returned JobHandle/JobDelegate, must never be called while holding a lock that could be acquired by JobTask::Run() or JobTask::GetMaxConcurrency() – that could result in a deadlock. This is because (1) JobTask::GetMaxConcurrency() may be invoked while holding internal lock (A), hence JobTask::GetMaxConcurrency() can only use a lock (B) if that lock is never held while calling back into JobHandle from any thread (A=>B/B=>A deadlock) and (2) JobTask::Run() or JobTask::GetMaxConcurrency() may be invoked synchronously from JobHandle (B=>JobHandle::foo=>B deadlock).

A sufficient PostJob() implementation that uses the default Job provided in libplatform looks like:

std::unique_ptr<JobHandle> PostJob(
TaskPriority priority, std::unique_ptr<JobTask> job_task) override {
return std::make_unique<DefaultJobHandle>(
std::make_shared<DefaultJobState>(
this, std::move(job_task), kNumThreads));
}
size_t priority
TaskPriority
Definition v8-platform.h:24

Reimplemented from cppgc::Platform.

Definition at line 48 of file default-platform.h.

Member Data Documentation

◆ kNoIsolate

v8::Isolate* cppgc::DefaultPlatform::kNoIsolate = nullptr
staticconstexprprotected

Definition at line 61 of file default-platform.h.

◆ v8_platform_

std::unique_ptr<v8::Platform> cppgc::DefaultPlatform::v8_platform_
protected

Definition at line 63 of file default-platform.h.


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