Skip to content

Commit

Permalink
Declare TaskTraits for blocking USB tasks in a constexpr
Browse files Browse the repository at this point in the history
This is the cleanup I suggested in https://crrev.com/2855233002/#msg5
to declare the appropriate task traits for posting blocking tasks in a
single place so that there is consistency (and reduced duplication)
across //device/usb.

BUG=None

Review-Url: https://codereview.chromium.org/2864473002
Cr-Commit-Position: refs/heads/master@{#473108}
  • Loading branch information
reillyeon authored and Commit bot committed May 19, 2017
1 parent 71170cc commit 375fc7a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
7 changes: 4 additions & 3 deletions device/usb/usb_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ void UsbService::Observer::OnDeviceRemovedCleanup(

void UsbService::Observer::WillDestroyUsbService() {}

// Declare storage for this constexpr.
constexpr base::TaskTraits UsbService::kBlockingTaskTraits;

// static
std::unique_ptr<UsbService> UsbService::Create() {
#if defined(OS_ANDROID)
Expand All @@ -64,9 +67,7 @@ std::unique_ptr<UsbService> UsbService::Create() {
// static
scoped_refptr<base::SequencedTaskRunner>
UsbService::CreateBlockingTaskRunner() {
return base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
return base::CreateSequencedTaskRunnerWithTraits(kBlockingTaskTraits);
}

UsbService::~UsbService() {
Expand Down
9 changes: 8 additions & 1 deletion device/usb/usb_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/observer_list.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/task_scheduler/task_traits.h"
#include "base/threading/non_thread_safe.h"

namespace device {
Expand Down Expand Up @@ -49,10 +50,16 @@ class UsbService : public base::NonThreadSafe {
virtual void WillDestroyUsbService();
};

// These task traits are to be used for posting blocking tasks to the task
// scheduler.
static constexpr base::TaskTraits kBlockingTaskTraits = {
base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};

// Returns nullptr when initialization fails.
static std::unique_ptr<UsbService> Create();

// Creates a SequencedTaskRunner appropriate for blocking I/O operations.
// Creates a SequencedTaskRunner with kBlockingTaskTraits.
static scoped_refptr<base::SequencedTaskRunner> CreateBlockingTaskRunner();

virtual ~UsbService();
Expand Down
8 changes: 2 additions & 6 deletions device/usb/usb_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ UsbServiceImpl::UsbServiceImpl()
#endif
weak_factory_(this) {
base::PostTaskWithTraits(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
FROM_HERE, kBlockingTaskTraits,
base::Bind(&InitializeUsbContextOnBlockingThread, task_runner(),
base::Bind(&UsbServiceImpl::OnUsbContext,
weak_factory_.GetWeakPtr())));
Expand Down Expand Up @@ -324,9 +322,7 @@ void UsbServiceImpl::RefreshDevices() {
pending_path_enumerations_.pop();
}

base::PostTaskWithTraits(FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::PostTaskWithTraits(FROM_HERE, kBlockingTaskTraits,
base::Bind(&GetDeviceListOnBlockingThread,
device_path, context_, task_runner(),
base::Bind(&UsbServiceImpl::OnDeviceList,
Expand Down

0 comments on commit 375fc7a

Please sign in to comment.