| 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 // DCHECKs if !HasSequencedTaskScope(). Prefer Get() to this when sequencing |
| 28 // is not required. |
| 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 single- |
| 36 // threaded sequence. This DCHECKs if !HasSingleThreadedTaskScope(). Only use |
| 37 // this when thread-affinity is required, prefer Get() or GetSequenced() |
| 38 // otherwise. |
| 39 static scoped_refptr<SingleThreadTaskRunner> GetSingleThreaded(); |
| 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. TODO(gab): This currently also supports |
| 46 // SequencedWorkerPool's threads although they don't explicitly have a |
| 47 // SequencedTaskScope but this support will be removed with the migration to |
| 48 // the TaskScheduler. |
| 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 a default context |
| 62 // (thread/sequence/etc.) that tasks can be posted to on the thread it lives |
| 63 // on. |
| 64 class BASE_EXPORT TaskScope { |
| 65 public: |
| 66 TaskScope(scoped_refptr<TaskRunner> task_runner); |
| 67 |
| 68 private: |
| 69 const internal::TaskScopeState state_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(TaskScope); |
| 72 }; |
| 73 |
| 74 // A SequencedTaskScope which, throughout its lifetime, identifies a default |
| 75 // sequenced context that tasks can be posted to on the thread it lives on. |
| 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 SequencedTaskScope which, throughout its lifetime, identifies a default |
| 88 // single-threaded context that tasks can be posted to on the thread it lives |
| 89 // on. |
| 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 |