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

5 6
#ifndef V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
#define V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
7

8
#include <memory>
9 10 11
#include <vector>

#include "include/libplatform/libplatform-export.h"
12
#include "include/v8-platform.h"
13 14 15
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
#include "src/libplatform/delayed-task-queue.h"
16 17 18 19

namespace v8 {
namespace platform {

20
class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner
21 22
    : public NON_EXPORTED_BASE(TaskRunner) {
 public:
23 24 25 26
  using TimeFunction = double (*)();

  DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size,
                                 TimeFunction time_function);
27

28
  ~DefaultWorkerThreadsTaskRunner() override;
29 30 31

  void Terminate();

32 33
  double MonotonicallyIncreasingTime();

34 35 36 37 38 39 40 41 42 43 44
  // v8::TaskRunner implementation.
  void PostTask(std::unique_ptr<Task> task) override;

  void PostDelayedTask(std::unique_ptr<Task> task,
                       double delay_in_seconds) override;

  void PostIdleTask(std::unique_ptr<IdleTask> task) override;

  bool IdleTasksEnabled() override;

 private:
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  class WorkerThread : public base::Thread {
   public:
    explicit WorkerThread(DefaultWorkerThreadsTaskRunner* runner);
    ~WorkerThread() override;

    // This thread attempts to get tasks in a loop from |runner_| and run them.
    void Run() override;

   private:
    DefaultWorkerThreadsTaskRunner* runner_;

    DISALLOW_COPY_AND_ASSIGN(WorkerThread);
  };

  // Called by the WorkerThread. Gets the next take (delayed or immediate) to be
  // executed. Blocks if no task is available.
  std::unique_ptr<Task> GetNext();

63 64
  bool terminated_ = false;
  base::Mutex lock_;
65
  DelayedTaskQueue queue_;
66
  std::vector<std::unique_ptr<WorkerThread>> thread_pool_;
67
  TimeFunction time_function_;
68 69 70 71
};

}  // namespace platform
}  // namespace v8
72

73
#endif  // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_