OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/task_scheduler/scheduler_lock.h" | 14 #include "base/task_scheduler/scheduler_lock.h" |
15 #include "base/task_scheduler/sequence.h" | 15 #include "base/task_scheduler/sequence.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 namespace internal { | 19 namespace internal { |
20 | 20 |
21 class SchedulerWorkerThreadDelegate; | 21 class SchedulerWorkerThreadDelegate; |
22 class TaskTracker; | 22 class TaskTracker; |
23 | 23 |
24 // A thread that runs Tasks from Sequences returned by a delegate. | 24 // A thread that runs Tasks from Sequences returned by a delegate. |
25 // | 25 // |
26 // A SchedulerWorkerThread is woken up when its WakeUp() method is called. After | 26 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to |
27 // a wake-up, a SchedulerWorkerThread runs Tasks from Sequences returned by the | 27 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences |
28 // GetWork() method of its delegate as long as it doesn't return nullptr. It | 28 // returned by the GetWork() method of its delegate as long as it doesn't return |
29 // also periodically checks with its TaskTracker whether shutdown has completed | 29 // nullptr. It also periodically checks with its TaskTracker whether shutdown |
30 // and exits when it has. | 30 // has completed and exits when it has. |
31 // | 31 // |
32 // This class is thread-safe. | 32 // This class is thread-safe. |
33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { | 33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { |
34 public: | 34 public: |
35 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 35 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs |
36 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to | 36 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to |
37 // handle shutdown behavior of Tasks. Returns nullptr if creating the | 37 // handle shutdown behavior of Tasks. Returns nullptr if creating the |
38 // underlying platform thread fails. | 38 // underlying platform thread fails. |
39 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( | 39 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( |
40 ThreadPriority thread_priority, | 40 ThreadPriority thread_priority, |
41 SchedulerWorkerThreadDelegate* delegate, | 41 SchedulerWorkerThreadDelegate* delegate, |
42 TaskTracker* task_tracker); | 42 TaskTracker* task_tracker); |
43 | 43 |
44 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 44 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
45 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 45 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
46 // has returned. | 46 // has returned. |
47 ~SchedulerWorkerThread() override; | 47 ~SchedulerWorkerThread() override; |
48 | 48 |
49 // Wakes up this SchedulerWorkerThread. After this is called, this | 49 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this |
50 // SchedulerWorkerThread will run Tasks from Sequences returned by the | 50 // is called, this SchedulerWorkerThread will run Tasks from Sequences |
51 // GetWork() method of its delegate until it returns nullptr. | 51 // returned by the GetWork() method of its delegate until it returns nullptr. |
52 void WakeUp(); | 52 void WakeUp(); |
53 | 53 |
54 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 54 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
55 // allowed to complete its execution. This can only be called once. | 55 // allowed to complete its execution. This can only be called once. |
56 void JoinForTesting(); | 56 void JoinForTesting(); |
57 | 57 |
58 private: | 58 private: |
59 SchedulerWorkerThread(ThreadPriority thread_priority, | 59 SchedulerWorkerThread(ThreadPriority thread_priority, |
60 SchedulerWorkerThreadDelegate* delegate, | 60 SchedulerWorkerThreadDelegate* delegate, |
61 TaskTracker* task_tracker); | 61 TaskTracker* task_tracker); |
(...skipping 18 matching lines...) Expand all Loading... |
80 // True once JoinForTesting() has been called. | 80 // True once JoinForTesting() has been called. |
81 bool should_exit_for_testing_ = false; | 81 bool should_exit_for_testing_ = false; |
82 | 82 |
83 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 83 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
84 }; | 84 }; |
85 | 85 |
86 } // namespace internal | 86 } // namespace internal |
87 } // namespace base | 87 } // namespace base |
88 | 88 |
89 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 89 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
OLD | NEW |