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

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

Issue 439007: Apply test isolation goodness to net_unittests. (Closed)
Patch Set: fix Linux failures Created 11 years 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
« no previous file with comments | « no previous file | net/base/run_all_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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_proc.h" 5 #include "net/base/host_resolver_proc.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <ws2tcpip.h> 10 #include <ws2tcpip.h>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // universal and even patched systems such as Jaunty appear to need calls 70 // universal and even patched systems such as Jaunty appear to need calls
71 // to res_ninit to reload the nameserver information in different threads. 71 // to res_ninit to reload the nameserver information in different threads.
72 // 72 //
73 // We adopt the Mozilla solution here which is to call res_ninit when 73 // We adopt the Mozilla solution here which is to call res_ninit when
74 // lookups fail and to rate limit the reloading to once per second per 74 // lookups fail and to rate limit the reloading to once per second per
75 // thread. 75 // thread.
76 76
77 // Keep a timer per calling thread to rate limit the calling of res_ninit. 77 // Keep a timer per calling thread to rate limit the calling of res_ninit.
78 class DnsReloadTimer { 78 class DnsReloadTimer {
79 public: 79 public:
80 DnsReloadTimer() {
81 tls_index_.Initialize(SlotReturnFunction);
82 }
83
84 ~DnsReloadTimer() { }
85
86 // Check if the timer for the calling thread has expired. When no 80 // Check if the timer for the calling thread has expired. When no
87 // timer exists for the calling thread, create one. 81 // timer exists for the calling thread, create one.
88 bool Expired() { 82 bool Expired() {
89 const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1); 83 const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1);
90 base::TimeTicks now = base::TimeTicks::Now(); 84 base::TimeTicks now = base::TimeTicks::Now();
91 base::TimeTicks* timer_ptr = 85 base::TimeTicks* timer_ptr =
92 static_cast<base::TimeTicks*>(tls_index_.Get()); 86 static_cast<base::TimeTicks*>(tls_index_.Get());
93 87
94 if (!timer_ptr) { 88 if (!timer_ptr) {
95 timer_ptr = new base::TimeTicks(); 89 timer_ptr = new base::TimeTicks();
96 *timer_ptr = base::TimeTicks::Now(); 90 *timer_ptr = base::TimeTicks::Now();
97 tls_index_.Set(timer_ptr); 91 tls_index_.Set(timer_ptr);
98 // Return true to reload dns info on the first call for each thread. 92 // Return true to reload dns info on the first call for each thread.
99 return true; 93 return true;
100 } else if (now - *timer_ptr > kRetryTime) { 94 } else if (now - *timer_ptr > kRetryTime) {
101 *timer_ptr = now; 95 *timer_ptr = now;
102 return true; 96 return true;
103 } else { 97 } else {
104 return false; 98 return false;
105 } 99 }
106 } 100 }
107 101
108 // Free the allocated timer. 102 // Free the allocated timer.
109 static void SlotReturnFunction(void* data) { 103 static void SlotReturnFunction(void* data) {
110 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); 104 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data);
111 delete tls_data; 105 delete tls_data;
112 } 106 }
113 107
114 private: 108 private:
109 friend struct DefaultSingletonTraits<DnsReloadTimer>;
wtc 2009/11/25 18:17:01 Do singleton classes need to have their constructo
110
111 DnsReloadTimer() {
112 tls_index_.Initialize(SlotReturnFunction);
113 }
114
115 ~DnsReloadTimer() {
116 tls_index_.Free();
117 }
118
115 // We use thread local storage to identify which base::TimeTicks to 119 // We use thread local storage to identify which base::TimeTicks to
116 // interact with. 120 // interact with.
117 static ThreadLocalStorage::Slot tls_index_ ; 121 static ThreadLocalStorage::Slot tls_index_ ;
118 122
119 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); 123 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer);
120 }; 124 };
121 125
122 // A TLS slot to the TimeTicks for the current thread. 126 // A TLS slot to the TimeTicks for the current thread.
123 // static 127 // static
124 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); 128 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 #endif 198 #endif
195 199
196 if (err) 200 if (err)
197 return ERR_NAME_NOT_RESOLVED; 201 return ERR_NAME_NOT_RESOLVED;
198 202
199 addrlist->Adopt(ai); 203 addrlist->Adopt(ai);
200 return OK; 204 return OK;
201 } 205 }
202 206
203 } // namespace net 207 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/run_all_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698