Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: base/task_scheduler/scheduler_worker_thread.h

Issue 1864333002: TaskScheduler: Delegate instead of callbacks for SchedulerWorkerThread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR robliao #5 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/callback.h"
12 #include "base/macros.h" 11 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
15 #include "base/task_scheduler/scheduler_lock.h" 14 #include "base/task_scheduler/scheduler_lock.h"
16 #include "base/task_scheduler/sequence.h" 15 #include "base/task_scheduler/sequence.h"
17 #include "base/threading/platform_thread.h" 16 #include "base/threading/platform_thread.h"
18 17
19 namespace base { 18 namespace base {
20 namespace internal { 19 namespace internal {
21 20
21 class SchedulerWorkerThreadDelegate;
22 class TaskTracker; 22 class TaskTracker;
23 23
24 // A thread that runs Tasks from Sequences returned by a callback. 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 is woken up when its WakeUp() method is called. After
27 // a wake- up, a SchedulerWorkerThread runs Tasks from Sequences returned by its 27 // a wake-up, a SchedulerWorkerThread runs Tasks from Sequences returned by the
28 // "get work" callback as long as it doesn't return nullptr. It also 28 // GetWork() method of its delegate as long as it doesn't return nullptr. It
29 // periodically checks with its TaskTracker whether shutdown has completed and 29 // also periodically checks with its TaskTracker whether shutdown has completed
30 // exits when it has. 30 // 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 // Callback invoked to get a Sequence from which to run a Task on
36 // |worker_thread|.
37 using GetWorkCallback =
38 Callback<scoped_refptr<Sequence>(SchedulerWorkerThread* worker_thread)>;
39
40 // Callback invoked after |worker_thread| has tried to run a Task from
41 // |sequence| (a TaskTracker might have prevented the Task from running).
42 using RanTaskFromSequenceCallback =
43 Callback<void(const SchedulerWorkerThread* worker_thread,
44 scoped_refptr<Sequence> sequence)>;
45
46 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs 35 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
47 // Tasks from Sequences returned by |get_work_callback|. |main_entry_callback| 36 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to
48 // is invoked when the main function of the SchedulerWorkerThread is entered. 37 // handle shutdown behavior of Tasks. Returns nullptr if creating the
49 // |ran_task_from_sequence_callback| is invoked after the 38 // underlying platform thread fails.
50 // SchedulerWorkerThread has tried to run a Task from a Sequence returned by
51 // |get_work_callback|. |task_tracker| is used to handle shutdown behavior of
52 // Tasks. Returns nullptr if creating the underlying platform thread fails.
53 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( 39 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread(
54 ThreadPriority thread_priority, 40 ThreadPriority thread_priority,
55 const Closure& main_entry_callback, 41 SchedulerWorkerThreadDelegate* delegate,
56 const GetWorkCallback& get_work_callback,
57 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
58 TaskTracker* task_tracker); 42 TaskTracker* task_tracker);
59 43
60 // Destroying a SchedulerWorkerThread in production is not allowed; it is 44 // Destroying a SchedulerWorkerThread in production is not allowed; it is
61 // always leaked. In tests, it can only be destroyed after JoinForTesting() 45 // always leaked. In tests, it can only be destroyed after JoinForTesting()
62 // has returned. 46 // has returned.
63 ~SchedulerWorkerThread() override; 47 ~SchedulerWorkerThread() override;
64 48
65 // Wakes up this SchedulerWorkerThread. After this is called, this 49 // Wakes up this SchedulerWorkerThread. After this is called, this
66 // SchedulerWorkerThread will run Tasks from Sequences returned by 50 // SchedulerWorkerThread will run Tasks from Sequences returned by the
67 // |get_work_callback_| until it returns nullptr. 51 // GetWork() method of its delegate until it returns nullptr.
68 void WakeUp(); 52 void WakeUp();
69 53
70 // 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
71 // allowed to complete its execution. This can only be called once. 55 // allowed to complete its execution. This can only be called once.
72 void JoinForTesting(); 56 void JoinForTesting();
73 57
74 private: 58 private:
75 SchedulerWorkerThread( 59 SchedulerWorkerThread(ThreadPriority thread_priority,
76 ThreadPriority thread_priority, 60 SchedulerWorkerThreadDelegate* delegate,
77 const Closure& main_entry_callback, 61 TaskTracker* task_tracker);
78 const GetWorkCallback& get_work_callback,
79 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
80 TaskTracker* task_tracker);
81 62
82 // PlatformThread::Delegate: 63 // PlatformThread::Delegate:
83 void ThreadMain() override; 64 void ThreadMain() override;
84 65
85 bool ShouldExitForTesting() const; 66 bool ShouldExitForTesting() const;
86 67
87 // Platform thread managed by this SchedulerWorkerThread. 68 // Platform thread managed by this SchedulerWorkerThread.
88 PlatformThreadHandle thread_handle_; 69 PlatformThreadHandle thread_handle_;
89 70
90 // Event signaled to wake up this SchedulerWorkerThread. 71 // Event signaled to wake up this SchedulerWorkerThread.
91 WaitableEvent wake_up_event_; 72 WaitableEvent wake_up_event_;
92 73
93 const Closure main_entry_callback_; 74 SchedulerWorkerThreadDelegate* const delegate_;
94 const GetWorkCallback get_work_callback_;
95 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_;
96 TaskTracker* const task_tracker_; 75 TaskTracker* const task_tracker_;
97 76
98 // Synchronizes access to |should_exit_for_testing_|. 77 // Synchronizes access to |should_exit_for_testing_|.
99 mutable SchedulerLock should_exit_for_testing_lock_; 78 mutable SchedulerLock should_exit_for_testing_lock_;
100 79
101 // True once JoinForTesting() has been called. 80 // True once JoinForTesting() has been called.
102 bool should_exit_for_testing_ = false; 81 bool should_exit_for_testing_ = false;
103 82
104 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); 83 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
105 }; 84 };
106 85
107 } // namespace internal 86 } // namespace internal
108 } // namespace base 87 } // namespace base
109 88
110 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ 89 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698