2020-03-24 13:35:51 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_PLATFORM_H_
|
|
|
|
#define INCLUDE_CPPGC_PLATFORM_H_
|
|
|
|
|
2021-05-20 09:36:52 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2022-02-17 10:40:48 +00:00
|
|
|
#include "cppgc/source-location.h"
|
2020-04-27 16:29:03 +00:00
|
|
|
#include "v8-platform.h" // NOLINT(build/include_directory)
|
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
2020-03-24 13:35:51 +00:00
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
|
2020-05-28 19:04:43 +00:00
|
|
|
// TODO(v8:10346): Create separate includes for concepts that are not
|
|
|
|
// V8-specific.
|
2020-06-08 09:53:26 +00:00
|
|
|
using IdleTask = v8::IdleTask;
|
2020-05-28 19:04:43 +00:00
|
|
|
using JobHandle = v8::JobHandle;
|
2020-07-22 21:24:25 +00:00
|
|
|
using JobDelegate = v8::JobDelegate;
|
2020-05-28 19:04:43 +00:00
|
|
|
using JobTask = v8::JobTask;
|
2020-03-24 13:35:51 +00:00
|
|
|
using PageAllocator = v8::PageAllocator;
|
2020-06-08 09:53:26 +00:00
|
|
|
using Task = v8::Task;
|
2020-05-28 19:04:43 +00:00
|
|
|
using TaskPriority = v8::TaskPriority;
|
|
|
|
using TaskRunner = v8::TaskRunner;
|
2020-11-19 12:27:35 +00:00
|
|
|
using TracingController = v8::TracingController;
|
2020-03-24 13:35:51 +00:00
|
|
|
|
2020-05-28 19:04:43 +00:00
|
|
|
/**
|
|
|
|
* Platform interface used by Heap. Contains allocators and executors.
|
|
|
|
*/
|
|
|
|
class V8_EXPORT Platform {
|
|
|
|
public:
|
|
|
|
virtual ~Platform() = default;
|
2020-05-26 14:40:57 +00:00
|
|
|
|
2020-05-28 19:04:43 +00:00
|
|
|
/**
|
2022-11-07 08:52:07 +00:00
|
|
|
* \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.
|
2020-05-28 19:04:43 +00:00
|
|
|
*/
|
|
|
|
virtual PageAllocator* GetPageAllocator() = 0;
|
2020-03-27 10:02:58 +00:00
|
|
|
|
2020-05-28 19:04:43 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
virtual double MonotonicallyIncreasingTime() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Foreground task runner that should be used by a Heap.
|
|
|
|
*/
|
|
|
|
virtual std::shared_ptr<TaskRunner> GetForegroundTaskRunner() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 08:12:55 +00:00
|
|
|
* Posts `job_task` to run in parallel. Returns a `JobHandle` associated with
|
|
|
|
* the `Job`, which can be joined or canceled.
|
2020-05-28 19:04:43 +00:00
|
|
|
* This avoids degenerate cases:
|
2020-10-19 08:12:55 +00:00
|
|
|
* - Calling `CallOnWorkerThread()` for each work item, causing significant
|
2020-05-28 19:04:43 +00:00
|
|
|
* overhead.
|
2020-10-19 08:12:55 +00:00
|
|
|
* - Fixed number of `CallOnWorkerThread()` calls that split the work and
|
|
|
|
* might run for a long time. This is problematic when many components post
|
2020-05-28 19:04:43 +00:00
|
|
|
* "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.
|
2020-10-19 08:12:55 +00:00
|
|
|
* A canonical implementation of `job_task` looks like:
|
|
|
|
* \code
|
2020-05-28 19:04:43 +00:00
|
|
|
* class MyJobTask : public JobTask {
|
|
|
|
* public:
|
|
|
|
* MyJobTask(...) : worker_queue_(...) {}
|
2020-10-19 08:12:55 +00:00
|
|
|
* // JobTask implementation.
|
2020-05-28 19:04:43 +00:00
|
|
|
* 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.
|
|
|
|
* }
|
|
|
|
* };
|
2020-10-19 08:12:55 +00:00
|
|
|
*
|
|
|
|
* // ...
|
2020-05-28 19:04:43 +00:00
|
|
|
* auto handle = PostJob(TaskPriority::kUserVisible,
|
|
|
|
* std::make_unique<MyJobTask>(...));
|
|
|
|
* handle->Join();
|
2020-10-19 08:12:55 +00:00
|
|
|
* \endcode
|
2020-05-28 19:04:43 +00:00
|
|
|
*
|
2020-10-19 08:12:55 +00:00
|
|
|
* `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).
|
2020-05-28 19:04:43 +00:00
|
|
|
*
|
2020-10-19 08:12:55 +00:00
|
|
|
* A sufficient `PostJob()` implementation that uses the default Job provided
|
|
|
|
* in libplatform looks like:
|
|
|
|
* \code
|
|
|
|
* 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));
|
2020-05-28 19:04:43 +00:00
|
|
|
* }
|
2020-10-19 08:12:55 +00:00
|
|
|
* \endcode
|
2020-05-28 19:04:43 +00:00
|
|
|
*/
|
|
|
|
virtual std::unique_ptr<JobHandle> PostJob(
|
|
|
|
TaskPriority priority, std::unique_ptr<JobTask> job_task) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-11-19 12:27:35 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-20 14:10:31 +00:00
|
|
|
* Returns an instance of a `TracingController`. This must be non-nullptr. The
|
|
|
|
* default implementation returns an empty `TracingController` that consumes
|
|
|
|
* trace data without effect.
|
2020-11-19 12:27:35 +00:00
|
|
|
*/
|
2020-11-20 14:10:31 +00:00
|
|
|
virtual TracingController* GetTracingController();
|
2020-05-28 19:04:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process-global initialization of the garbage collector. Must be called before
|
2021-02-10 16:03:23 +00:00
|
|
|
* creating a Heap.
|
|
|
|
*
|
|
|
|
* Can be called multiple times when paired with `ShutdownProcess()`.
|
|
|
|
*
|
2022-06-28 17:36:45 +00:00
|
|
|
* \param page_allocator The allocator used for maintaining meta data. Must stay
|
2022-11-07 08:52:07 +00:00
|
|
|
* always alive and not change between multiple calls to InitializeProcess. If
|
|
|
|
* no allocator is provided, a default internal version will be used.
|
2020-05-28 19:04:43 +00:00
|
|
|
*/
|
2022-11-07 08:52:07 +00:00
|
|
|
V8_EXPORT void InitializeProcess(PageAllocator* page_allocator = nullptr);
|
2020-05-28 19:04:43 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-10 16:03:23 +00:00
|
|
|
* Must be called after destroying the last used heap. Some process-global
|
|
|
|
* metadata may not be returned and reused upon a subsequent
|
|
|
|
* `InitializeProcess()` call.
|
2020-05-28 19:04:43 +00:00
|
|
|
*/
|
|
|
|
V8_EXPORT void ShutdownProcess();
|
2020-03-27 10:02:58 +00:00
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2022-02-17 10:40:48 +00:00
|
|
|
V8_EXPORT void Fatal(const std::string& reason = std::string(),
|
|
|
|
const SourceLocation& = SourceLocation::Current());
|
2020-03-27 10:02:58 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
2021-08-09 14:19:56 +00:00
|
|
|
|
2020-03-24 13:35:51 +00:00
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_CPPGC_PLATFORM_H_
|