11cb0ef41Sopenharmony_ci// Copyright 2017 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_WORKER_THREADS_TASK_RUNNER_H_
61cb0ef41Sopenharmony_ci#define V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "include/libplatform/libplatform-export.h"
121cb0ef41Sopenharmony_ci#include "include/v8-platform.h"
131cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h"
141cb0ef41Sopenharmony_ci#include "src/base/platform/platform.h"
151cb0ef41Sopenharmony_ci#include "src/libplatform/delayed-task-queue.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_cinamespace platform {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner
211cb0ef41Sopenharmony_ci    : public NON_EXPORTED_BASE(TaskRunner) {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  using TimeFunction = double (*)();
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size,
261cb0ef41Sopenharmony_ci                                 TimeFunction time_function);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  ~DefaultWorkerThreadsTaskRunner() override;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  void Terminate();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  double MonotonicallyIncreasingTime();
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  // v8::TaskRunner implementation.
351cb0ef41Sopenharmony_ci  void PostTask(std::unique_ptr<Task> task) override;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  void PostDelayedTask(std::unique_ptr<Task> task,
381cb0ef41Sopenharmony_ci                       double delay_in_seconds) override;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  void PostIdleTask(std::unique_ptr<IdleTask> task) override;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  bool IdleTasksEnabled() override;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci private:
451cb0ef41Sopenharmony_ci  class WorkerThread : public base::Thread {
461cb0ef41Sopenharmony_ci   public:
471cb0ef41Sopenharmony_ci    explicit WorkerThread(DefaultWorkerThreadsTaskRunner* runner);
481cb0ef41Sopenharmony_ci    ~WorkerThread() override;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    WorkerThread(const WorkerThread&) = delete;
511cb0ef41Sopenharmony_ci    WorkerThread& operator=(const WorkerThread&) = delete;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    // This thread attempts to get tasks in a loop from |runner_| and run them.
541cb0ef41Sopenharmony_ci    void Run() override;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci   private:
571cb0ef41Sopenharmony_ci    DefaultWorkerThreadsTaskRunner* runner_;
581cb0ef41Sopenharmony_ci  };
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  // Called by the WorkerThread. Gets the next take (delayed or immediate) to be
611cb0ef41Sopenharmony_ci  // executed. Blocks if no task is available.
621cb0ef41Sopenharmony_ci  std::unique_ptr<Task> GetNext();
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  bool terminated_ = false;
651cb0ef41Sopenharmony_ci  base::Mutex lock_;
661cb0ef41Sopenharmony_ci  DelayedTaskQueue queue_;
671cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<WorkerThread>> thread_pool_;
681cb0ef41Sopenharmony_ci  TimeFunction time_function_;
691cb0ef41Sopenharmony_ci};
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci}  // namespace platform
721cb0ef41Sopenharmony_ci}  // namespace v8
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci#endif  // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
75