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

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

Issue 1704113002: TaskScheduler [6] SchedulerWorkerThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_4_shutdown
Patch Set: add main entry callback 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
(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 "base/base_export.h"
9 #include "base/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.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
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 SchedulerWorkerThread is woken up when its WakeUp() method is called. After
26 // a wake- up, a SchedulerWorkerThread runs Tasks from Sequences returned by its
27 // "get work" callback as long as it doesn't return nullptr. It also
28 // periodically checks with its TaskTracker whether shutdown has completed and
29 // exits when it has.
30 //
31 // This class is thread-safe.
32 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
33 public:
34 // Callback invoked to get a Sequence from which to run a Task on
35 // |worker_thread|.
36 using GetWorkCallback =
37 Callback<scoped_refptr<Sequence>(SchedulerWorkerThread* worker_thread)>;
38
39 // Callback invoked after |worker_thread| has tried to run a Task from
40 // |sequence| (a TaskTracker might have prevented the Task from running).
41 using RanTaskFromSequenceCallback =
42 Callback<void(const SchedulerWorkerThread* worker_thread,
43 scoped_refptr<Sequence> sequence)>;
44
45 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
46 // Tasks from Sequences returned by |get_work_callback|. |main_entry_callback|
47 // is invoked when the main function of the SchedulerWorkerThread is entered.
48 // |ran_task_from_sequence_callback| is invoked after the
49 // SchedulerWorkerThread has tried to run a Task from a Sequence returned by
50 // |get_work_callback|. |task_tracker| is used to handle shutdown behavior of
51 // Tasks. Returns nullptr if creating the underlying platform thread fails.
52 static scoped_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread(
53 ThreadPriority thread_priority,
54 const Closure& main_entry_callback,
55 const GetWorkCallback& get_work_callback,
56 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
57 TaskTracker* task_tracker);
58
59 // Destroying a SchedulerWorkerThread in production is not allowed; it is
60 // always leaked. In tests, it can only be destroyed after JoinForTesting()
61 // has returned.
62 ~SchedulerWorkerThread() override;
63
64 // Wakes up this SchedulerWorkerThread. After this is called, this
65 // SchedulerWorkerThread will run Tasks from Sequences returned by
66 // |get_work_callback_| until it returns nullptr.
67 void WakeUp();
68
69 // Joins this SchedulerWorkerThread. If a Task is already running, it will be
70 // allowed to complete its execution. This can only be called once.
71 void JoinForTesting();
72
73 private:
74 SchedulerWorkerThread(
75 ThreadPriority thread_priority,
76 const Closure& main_entry_callback,
77 const GetWorkCallback& get_work_callback,
78 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
gab 2016/04/05 23:35:20 1 callback is essentially a mini Delegate, 2 callb
fdoray 2016/04/07 16:07:43 Done here https://codereview.chromium.org/18643330
79 TaskTracker* task_tracker);
80
81 // PlatformThread::Delegate:
82 void ThreadMain() override;
83
84 bool ShouldExitForTesting() const;
85
86 // Platform thread managed by this SchedulerWorkerThread.
87 PlatformThreadHandle thread_handle_;
88
89 // Event signaled to wake up this SchedulerWorkerThread.
90 WaitableEvent wake_up_event_;
91
92 const Closure main_entry_callback_;
93 const GetWorkCallback get_work_callback_;
94 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_;
95 TaskTracker* const task_tracker_;
96
97 // Synchronizes access to |should_exit_for_testing_|.
98 mutable SchedulerLock should_exit_for_testing_lock_;
gab 2016/04/05 23:35:20 Instead of using a lock just for this here we coul
fdoray 2016/04/07 16:07:43 If should_exit_for_testing_ is volatile: Thread 1
gab 2016/04/07 16:49:07 Ah interesting, so it's not like Java's volatile w
99
100 // True once JoinForTesting() has been called.
101 bool should_exit_for_testing_ = false;
102
103 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
104 };
gab 2016/04/05 23:35:20 Where did the single threaded PQ go?
fdoray 2016/04/07 16:07:43 I will add it in another CL. It will be probably b
105
106 } // namespace internal
107 } // namespace base
108
109 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698