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

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

Issue 2077093002: Rename SchedulerWorkerThread* to SchedulerWorker* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@rename2
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
(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_SCHEDULER_WORKER_THREAD_H_
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
7
8 #include <memory>
9
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/task_scheduler/scheduler_lock.h"
15 #include "base/task_scheduler/sequence.h"
16 #include "base/threading/platform_thread.h"
17 #include "base/time/time.h"
18
19 namespace base {
20 namespace internal {
21
22 class TaskTracker;
23
24 // A thread that runs Tasks from Sequences returned by a delegate.
25 //
26 // 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 // 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 // has completed and exits when it has.
31 //
32 // This class is thread-safe.
33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
34 public:
35 // Delegate interface for SchedulerWorkerThread. The methods are always called
36 // from the thread managed by the SchedulerWorkerThread instance.
37 class Delegate {
38 public:
39 virtual ~Delegate() = default;
40
41 // Called by |worker_thread| when it enters its main function.
42 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0;
43
44 // Called by |worker_thread| to get a Sequence from which to run a Task.
45 virtual scoped_refptr<Sequence> GetWork(
46 SchedulerWorkerThread* worker_thread) = 0;
47
48 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a
49 // Task from it. |sequence| is the last Sequence returned by GetWork().
50 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0;
51
52 // 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 // if the thread's WakeUp() method is called.
55 virtual TimeDelta GetSleepTimeout() = 0;
56 };
57
58 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
59 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to
60 // handle shutdown behavior of Tasks. Returns nullptr if creating the
61 // underlying platform thread fails.
62 static std::unique_ptr<SchedulerWorkerThread> Create(
63 ThreadPriority thread_priority,
64 std::unique_ptr<Delegate> delegate,
65 TaskTracker* task_tracker);
66
67 // Destroying a SchedulerWorkerThread in production is not allowed; it is
68 // always leaked. In tests, it can only be destroyed after JoinForTesting()
69 // has returned.
70 ~SchedulerWorkerThread() override;
71
72 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this
73 // is called, this SchedulerWorkerThread will run Tasks from Sequences
74 // returned by the GetWork() method of its delegate until it returns nullptr.
75 void WakeUp();
76
77 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); }
78
79 // Joins this SchedulerWorkerThread. If a Task is already running, it will be
80 // allowed to complete its execution. This can only be called once.
81 void JoinForTesting();
82
83 private:
84 SchedulerWorkerThread(ThreadPriority thread_priority,
85 std::unique_ptr<Delegate> delegate,
86 TaskTracker* task_tracker);
87
88 // PlatformThread::Delegate:
89 void ThreadMain() override;
90
91 bool ShouldExitForTesting() const;
92
93 // Platform thread managed by this SchedulerWorkerThread.
94 PlatformThreadHandle thread_handle_;
95
96 // Event signaled to wake up this SchedulerWorkerThread.
97 WaitableEvent wake_up_event_;
98
99 const std::unique_ptr<Delegate> delegate_;
100 TaskTracker* const task_tracker_;
101
102 // Synchronizes access to |should_exit_for_testing_|.
103 mutable SchedulerLock should_exit_for_testing_lock_;
104
105 // True once JoinForTesting() has been called.
106 bool should_exit_for_testing_ = false;
107
108 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
109 };
110
111 } // namespace internal
112 } // namespace base
113
114 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698