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

Side by Side Diff: base/threading/task_runner_handle.h

Issue 2042383004: Introduce TaskRunnerHandle. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +tests Created 4 years, 6 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_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
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
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();
fdoray 2016/06/08 18:10:44 HasTaskContext? or just HasContext? since the comm
gab 2016/06/08 20:29:19 I agree that sounds a bit better but it clashes wi
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();
fdoray 2016/06/08 18:10:44 HasSequencedContext?
gab 2016/06/08 20:29:19 Acknowledged.
49
50 // Returns true if the current task is running as part of a
51 // SingleThreadTaskScope.
52 static bool HasSingleThreadTaskScope();
53
54 // Each task in Chromium should run in one and only one Scope. The
fdoray 2016/06/08 18:10:44 Remove Chromium?
gab 2016/06/08 20:29:19 Done.
55 // more specific Scopes enable the more generic getters but not vice-versa
56 // (e.g. SequencedTaskScope enables Get() and GetSequenced() but not
57 // GetSingleThreaded()).
58
59 // A TaskScope which, throughout its lifetime, holds the context (TaskRunner)
60 // in which tasks on the thread it lives on run in.
61 class BASE_EXPORT TaskScope {
62 public:
63 TaskScope(scoped_refptr<TaskRunner> task_runner);
64 ~TaskScope();
65
66 private:
67 friend class TaskRunnerHandle;
68
69 // Immutable TaskRunner state.
70 const scoped_refptr<TaskRunner> task_runner_;
71
72 DISALLOW_COPY_AND_ASSIGN(TaskScope);
73 };
74
75 // A SequencedTaskScope which, throughout its lifetime, holds the sequenced
76 // context (SequencedTaskRunner) in which tasks on the thread it lives on run
77 // in.
78 class BASE_EXPORT SequencedTaskScope {
79 public:
80 SequencedTaskScope(
81 scoped_refptr<SequencedTaskRunner> sequenced_task_runner);
82 ~SequencedTaskScope();
83
84 private:
85 friend class TaskRunnerHandle;
86
87 // Immutable SequencedTaskRunner state.
88 const scoped_refptr<SequencedTaskRunner> sequenced_task_runner_;
89
90 DISALLOW_COPY_AND_ASSIGN(SequencedTaskScope);
91 };
92
93 // A SingleThreadedTaskScope which, throughout its lifetime, holds the single-
94 // threaded context (SingleThreadedTaskRunner) in which tasks on the thread it
95 // lives on run in.
96 class BASE_EXPORT SingleThreadTaskScope {
97 public:
98 SingleThreadTaskScope(
99 scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner);
100 ~SingleThreadTaskScope();
101
102 private:
103 friend class TaskRunnerHandle;
104
105 // Immutable SingleThreadTaskRunner state.
106 const scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_;
107
108 DISALLOW_COPY_AND_ASSIGN(SingleThreadTaskScope);
109 };
110
111 private:
112 DISALLOW_IMPLICIT_CONSTRUCTORS(TaskRunnerHandle);
113 };
114
115 } // namespace base
116
117 #endif // BASE_THREADING_TASK_RUNNER_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698