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

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

Issue 2362253002: TaskScheduler: Add FlushForTesting(). (Closed)
Patch Set: self-review Created 4 years, 2 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_TASK_TRACKER_H_ 5 #ifndef BASE_TASK_SCHEDULER_TASK_TRACKER_H_
6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_ 6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/atomicops.h"
10 #include "base/base_export.h" 11 #include "base/base_export.h"
11 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/metrics/histogram_base.h" 14 #include "base/metrics/histogram_base.h"
14 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
15 #include "base/task_scheduler/scheduler_lock.h" 16 #include "base/task_scheduler/scheduler_lock.h"
16 #include "base/task_scheduler/sequence.h" 17 #include "base/task_scheduler/sequence.h"
17 #include "base/task_scheduler/task.h" 18 #include "base/task_scheduler/task.h"
18 #include "base/task_scheduler/task_traits.h" 19 #include "base/task_scheduler/task_traits.h"
19 20
20 namespace base { 21 namespace base {
21 22
23 class ConditionVariable;
22 class SequenceToken; 24 class SequenceToken;
23 25
24 namespace internal { 26 namespace internal {
25 27
26 // All tasks go through the scheduler's TaskTracker when they are posted and 28 // All tasks go through the scheduler's TaskTracker when they are posted and
27 // when they are executed. The TaskTracker enforces shutdown semantics and takes 29 // when they are executed. The TaskTracker enforces shutdown semantics and takes
28 // care of tracing and profiling. This class is thread-safe. 30 // care of tracing and profiling. This class is thread-safe.
29 class BASE_EXPORT TaskTracker { 31 class BASE_EXPORT TaskTracker {
30 public: 32 public:
31 TaskTracker(); 33 TaskTracker();
32 ~TaskTracker(); 34 ~TaskTracker();
33 35
34 // Synchronously shuts down the scheduler. Once this is called, only tasks 36 // Synchronously shuts down the scheduler. Once this is called, only tasks
35 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: 37 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when:
36 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their 38 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their
37 // execution. 39 // execution.
38 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. 40 // - All posted BLOCK_SHUTDOWN tasks have completed their execution.
39 // CONTINUE_ON_SHUTDOWN tasks still may be running after Shutdown returns. 41 // CONTINUE_ON_SHUTDOWN tasks still may be running after Shutdown returns.
40 // This can only be called once. 42 // This can only be called once.
41 void Shutdown(); 43 void Shutdown();
42 44
45 // Waits until there are no pending undelayed tasks. May be called in tests
46 // to validate that a condition is met after all undelayed tasks have run.
47 //
48 // Does not wait for delayed tasks. Waits for undelayed tasks posted from
49 // other threads during the call. Returns immediately when shutdown completes.
50 void FlushForTesting();
51
43 // Informs this TaskTracker that |task| is about to be posted. Returns true if 52 // Informs this TaskTracker that |task| is about to be posted. Returns true if
44 // this operation is allowed (|task| should be posted if-and-only-if it is). 53 // this operation is allowed (|task| should be posted if-and-only-if it is).
45 bool WillPostTask(const Task* task); 54 bool WillPostTask(const Task* task);
46 55
47 // Runs |task| unless the current shutdown state prevents that. 56 // Runs |task| unless the current shutdown state prevents that.
48 // |sequence_token| is the token identifying the sequence from which |task| 57 // |sequence_token| is the token identifying the sequence from which |task|
49 // was extracted. Returns true if |task| ran. WillPostTask() must have allowed 58 // was extracted. Returns true if |task| ran. WillPostTask() must have allowed
50 // |task| to be posted before this is called. 59 // |task| to be posted before this is called.
51 bool RunTask(const Task* task, const SequenceToken& sequence_token); 60 bool RunTask(const Task* task, const SequenceToken& sequence_token);
52 61
53 // Returns true once shutdown has started (Shutdown() has been called but 62 // Returns true once shutdown has started (Shutdown() has been called but
54 // might not have returned). Note: sequential consistency with the thread 63 // might not have returned). Note: sequential consistency with the thread
55 // calling Shutdown() (or SetHasShutdownStartedForTesting()) isn't guaranteed 64 // calling Shutdown() (or SetHasShutdownStartedForTesting()) isn't guaranteed
56 // by this call. 65 // by this call.
57 bool HasShutdownStarted() const; 66 bool HasShutdownStarted() const;
58 67
59 // Returns true if shutdown has completed (Shutdown() has returned). 68 // Returns true if shutdown has completed (Shutdown() has returned).
60 bool IsShutdownComplete() const; 69 bool IsShutdownComplete() const;
61 70
62 // Causes HasShutdownStarted() to return true. Unlike when Shutdown() returns, 71 // Causes HasShutdownStarted() to return true. Unlike when Shutdown() returns,
63 // IsShutdownComplete() won't return true after this returns. Shutdown() 72 // IsShutdownComplete() won't return true after this returns. Shutdown()
64 // cannot be called after this. 73 // cannot be called after this.
65 void SetHasShutdownStartedForTesting(); 74 void SetHasShutdownStartedForTesting();
66 75
67 private: 76 private:
68 class State; 77 class State;
69 78
79 void PerformShutdown();
80
70 // Called before WillPostTask() informs the tracing system that a task has 81 // Called before WillPostTask() informs the tracing system that a task has
71 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and 82 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and
72 // returns true if the current shutdown state allows the task to be posted. 83 // returns true if the current shutdown state allows the task to be posted.
73 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior); 84 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior);
74 85
75 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates 86 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates
76 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current 87 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current
77 // shutdown state allows the task to be run. 88 // shutdown state allows the task to be run.
78 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior); 89 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior);
79 90
80 // Called after a task with |shutdown_behavior| has been run by RunTask(). 91 // Called after a task with |shutdown_behavior| has been run by RunTask().
81 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if 92 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if
82 // necessary. 93 // necessary.
83 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); 94 void AfterRunTask(TaskShutdownBehavior shutdown_behavior);
84 95
85 // Called when the number of tasks blocking shutdown becomes zero after 96 // Called when the number of tasks blocking shutdown becomes zero after
86 // shutdown has started. 97 // shutdown has started.
87 void OnBlockingShutdownTasksComplete(); 98 void OnBlockingShutdownTasksComplete();
88 99
100 // Decrements the number of pending undelayed tasks and signals
101 // |flush_for_testing_cv_| if it reaches zero.
102 void DecrementNumPendingUndelayedTasks();
103
89 // Number of tasks blocking shutdown and boolean indicating whether shutdown 104 // Number of tasks blocking shutdown and boolean indicating whether shutdown
90 // has started. 105 // has started.
91 const std::unique_ptr<State> state_; 106 const std::unique_ptr<State> state_;
92 107
108 // Number of undelayed tasks that haven't completed their execution.
109 subtle::Atomic32 num_pending_undelayed_tasks_ = 0;
danakj 2016/09/23 20:51:41 I think it'd be nice to document that you intend t
fdoray 2016/09/23 21:09:46 Done.
110
111 // Synchronizes access to |flush_for_testing_cv_|.
112 mutable SchedulerLock flush_for_testing_lock_;
113
114 // Signaled when |num_pending_undelayed_tasks_| is zero or when shutdown
115 // completes.
116 const std::unique_ptr<ConditionVariable> flush_for_testing_cv_;
117
93 // Synchronizes access to shutdown related members below. 118 // Synchronizes access to shutdown related members below.
94 mutable SchedulerLock shutdown_lock_; 119 mutable SchedulerLock shutdown_lock_;
95 120
96 // Event instantiated when shutdown starts and signaled when shutdown 121 // Event instantiated when shutdown starts and signaled when shutdown
97 // completes. 122 // completes.
98 std::unique_ptr<WaitableEvent> shutdown_event_; 123 std::unique_ptr<WaitableEvent> shutdown_event_;
99 124
100 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. 125 // Number of BLOCK_SHUTDOWN tasks posted during shutdown.
101 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; 126 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0;
102 127
103 DISALLOW_COPY_AND_ASSIGN(TaskTracker); 128 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
104 }; 129 };
105 130
106 } // namespace internal 131 } // namespace internal
107 } // namespace base 132 } // namespace base
108 133
109 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ 134 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698