v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
default-worker-threads-task-runner.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
6#define V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
7
8#include <memory>
9#include <vector>
10
12#include "include/v8-platform.h"
17
18namespace v8 {
19namespace platform {
20
22 : public NON_EXPORTED_BASE(TaskRunner) {
23 public:
24 using TimeFunction = double (*)();
25
27 uint32_t thread_pool_size, TimeFunction time_function,
28 base::Thread::Priority priority = base::Thread::Priority::kDefault);
29
31
32 void Terminate();
33
34 double MonotonicallyIncreasingTime();
35
36 // v8::TaskRunner implementation.
37 bool IdleTasksEnabled() override;
38
39 private:
40 // v8::TaskRunner implementation.
41 void PostTaskImpl(std::unique_ptr<Task> task,
42 const SourceLocation& location) override;
43
44 void PostDelayedTaskImpl(std::unique_ptr<Task> task, double delay_in_seconds,
45 const SourceLocation& location) override;
46
47 void PostIdleTaskImpl(std::unique_ptr<IdleTask> task,
48 const SourceLocation& location) override;
49
50 class WorkerThread : public base::Thread {
51 public:
54 ~WorkerThread() override;
55
56 WorkerThread(const WorkerThread&) = delete;
58
59 // This thread attempts to get tasks in a loop from |runner_| and run them.
60 void Run() override;
61
62 void Notify();
63
64 private:
67 };
68
69 // Called by the WorkerThread. Gets the next take (delayed or immediate) to be
70 // executed. Blocks if no task is available.
71 std::unique_ptr<Task> GetNext();
72
73 bool terminated_ = false;
75 // Vector of idle threads -- these are pushed in LIFO order, so that the most
76 // recently active thread is the first to be reactivated.
77 std::vector<WorkerThread*> idle_threads_;
78 std::vector<std::unique_ptr<WorkerThread>> thread_pool_;
79 // Worker threads access this queue, so we can only destroy it after all
80 // workers stopped.
82 std::queue<std::unique_ptr<Task>> task_queue_;
84};
85
86} // namespace platform
87} // namespace v8
88
89#endif // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
WorkerThread & operator=(const WorkerThread &)=delete
std::vector< std::unique_ptr< WorkerThread > > thread_pool_
#define V8_PLATFORM_EXPORT
size_t priority
#define NON_EXPORTED_BASE(code)