Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_THREADING_TASK_RUNNER_HANDLE_H_ | |
| 6 #define BASE_THREADING_TASK_RUNNER_HANDLE_H_ | |
| 7 | |
| 8 #include "base/base_export.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/sequenced_task_runner.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/task_runner.h" | |
| 14 #include "base/threading/task_runner_handle_internal.h" | |
| 15 | |
| 16 namespace base { | |
| 17 | |
| 18 class BASE_EXPORT TaskRunnerHandle { | |
| 19 public: | |
| 20 // Returns a TaskRunner which will run tasks in the same context as the | |
| 21 // current task. That context could be: a MessageLoop, a sequence on a thread | |
| 22 // pool, or merely the current set of TaskTraits on a non-sequenced | |
| 23 // TaskScheduler task. | |
| 24 static scoped_refptr<TaskRunner> Get(); | |
| 25 | |
| 26 // Returns a SequencedTaskRunner associated with the current sequence. This | |
| 27 // must only be called if HasSequencedTaskScope(). Prefer Get() to this when | |
| 28 // sequencing is not required. | |
|
fdoray
2016/06/17 14:29:02
s/This must only be called.../This DCHECKs if !Has
gab
2016/06/20 19:55:36
Done.
| |
| 29 // TODO(gab): Migrate all callsites using SequencedTaskRunnerHandle::Get() to | |
| 30 // TaskRunnerHandle::Get() and remove support for getting a | |
| 31 // SequencedTaskRunner from a non-sequenced task in SequencedWorkerPool which | |
| 32 // currently breaks the semantics intended here. | |
| 33 static scoped_refptr<SequencedTaskRunner> GetSequenced(); | |
| 34 | |
| 35 // Returns a SingleThreadTaskRunner associated with the current | |
| 36 // single-threaded sequence. This must only be called if | |
|
fdoray
2016/06/17 14:29:02
s/This must only be called.../This DCHECKS if !Has
gab
2016/06/20 19:55:36
Done.
| |
| 37 // HasSingleThreadedTaskScope(). Only use this when thread-affinity is | |
| 38 // required, prefer Get() or GetSequenced() otherwise. | |
| 39 static scoped_refptr<SingleThreadTaskRunner> GetSingleThreaded(); | |
|
fdoray
2016/06/17 14:29:02
Why do we have GetSingleThread*ed*() and HasSingle
gab
2016/06/20 19:55:36
Because we have SingleThread*TaskRunner already bu
| |
| 40 | |
| 41 // Returns true if the current task is running as part of any TaskScope. | |
| 42 static bool HasTaskScope(); | |
| 43 | |
| 44 // Returns true if the current task is running as part of a SequencedTaskScope | |
| 45 // or a SingleThreadTaskScope. Note: This currently also supports | |
| 46 // SequencedWorkerPool's threads although they don't explicitly have a | |
| 47 // SequencedTaskScope but this support will be going away with the migration | |
| 48 // to the TaskScheduler. | |
|
fdoray
2016/06/17 14:29:02
TODO(gab): Remove this comment after SequencedWork
gab
2016/06/20 19:55:36
Reworded to include TODO
| |
| 49 static bool HasSequencedTaskScope(); | |
| 50 | |
| 51 // Returns true if the current task is running as part of a | |
| 52 // SingleThreadTaskScope. | |
| 53 static bool HasSingleThreadTaskScope(); | |
| 54 | |
| 55 // Each task should run in one and only one TaskScope (TaskScopes are held on | |
| 56 // the stack in the scope of a task and no TaskScope can live inside another | |
| 57 // TaskScope). The more specific Scopes enable the more generic getters but | |
| 58 // not vice-versa (e.g. SequencedTaskScope enables Get() and GetSequenced() | |
| 59 // but not GetSingleThreaded()). | |
| 60 | |
| 61 // A TaskScope which, throughout its lifetime, identifies the context | |
| 62 // (TaskRunner) in which tasks on the thread it lives on run in. | |
| 63 class BASE_EXPORT TaskScope { | |
| 64 public: | |
| 65 TaskScope(scoped_refptr<TaskRunner> task_runner); | |
|
fdoray
2016/06/17 14:29:02
The destructor is not trivial. ~TaskScope() here a
gab
2016/06/20 19:55:36
It isn't but it only deletes TaskScopeState which
| |
| 66 | |
| 67 private: | |
| 68 const internal::TaskScopeState state_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(TaskScope); | |
| 71 }; | |
| 72 | |
| 73 // A SequencedTaskScope which, throughout its lifetime, identifies the | |
| 74 // sequenced context (SequencedTaskRunner) in which tasks on the thread it | |
| 75 // lives on run in. | |
| 76 class BASE_EXPORT SequencedTaskScope { | |
| 77 public: | |
| 78 SequencedTaskScope( | |
| 79 scoped_refptr<SequencedTaskRunner> sequenced_task_runner); | |
| 80 | |
| 81 private: | |
| 82 const internal::TaskScopeState state_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(SequencedTaskScope); | |
| 85 }; | |
| 86 | |
| 87 // A SingleThreadedTaskScope which, throughout its lifetime, identifies the | |
| 88 // single- threaded context (SingleThreadedTaskRunner) in which tasks on the | |
|
fdoray
2016/06/17 14:29:02
SingleThread**TaskRunner :)
fdoray
2016/06/17 14:29:02
single- threaded
remove extra space
gab
2016/06/20 19:55:36
Done.
gab
2016/06/20 19:55:36
Done (I hate that name!)
| |
| 89 // thread it lives on run in. | |
| 90 class BASE_EXPORT SingleThreadTaskScope { | |
| 91 public: | |
| 92 SingleThreadTaskScope( | |
| 93 scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner); | |
| 94 | |
| 95 private: | |
| 96 const internal::TaskScopeState state_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(SingleThreadTaskScope); | |
| 99 }; | |
| 100 | |
| 101 private: | |
| 102 DISALLOW_IMPLICIT_CONSTRUCTORS(TaskRunnerHandle); | |
| 103 }; | |
| 104 | |
| 105 } // namespace base | |
| 106 | |
| 107 #endif // BASE_THREADING_TASK_RUNNER_HANDLE_H_ | |
| OLD | NEW |