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

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

Issue 2019763002: TaskScheduler: Atomic operations in TaskTracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR gab/robliao #30-31 Created 4 years, 5 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/base_export.h" 10 #include "base/base_export.h"
11 #include "base/callback_forward.h" 11 #include "base/callback_forward.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/metrics/histogram_base.h" 13 #include "base/metrics/histogram_base.h"
14 #include "base/synchronization/condition_variable.h" 14 #include "base/synchronization/waitable_event.h"
15 #include "base/task_scheduler/scheduler_lock.h" 15 #include "base/task_scheduler/scheduler_lock.h"
16 #include "base/task_scheduler/task.h" 16 #include "base/task_scheduler/task.h"
17 #include "base/task_scheduler/task_traits.h" 17 #include "base/task_scheduler/task_traits.h"
18 18
19 namespace base { 19 namespace base {
20 namespace internal { 20 namespace internal {
21 21
22 // All tasks go through the scheduler's TaskTracker when they are posted and 22 // All tasks go through the scheduler's TaskTracker when they are posted and
23 // when they are executed. The TaskTracker enforces shutdown semantics and takes 23 // when they are executed. The TaskTracker enforces shutdown semantics and takes
24 // care of tracing and profiling. This class is thread-safe. 24 // care of tracing and profiling. This class is thread-safe.
(...skipping 12 matching lines...) Expand all
37 void Shutdown(); 37 void Shutdown();
38 38
39 // Informs this TaskTracker that |task| is about to be posted. Returns true if 39 // Informs this TaskTracker that |task| is about to be posted. Returns true if
40 // this operation is allowed (|task| should be posted if-and-only-if it is). 40 // this operation is allowed (|task| should be posted if-and-only-if it is).
41 bool WillPostTask(const Task* task); 41 bool WillPostTask(const Task* task);
42 42
43 // Runs |task| unless the current shutdown state prevents that. WillPostTask() 43 // Runs |task| unless the current shutdown state prevents that. WillPostTask()
44 // must have allowed |task| to be posted. 44 // must have allowed |task| to be posted.
45 void RunTask(const Task* task); 45 void RunTask(const Task* task);
46 46
47 // Returns true if shutdown has completed.
48 bool ShutdownCompleted() const;
49
47 // Returns true while shutdown is in progress (i.e. Shutdown() has been called 50 // Returns true while shutdown is in progress (i.e. Shutdown() has been called
48 // but hasn't returned). 51 // but hasn't returned).
49 bool IsShuttingDownForTesting() const; 52 bool IsShuttingDownForTesting() const;
50 53
51 bool shutdown_completed() const { 54 private:
52 AutoSchedulerLock auto_lock(lock_); 55 class State;
53 return shutdown_completed_;
54 }
55 56
56 private:
57 // Called before WillPostTask() informs the tracing system that a task has 57 // Called before WillPostTask() informs the tracing system that a task has
58 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and 58 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and
59 // returns true if the current shutdown state allows the task to be posted. 59 // returns true if the current shutdown state allows the task to be posted.
60 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior); 60 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior);
61 61
62 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates 62 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates
63 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current 63 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current
64 // shutdown state allows the task to be run. 64 // shutdown state allows the task to be run.
65 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior); 65 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior);
66 66
67 // Called after a task with |shutdown_behavior| has been run by RunTask(). 67 // Called after a task with |shutdown_behavior| has been run by RunTask().
68 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if 68 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if
69 // necessary. 69 // necessary.
70 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); 70 void AfterRunTask(TaskShutdownBehavior shutdown_behavior);
71 71
72 // Synchronizes access to all members. 72 // Called when the number of tasks blocking shutdown becomes zero after
73 mutable SchedulerLock lock_; 73 // shutdown has started.
74 void OnBlockingShutdownTasksComplete();
74 75
75 // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches 76 // Number of tasks blocking shutdown and boolean indicating whether shutdown
76 // zero while shutdown is in progress. Null if shutdown isn't in progress. 77 // has started.
77 std::unique_ptr<ConditionVariable> shutdown_cv_; 78 const std::unique_ptr<State> state_;
78 79
79 // Number of tasks blocking shutdown. 80 // Synchronizes access to shutdown related members below.
80 size_t num_tasks_blocking_shutdown_ = 0; 81 mutable SchedulerLock shutdown_lock_;
82
83 // Event instantiated when shutdown starts and signaled when shutdown
84 // completes.
85 std::unique_ptr<WaitableEvent> shutdown_event_;
81 86
82 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. 87 // Number of BLOCK_SHUTDOWN tasks posted during shutdown.
83 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; 88 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0;
84 89
85 // True once Shutdown() has returned. No new task can be scheduled after this.
86 bool shutdown_completed_ = false;
87
88 DISALLOW_COPY_AND_ASSIGN(TaskTracker); 90 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
89 }; 91 };
90 92
91 } // namespace internal 93 } // namespace internal
92 } // namespace base 94 } // namespace base
93 95
94 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ 96 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698