11cb0ef41Sopenharmony_ci// Copyright 2013 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 V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
61cb0ef41Sopenharmony_ci#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <map>
91cb0ef41Sopenharmony_ci#include <memory>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "include/libplatform/libplatform-export.h"
121cb0ef41Sopenharmony_ci#include "include/libplatform/libplatform.h"
131cb0ef41Sopenharmony_ci#include "include/libplatform/v8-tracing.h"
141cb0ef41Sopenharmony_ci#include "include/v8-platform.h"
151cb0ef41Sopenharmony_ci#include "src/base/compiler-specific.h"
161cb0ef41Sopenharmony_ci#include "src/base/macros.h"
171cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
181cb0ef41Sopenharmony_ci#include "src/base/platform/time.h"
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cinamespace v8 {
211cb0ef41Sopenharmony_cinamespace platform {
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciclass Thread;
241cb0ef41Sopenharmony_ciclass WorkerThread;
251cb0ef41Sopenharmony_ciclass DefaultForegroundTaskRunner;
261cb0ef41Sopenharmony_ciclass DefaultWorkerThreadsTaskRunner;
271cb0ef41Sopenharmony_ciclass DefaultPageAllocator;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciclass V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
301cb0ef41Sopenharmony_ci public:
311cb0ef41Sopenharmony_ci  explicit DefaultPlatform(
321cb0ef41Sopenharmony_ci      int thread_pool_size = 0,
331cb0ef41Sopenharmony_ci      IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
341cb0ef41Sopenharmony_ci      std::unique_ptr<v8::TracingController> tracing_controller = {});
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  ~DefaultPlatform() override;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  DefaultPlatform(const DefaultPlatform&) = delete;
391cb0ef41Sopenharmony_ci  DefaultPlatform& operator=(const DefaultPlatform&) = delete;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  void EnsureBackgroundTaskRunnerInitialized();
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  bool PumpMessageLoop(
441cb0ef41Sopenharmony_ci      v8::Isolate* isolate,
451cb0ef41Sopenharmony_ci      MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  void SetTracingController(
501cb0ef41Sopenharmony_ci      std::unique_ptr<v8::TracingController> tracing_controller);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  using TimeFunction = double (*)();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  void SetTimeFunctionForTesting(TimeFunction time_function);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  // v8::Platform implementation.
571cb0ef41Sopenharmony_ci  int NumberOfWorkerThreads() override;
581cb0ef41Sopenharmony_ci  std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
591cb0ef41Sopenharmony_ci      v8::Isolate* isolate) override;
601cb0ef41Sopenharmony_ci  void CallOnWorkerThread(std::unique_ptr<Task> task) override;
611cb0ef41Sopenharmony_ci  void CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
621cb0ef41Sopenharmony_ci                                 double delay_in_seconds) override;
631cb0ef41Sopenharmony_ci  bool IdleTasksEnabled(Isolate* isolate) override;
641cb0ef41Sopenharmony_ci  std::unique_ptr<JobHandle> PostJob(
651cb0ef41Sopenharmony_ci      TaskPriority priority, std::unique_ptr<JobTask> job_state) override;
661cb0ef41Sopenharmony_ci  double MonotonicallyIncreasingTime() override;
671cb0ef41Sopenharmony_ci  double CurrentClockTimeMillis() override;
681cb0ef41Sopenharmony_ci  v8::TracingController* GetTracingController() override;
691cb0ef41Sopenharmony_ci  StackTracePrinter GetStackTracePrinter() override;
701cb0ef41Sopenharmony_ci  v8::PageAllocator* GetPageAllocator() override;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  void NotifyIsolateShutdown(Isolate* isolate);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci private:
751cb0ef41Sopenharmony_ci  base::Mutex lock_;
761cb0ef41Sopenharmony_ci  const int thread_pool_size_;
771cb0ef41Sopenharmony_ci  IdleTaskSupport idle_task_support_;
781cb0ef41Sopenharmony_ci  std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runner_;
791cb0ef41Sopenharmony_ci  std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
801cb0ef41Sopenharmony_ci      foreground_task_runner_map_;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  std::unique_ptr<TracingController> tracing_controller_;
831cb0ef41Sopenharmony_ci  std::unique_ptr<PageAllocator> page_allocator_;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  TimeFunction time_function_for_testing_ = nullptr;
861cb0ef41Sopenharmony_ci};
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci}  // namespace platform
891cb0ef41Sopenharmony_ci}  // namespace v8
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#endif  // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
93