11cb0ef41Sopenharmony_ci// Copyright 2014 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_LIBPLATFORM_H_
61cb0ef41Sopenharmony_ci#define V8_LIBPLATFORM_LIBPLATFORM_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "libplatform/libplatform-export.h"
111cb0ef41Sopenharmony_ci#include "libplatform/v8-tracing.h"
121cb0ef41Sopenharmony_ci#include "v8-platform.h"  // NOLINT(build/include_directory)
131cb0ef41Sopenharmony_ci#include "v8config.h"     // NOLINT(build/include_directory)
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace platform {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cienum class IdleTaskSupport { kDisabled, kEnabled };
191cb0ef41Sopenharmony_cienum class InProcessStackDumping { kDisabled, kEnabled };
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cienum class MessageLoopBehavior : bool {
221cb0ef41Sopenharmony_ci  kDoNotWait = false,
231cb0ef41Sopenharmony_ci  kWaitForWork = true
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci/**
271cb0ef41Sopenharmony_ci * Returns a new instance of the default v8::Platform implementation.
281cb0ef41Sopenharmony_ci *
291cb0ef41Sopenharmony_ci * The caller will take ownership of the returned pointer. |thread_pool_size|
301cb0ef41Sopenharmony_ci * is the number of worker threads to allocate for background jobs. If a value
311cb0ef41Sopenharmony_ci * of zero is passed, a suitable default based on the current number of
321cb0ef41Sopenharmony_ci * processors online will be chosen.
331cb0ef41Sopenharmony_ci * If |idle_task_support| is enabled then the platform will accept idle
341cb0ef41Sopenharmony_ci * tasks (IdleTasksEnabled will return true) and will rely on the embedder
351cb0ef41Sopenharmony_ci * calling v8::platform::RunIdleTasks to process the idle tasks.
361cb0ef41Sopenharmony_ci * If |tracing_controller| is nullptr, the default platform will create a
371cb0ef41Sopenharmony_ci * v8::platform::TracingController instance and use it.
381cb0ef41Sopenharmony_ci */
391cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT std::unique_ptr<v8::Platform> NewDefaultPlatform(
401cb0ef41Sopenharmony_ci    int thread_pool_size = 0,
411cb0ef41Sopenharmony_ci    IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
421cb0ef41Sopenharmony_ci    InProcessStackDumping in_process_stack_dumping =
431cb0ef41Sopenharmony_ci        InProcessStackDumping::kDisabled,
441cb0ef41Sopenharmony_ci    std::unique_ptr<v8::TracingController> tracing_controller = {});
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci/**
471cb0ef41Sopenharmony_ci * The same as NewDefaultPlatform but disables the worker thread pool.
481cb0ef41Sopenharmony_ci * It must be used with the --single-threaded V8 flag.
491cb0ef41Sopenharmony_ci */
501cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT std::unique_ptr<v8::Platform>
511cb0ef41Sopenharmony_ciNewSingleThreadedDefaultPlatform(
521cb0ef41Sopenharmony_ci    IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
531cb0ef41Sopenharmony_ci    InProcessStackDumping in_process_stack_dumping =
541cb0ef41Sopenharmony_ci        InProcessStackDumping::kDisabled,
551cb0ef41Sopenharmony_ci    std::unique_ptr<v8::TracingController> tracing_controller = {});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci/**
581cb0ef41Sopenharmony_ci * Returns a new instance of the default v8::JobHandle implementation.
591cb0ef41Sopenharmony_ci *
601cb0ef41Sopenharmony_ci * The job will be executed by spawning up to |num_worker_threads| many worker
611cb0ef41Sopenharmony_ci * threads on the provided |platform| with the given |priority|.
621cb0ef41Sopenharmony_ci */
631cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT std::unique_ptr<v8::JobHandle> NewDefaultJobHandle(
641cb0ef41Sopenharmony_ci    v8::Platform* platform, v8::TaskPriority priority,
651cb0ef41Sopenharmony_ci    std::unique_ptr<v8::JobTask> job_task, size_t num_worker_threads);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci/**
681cb0ef41Sopenharmony_ci * Pumps the message loop for the given isolate.
691cb0ef41Sopenharmony_ci *
701cb0ef41Sopenharmony_ci * The caller has to make sure that this is called from the right thread.
711cb0ef41Sopenharmony_ci * Returns true if a task was executed, and false otherwise. If the call to
721cb0ef41Sopenharmony_ci * PumpMessageLoop is nested within another call to PumpMessageLoop, only
731cb0ef41Sopenharmony_ci * nestable tasks may run. Otherwise, any task may run. Unless requested through
741cb0ef41Sopenharmony_ci * the |behavior| parameter, this call does not block if no task is pending. The
751cb0ef41Sopenharmony_ci * |platform| has to be created using |NewDefaultPlatform|.
761cb0ef41Sopenharmony_ci */
771cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT bool PumpMessageLoop(
781cb0ef41Sopenharmony_ci    v8::Platform* platform, v8::Isolate* isolate,
791cb0ef41Sopenharmony_ci    MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci/**
821cb0ef41Sopenharmony_ci * Runs pending idle tasks for at most |idle_time_in_seconds| seconds.
831cb0ef41Sopenharmony_ci *
841cb0ef41Sopenharmony_ci * The caller has to make sure that this is called from the right thread.
851cb0ef41Sopenharmony_ci * This call does not block if no task is pending. The |platform| has to be
861cb0ef41Sopenharmony_ci * created using |NewDefaultPlatform|.
871cb0ef41Sopenharmony_ci */
881cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT void RunIdleTasks(v8::Platform* platform,
891cb0ef41Sopenharmony_ci                                     v8::Isolate* isolate,
901cb0ef41Sopenharmony_ci                                     double idle_time_in_seconds);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci/**
931cb0ef41Sopenharmony_ci * Notifies the given platform about the Isolate getting deleted soon. Has to be
941cb0ef41Sopenharmony_ci * called for all Isolates which are deleted - unless we're shutting down the
951cb0ef41Sopenharmony_ci * platform.
961cb0ef41Sopenharmony_ci *
971cb0ef41Sopenharmony_ci * The |platform| has to be created using |NewDefaultPlatform|.
981cb0ef41Sopenharmony_ci *
991cb0ef41Sopenharmony_ci */
1001cb0ef41Sopenharmony_ciV8_PLATFORM_EXPORT void NotifyIsolateShutdown(v8::Platform* platform,
1011cb0ef41Sopenharmony_ci                                              Isolate* isolate);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci}  // namespace platform
1041cb0ef41Sopenharmony_ci}  // namespace v8
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci#endif  // V8_LIBPLATFORM_LIBPLATFORM_H_
107