1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// 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.
#include "base/task_scheduler/post_task.h"
#include <utility>
#include "base/logging.h"
#include "base/task_scheduler/scoped_set_task_priority_for_current_thread.h"
#include "base/task_scheduler/task_scheduler.h"
#include "base/threading/post_task_and_reply_impl.h"
namespace base {
namespace {
class PostTaskAndReplyTaskRunner : public internal::PostTaskAndReplyImpl {
public:
explicit PostTaskAndReplyTaskRunner(const TaskTraits& traits)
: traits_(traits) {}
private:
bool PostTask(const tracked_objects::Location& from_here,
OnceClosure task) override {
PostTaskWithTraits(from_here, traits_, std::move(task));
return true;
}
const TaskTraits traits_;
};
// Returns TaskTraits based on |traits|. If TaskPriority hasn't been set
// explicitly in |traits|, the returned TaskTraits have the current
// TaskPriority.
TaskTraits GetTaskTraitsWithExplicitPriority(const TaskTraits& traits) {
if (traits.priority_set_explicitly())
return traits;
return TaskTraits::Override(traits,
{internal::GetTaskPriorityForCurrentThread()});
}
} // namespace
void PostTask(const tracked_objects::Location& from_here, OnceClosure task) {
PostDelayedTask(from_here, std::move(task), TimeDelta());
}
void PostDelayedTask(const tracked_objects::Location& from_here,
OnceClosure task,
TimeDelta delay) {
PostDelayedTaskWithTraits(from_here, TaskTraits(), std::move(task), delay);
}
void PostTaskAndReply(const tracked_objects::Location& from_here,
OnceClosure task,
OnceClosure reply) {
PostTaskWithTraitsAndReply(from_here, TaskTraits(), std::move(task),
std::move(reply));
}
void PostTaskWithTraits(const tracked_objects::Location& from_here,
const TaskTraits& traits,
OnceClosure task) {
PostDelayedTaskWithTraits(from_here, traits, std::move(task), TimeDelta());
}
void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here,
const TaskTraits& traits,
OnceClosure task,
TimeDelta delay) {
DCHECK(TaskScheduler::GetInstance())
<< "Ref. Prerequisite section of post_task.h";
TaskScheduler::GetInstance()->PostDelayedTaskWithTraits(
from_here, GetTaskTraitsWithExplicitPriority(traits), std::move(task),
std::move(delay));
}
void PostTaskWithTraitsAndReply(const tracked_objects::Location& from_here,
const TaskTraits& traits,
OnceClosure task,
OnceClosure reply) {
PostTaskAndReplyTaskRunner(traits).PostTaskAndReply(
from_here, std::move(task), std::move(reply));
}
scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(const TaskTraits& traits) {
DCHECK(TaskScheduler::GetInstance())
<< "Ref. Prerequisite section of post_task.h";
return TaskScheduler::GetInstance()->CreateTaskRunnerWithTraits(
GetTaskTraitsWithExplicitPriority(traits));
}
scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits(
const TaskTraits& traits) {
DCHECK(TaskScheduler::GetInstance())
<< "Ref. Prerequisite section of post_task.h";
return TaskScheduler::GetInstance()->CreateSequencedTaskRunnerWithTraits(
GetTaskTraitsWithExplicitPriority(traits));
}
scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits(
const TaskTraits& traits,
SingleThreadTaskRunnerThreadMode thread_mode) {
DCHECK(TaskScheduler::GetInstance())
<< "Ref. Prerequisite section of post_task.h";
return TaskScheduler::GetInstance()->CreateSingleThreadTaskRunnerWithTraits(
GetTaskTraitsWithExplicitPriority(traits), thread_mode);
}
#if defined(OS_WIN)
scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits(
const TaskTraits& traits,
SingleThreadTaskRunnerThreadMode thread_mode) {
DCHECK(TaskScheduler::GetInstance())
<< "Ref. Prerequisite section of post_task.h";
return TaskScheduler::GetInstance()->CreateCOMSTATaskRunnerWithTraits(
GetTaskTraitsWithExplicitPriority(traits), thread_mode);
}
#endif // defined(OS_WIN)
} // namespace base
|