From c20610d3cba2e3eaeb01927f39cc9cc21dfa1d33 Mon Sep 17 00:00:00 2001 From: "nick@chromium.org" Date: Fri, 16 Oct 2009 00:52:50 +0000 Subject: Within libjingle, add an abstraction of winsock initialization. When building chrome, override the default implementation so that we share the winsock initialization singleton with net/base. Trigger the winsock initialization lazily in a few places. This is intended to fix a startup perf regression that appeared when sync (and libjingle) became linked into chrome.dll, rather than a separate delay-loaded sync dll. BUG=24448 TEST=Verified deferred winsock initialization via debugger. Measured before and after time of the StartupTest.PerfTest. Verified sync notification still work. Review URL: http://codereview.chromium.org/267089 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29228 0039d316-1c4b-4281-b951-d872f2087c98 --- .../files/talk/base/physicalsocketserver.cc | 24 ++------ .../libjingle/files/talk/base/win32socketserver.cc | 2 + .../files/talk/base/winsock_initializer.cc | 65 ++++++++++++++++++++++ .../files/talk/base/winsock_initializer.h | 40 +++++++++++++ third_party/libjingle/libjingle.gyp | 3 + .../overrides/talk/base/winsock_initializer.cc | 22 ++++++++ 6 files changed, 136 insertions(+), 20 deletions(-) create mode 100755 third_party/libjingle/files/talk/base/winsock_initializer.cc create mode 100755 third_party/libjingle/files/talk/base/winsock_initializer.h create mode 100755 third_party/libjingle/overrides/talk/base/winsock_initializer.cc (limited to 'third_party/libjingle') diff --git a/third_party/libjingle/files/talk/base/physicalsocketserver.cc b/third_party/libjingle/files/talk/base/physicalsocketserver.cc index 9cdacdfa..1eb9f3a 100644 --- a/third_party/libjingle/files/talk/base/physicalsocketserver.cc +++ b/third_party/libjingle/files/talk/base/physicalsocketserver.cc @@ -59,31 +59,12 @@ extern "C" { #include "talk/base/physicalsocketserver.h" #include "talk/base/time.h" #include "talk/base/winping.h" +#include "talk/base/winsock_initializer.h" #ifdef __linux #define IP_MTU 14 // Until this is integrated from linux/in.h to netinet/in.h #endif // __linux -#ifdef WIN32 -class WinsockInitializer { -public: - WinsockInitializer() { - WSADATA wsaData; - WORD wVersionRequested = MAKEWORD(1, 0); - err_ = WSAStartup(wVersionRequested, &wsaData); - } - ~WinsockInitializer() { - WSACleanup(); - } - int error() { - return err_; - } -private: - int err_; -}; -WinsockInitializer g_winsockinit; -#endif - namespace talk_base { const int kfRead = 0x0001; @@ -122,6 +103,9 @@ public: PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET) : ss_(ss), s_(s), enabled_events_(0), error_(0), state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED) { +#ifdef WIN32 + EnsureWinsockInit(); +#endif if (s != INVALID_SOCKET) enabled_events_ = kfRead | kfWrite; } diff --git a/third_party/libjingle/files/talk/base/win32socketserver.cc b/third_party/libjingle/files/talk/base/win32socketserver.cc index 49c8fee..bbec42e 100644 --- a/third_party/libjingle/files/talk/base/win32socketserver.cc +++ b/third_party/libjingle/files/talk/base/win32socketserver.cc @@ -29,6 +29,7 @@ #include "talk/base/common.h" #include "talk/base/logging.h" #include "talk/base/winping.h" +#include "talk/base/winsock_initializer.h" #include "talk/base/win32socketserver.h" #include "talk/base/win32window.h" #include @@ -267,6 +268,7 @@ Win32Socket::EventSink::OnFinalMessage(HWND hWnd) { Win32Socket::Win32Socket() : socket_(INVALID_SOCKET), error_(0), state_(CS_CLOSED), signal_close_(false), sink_(NULL), dns_(NULL) { + talk_base::EnsureWinsockInit(); // TODO: replace addr_ with SocketAddress memset(&addr_, 0, sizeof(addr_)); } diff --git a/third_party/libjingle/files/talk/base/winsock_initializer.cc b/third_party/libjingle/files/talk/base/winsock_initializer.cc new file mode 100755 index 0000000..f61ec80 --- /dev/null +++ b/third_party/libjingle/files/talk/base/winsock_initializer.cc @@ -0,0 +1,65 @@ +/* + * libjingle + * Copyright 2009, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "talk/base/winsock_init.h" + +#ifndef WIN32 +#error "Only compile this on Windows" +#endif + +#include + +namespace talk_base { + +void EnsureWinsockInit() { + // The default implementation uses a global initializer, so WSAStartup + // happens at module load time. Thus we don't need to do anything here. + // The hook is provided so that a client that statically links with + // libjingle can override it, to provide its own initialization. +} + +class WinsockInitializer { + public: + WinsockInitializer() { + WSADATA wsaData; + WORD wVersionRequested = MAKEWORD(1, 0); + err_ = WSAStartup(wVersionRequested, &wsaData); + } + ~WinsockInitializer() { + WSACleanup(); + } + int error() { + return err_; + } + private: + int err_; +}; +WinsockInitializer g_winsockinit; + +} // namespace talk_base + +#endif // TALK_BASE_WINSOCK_INIT_H__ diff --git a/third_party/libjingle/files/talk/base/winsock_initializer.h b/third_party/libjingle/files/talk/base/winsock_initializer.h new file mode 100755 index 0000000..5a7c499 --- /dev/null +++ b/third_party/libjingle/files/talk/base/winsock_initializer.h @@ -0,0 +1,40 @@ +/* + * libjingle + * Copyright 2009, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TALK_BASE_WINSOCK_INIT_H__ +#define TALK_BASE_WINSOCK_INIT_H__ + +namespace talk_base { + +#ifdef WIN32 +// Make sure that Winsock is initialized, calling WSAStartup if needed. +void EnsureWinsockInit(); +#endif + +} // namespace talk_base + +#endif // TALK_BASE_WINSOCK_INIT_H__ diff --git a/third_party/libjingle/libjingle.gyp b/third_party/libjingle/libjingle.gyp index 8730ca3..b360568 100644 --- a/third_party/libjingle/libjingle.gyp +++ b/third_party/libjingle/libjingle.gyp @@ -61,6 +61,7 @@ # 'files/talk/base/natserver_main.cc', # has a main() # 'files/talk/base/openssladapter.cc', # openssl # 'files/talk/base/openssladapter.h', # openssl + # 'files/talk/base/winsock_initializer.cc', # overridden 'files/talk/base/asynchttprequest.cc', 'files/talk/base/asynchttprequest.h', 'files/talk/base/asyncpacketsocket.cc', @@ -164,6 +165,7 @@ 'files/talk/base/urlencode.h', 'files/talk/base/virtualsocketserver.cc', 'files/talk/base/virtualsocketserver.h', + 'files/talk/base/winsock_initializer.h', 'files/talk/p2p/base/candidate.h', 'files/talk/p2p/base/common.h', 'files/talk/p2p/base/constants.cc', @@ -266,6 +268,7 @@ 'files/talk/base/winfirewall.h', 'files/talk/base/winping.cc', 'files/talk/base/winping.h', + 'overrides/talk/base/winsock_initializer.cc', ], }], ['OS=="linux" or OS=="mac"', { diff --git a/third_party/libjingle/overrides/talk/base/winsock_initializer.cc b/third_party/libjingle/overrides/talk/base/winsock_initializer.cc new file mode 100755 index 0000000..7e1858a --- /dev/null +++ b/third_party/libjingle/overrides/talk/base/winsock_initializer.cc @@ -0,0 +1,22 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Redirect Libjingle's winsock initialization activity into Chromium's +// singleton object that managest precisely that for the browser. + +#include "talk/base/winsock_initializer.h" + +#include "net/base/winsock_init.h" + +#ifndef WIN32 +#error "Only compile this on Windows" +#endif + +namespace talk_base { + +void EnsureWinsockInit() { + net::EnsureWinsockInit(); +} + +} // namespace talk_base -- cgit v1.1