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

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

Issue 2044023003: Virtualize The Existence of a Scheduler Worker Thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@detach
Patch Set: Created 4 years, 6 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/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 #include "base/time/time.h" 17 #include "base/time/time.h"
18 18
19 namespace base { 19 namespace base {
20
20 namespace internal { 21 namespace internal {
21 22
22 class TaskTracker; 23 class TaskTracker;
23 24
24 // A thread that runs Tasks from Sequences returned by a delegate. 25 // A thread that runs Tasks from Sequences returned by a delegate.
fdoray 2016/06/08 17:51:48 s/SchedulerWorkerThread/SchedulerWorker/ ? This is
robliao 2016/06/08 19:00:08 This comes down to if the thread should be virtual
25 // 26 //
26 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to 27 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to
27 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences 28 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences
28 // returned by the GetWork() method of its delegate as long as it doesn't return 29 // returned by the GetWork() method of its delegate as long as it doesn't return
29 // nullptr. It also periodically checks with its TaskTracker whether shutdown 30 // nullptr. It also periodically checks with its TaskTracker whether shutdown
30 // has completed and exits when it has. 31 // has completed and exits when it has.
31 // 32 //
32 // This class is thread-safe. 33 // This class is thread-safe.
33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { 34 class BASE_EXPORT SchedulerWorkerThread {
34 public: 35 public:
35 // Delegate interface for SchedulerWorkerThread. The methods are always called 36 // Delegate interface for SchedulerWorkerThread. The methods are always called
36 // from the thread managed by the SchedulerWorkerThread instance. 37 // from the thread managed by the SchedulerWorkerThread instance.
37 class Delegate { 38 class Delegate {
38 public: 39 public:
39 virtual ~Delegate() = default; 40 virtual ~Delegate() = default;
40 41
41 // Called by |worker_thread| when it enters its main function. 42 // Called by |worker_thread| when it enters its main function.
fdoray 2016/06/08 17:51:48 Comment that this is called every time a "real" th
robliao 2016/06/08 19:00:09 Updated comment.
42 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0; 43 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0;
43 44
44 // Called by |worker_thread| to get a Sequence from which to run a Task. 45 // Called by |worker_thread| to get a Sequence from which to run a Task.
45 virtual scoped_refptr<Sequence> GetWork( 46 virtual scoped_refptr<Sequence> GetWork(
46 SchedulerWorkerThread* worker_thread) = 0; 47 SchedulerWorkerThread* worker_thread) = 0;
47 48
48 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a 49 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a
49 // Task from it. |sequence| is the last Sequence returned by GetWork(). 50 // Task from it. |sequence| is the last Sequence returned by GetWork().
50 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; 51 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0;
51 52
52 // Called by |worker_thread| to determine how long to sleep before the next 53 // Called by |worker_thread| to determine how long to sleep before the next
53 // call to GetWork(). GetWork() may be called before this timeout expires 54 // call to GetWork(). GetWork() may be called before this timeout expires
54 // if the thread's WakeUp() method is called. 55 // if the thread's WakeUp() method is called.
55 virtual TimeDelta GetSleepTimeout() = 0; 56 virtual TimeDelta GetSleepTimeout() = 0;
57
58 // Called by |worker_thread| to see if it is allowed to detach.
fdoray 2016/06/08 17:51:48 s/CanDetach/CanHibernate/ ? I think those who impl
robliao 2016/06/08 19:00:08 Hibernation in the natural sense doesn't actually
59 virtual bool CanDetach(SchedulerWorkerThread* worker_thread) = 0;
fdoray 2016/06/08 17:51:48 This is called after GetWork() returns nullptr.
robliao 2016/06/08 19:00:09 Updated loosing the ordering constraint.
56 }; 60 };
57 61
62 enum class InitialWorkerState { ALIVE, DETACHED };
fdoray 2016/06/08 17:51:49 ALIVE, HIBERNATING
robliao 2016/06/08 19:00:09 See discussion about hibernation vs detachment.
63
58 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs 64 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
59 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to 65 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to
60 // handle shutdown behavior of Tasks. Returns nullptr if creating the 66 // handle shutdown behavior of Tasks. Returns nullptr if creating the
61 // underlying platform thread fails. 67 // underlying platform thread fails.
62 static std::unique_ptr<SchedulerWorkerThread> Create( 68 static std::unique_ptr<SchedulerWorkerThread> Create(
63 ThreadPriority thread_priority, 69 ThreadPriority thread_priority,
64 std::unique_ptr<Delegate> delegate, 70 std::unique_ptr<Delegate> delegate,
65 TaskTracker* task_tracker); 71 TaskTracker* task_tracker,
72 InitialWorkerState worker_state);
fdoray 2016/06/08 17:51:49 I would remove this argument and always start HIBE
robliao 2016/06/08 19:00:08 The main benefit here is that it allows the Schedu
66 73
67 // Destroying a SchedulerWorkerThread in production is not allowed; it is 74 // Destroying a SchedulerWorkerThread in production is not allowed; it is
68 // always leaked. In tests, it can only be destroyed after JoinForTesting() 75 // always leaked. In tests, it can only be destroyed after JoinForTesting()
69 // has returned. 76 // has returned.
70 ~SchedulerWorkerThread() override; 77 ~SchedulerWorkerThread();
71 78
72 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this 79 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this
73 // is called, this SchedulerWorkerThread will run Tasks from Sequences 80 // is called, this SchedulerWorkerThread will run Tasks from Sequences
74 // returned by the GetWork() method of its delegate until it returns nullptr. 81 // returned by the GetWork() method of its delegate until it returns nullptr.
fdoray 2016/06/08 17:51:49 May fail if the thread was hibernating.
robliao 2016/06/08 19:00:09 Updated comment.
75 void WakeUp(); 82 void WakeUp();
76 83
77 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); } 84 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); }
78 85
79 // Joins this SchedulerWorkerThread. If a Task is already running, it will be 86 // Joins this SchedulerWorkerThread. If a Task is already running, it will be
80 // allowed to complete its execution. This can only be called once. 87 // allowed to complete its execution. This can only be called once.
81 void JoinForTesting(); 88 void JoinForTesting();
82 89
90 // Whether is worker is alive.
fdoray 2016/06/08 17:51:48 // Returns true if the worker is alive. + const
robliao 2016/06/08 19:00:09 I think I was falling into java style habits with
91 bool WorkerAliveForTesting();
92
83 private: 93 private:
94 class Worker;
95
84 SchedulerWorkerThread(ThreadPriority thread_priority, 96 SchedulerWorkerThread(ThreadPriority thread_priority,
85 std::unique_ptr<Delegate> delegate, 97 std::unique_ptr<Delegate> delegate,
86 TaskTracker* task_tracker); 98 TaskTracker* task_tracker);
87 99
88 // PlatformThread::Delegate: 100 // Returns the worker for ownership by the worker thread if the detach was
89 void ThreadMain() override; 101 // successful. If the detach is not possible, returns nullptr.
102 std::unique_ptr<SchedulerWorkerThread::Worker> Detach();
103
104 void CreateWorker();
105
106 void CreateWorkerAssertSynchronized();
90 107
91 bool ShouldExitForTesting() const; 108 bool ShouldExitForTesting() const;
92 109
93 // Platform thread managed by this SchedulerWorkerThread. 110 // Synchronizes access to |worker_|
94 PlatformThreadHandle thread_handle_; 111 SchedulerLock worker_lock_;
95 112
96 // Event signaled to wake up this SchedulerWorkerThread. 113 // The underlying thread for this SchedulerWorkerThread.
97 WaitableEvent wake_up_event_; 114 std::unique_ptr<Worker> worker_;
98 115
116 const ThreadPriority thread_priority_;
99 const std::unique_ptr<Delegate> delegate_; 117 const std::unique_ptr<Delegate> delegate_;
100 TaskTracker* const task_tracker_; 118 TaskTracker* const task_tracker_;
101 119
102 // Synchronizes access to |should_exit_for_testing_|. 120 // Synchronizes access to |should_exit_for_testing_|.
103 mutable SchedulerLock should_exit_for_testing_lock_; 121 mutable SchedulerLock should_exit_for_testing_lock_;
104 122
105 // True once JoinForTesting() has been called. 123 // True once JoinForTesting() has been called.
106 bool should_exit_for_testing_ = false; 124 bool should_exit_for_testing_ = false;
107 125
108 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); 126 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
109 }; 127 };
110 128
111 } // namespace internal 129 } // namespace internal
112 } // namespace base 130 } // namespace base
113 131
114 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ 132 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698