From: Lucian Grijincu <lucian@fb.com>
Date: Wed, 16 Sep 2015 17:05:52 +0000 (-0700)
Subject: folly: io: SSLContext/AsyncSSLSocket: drop global static mutex (siof)
X-Git-Tag: deprecate-dynamic-initializer~399
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=60331af0cfde1c9642367d4e40dc8cec17159ab4;p=folly.git

folly: io: SSLContext/AsyncSSLSocket: drop global static mutex (siof)

Reviewed By: @yfeldblum

Differential Revision: D2446538
---

diff --git a/folly/io/async/AsyncSSLSocket.cpp b/folly/io/async/AsyncSSLSocket.cpp
index c6dd25b5..f6f13e75 100644
--- a/folly/io/async/AsyncSSLSocket.cpp
+++ b/folly/io/async/AsyncSSLSocket.cpp
@@ -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) {
diff --git a/folly/io/async/AsyncSSLSocket.h b/folly/io/async/AsyncSSLSocket.h
index 87d91541..6385eef5 100644
--- a/folly/io/async/AsyncSSLSocket.h
+++ b/folly/io/async/AsyncSSLSocket.h
@@ -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.
diff --git a/folly/io/async/SSLContext.cpp b/folly/io/async/SSLContext.cpp
index 7a9a198f..cfd25c7f 100644
--- a/folly/io/async/SSLContext.cpp
+++ b/folly/io/async/SSLContext.cpp
@@ -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();
 }
 
diff --git a/folly/io/async/SSLContext.h b/folly/io/async/SSLContext.h
index 1b5592b8..7585c168 100644
--- a/folly/io/async/SSLContext.h
+++ b/folly/io/async/SSLContext.h
@@ -431,7 +431,6 @@ class SSLContext {
   std::vector<ClientHelloCallback> clientHelloCbs_;
 #endif
 
-  static std::mutex mutex_;
   static bool initialized_;
 
 #ifdef OPENSSL_NPN_NEGOTIATED