OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
6 #define BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
7 | |
8 #include "base/atomicops.h" | |
9 #include "base/base_export.h" | |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/synchronization/waitable_event.h" | |
15 #include "base/task_scheduler/sequence.h" | |
16 #include "base/threading/platform_thread.h" | |
17 | |
18 namespace base { | |
19 namespace internal { | |
20 | |
21 class TaskTracker; | |
22 | |
23 // A thread that runs Tasks from Sequences returned by a callback. | |
24 // | |
25 // A WorkerThread is woken up when its WakeUp() method is called. After a wake- | |
26 // up, a WorkerThread runs Tasks from Sequences returned by its "get work" | |
27 // callback as long as it doesn't return nullptr. It also periodically checks | |
28 // with its TaskTracker whether shutdown has completed and exits when it has. | |
29 // | |
30 // This class is thread-safe. | |
31 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate { | |
danakj
2016/03/31 00:25:13
Did you know there's already a class named WorkerT
fdoray
2016/03/31 03:26:31
Done. Renamed to SchedulerWorkerThread.
fdoray
2016/03/31 13:29:24
Another solution would be to have a base::task_sch
danakj
2016/03/31 22:33:49
I prefer changing the class name as you did.
| |
32 public: | |
33 // Callback invoked to get a Sequence from which to run a Task on | |
34 // |worker_thread|. | |
35 using GetWorkCallback = | |
36 Callback<scoped_refptr<Sequence>(const WorkerThread* worker_thread)>; | |
37 | |
38 // Callback invoked after |worker_thread| has tried to run a Task from | |
39 // |sequence| (a TaskTracker might have prevented the Task from running). | |
40 using RanTaskFromSequenceCallback = | |
41 Callback<void(const WorkerThread* worker_thread, | |
42 scoped_refptr<Sequence> sequence)>; | |
43 | |
44 // Creates a WorkerThread with priority |thread_priority| that runs Tasks from | |
45 // Sequences returned by |get_work_callback|. | |
46 // |ran_task_from_sequence_callback| is invoked after the WorkerThread has | |
47 // tried to run a Task from a Sequence returned by |get_work_callback|. | |
48 // |task_tracker| is used to handle shutdown behavior of Tasks. | |
49 static scoped_ptr<WorkerThread> CreateWorkerThread( | |
danakj
2016/03/31 00:25:13
When can this return null? Please document.
fdoray
2016/03/31 03:26:31
Done.
| |
50 ThreadPriority thread_priority, | |
51 const GetWorkCallback& get_work_callback, | |
52 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
53 TaskTracker* task_tracker); | |
54 | |
55 // Destroying a WorkerThread in production is not allowed; it is always | |
56 // leaked. In tests, it can only be destroyed after JoinForTesting() has | |
57 // returned. | |
58 ~WorkerThread() override; | |
59 | |
60 // Wakes up this WorkerThread. After this is called, this WorkerThread will | |
61 // run Tasks from Sequences returned by |get_work_callback_| until it returns | |
62 // nullptr. | |
63 void WakeUp(); | |
64 | |
65 // Joins this WorkerThread. If a Task is already running, it will be allowed | |
66 // to complete its execution. This can only be called once. | |
67 void JoinForTesting(); | |
68 | |
69 private: | |
70 WorkerThread( | |
71 ThreadPriority thread_priority, | |
72 const GetWorkCallback& get_work_callback, | |
73 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
74 TaskTracker* task_tracker); | |
75 | |
76 // PlatformThread::Delegate: | |
77 void ThreadMain() override; | |
78 | |
79 bool should_exit_for_testing() const { | |
80 base::subtle::MemoryBarrier(); | |
danakj
2016/03/31 00:25:13
uhmm.. I never expected to see this. :P Why aren't
fdoray
2016/03/31 03:26:31
I wanted to avoid taking a lock in production code
| |
81 return should_exit_for_testing_; | |
82 } | |
83 | |
84 // Platform thread managed by this WorkerThread. | |
85 PlatformThreadHandle thread_handle_; | |
86 | |
87 // Event signaled to wake up this WorkerThread. | |
88 WaitableEvent wake_up_event_; | |
89 | |
90 const GetWorkCallback get_work_callback_; | |
91 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_; | |
92 TaskTracker* const task_tracker_; | |
93 | |
94 // True once JoinForTesting() has been called. | |
95 bool should_exit_for_testing_ = false; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(WorkerThread); | |
98 }; | |
99 | |
100 } // namespace internal | |
101 } // namespace base | |
102 | |
103 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
OLD | NEW |