default-platform.h 3.01 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#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/platform/mutex.h"
17 18

namespace v8 {
19
namespace platform {
20

21 22
class Thread;
class WorkerThread;
23
class DefaultForegroundTaskRunner;
24
class DefaultWorkerThreadsTaskRunner;
25
class DefaultPageAllocator;
26

27
class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
28
 public:
29
  explicit DefaultPlatform(
30
      int thread_pool_size = 0,
31
      IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
32 33
      std::unique_ptr<v8::TracingController> tracing_controller = {});

34
  ~DefaultPlatform() override;
35

36 37 38
  DefaultPlatform(const DefaultPlatform&) = delete;
  DefaultPlatform& operator=(const DefaultPlatform&) = delete;

39
  void EnsureBackgroundTaskRunnerInitialized();
40

41 42 43
  bool PumpMessageLoop(
      v8::Isolate* isolate,
      MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
44

45 46
  void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds);

47 48
  void SetTracingController(
      std::unique_ptr<v8::TracingController> tracing_controller);
49

50 51 52 53
  using TimeFunction = double (*)();

  void SetTimeFunctionForTesting(TimeFunction time_function);

54
  // v8::Platform implementation.
55
  int NumberOfWorkerThreads() override;
56 57
  std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
      v8::Isolate* isolate) override;
58
  void CallOnWorkerThread(std::unique_ptr<Task> task) override;
59 60
  void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
                                 double delay_in_seconds) override;
61
  bool IdleTasksEnabled(Isolate* isolate) override;
62 63
  std::unique_ptr<JobHandle> PostJob(
      TaskPriority priority, std::unique_ptr<JobTask> job_state) override;
64 65
  std::unique_ptr<JobHandle> CreateJob(
      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 73
  void NotifyIsolateShutdown(Isolate* isolate);

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 80 81
  std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
      foreground_task_runner_map_;

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 89
}  // namespace platform
}  // namespace v8
90 91


92
#endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_