OLD | NEW |
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 <winsock2.h> | 5 #include <winsock2.h> |
6 | 6 |
7 #include "net/base/winsock_init.h" | 7 #include "net/base/winsock_init.h" |
8 | 8 |
| 9 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/singleton.h" | |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 class WinsockInitSingleton { | 14 class WinsockInitSingleton { |
15 public: | 15 public: |
16 WinsockInitSingleton() { | 16 WinsockInitSingleton() { |
17 WORD winsock_ver = MAKEWORD(2, 2); | 17 WORD winsock_ver = MAKEWORD(2, 2); |
18 WSAData wsa_data; | 18 WSAData wsa_data; |
19 bool did_init = (WSAStartup(winsock_ver, &wsa_data) == 0); | 19 bool did_init = (WSAStartup(winsock_ver, &wsa_data) == 0); |
20 if (did_init) { | 20 if (did_init) { |
21 DCHECK(wsa_data.wVersion == winsock_ver); | 21 DCHECK(wsa_data.wVersion == winsock_ver); |
22 | 22 |
23 // The first time WSAGetLastError is called, the delay load helper will | 23 // The first time WSAGetLastError is called, the delay load helper will |
24 // resolve the address with GetProcAddress and fixup the import. If a | 24 // resolve the address with GetProcAddress and fixup the import. If a |
25 // third party application hooks system functions without correctly | 25 // third party application hooks system functions without correctly |
26 // restoring the error code, it is possible that the error code will be | 26 // restoring the error code, it is possible that the error code will be |
27 // overwritten during delay load resolution. The result of the first | 27 // overwritten during delay load resolution. The result of the first |
28 // call may be incorrect, so make sure the function is bound and future | 28 // call may be incorrect, so make sure the function is bound and future |
29 // results will be correct. | 29 // results will be correct. |
30 WSAGetLastError(); | 30 WSAGetLastError(); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 ~WinsockInitSingleton() { | 34 ~WinsockInitSingleton() { |
35 // Don't call WSACleanup() since the worker pool threads can continue to | 35 // Don't call WSACleanup() since the worker pool threads can continue to |
36 // call getaddrinfo() after Winsock has shutdown, which can lead to crashes. | 36 // call getaddrinfo() after Winsock has shutdown, which can lead to crashes. |
37 } | 37 } |
38 }; | 38 }; |
39 | 39 |
| 40 static base::LazyInstance<WinsockInitSingleton> g_winsock_init_singleton( |
| 41 base::LINKER_INITIALIZED); |
| 42 |
40 } // namespace | 43 } // namespace |
41 | 44 |
42 namespace net { | 45 namespace net { |
43 | 46 |
44 void EnsureWinsockInit() { | 47 void EnsureWinsockInit() { |
45 Singleton<WinsockInitSingleton>::get(); | 48 g_winsock_init_singleton.Get(); |
46 } | 49 } |
47 | 50 |
48 } // namespace net | 51 } // namespace net |
OLD | NEW |