Line data Source code
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 <vector>
9 :
10 : #include "include/libplatform/libplatform-export.h"
11 : #include "include/v8-platform.h"
12 : #include "src/base/platform/mutex.h"
13 : #include "src/base/platform/platform.h"
14 : #include "src/libplatform/delayed-task-queue.h"
15 :
16 : namespace v8 {
17 : namespace platform {
18 :
19 114112 : class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner
20 : : public NON_EXPORTED_BASE(TaskRunner) {
21 : public:
22 : using TimeFunction = double (*)();
23 :
24 : DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size,
25 : TimeFunction time_function);
26 :
27 : ~DefaultWorkerThreadsTaskRunner() override;
28 :
29 : void Terminate();
30 :
31 : double MonotonicallyIncreasingTime();
32 :
33 : // v8::TaskRunner implementation.
34 : void PostTask(std::unique_ptr<Task> task) override;
35 :
36 : void PostDelayedTask(std::unique_ptr<Task> task,
37 : double delay_in_seconds) override;
38 :
39 : void PostIdleTask(std::unique_ptr<IdleTask> task) override;
40 :
41 : bool IdleTasksEnabled() override;
42 :
43 : private:
44 : class WorkerThread : public base::Thread {
45 : public:
46 : explicit WorkerThread(DefaultWorkerThreadsTaskRunner* runner);
47 : ~WorkerThread() override;
48 :
49 : // This thread attempts to get tasks in a loop from |runner_| and run them.
50 : void Run() override;
51 :
52 : private:
53 : DefaultWorkerThreadsTaskRunner* runner_;
54 :
55 : DISALLOW_COPY_AND_ASSIGN(WorkerThread);
56 : };
57 :
58 : // Called by the WorkerThread. Gets the next take (delayed or immediate) to be
59 : // executed. Blocks if no task is available.
60 : std::unique_ptr<Task> GetNext();
61 :
62 : bool terminated_ = false;
63 : base::Mutex lock_;
64 : DelayedTaskQueue queue_;
65 : std::vector<std::unique_ptr<WorkerThread>> thread_pool_;
66 : TimeFunction time_function_;
67 : };
68 :
69 : } // namespace platform
70 : } // namespace v8
71 :
72 : #endif // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
|