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 | |
| 12 namespace base { | |
| 13 | |
| 14 class SequencedTaskRunner; | |
| 15 class SingleThreadTaskRunner; | |
| 16 class TaskRunner; | |
| 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, for example, be: a MessageLoop, a | |
|
robliao
2016/06/09 15:40:18
Nit: Remove "for example." The hypothetical is cle
gab
2016/06/14 15:38:59
Done.
| |
| 22 // sequence on a thread pool, or merely the current set of TaskTraits on a | |
| 23 // non-sequenced 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. TODO(gab): Migrate all callsites using | |
|
robliao
2016/06/09 15:40:18
Linebreak this TODO so it's easier to see.
gab
2016/06/14 15:38:59
Done.
| |
| 29 // SequencedTaskRunnerHandle::Get() to TaskRunnerHandle::Get() and remove | |
| 30 // support for getting a SequencedTaskRunner from a non-sequenced task in | |
| 31 // SequencedWorkerPool which currently breaks the semantics intended here. | |
| 32 static scoped_refptr<SequencedTaskRunner> GetSequenced(); | |
| 33 | |
| 34 // Returns a SingleThreadTaskRunner associated with the current single- | |
| 35 // threaded sequence. This must only be called if | |
| 36 // HasSingleThreadedTaskScope(). Only use this when thread-affinity is | |
| 37 // required, prefer Get() or GetSequenced() otherwise. | |
| 38 static scoped_refptr<SingleThreadTaskRunner> GetSingleThreaded(); | |
| 39 | |
| 40 // Returns true if the current task is running as part of any TaskScope. | |
| 41 static bool HasTaskScope(); | |
| 42 | |
| 43 // Returns true if the current task is running as part of a SequencedTaskScope | |
| 44 // or a SingleThreadTaskScope. Note: This currently also supports | |
| 45 // SequencedWorkerPool's threads although they don't explicitly have a | |
| 46 // SequencedTaskScope but this support will be going away with the migration | |
| 47 // to the TaskScheduler. | |
| 48 static bool HasSequencedTaskScope(); | |
| 49 | |
| 50 // Returns true if the current task is running as part of a | |
| 51 // SingleThreadTaskScope. | |
| 52 static bool HasSingleThreadTaskScope(); | |
| 53 | |
| 54 // Each task should run in one and only one Scope (TaskScopes are held on the | |
| 55 // stack in the scope of a task and no TaskScope can live inside another | |
| 56 // TaskScope). The more specific Scopes enable the more generic getters but | |
| 57 // not vice-versa (e.g. SequencedTaskScope enables Get() and GetSequenced() | |
| 58 // but not GetSingleThreaded()). | |
| 59 | |
| 60 // A TaskScope which, throughout its lifetime, identifies the context | |
| 61 // (TaskRunner) in which tasks on the thread it lives on run in. | |
| 62 class BASE_EXPORT TaskScope { | |
| 63 public: | |
| 64 TaskScope(scoped_refptr<TaskRunner> task_runner); | |
| 65 ~TaskScope(); | |
| 66 | |
| 67 private: | |
| 68 friend class TaskRunnerHandle; | |
| 69 | |
| 70 // Immutable TaskRunner state. | |
| 71 const scoped_refptr<TaskRunner> task_runner_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(TaskScope); | |
| 74 }; | |
| 75 | |
| 76 // A SequencedTaskScope which, throughout its lifetime, identifies the | |
| 77 // sequenced context (SequencedTaskRunner) in which tasks on the thread it | |
| 78 // lives on run in. | |
| 79 class BASE_EXPORT SequencedTaskScope { | |
| 80 public: | |
| 81 SequencedTaskScope( | |
| 82 scoped_refptr<SequencedTaskRunner> sequenced_task_runner); | |
| 83 ~SequencedTaskScope(); | |
| 84 | |
| 85 private: | |
| 86 friend class TaskRunnerHandle; | |
| 87 | |
| 88 // Immutable SequencedTaskRunner state. | |
| 89 const scoped_refptr<SequencedTaskRunner> sequenced_task_runner_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(SequencedTaskScope); | |
| 92 }; | |
| 93 | |
| 94 // A SingleThreadedTaskScope which, throughout its lifetime, identifies the | |
| 95 // single- threaded context (SingleThreadedTaskRunner) in which tasks on the | |
|
robliao
2016/06/09 15:40:18
Nit: single- threaded -> single-threaded
gab
2016/06/14 15:38:59
Done.
| |
| 96 // thread it lives on run in. | |
| 97 class BASE_EXPORT SingleThreadTaskScope { | |
| 98 public: | |
| 99 SingleThreadTaskScope( | |
| 100 scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner); | |
| 101 ~SingleThreadTaskScope(); | |
| 102 | |
| 103 private: | |
| 104 friend class TaskRunnerHandle; | |
| 105 | |
| 106 // Immutable SingleThreadTaskRunner state. | |
| 107 const scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(SingleThreadTaskScope); | |
| 110 }; | |
| 111 | |
| 112 private: | |
| 113 DISALLOW_IMPLICIT_CONSTRUCTORS(TaskRunnerHandle); | |
| 114 }; | |
| 115 | |
| 116 } // namespace base | |
| 117 | |
| 118 #endif // BASE_THREADING_TASK_RUNNER_HANDLE_H_ | |
| OLD | NEW |