Chromium Code Reviews| Index: base/threading/task_runner_handle.h |
| diff --git a/base/threading/task_runner_handle.h b/base/threading/task_runner_handle.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6dbe385f50d932ad2d8e9b435f14dad96f4445f9 |
| --- /dev/null |
| +++ b/base/threading/task_runner_handle.h |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_THREADING_TASK_RUNNER_HANDLE_H_ |
| +#define BASE_THREADING_TASK_RUNNER_HANDLE_H_ |
| + |
| +#include "base/base_export.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| + |
| +namespace base { |
| + |
| +class SequencedTaskRunner; |
| +class SingleThreadTaskRunner; |
| +class TaskRunner; |
| + |
| +class BASE_EXPORT TaskRunnerHandle { |
| + public: |
| + // Returns a TaskRunner which will run tasks in the same context as the |
| + // 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.
|
| + // sequence on a thread pool, or merely the current set of TaskTraits on a |
| + // non-sequenced TaskScheduler task. |
| + static scoped_refptr<TaskRunner> Get(); |
| + |
| + // Returns a SequencedTaskRunner associated with the current sequence. This |
| + // must only be called if HasSequencedTaskScope(). Prefer Get() to this when |
| + // 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.
|
| + // SequencedTaskRunnerHandle::Get() to TaskRunnerHandle::Get() and remove |
| + // support for getting a SequencedTaskRunner from a non-sequenced task in |
| + // SequencedWorkerPool which currently breaks the semantics intended here. |
| + static scoped_refptr<SequencedTaskRunner> GetSequenced(); |
| + |
| + // Returns a SingleThreadTaskRunner associated with the current single- |
| + // threaded sequence. This must only be called if |
| + // HasSingleThreadedTaskScope(). Only use this when thread-affinity is |
| + // required, prefer Get() or GetSequenced() otherwise. |
| + static scoped_refptr<SingleThreadTaskRunner> GetSingleThreaded(); |
| + |
| + // Returns true if the current task is running as part of any TaskScope. |
| + static bool HasTaskScope(); |
| + |
| + // Returns true if the current task is running as part of a SequencedTaskScope |
| + // or a SingleThreadTaskScope. Note: This currently also supports |
| + // SequencedWorkerPool's threads although they don't explicitly have a |
| + // SequencedTaskScope but this support will be going away with the migration |
| + // to the TaskScheduler. |
| + static bool HasSequencedTaskScope(); |
| + |
| + // Returns true if the current task is running as part of a |
| + // SingleThreadTaskScope. |
| + static bool HasSingleThreadTaskScope(); |
| + |
| + // Each task should run in one and only one Scope (TaskScopes are held on the |
| + // stack in the scope of a task and no TaskScope can live inside another |
| + // TaskScope). The more specific Scopes enable the more generic getters but |
| + // not vice-versa (e.g. SequencedTaskScope enables Get() and GetSequenced() |
| + // but not GetSingleThreaded()). |
| + |
| + // A TaskScope which, throughout its lifetime, identifies the context |
| + // (TaskRunner) in which tasks on the thread it lives on run in. |
| + class BASE_EXPORT TaskScope { |
| + public: |
| + TaskScope(scoped_refptr<TaskRunner> task_runner); |
| + ~TaskScope(); |
| + |
| + private: |
| + friend class TaskRunnerHandle; |
| + |
| + // Immutable TaskRunner state. |
| + const scoped_refptr<TaskRunner> task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TaskScope); |
| + }; |
| + |
| + // A SequencedTaskScope which, throughout its lifetime, identifies the |
| + // sequenced context (SequencedTaskRunner) in which tasks on the thread it |
| + // lives on run in. |
| + class BASE_EXPORT SequencedTaskScope { |
| + public: |
| + SequencedTaskScope( |
| + scoped_refptr<SequencedTaskRunner> sequenced_task_runner); |
| + ~SequencedTaskScope(); |
| + |
| + private: |
| + friend class TaskRunnerHandle; |
| + |
| + // Immutable SequencedTaskRunner state. |
| + const scoped_refptr<SequencedTaskRunner> sequenced_task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SequencedTaskScope); |
| + }; |
| + |
| + // A SingleThreadedTaskScope which, throughout its lifetime, identifies the |
| + // 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.
|
| + // thread it lives on run in. |
| + class BASE_EXPORT SingleThreadTaskScope { |
| + public: |
| + SingleThreadTaskScope( |
| + scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner); |
| + ~SingleThreadTaskScope(); |
| + |
| + private: |
| + friend class TaskRunnerHandle; |
| + |
| + // Immutable SingleThreadTaskRunner state. |
| + const scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SingleThreadTaskScope); |
| + }; |
| + |
| + private: |
| + DISALLOW_IMPLICIT_CONSTRUCTORS(TaskRunnerHandle); |
| +}; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_THREADING_TASK_RUNNER_HANDLE_H_ |