1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
6#define INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
7
8#include <memory>
9
10#include "cppgc/platform.h"
11#include "libplatform/libplatform.h"
12#include "v8config.h"  // NOLINT(build/include_directory)
13
14namespace cppgc {
15
16/**
17 * Platform provided by cppgc. Uses V8's DefaultPlatform provided by
18 * libplatform internally. Exception: `GetForegroundTaskRunner()`, see below.
19 */
20class V8_EXPORT DefaultPlatform : public Platform {
21 public:
22  using IdleTaskSupport = v8::platform::IdleTaskSupport;
23  explicit DefaultPlatform(
24      int thread_pool_size = 0,
25      IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
26      std::unique_ptr<TracingController> tracing_controller = {})
27      : v8_platform_(v8::platform::NewDefaultPlatform(
28            thread_pool_size, idle_task_support,
29            v8::platform::InProcessStackDumping::kDisabled,
30            std::move(tracing_controller))) {}
31
32  cppgc::PageAllocator* GetPageAllocator() override {
33    return v8_platform_->GetPageAllocator();
34  }
35
36  double MonotonicallyIncreasingTime() override {
37    return v8_platform_->MonotonicallyIncreasingTime();
38  }
39
40  std::shared_ptr<cppgc::TaskRunner> GetForegroundTaskRunner() override {
41    // V8's default platform creates a new task runner when passed the
42    // `v8::Isolate` pointer the first time. For non-default platforms this will
43    // require getting the appropriate task runner.
44    return v8_platform_->GetForegroundTaskRunner(kNoIsolate);
45  }
46
47  std::unique_ptr<cppgc::JobHandle> PostJob(
48      cppgc::TaskPriority priority,
49      std::unique_ptr<cppgc::JobTask> job_task) override {
50    return v8_platform_->PostJob(priority, std::move(job_task));
51  }
52
53  TracingController* GetTracingController() override {
54    return v8_platform_->GetTracingController();
55  }
56
57  v8::Platform* GetV8Platform() const { return v8_platform_.get(); }
58
59 protected:
60  static constexpr v8::Isolate* kNoIsolate = nullptr;
61
62  std::unique_ptr<v8::Platform> v8_platform_;
63};
64
65}  // namespace cppgc
66
67#endif  // INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
68