folly: io: SSLContext/AsyncSSLSocket: drop global static mutex (siof)
authorLucian Grijincu <lucian@fb.com>
Wed, 16 Sep 2015 17:05:52 +0000 (10:05 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Wed, 16 Sep 2015 17:20:18 +0000 (10:20 -0700)
Reviewed By: @yfeldblum

Differential Revision: D2446538

folly/io/async/AsyncSSLSocket.cpp
folly/io/async/AsyncSSLSocket.h
folly/io/async/SSLContext.cpp
folly/io/async/SSLContext.h

index c6dd25b519a3f93b2ec4d2b51ce5d3d2ef55889a..f6f13e7585759615f885ea16d16c815fa6cfd6db 100644 (file)
@@ -607,18 +607,10 @@ void AsyncSSLSocket::timeoutExpired() noexcept {
   }
 }
 
-int AsyncSSLSocket::sslExDataIndex_ = -1;
-std::mutex AsyncSSLSocket::mutex_;
-
 int AsyncSSLSocket::getSSLExDataIndex() {
-  if (sslExDataIndex_ < 0) {
-    std::lock_guard<std::mutex> g(mutex_);
-    if (sslExDataIndex_ < 0) {
-      sslExDataIndex_ = SSL_get_ex_new_index(0,
-          (void*)"AsyncSSLSocket data index", nullptr, nullptr, nullptr);
-    }
-  }
-  return sslExDataIndex_;
+  static auto index = SSL_get_ex_new_index(
+      0, (void*)"AsyncSSLSocket data index", nullptr, nullptr, nullptr);
+  return index;
 }
 
 AsyncSSLSocket* AsyncSSLSocket::getFromSSL(const SSL *ssl) {
index 87d9154127963e07a362a3eab8266438bfd157ee..6385eef50338ffccca33f2e643f4b00b88db827d 100644 (file)
@@ -803,8 +803,6 @@ class AsyncSSLSocket : public virtual AsyncSocket {
 
   static void sslInfoCallback(const SSL *ssl, int type, int val);
 
-  static std::mutex mutex_;
-  static int sslExDataIndex_;
   // Whether we've applied the TCP_CORK option to the socket
   bool corked_{false};
   // SSL related members.
index 7a9a198f7d0582d4402bc24373858a5464c35a47..cfd25c7f7c55341b944ec7344afdcf4b7f2a8801 100644 (file)
@@ -35,7 +35,16 @@ struct CRYPTO_dynlock_value {
 namespace folly {
 
 bool SSLContext::initialized_ = false;
-std::mutex    SSLContext::mutex_;
+
+namespace {
+
+std::mutex& initMutex() {
+  static std::mutex m;
+  return m;
+}
+
+}  // anonymous namespace
+
 #ifdef OPENSSL_NPN_NEGOTIATED
 int SSLContext::sNextProtocolsExDataIndex_ = -1;
 #endif
@@ -43,7 +52,7 @@ int SSLContext::sNextProtocolsExDataIndex_ = -1;
 // SSLContext implementation
 SSLContext::SSLContext(SSLVersion version) {
   {
-    std::lock_guard<std::mutex> g(mutex_);
+    std::lock_guard<std::mutex> g(initMutex());
     initializeOpenSSLLocked();
   }
 
@@ -679,12 +688,12 @@ void SSLContext::setSSLLockTypes(std::map<int, SSLLockType> inLockTypes) {
 }
 
 void SSLContext::markInitialized() {
-  std::lock_guard<std::mutex> g(mutex_);
+  std::lock_guard<std::mutex> g(initMutex());
   initialized_ = true;
 }
 
 void SSLContext::initializeOpenSSL() {
-  std::lock_guard<std::mutex> g(mutex_);
+  std::lock_guard<std::mutex> g(initMutex());
   initializeOpenSSLLocked();
 }
 
@@ -715,7 +724,7 @@ void SSLContext::initializeOpenSSLLocked() {
 }
 
 void SSLContext::cleanupOpenSSL() {
-  std::lock_guard<std::mutex> g(mutex_);
+  std::lock_guard<std::mutex> g(initMutex());
   cleanupOpenSSLLocked();
 }
 
index 1b5592b8f1f76067a5e44d1b94c751870ee5f868..7585c168b8a285cda5cc79b22e8ca31cc3492794 100644 (file)
@@ -431,7 +431,6 @@ class SSLContext {
   std::vector<ClientHelloCallback> clientHelloCbs_;
 #endif
 
-  static std::mutex mutex_;
   static bool initialized_;
 
 #ifdef OPENSSL_NPN_NEGOTIATED