summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-04-05 20:10:03 -0500
committerJosh Gao2018-04-11 14:54:38 -0500
commit2e93df2e1446abb250472f2d39b20e00bba97dcc (patch)
tree4ea7aa47bab75c6e9f5634b76813f6a2595b221f
parent011ba4b9bf9f0b1b1f99639f8395a76760f717c0 (diff)
downloadplatform-system-core-2e93df2e1446abb250472f2d39b20e00bba97dcc.tar.gz
platform-system-core-2e93df2e1446abb250472f2d39b20e00bba97dcc.tar.xz
platform-system-core-2e93df2e1446abb250472f2d39b20e00bba97dcc.zip
adb: win32: cleanup winsock initialization.
Instead of doing it in 3 arbitrary functions, do it at startup always. Test: wine adb_test.exe Change-Id: Ida272d218aee6c331471250edce64d512d3b628a
-rw-r--r--adb/sysdeps_win32.cpp29
1 files changed, 9 insertions, 20 deletions
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index caad50bf3..bfac34208 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -726,23 +726,15 @@ static int _fh_socket_writev(FH f, const adb_iovec* iov, int iovcnt) {
726/**************************************************************************/ 726/**************************************************************************/
727/**************************************************************************/ 727/**************************************************************************/
728 728
729#include <winsock2.h> 729static int _init_winsock(void) {
730 730 static std::once_flag once;
731static int _winsock_init; 731 std::call_once(once, []() {
732 732 WSADATA wsaData;
733static void 733 int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
734_init_winsock( void )
735{
736 // TODO: Multiple threads calling this may potentially cause multiple calls
737 // to WSAStartup() which offers no real benefit.
738 if (!_winsock_init) {
739 WSADATA wsaData;
740 int rc = WSAStartup( MAKEWORD(2,2), &wsaData);
741 if (rc != 0) { 734 if (rc != 0) {
742 fatal("adb: could not initialize Winsock: %s", 735 fatal("adb: could not initialize Winsock: %s",
743 android::base::SystemErrorCodeToString(rc).c_str()); 736 android::base::SystemErrorCodeToString(rc).c_str());
744 } 737 }
745 _winsock_init = 1;
746 738
747 // Note that we do not call atexit() to register WSACleanup to be called 739 // Note that we do not call atexit() to register WSACleanup to be called
748 // at normal process termination because: 740 // at normal process termination because:
@@ -757,9 +749,12 @@ _init_winsock( void )
757 // setupapi.dll which tries to load wintrust.dll which tries to load 749 // setupapi.dll which tries to load wintrust.dll which tries to load
758 // crypt32.dll which calls atexit() which tries to acquire the C 750 // crypt32.dll which calls atexit() which tries to acquire the C
759 // Runtime lock that the other thread holds. 751 // Runtime lock that the other thread holds.
760 } 752 });
753 return 0;
761} 754}
762 755
756static int _winsock_init = _init_winsock();
757
763// Map a socket type to an explicit socket protocol instead of using the socket 758// Map a socket type to an explicit socket protocol instead of using the socket
764// protocol of 0. Explicit socket protocols are used by most apps and we should 759// protocol of 0. Explicit socket protocols are used by most apps and we should
765// do the same to reduce the chance of exercising uncommon code-paths that might 760// do the same to reduce the chance of exercising uncommon code-paths that might
@@ -787,8 +782,6 @@ int network_loopback_client(int port, int type, std::string* error) {
787 return -1; 782 return -1;
788 } 783 }
789 784
790 if (!_winsock_init) _init_winsock();
791
792 memset(&addr, 0, sizeof(addr)); 785 memset(&addr, 0, sizeof(addr));
793 addr.sin_family = AF_INET; 786 addr.sin_family = AF_INET;
794 addr.sin_port = htons(port); 787 addr.sin_port = htons(port);
@@ -837,8 +830,6 @@ static int _network_server(int port, int type, u_long interface_address, std::st
837 return -1; 830 return -1;
838 } 831 }
839 832
840 if (!_winsock_init) _init_winsock();
841
842 memset(&addr, 0, sizeof(addr)); 833 memset(&addr, 0, sizeof(addr));
843 addr.sin_family = AF_INET; 834 addr.sin_family = AF_INET;
844 addr.sin_port = htons(port); 835 addr.sin_port = htons(port);
@@ -915,8 +906,6 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
915 return -1; 906 return -1;
916 } 907 }
917 908
918 if (!_winsock_init) _init_winsock();
919
920 struct addrinfo hints; 909 struct addrinfo hints;
921 memset(&hints, 0, sizeof(hints)); 910 memset(&hints, 0, sizeof(hints));
922 hints.ai_family = AF_UNSPEC; 911 hints.ai_family = AF_UNSPEC;