1 // Copyright (c) 2012 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.
5 #include "net/dns/serial_worker.h"
8 #include "base/location.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/threading/worker_pool.h"
14 SerialWorker::SerialWorker()
15 : task_runner_(base::ThreadTaskRunnerHandle::Get()), state_(IDLE
) {
18 SerialWorker::~SerialWorker() {}
20 void SerialWorker::WorkNow() {
21 DCHECK(task_runner_
->BelongsToCurrentThread());
24 if (!base::WorkerPool::PostTask(FROM_HERE
, base::Bind(
25 &SerialWorker::DoWorkJob
, this), false)) {
27 // See worker_pool_posix.cc.
28 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix";
30 LOG(WARNING
) << "Failed to WorkerPool::PostTask, will retry later";
31 const int kWorkerPoolRetryDelayMs
= 100;
32 task_runner_
->PostDelayedTask(
34 base::Bind(&SerialWorker::RetryWork
, this),
35 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs
));
43 // Remember to re-read after |DoRead| finishes.
51 NOTREACHED() << "Unexpected state " << state_
;
55 void SerialWorker::Cancel() {
56 DCHECK(task_runner_
->BelongsToCurrentThread());
60 void SerialWorker::DoWorkJob() {
62 // If this fails, the loop is gone, so there is no point retrying.
63 task_runner_
->PostTask(FROM_HERE
,
64 base::Bind(&SerialWorker::OnWorkJobFinished
, this));
67 void SerialWorker::OnWorkJobFinished() {
68 DCHECK(task_runner_
->BelongsToCurrentThread());
74 this->OnWorkFinished();
81 NOTREACHED() << "Unexpected state " << state_
;
85 void SerialWorker::RetryWork() {
86 DCHECK(task_runner_
->BelongsToCurrentThread());
95 NOTREACHED() << "Unexpected state " << state_
;