Loading...
Searching...
No Matches
Public Member Functions | List of all members
cppgc::Platform Class Referenceabstract

#include <platform.h>

Inheritance diagram for cppgc::Platform:
[legend]

Public Member Functions

virtual ~Platform ()=default
 
virtual PageAllocatorGetPageAllocator ()=0
 
virtual double MonotonicallyIncreasingTime ()=0
 
virtual std::shared_ptr< TaskRunnerGetForegroundTaskRunner ()
 
virtual std::shared_ptr< TaskRunnerGetForegroundTaskRunner (TaskPriority priority)
 
virtual std::unique_ptr< JobHandlePostJob (TaskPriority priority, std::unique_ptr< JobTask > job_task)
 
virtual TracingControllerGetTracingController ()
 

Detailed Description

Platform interface used by Heap. Contains allocators and executors.

Constructor & Destructor Documentation

◆ ~Platform()

virtual cppgc::Platform::~Platform ( )
virtualdefault

Member Function Documentation

◆ GetForegroundTaskRunner() [1/2]

virtual std::shared_ptr< TaskRunner > cppgc::Platform::GetForegroundTaskRunner ( )
inlinevirtual

Foreground task runner that should be used by a Heap.

◆ GetForegroundTaskRunner() [2/2]

virtual std::shared_ptr< TaskRunner > cppgc::Platform::GetForegroundTaskRunner ( TaskPriority  priority)
inlinevirtual

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

Reimplemented in cppgc::DefaultPlatform.

◆ GetPageAllocator()

virtual PageAllocator * cppgc::Platform::GetPageAllocator ( )
pure virtual
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.

Implemented in cppgc::DefaultPlatform.

◆ GetTracingController()

virtual TracingController * cppgc::Platform::GetTracingController ( )
virtual

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 in cppgc::DefaultPlatform.

◆ MonotonicallyIncreasingTime()

virtual double cppgc::Platform::MonotonicallyIncreasingTime ( )
pure virtual

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.

Implemented in cppgc::DefaultPlatform.

◆ PostJob()

virtual std::unique_ptr< JobHandle > cppgc::Platform::PostJob ( TaskPriority  priority,
std::unique_ptr< JobTask job_task 
)
inlinevirtual

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();
    virtual std::unique_ptr< JobHandle > PostJob(TaskPriority priority, std::unique_ptr< JobTask > job_task)
    Definition: platform.h:126
    Definition: v8-platform.h:206
    virtual bool ShouldYield()=0
    Definition: v8-platform.h:300

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));
}
TaskPriority
Definition: v8-platform.h:24

Reimplemented in cppgc::DefaultPlatform.


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