| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/serial_worker.h" | 5 #include "net/dns/serial_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 SerialWorker::SerialWorker() | 14 SerialWorker::SerialWorker() |
| 15 : message_loop_(base::MessageLoopProxy::current()), | 15 : message_loop_(base::MessageLoopProxy::current()), state_(IDLE) { |
| 16 state_(IDLE) {} | 16 } |
| 17 | 17 |
| 18 SerialWorker::~SerialWorker() {} | 18 SerialWorker::~SerialWorker() { |
| 19 } |
| 19 | 20 |
| 20 void SerialWorker::WorkNow() { | 21 void SerialWorker::WorkNow() { |
| 21 DCHECK(message_loop_->BelongsToCurrentThread()); | 22 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 22 switch (state_) { | 23 switch (state_) { |
| 23 case IDLE: | 24 case IDLE: |
| 24 if (!base::WorkerPool::PostTask(FROM_HERE, base::Bind( | 25 if (!base::WorkerPool::PostTask( |
| 25 &SerialWorker::DoWorkJob, this), false)) { | 26 FROM_HERE, base::Bind(&SerialWorker::DoWorkJob, this), false)) { |
| 26 #if defined(OS_POSIX) | 27 #if defined(OS_POSIX) |
| 27 // See worker_pool_posix.cc. | 28 // See worker_pool_posix.cc. |
| 28 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix"; | 29 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix"; |
| 29 #else | 30 #else |
| 30 LOG(WARNING) << "Failed to WorkerPool::PostTask, will retry later"; | 31 LOG(WARNING) << "Failed to WorkerPool::PostTask, will retry later"; |
| 31 const int kWorkerPoolRetryDelayMs = 100; | 32 const int kWorkerPoolRetryDelayMs = 100; |
| 32 message_loop_->PostDelayedTask( | 33 message_loop_->PostDelayedTask( |
| 33 FROM_HERE, | 34 FROM_HERE, |
| 34 base::Bind(&SerialWorker::RetryWork, this), | 35 base::Bind(&SerialWorker::RetryWork, this), |
| 35 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs)); | 36 base::TimeDelta::FromMilliseconds(kWorkerPoolRetryDelayMs)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 53 } | 54 } |
| 54 | 55 |
| 55 void SerialWorker::Cancel() { | 56 void SerialWorker::Cancel() { |
| 56 DCHECK(message_loop_->BelongsToCurrentThread()); | 57 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 57 state_ = CANCELLED; | 58 state_ = CANCELLED; |
| 58 } | 59 } |
| 59 | 60 |
| 60 void SerialWorker::DoWorkJob() { | 61 void SerialWorker::DoWorkJob() { |
| 61 this->DoWork(); | 62 this->DoWork(); |
| 62 // If this fails, the loop is gone, so there is no point retrying. | 63 // If this fails, the loop is gone, so there is no point retrying. |
| 63 message_loop_->PostTask(FROM_HERE, base::Bind( | 64 message_loop_->PostTask(FROM_HERE, |
| 64 &SerialWorker::OnWorkJobFinished, this)); | 65 base::Bind(&SerialWorker::OnWorkJobFinished, this)); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void SerialWorker::OnWorkJobFinished() { | 68 void SerialWorker::OnWorkJobFinished() { |
| 68 DCHECK(message_loop_->BelongsToCurrentThread()); | 69 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 69 switch (state_) { | 70 switch (state_) { |
| 70 case CANCELLED: | 71 case CANCELLED: |
| 71 return; | 72 return; |
| 72 case WORKING: | 73 case WORKING: |
| 73 state_ = IDLE; | 74 state_ = IDLE; |
| 74 this->OnWorkFinished(); | 75 this->OnWorkFinished(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 90 case WAITING: | 91 case WAITING: |
| 91 state_ = IDLE; | 92 state_ = IDLE; |
| 92 WorkNow(); | 93 WorkNow(); |
| 93 return; | 94 return; |
| 94 default: | 95 default: |
| 95 NOTREACHED() << "Unexpected state " << state_; | 96 NOTREACHED() << "Unexpected state " << state_; |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 } // namespace net | 100 } // namespace net |
| 100 | |
| OLD | NEW |