1// Copyright 2013 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 V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
6#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
7
8#include <map>
9#include <memory>
10
11#include "include/libplatform/libplatform-export.h"
12#include "include/libplatform/libplatform.h"
13#include "include/libplatform/v8-tracing.h"
14#include "include/v8-platform.h"
15#include "src/base/compiler-specific.h"
16#include "src/base/macros.h"
17#include "src/base/platform/mutex.h"
18#include "src/base/platform/time.h"
19
20namespace v8 {
21namespace platform {
22
23class Thread;
24class WorkerThread;
25class DefaultForegroundTaskRunner;
26class DefaultWorkerThreadsTaskRunner;
27class DefaultPageAllocator;
28
29class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
30 public:
31  explicit DefaultPlatform(
32      int thread_pool_size = 0,
33      IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
34      std::unique_ptr<v8::TracingController> tracing_controller = {});
35
36  ~DefaultPlatform() override;
37
38  DefaultPlatform(const DefaultPlatform&) = delete;
39  DefaultPlatform& operator=(const DefaultPlatform&) = delete;
40
41  void EnsureBackgroundTaskRunnerInitialized();
42
43  bool PumpMessageLoop(
44      v8::Isolate* isolate,
45      MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
46
47  void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds);
48
49  void SetTracingController(
50      std::unique_ptr<v8::TracingController> tracing_controller);
51
52  using TimeFunction = double (*)();
53
54  void SetTimeFunctionForTesting(TimeFunction time_function);
55
56  // v8::Platform implementation.
57  int NumberOfWorkerThreads() override;
58  std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
59      v8::Isolate* isolate) override;
60  void CallOnWorkerThread(std::unique_ptr<Task> task) override;
61  void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
62                                 double delay_in_seconds) override;
63  bool IdleTasksEnabled(Isolate* isolate) override;
64  std::unique_ptr<JobHandle> PostJob(
65      TaskPriority priority, std::unique_ptr<JobTask> job_state) override;
66  double MonotonicallyIncreasingTime() override;
67  double CurrentClockTimeMillis() override;
68  v8::TracingController* GetTracingController() override;
69  StackTracePrinter GetStackTracePrinter() override;
70  v8::PageAllocator* GetPageAllocator() override;
71
72  void NotifyIsolateShutdown(Isolate* isolate);
73
74 private:
75  base::Mutex lock_;
76  const int thread_pool_size_;
77  IdleTaskSupport idle_task_support_;
78  std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_;
79  std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
80      foreground_task_runner_map_;
81
82  std::unique_ptr<TracingController> tracing_controller_;
83  std::unique_ptr<PageAllocator> page_allocator_;
84
85  TimeFunction time_function_for_testing_ = nullptr;
86};
87
88}  // namespace platform
89}  // namespace v8
90
91
92#endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
93