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/message_loop/message_loop_proxy.h"
10 #include "base/threading/worker_pool.h"
14 SerialWorker::SerialWorker()
15 : message_loop_(base::MessageLoopProxy::current()),
18 SerialWorker::~SerialWorker() {}
20 void SerialWorker::WorkNow() {
21 DCHECK(message_loop_
->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 message_loop_
->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(message_loop_
->BelongsToCurrentThread());
60 void SerialWorker::DoWorkJob() {
62 // If this fails, the loop is gone, so there is no point retrying.
63 message_loop_
->PostTask(FROM_HERE
, base::Bind(
64 &SerialWorker::OnWorkJobFinished
, this));
67 void SerialWorker::OnWorkJobFinished() {
68 DCHECK(message_loop_
->BelongsToCurrentThread());
74 this->OnWorkFinished();
81 NOTREACHED() << "Unexpected state " << state_
;
85 void SerialWorker::RetryWork() {
86 DCHECK(message_loop_
->BelongsToCurrentThread());
95 NOTREACHED() << "Unexpected state " << state_
;