v8  8.6.395 (node 15.0.1)
V8 is Google's open source JavaScript engine
default-platform.h
Go to the documentation of this file.
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 #include <thread> // NOLINT(build/c++11)
10 #include <vector>
11 
12 #include "cppgc/platform.h"
13 #include "v8config.h" // NOLINT(build/include_directory)
14 
15 namespace cppgc {
16 
17 /**
18  * Default task runner implementation. Keep posted tasks in a list that can be
19  * processed by calling RunSingleTask() or RunUntilIdle().
20  */
21 class V8_EXPORT DefaultTaskRunner final : public cppgc::TaskRunner {
22  public:
23  DefaultTaskRunner() = default;
24 
25  DefaultTaskRunner(const DefaultTaskRunner&) = delete;
26  DefaultTaskRunner& operator=(const DefaultTaskRunner&) = delete;
27 
28  void PostTask(std::unique_ptr<cppgc::Task> task) override;
29  void PostNonNestableTask(std::unique_ptr<cppgc::Task> task) override;
30  void PostDelayedTask(std::unique_ptr<cppgc::Task> task, double) override;
31  void PostNonNestableDelayedTask(std::unique_ptr<cppgc::Task> task,
32  double) override;
33 
34  void PostIdleTask(std::unique_ptr<cppgc::IdleTask> task) override;
35  bool IdleTasksEnabled() override { return true; }
36 
37  bool RunSingleTask();
38  bool RunSingleIdleTask(double duration_in_seconds);
39 
40  void RunUntilIdle();
41 
42  private:
43  std::vector<std::unique_ptr<cppgc::Task>> tasks_;
44  std::vector<std::unique_ptr<cppgc::IdleTask>> idle_tasks_;
45 };
46 
47 /**
48  * Default platform implementation that uses std::thread for spawning job tasks.
49  */
50 class V8_EXPORT DefaultPlatform final : public Platform {
51  public:
53  ~DefaultPlatform() noexcept override;
54 
55  cppgc::PageAllocator* GetPageAllocator() final;
56 
58 
59  std::shared_ptr<cppgc::TaskRunner> GetForegroundTaskRunner() final;
60 
61  std::unique_ptr<cppgc::JobHandle> PostJob(
62  cppgc::TaskPriority priority,
63  std::unique_ptr<cppgc::JobTask> job_task) final;
64 
67 
68  private:
69  std::unique_ptr<PageAllocator> page_allocator_;
70  std::shared_ptr<DefaultTaskRunner> foreground_task_runner_;
71  std::vector<std::shared_ptr<std::thread>> job_threads_;
72 };
73 
74 } // namespace cppgc
75 
76 #endif // INCLUDE_CPPGC_DEFAULT_PLATFORM_H_