| OLD | NEW |
| 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/dns_reload_timer.h" | 5 #include "net/base/dns_reload_timer.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
| 8 #include "base/singleton.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/thread_local_storage.h" | 9 #include "base/thread_local_storage.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace { |
| 13 | 13 |
| 14 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting | 14 // On Linux/BSD, changes to /etc/resolv.conf can go unnoticed thus resulting |
| 15 // in DNS queries failing either because nameservers are unknown on startup | 15 // in DNS queries failing either because nameservers are unknown on startup |
| 16 // or because nameserver info has changed as a result of e.g. connecting to | 16 // or because nameserver info has changed as a result of e.g. connecting to |
| 17 // a new network. Some distributions patch glibc to stat /etc/resolv.conf | 17 // a new network. Some distributions patch glibc to stat /etc/resolv.conf |
| 18 // to try to automatically detect such changes but these patches are not | 18 // to try to automatically detect such changes but these patches are not |
| 19 // universal and even patched systems such as Jaunty appear to need calls | 19 // universal and even patched systems such as Jaunty appear to need calls |
| 20 // to res_ninit to reload the nameserver information in different threads. | 20 // to res_ninit to reload the nameserver information in different threads. |
| 21 // | 21 // |
| 22 // We adopt the Mozilla solution here which is to call res_ninit when | 22 // We adopt the Mozilla solution here which is to call res_ninit when |
| (...skipping 28 matching lines...) Expand all Loading... |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Free the allocated timer. | 54 // Free the allocated timer. |
| 55 static void SlotReturnFunction(void* data) { | 55 static void SlotReturnFunction(void* data) { |
| 56 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); | 56 base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); |
| 57 delete tls_data; | 57 delete tls_data; |
| 58 } | 58 } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 friend struct DefaultSingletonTraits<DnsReloadTimer>; | 61 friend struct base::DefaultLazyInstanceTraits<DnsReloadTimer>; |
| 62 | 62 |
| 63 DnsReloadTimer() { | 63 DnsReloadTimer() { |
| 64 // During testing the DnsReloadTimer Singleton may be created and destroyed | 64 // During testing the DnsReloadTimer Singleton may be created and destroyed |
| 65 // multiple times. Initialize the ThreadLocalStorage slot only once. | 65 // multiple times. Initialize the ThreadLocalStorage slot only once. |
| 66 if (!tls_index_.initialized()) | 66 if (!tls_index_.initialized()) |
| 67 tls_index_.Initialize(SlotReturnFunction); | 67 tls_index_.Initialize(SlotReturnFunction); |
| 68 } | 68 } |
| 69 | 69 |
| 70 ~DnsReloadTimer() { | 70 ~DnsReloadTimer() { |
| 71 } | 71 } |
| 72 | 72 |
| 73 // We use thread local storage to identify which base::TimeTicks to | 73 // We use thread local storage to identify which base::TimeTicks to |
| 74 // interact with. | 74 // interact with. |
| 75 static ThreadLocalStorage::Slot tls_index_ ; | 75 static ThreadLocalStorage::Slot tls_index_ ; |
| 76 | 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); | 77 DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 // A TLS slot to the TimeTicks for the current thread. | 80 // A TLS slot to the TimeTicks for the current thread. |
| 81 // static | 81 // static |
| 82 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); | 82 ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); |
| 83 | 83 |
| 84 base::LazyInstance<DnsReloadTimer, |
| 85 base::LeakyLazyInstanceTraits<DnsReloadTimer> > |
| 86 g_dns_reload_timer(base::LINKER_INITIALIZED); |
| 87 |
| 88 } // namespace |
| 89 |
| 90 namespace net { |
| 91 |
| 84 bool DnsReloadTimerHasExpired() { | 92 bool DnsReloadTimerHasExpired() { |
| 85 DnsReloadTimer* dns_timer = Singleton<DnsReloadTimer>::get(); | 93 DnsReloadTimer* dns_timer = g_dns_reload_timer.Pointer(); |
| 86 return dns_timer->Expired(); | 94 return dns_timer->Expired(); |
| 87 } | 95 } |
| 88 | 96 |
| 89 } // namespace net | 97 } // namespace net |
| 90 | 98 |
| 91 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 99 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
| OLD | NEW |