forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[base/task] Introduce base::ThreadPool:: for Task APIs v3.
Design doc : https://docs.google.com/document/d/1tssusPykvx3g0gvbvU4HxGyn3MjJlIylnsH13-Tv6s4/edit BrowserThread counterpart to move away from post_task.h is @ https://chromium-review.googlesource.com/c/chromium/src/+/2014055 Bug: 1026641 Change-Id: I11c0633779cd2cfe75e29d3318b953c86e32bbec Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1977964 Commit-Queue: Gabriel Charette <gab@chromium.org> Reviewed-by: François Doray <fdoray@chromium.org> Cr-Commit-Position: refs/heads/master@{#735609}
- Loading branch information
Gabriel Charette
authored and
Commit Bot
committed
Jan 27, 2020
1 parent
70031b4
commit 43de5c4
Showing
10 changed files
with
511 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2019 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/thread_pool.h" | ||
|
||
#include "base/logging.h" | ||
#include "base/task/scoped_set_task_priority_for_current_thread.h" | ||
#include "base/task/task_traits.h" | ||
#include "base/task/thread_pool/thread_pool_impl.h" | ||
#include "base/task/thread_pool/thread_pool_instance.h" | ||
#include "base/threading/post_task_and_reply_impl.h" | ||
|
||
namespace base { | ||
|
||
namespace { | ||
|
||
class PostTaskAndReplyWithTraitsTaskRunner | ||
: public internal::PostTaskAndReplyImpl { | ||
public: | ||
explicit PostTaskAndReplyWithTraitsTaskRunner(const TaskTraits& traits) | ||
: traits_(traits) {} | ||
|
||
private: | ||
bool PostTask(const Location& from_here, OnceClosure task) override { | ||
ThreadPool::PostTask(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 will inherit the current | ||
// TaskPriority. | ||
TaskTraits GetTaskTraitsWithExplicitPriority(TaskTraits traits) { | ||
traits.InheritPriority(internal::GetTaskPriorityForCurrentThread()); | ||
return traits; | ||
} | ||
|
||
internal::ThreadPoolImpl* GetThreadPoolImpl() { | ||
auto* instance = ThreadPoolInstance::Get(); | ||
DCHECK(instance) | ||
<< "Ref. Prerequisite section of base/task/thread_pool.h.\n" | ||
"Hint: if this is in a unit test, you're likely merely missing a " | ||
"base::test::TaskEnvironment member in your fixture (or your fixture " | ||
"is using a base::test::SingleThreadTaskEnvironment and now needs a " | ||
"full base::test::TaskEnvironment).\n"; | ||
return static_cast<internal::ThreadPoolImpl*>(instance); | ||
} | ||
|
||
} // namespace | ||
|
||
// static | ||
bool ThreadPool::PostTask(const Location& from_here, OnceClosure task) { | ||
return ThreadPool::PostDelayedTask(from_here, std::move(task), TimeDelta()); | ||
} | ||
|
||
// static | ||
bool ThreadPool::PostDelayedTask(const Location& from_here, | ||
OnceClosure task, | ||
TimeDelta delay) { | ||
return ThreadPool::PostDelayedTask(from_here, {}, std::move(task), delay); | ||
} | ||
|
||
// static | ||
bool ThreadPool::PostTaskAndReply(const Location& from_here, | ||
OnceClosure task, | ||
OnceClosure reply) { | ||
return ThreadPool::PostTaskAndReply(from_here, {}, std::move(task), | ||
std::move(reply)); | ||
} | ||
|
||
// static | ||
bool ThreadPool::PostTask(const Location& from_here, | ||
const TaskTraits& traits, | ||
OnceClosure task) { | ||
return ThreadPool::PostDelayedTask(from_here, traits, std::move(task), | ||
TimeDelta()); | ||
} | ||
|
||
// static | ||
bool ThreadPool::PostDelayedTask(const Location& from_here, | ||
const TaskTraits& traits, | ||
OnceClosure task, | ||
TimeDelta delay) { | ||
const TaskTraits adjusted_traits = GetTaskTraitsWithExplicitPriority(traits); | ||
return GetThreadPoolImpl()->PostDelayedTask(from_here, adjusted_traits, | ||
std::move(task), delay); | ||
} | ||
|
||
// static | ||
bool ThreadPool::PostTaskAndReply(const Location& from_here, | ||
const TaskTraits& traits, | ||
OnceClosure task, | ||
OnceClosure reply) { | ||
return PostTaskAndReplyWithTraitsTaskRunner(traits).PostTaskAndReply( | ||
from_here, std::move(task), std::move(reply)); | ||
} | ||
|
||
// static | ||
scoped_refptr<TaskRunner> ThreadPool::CreateTaskRunner( | ||
const TaskTraits& traits) { | ||
return GetThreadPoolImpl()->CreateTaskRunner(traits); | ||
} | ||
|
||
// static | ||
scoped_refptr<SequencedTaskRunner> ThreadPool::CreateSequencedTaskRunner( | ||
const TaskTraits& traits) { | ||
return GetThreadPoolImpl()->CreateSequencedTaskRunner(traits); | ||
} | ||
|
||
// static | ||
scoped_refptr<UpdateableSequencedTaskRunner> | ||
ThreadPool::CreateUpdateableSequencedTaskRunner(const TaskTraits& traits) { | ||
const TaskTraits adjusted_traits = GetTaskTraitsWithExplicitPriority(traits); | ||
return GetThreadPoolImpl()->CreateUpdateableSequencedTaskRunner( | ||
adjusted_traits); | ||
} | ||
|
||
// static | ||
scoped_refptr<SingleThreadTaskRunner> ThreadPool::CreateSingleThreadTaskRunner( | ||
const TaskTraits& traits, | ||
SingleThreadTaskRunnerThreadMode thread_mode) { | ||
return GetThreadPoolImpl()->CreateSingleThreadTaskRunner(traits, thread_mode); | ||
} | ||
|
||
#if defined(OS_WIN) | ||
// static | ||
scoped_refptr<SingleThreadTaskRunner> ThreadPool::CreateCOMSTATaskRunner( | ||
const TaskTraits& traits, | ||
SingleThreadTaskRunnerThreadMode thread_mode) { | ||
return GetThreadPoolImpl()->CreateCOMSTATaskRunner(traits, thread_mode); | ||
} | ||
#endif // defined(OS_WIN) | ||
|
||
} // namespace base |
Oops, something went wrong.