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

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

Issue 1705943002: TaskScheduler [5/9] Task Tracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_3_pq
Patch Set: self review Created 4 years, 9 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_TASK_TRACKER_H_
6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_
7
8 #include "base/base_export.h"
9 #include "base/callback_forward.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/synchronization/condition_variable.h"
13 #include "base/task_scheduler/scheduler_lock.h"
14 #include "base/task_scheduler/task.h"
15 #include "base/task_scheduler/task_traits.h"
16
17 namespace base {
18 namespace internal {
19
20 // All tasks of the scheduler go through the TaskTracker when they are posted
21 // and when they are executed. The TaskTracker enforces shutdown semantics and
22 // takes care of tracing and profiling. This class is thread-safe.
23 class BASE_EXPORT TaskTracker {
24 public:
25 TaskTracker();
26 ~TaskTracker();
27
28 // Synchronously shuts down the task tracker. Once this is called, only tasks
robliao 2016/03/17 23:34:21 I would go further and say this shuts down the sch
fdoray 2016/03/18 20:35:39 Done.
29 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when:
30 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their
31 // execution.
32 // - All posted BLOCK_SHUTDOWN tasks have completed their execution.
33 // This must only be called once.
34 void Shutdown();
35
36 // Posts |task| by calling |post_task_callback| unless the current shutdown
37 // state prevents that. A task forwarded to |post_task_callback| must be
38 // handed back to this class' RunTask() when it is to be executed.
robliao 2016/03/17 23:34:21 Nit: class' -> instance's
fdoray 2016/03/18 20:35:39 Done.
39 void PostTask(const Callback<void(scoped_ptr<Task>)>& post_task_callback,
40 scoped_ptr<Task> task);
41
42 // Runs |task| unless the current shutdown state prevents that. |task| must
43 // have been successfully posted via PostTask() first.
44 void RunTask(const Task* task);
45
46 // Returns true while shutdown is in progress (i.e. Shutdown() has been called
47 // but hasn't returned).
48 bool IsShuttingDownForTesting() const;
49
50 bool shutdown_completed() const {
51 AutoSchedulerLock auto_lock(lock_);
52 return shutdown_completed_;
53 }
54
55 private:
56 // Called before a task with |shutdown_behavior| is handed off to
57 // |post_task_callback| by PostTask(). Updates |num_tasks_blocking_shutdown_|
58 // if necessary and returns true if the current shutdown state allows the task
59 // to be posted.
60 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior);
61
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
64 // shutdown state allows the task to be run.
65 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior);
66
67 // Called after a task with |shutdown_behavior| has been run by RunTask().
68 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if
69 // necessary.
70 void AfterRunTask(TaskShutdownBehavior shutdown_behavior);
71
72 // Synchronizes access to all members.
73 mutable SchedulerLock lock_;
74
75 // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches
76 // zero while shutdown is in progress.
gab 2016/03/18 18:48:17 Add: "null if shutdown isn't in progress" as the i
fdoray 2016/03/18 20:35:39 Done.
77 scoped_ptr<ConditionVariable> shutdown_cv_;
78
79 // Number of tasks blocking shutdown.
80 size_t num_tasks_blocking_shutdown_ = 0;
81
82 // True once Shutdown() has returned. No new task can be scheduled after this.
83 bool shutdown_completed_ = false;
84
85 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
86 };
87
88 } // namespace internal
89 } // namespace base
90
91 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698