11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_PLATFORM_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_PLATFORM_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <memory> 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include "cppgc/source-location.h" 111cb0ef41Sopenharmony_ci#include "v8-platform.h" // NOLINT(build/include_directory) 121cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cinamespace cppgc { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// TODO(v8:10346): Create separate includes for concepts that are not 171cb0ef41Sopenharmony_ci// V8-specific. 181cb0ef41Sopenharmony_ciusing IdleTask = v8::IdleTask; 191cb0ef41Sopenharmony_ciusing JobHandle = v8::JobHandle; 201cb0ef41Sopenharmony_ciusing JobDelegate = v8::JobDelegate; 211cb0ef41Sopenharmony_ciusing JobTask = v8::JobTask; 221cb0ef41Sopenharmony_ciusing PageAllocator = v8::PageAllocator; 231cb0ef41Sopenharmony_ciusing Task = v8::Task; 241cb0ef41Sopenharmony_ciusing TaskPriority = v8::TaskPriority; 251cb0ef41Sopenharmony_ciusing TaskRunner = v8::TaskRunner; 261cb0ef41Sopenharmony_ciusing TracingController = v8::TracingController; 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci/** 291cb0ef41Sopenharmony_ci * Platform interface used by Heap. Contains allocators and executors. 301cb0ef41Sopenharmony_ci */ 311cb0ef41Sopenharmony_ciclass V8_EXPORT Platform { 321cb0ef41Sopenharmony_ci public: 331cb0ef41Sopenharmony_ci virtual ~Platform() = default; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci /** 361cb0ef41Sopenharmony_ci * Returns the allocator used by cppgc to allocate its heap and various 371cb0ef41Sopenharmony_ci * support structures. 381cb0ef41Sopenharmony_ci */ 391cb0ef41Sopenharmony_ci virtual PageAllocator* GetPageAllocator() = 0; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci /** 421cb0ef41Sopenharmony_ci * Monotonically increasing time in seconds from an arbitrary fixed point in 431cb0ef41Sopenharmony_ci * the past. This function is expected to return at least 441cb0ef41Sopenharmony_ci * millisecond-precision values. For this reason, 451cb0ef41Sopenharmony_ci * it is recommended that the fixed point be no further in the past than 461cb0ef41Sopenharmony_ci * the epoch. 471cb0ef41Sopenharmony_ci **/ 481cb0ef41Sopenharmony_ci virtual double MonotonicallyIncreasingTime() = 0; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci /** 511cb0ef41Sopenharmony_ci * Foreground task runner that should be used by a Heap. 521cb0ef41Sopenharmony_ci */ 531cb0ef41Sopenharmony_ci virtual std::shared_ptr<TaskRunner> GetForegroundTaskRunner() { 541cb0ef41Sopenharmony_ci return nullptr; 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci /** 581cb0ef41Sopenharmony_ci * Posts `job_task` to run in parallel. Returns a `JobHandle` associated with 591cb0ef41Sopenharmony_ci * the `Job`, which can be joined or canceled. 601cb0ef41Sopenharmony_ci * This avoids degenerate cases: 611cb0ef41Sopenharmony_ci * - Calling `CallOnWorkerThread()` for each work item, causing significant 621cb0ef41Sopenharmony_ci * overhead. 631cb0ef41Sopenharmony_ci * - Fixed number of `CallOnWorkerThread()` calls that split the work and 641cb0ef41Sopenharmony_ci * might run for a long time. This is problematic when many components post 651cb0ef41Sopenharmony_ci * "num cores" tasks and all expect to use all the cores. In these cases, 661cb0ef41Sopenharmony_ci * the scheduler lacks context to be fair to multiple same-priority requests 671cb0ef41Sopenharmony_ci * and/or ability to request lower priority work to yield when high priority 681cb0ef41Sopenharmony_ci * work comes in. 691cb0ef41Sopenharmony_ci * A canonical implementation of `job_task` looks like: 701cb0ef41Sopenharmony_ci * \code 711cb0ef41Sopenharmony_ci * class MyJobTask : public JobTask { 721cb0ef41Sopenharmony_ci * public: 731cb0ef41Sopenharmony_ci * MyJobTask(...) : worker_queue_(...) {} 741cb0ef41Sopenharmony_ci * // JobTask implementation. 751cb0ef41Sopenharmony_ci * void Run(JobDelegate* delegate) override { 761cb0ef41Sopenharmony_ci * while (!delegate->ShouldYield()) { 771cb0ef41Sopenharmony_ci * // Smallest unit of work. 781cb0ef41Sopenharmony_ci * auto work_item = worker_queue_.TakeWorkItem(); // Thread safe. 791cb0ef41Sopenharmony_ci * if (!work_item) return; 801cb0ef41Sopenharmony_ci * ProcessWork(work_item); 811cb0ef41Sopenharmony_ci * } 821cb0ef41Sopenharmony_ci * } 831cb0ef41Sopenharmony_ci * 841cb0ef41Sopenharmony_ci * size_t GetMaxConcurrency() const override { 851cb0ef41Sopenharmony_ci * return worker_queue_.GetSize(); // Thread safe. 861cb0ef41Sopenharmony_ci * } 871cb0ef41Sopenharmony_ci * }; 881cb0ef41Sopenharmony_ci * 891cb0ef41Sopenharmony_ci * // ... 901cb0ef41Sopenharmony_ci * auto handle = PostJob(TaskPriority::kUserVisible, 911cb0ef41Sopenharmony_ci * std::make_unique<MyJobTask>(...)); 921cb0ef41Sopenharmony_ci * handle->Join(); 931cb0ef41Sopenharmony_ci * \endcode 941cb0ef41Sopenharmony_ci * 951cb0ef41Sopenharmony_ci * `PostJob()` and methods of the returned JobHandle/JobDelegate, must never 961cb0ef41Sopenharmony_ci * be called while holding a lock that could be acquired by `JobTask::Run()` 971cb0ef41Sopenharmony_ci * or `JobTask::GetMaxConcurrency()` -- that could result in a deadlock. This 981cb0ef41Sopenharmony_ci * is because (1) `JobTask::GetMaxConcurrency()` may be invoked while holding 991cb0ef41Sopenharmony_ci * internal lock (A), hence `JobTask::GetMaxConcurrency()` can only use a lock 1001cb0ef41Sopenharmony_ci * (B) if that lock is *never* held while calling back into `JobHandle` from 1011cb0ef41Sopenharmony_ci * any thread (A=>B/B=>A deadlock) and (2) `JobTask::Run()` or 1021cb0ef41Sopenharmony_ci * `JobTask::GetMaxConcurrency()` may be invoked synchronously from 1031cb0ef41Sopenharmony_ci * `JobHandle` (B=>JobHandle::foo=>B deadlock). 1041cb0ef41Sopenharmony_ci * 1051cb0ef41Sopenharmony_ci * A sufficient `PostJob()` implementation that uses the default Job provided 1061cb0ef41Sopenharmony_ci * in libplatform looks like: 1071cb0ef41Sopenharmony_ci * \code 1081cb0ef41Sopenharmony_ci * std::unique_ptr<JobHandle> PostJob( 1091cb0ef41Sopenharmony_ci * TaskPriority priority, std::unique_ptr<JobTask> job_task) override { 1101cb0ef41Sopenharmony_ci * return std::make_unique<DefaultJobHandle>( 1111cb0ef41Sopenharmony_ci * std::make_shared<DefaultJobState>( 1121cb0ef41Sopenharmony_ci * this, std::move(job_task), kNumThreads)); 1131cb0ef41Sopenharmony_ci * } 1141cb0ef41Sopenharmony_ci * \endcode 1151cb0ef41Sopenharmony_ci */ 1161cb0ef41Sopenharmony_ci virtual std::unique_ptr<JobHandle> PostJob( 1171cb0ef41Sopenharmony_ci TaskPriority priority, std::unique_ptr<JobTask> job_task) { 1181cb0ef41Sopenharmony_ci return nullptr; 1191cb0ef41Sopenharmony_ci } 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci /** 1221cb0ef41Sopenharmony_ci * Returns an instance of a `TracingController`. This must be non-nullptr. The 1231cb0ef41Sopenharmony_ci * default implementation returns an empty `TracingController` that consumes 1241cb0ef41Sopenharmony_ci * trace data without effect. 1251cb0ef41Sopenharmony_ci */ 1261cb0ef41Sopenharmony_ci virtual TracingController* GetTracingController(); 1271cb0ef41Sopenharmony_ci}; 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci/** 1301cb0ef41Sopenharmony_ci * Process-global initialization of the garbage collector. Must be called before 1311cb0ef41Sopenharmony_ci * creating a Heap. 1321cb0ef41Sopenharmony_ci * 1331cb0ef41Sopenharmony_ci * Can be called multiple times when paired with `ShutdownProcess()`. 1341cb0ef41Sopenharmony_ci * 1351cb0ef41Sopenharmony_ci * \param page_allocator The allocator used for maintaining meta data. Must not 1361cb0ef41Sopenharmony_ci * change between multiple calls to InitializeProcess. 1371cb0ef41Sopenharmony_ci */ 1381cb0ef41Sopenharmony_ciV8_EXPORT void InitializeProcess(PageAllocator* page_allocator); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci/** 1411cb0ef41Sopenharmony_ci * Must be called after destroying the last used heap. Some process-global 1421cb0ef41Sopenharmony_ci * metadata may not be returned and reused upon a subsequent 1431cb0ef41Sopenharmony_ci * `InitializeProcess()` call. 1441cb0ef41Sopenharmony_ci */ 1451cb0ef41Sopenharmony_ciV8_EXPORT void ShutdownProcess(); 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_cinamespace internal { 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ciV8_EXPORT void Fatal(const std::string& reason = std::string(), 1501cb0ef41Sopenharmony_ci const SourceLocation& = SourceLocation::Current()); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci} // namespace internal 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci} // namespace cppgc 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci#endif // INCLUDE_CPPGC_PLATFORM_H_ 157