Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: net/base/host_resolver_impl.cc

Issue 6052006: Proof of concept for tr1-based Task replacement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/net/base
Patch Set: Examples of replacing CreateFunctor, ScopedRMF, CompletionCallback Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/base/host_resolver_impl.h" 5 #include "net/base/host_resolver_impl.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <Winsock2.h> 8 #include <Winsock2.h>
9 #elif defined(OS_POSIX) 9 #elif defined(OS_POSIX)
10 #include <netdb.h> 10 #include <netdb.h>
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 375
376 if (!req->info().is_speculative()) 376 if (!req->info().is_speculative())
377 had_non_speculative_request_ = true; 377 had_non_speculative_request_ = true;
378 } 378 }
379 379
380 // Called from origin loop. 380 // Called from origin loop.
381 void Start() { 381 void Start() {
382 start_time_ = base::TimeTicks::Now(); 382 start_time_ = base::TimeTicks::Now();
383 383
384 // Dispatch the job to a worker thread. 384 // Dispatch the job to a worker thread.
385 if (!WorkerPool::PostTask(FROM_HERE, 385 if (!WorkerPool::PostClosure(FROM_HERE,
386 NewRunnableMethod(this, &Job::DoLookup), true)) { 386 base::Closure(&Job::DoLookup, this), true)) {
387 NOTREACHED(); 387 NOTREACHED();
388 388
389 // Since we could be running within Resolve() right now, we can't just 389 // Since we could be running within Resolve() right now, we can't just
390 // call OnLookupComplete(). Instead we must wait until Resolve() has 390 // call OnLookupComplete(). Instead we must wait until Resolve() has
391 // returned (IO_PENDING). 391 // returned (IO_PENDING).
392 error_ = ERR_UNEXPECTED; 392 error_ = ERR_UNEXPECTED;
393 MessageLoop::current()->PostTask( 393 MessageLoop::current()->PostClosure(
394 FROM_HERE, NewRunnableMethod(this, &Job::OnLookupComplete)); 394 FROM_HERE, base::Closure(&Job::OnLookupComplete, this));
395 } 395 }
396 } 396 }
397 397
398 // Cancels the current job. Callable from origin thread. 398 // Cancels the current job. Callable from origin thread.
399 void Cancel() { 399 void Cancel() {
400 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); 400 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL);
401 401
402 HostResolver* resolver = resolver_; 402 HostResolver* resolver = resolver_;
403 resolver_ = NULL; 403 resolver_ = NULL;
404 404
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 key_.address_family, 477 key_.address_family,
478 key_.host_resolver_flags, 478 key_.host_resolver_flags,
479 &results_, 479 &results_,
480 &os_error_); 480 &os_error_);
481 481
482 // The origin loop could go away while we are trying to post to it, so we 482 // The origin loop could go away while we are trying to post to it, so we
483 // need to call its PostTask method inside a lock. See ~HostResolver. 483 // need to call its PostTask method inside a lock. See ~HostResolver.
484 { 484 {
485 AutoLock locked(origin_loop_lock_); 485 AutoLock locked(origin_loop_lock_);
486 if (origin_loop_) { 486 if (origin_loop_) {
487 origin_loop_->PostTask(FROM_HERE, 487 origin_loop_->PostClosure(FROM_HERE,
488 NewRunnableMethod(this, &Job::OnLookupComplete)); 488 base::Closure(&Job::OnLookupComplete, this));
489 } 489 }
490 } 490 }
491 } 491 }
492 492
493 // Callback for when DoLookup() completes (runs on origin thread). 493 // Callback for when DoLookup() completes (runs on origin thread).
494 void OnLookupComplete() { 494 void OnLookupComplete() {
495 // Should be running on origin loop. 495 // Should be running on origin loop.
496 // TODO(eroman): this is being hit by URLRequestTest.CancelTest*, 496 // TODO(eroman): this is being hit by URLRequestTest.CancelTest*,
497 // because MessageLoop::current() == NULL. 497 // because MessageLoop::current() == NULL.
498 //DCHECK_EQ(origin_loop_, MessageLoop::current()); 498 //DCHECK_EQ(origin_loop_, MessageLoop::current());
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 : resolver_(resolver), 643 : resolver_(resolver),
644 origin_loop_(MessageLoop::current()) { 644 origin_loop_(MessageLoop::current()) {
645 DCHECK(!was_cancelled()); 645 DCHECK(!was_cancelled());
646 } 646 }
647 647
648 void Start() { 648 void Start() {
649 if (was_cancelled()) 649 if (was_cancelled())
650 return; 650 return;
651 DCHECK(IsOnOriginThread()); 651 DCHECK(IsOnOriginThread());
652 const bool kIsSlow = true; 652 const bool kIsSlow = true;
653 WorkerPool::PostTask( 653 WorkerPool::PostClosure(
654 FROM_HERE, NewRunnableMethod(this, &IPv6ProbeJob::DoProbe), kIsSlow); 654 FROM_HERE, base::Closure(&IPv6ProbeJob::DoProbe, this), kIsSlow);
655 } 655 }
656 656
657 // Cancels the current job. 657 // Cancels the current job.
658 void Cancel() { 658 void Cancel() {
659 if (was_cancelled()) 659 if (was_cancelled())
660 return; 660 return;
661 DCHECK(IsOnOriginThread()); 661 DCHECK(IsOnOriginThread());
662 resolver_ = NULL; // Read/write ONLY on origin thread. 662 resolver_ = NULL; // Read/write ONLY on origin thread.
663 { 663 {
664 AutoLock locked(origin_loop_lock_); 664 AutoLock locked(origin_loop_lock_);
(...skipping 17 matching lines...) Expand all
682 } 682 }
683 return false; 683 return false;
684 } 684 }
685 685
686 // Run on worker thread. 686 // Run on worker thread.
687 void DoProbe() { 687 void DoProbe() {
688 // Do actual testing on this thread, as it takes 40-100ms. 688 // Do actual testing on this thread, as it takes 40-100ms.
689 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED 689 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED
690 : ADDRESS_FAMILY_IPV4; 690 : ADDRESS_FAMILY_IPV4;
691 691
692 Task* reply = NewRunnableMethod(this, &IPv6ProbeJob::OnProbeComplete, 692 base::Closure reply = base::Closure(&IPv6ProbeJob::OnProbeComplete, this,
693 family); 693 family);
694 694
695 // The origin loop could go away while we are trying to post to it, so we 695 // The origin loop could go away while we are trying to post to it, so we
696 // need to call its PostTask method inside a lock. See ~HostResolver. 696 // need to call its PostTask method inside a lock. See ~HostResolver.
697 { 697 {
698 AutoLock locked(origin_loop_lock_); 698 AutoLock locked(origin_loop_lock_);
699 if (origin_loop_) { 699 if (origin_loop_) {
700 origin_loop_->PostTask(FROM_HERE, reply); 700 origin_loop_->PostClosure(FROM_HERE, reply);
701 return; 701 return;
702 } 702 }
703 } 703 }
704 704
705 // We didn't post, so delete the reply.
706 delete reply;
707 } 705 }
708 706
709 // Callback for when DoProbe() completes (runs on origin thread). 707 // Callback for when DoProbe() completes (runs on origin thread).
710 void OnProbeComplete(AddressFamily address_family) { 708 void OnProbeComplete(AddressFamily address_family) {
711 if (was_cancelled()) 709 if (was_cancelled())
712 return; 710 return;
713 DCHECK(IsOnOriginThread()); 711 DCHECK(IsOnOriginThread());
714 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); 712 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family);
715 } 713 }
716 714
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after
1452 job_pools_[i]->ResetNumOutstandingJobs(); 1450 job_pools_[i]->ResetNumOutstandingJobs();
1453 JobMap jobs; 1451 JobMap jobs;
1454 jobs.swap(jobs_); 1452 jobs.swap(jobs_);
1455 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) { 1453 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) {
1456 AbortJob(it->second); 1454 AbortJob(it->second);
1457 it->second->Cancel(); 1455 it->second->Cancel();
1458 } 1456 }
1459 } 1457 }
1460 1458
1461 } // namespace net 1459 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698