Move RequestContext definitions to source files
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 23 Jul 2016 22:44:57 +0000 (15:44 -0700)
committerFacebook Github Bot 3 <facebook-github-bot-3-bot@fb.com>
Sat, 23 Jul 2016 22:53:23 +0000 (15:53 -0700)
Summary:
[Folly] Move `RequestContext` definitions to source files.

Keeping headers lightweight can help with build times.

Reviewed By: djwatson

Differential Revision: D3609809

fbshipit-source-id: 20608e3ff764c86c24355a328da1dcca9a08fce4

folly/fibers/Fiber.h
folly/futures/test/WindowTest.cpp
folly/io/async/HHWheelTimer.cpp
folly/io/async/Request.cpp
folly/io/async/Request.h
folly/io/async/test/AsyncSSLSocketTest.cpp
folly/io/async/test/AsyncSSLSocketTest.h
folly/io/async/test/EventBaseTest.cpp

index f543321ba54aa3b0cba22874437fc73eabbd6eb2..29269b282fc599e6b7c2ebcd5caefd1fc2e59f76 100644 (file)
@@ -16,6 +16,7 @@
 #pragma once
 
 #include <functional>
+#include <thread>
 #include <typeinfo>
 
 #include <folly/AtomicIntrusiveLinkedList.h>
index ddd43d973887f1ea96c69794dbfc038522e0090c..6eecedc199ef544d90155c475b04f447acb91ecf 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <boost/thread/barrier.hpp>
 
+#include <folly/Conv.h>
 #include <folly/futures/Future.h>
 
 #include <vector>
index e16e4b39b93f686912d461b9a1400956f488edf8..55d489b5272c53de4eedf4cbdf7c33972d5f5c7d 100644 (file)
@@ -21,6 +21,7 @@
 #include <folly/io/async/HHWheelTimer.h>
 #include <folly/io/async/Request.h>
 
+#include <folly/Memory.h>
 #include <folly/Optional.h>
 #include <folly/ScopeGuard.h>
 
index d2d17ca7d83cd344c8821ee4411ab9685dc3e5a5..939ac2e0b93bbe53e917d1562e6647231cc80b68 100644 (file)
  * specific language governing permissions and limitations
  * under the License.
  */
+
 #include <folly/io/async/Request.h>
 
+#include <glog/logging.h>
+
+#include <folly/SingletonThreadLocal.h>
+
 namespace folly {
 
+void RequestContext::setContextData(
+    const std::string& val,
+    std::unique_ptr<RequestData> data) {
+  folly::RWSpinLock::WriteHolder guard(lock);
+  if (data_.find(val) != data_.end()) {
+    LOG_FIRST_N(WARNING, 1)
+        << "Called RequestContext::setContextData with data already set";
+
+    data_[val] = nullptr;
+  } else {
+    data_[val] = std::move(data);
+  }
+}
+
+bool RequestContext::setContextDataIfAbsent(
+    const std::string& val,
+    std::unique_ptr<RequestData> data) {
+  folly::RWSpinLock::UpgradedHolder guard(lock);
+  if (data_.find(val) != data_.end()) {
+    return false;
+  }
+
+  folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
+  data_[val] = std::move(data);
+  return true;
+}
+
+bool RequestContext::hasContextData(const std::string& val) const {
+  folly::RWSpinLock::ReadHolder guard(lock);
+  return data_.find(val) != data_.end();
+}
+
+RequestData* RequestContext::getContextData(const std::string& val) {
+  folly::RWSpinLock::ReadHolder guard(lock);
+  auto r = data_.find(val);
+  if (r == data_.end()) {
+    return nullptr;
+  } else {
+    return r->second.get();
+  }
+}
+
+const RequestData* RequestContext::getContextData(
+    const std::string& val) const {
+  folly::RWSpinLock::ReadHolder guard(lock);
+  auto r = data_.find(val);
+  if (r == data_.end()) {
+    return nullptr;
+  } else {
+    return r->second.get();
+  }
+}
+
+void RequestContext::onSet() {
+  folly::RWSpinLock::ReadHolder guard(lock);
+  for (auto const& ent : data_) {
+    if (RequestData* data = ent.second.get()) {
+      data->onSet();
+    }
+  }
+}
+
+void RequestContext::onUnset() {
+  folly::RWSpinLock::ReadHolder guard(lock);
+  for (auto const& ent : data_) {
+    if (RequestData* data = ent.second.get()) {
+      data->onUnset();
+    }
+  }
+}
+
+void RequestContext::clearContextData(const std::string& val) {
+  folly::RWSpinLock::WriteHolder guard(lock);
+  data_.erase(val);
+}
+
+std::shared_ptr<RequestContext> RequestContext::setContext(
+    std::shared_ptr<RequestContext> ctx) {
+  auto& prev = getStaticContext();
+  if (ctx != prev) {
+    using std::swap;
+    if (ctx) {
+      ctx->onSet();
+    }
+    if (prev) {
+      prev->onUnset();
+    }
+    swap(ctx, prev);
+  }
+  return ctx;
+}
+
 std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
   using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
   static SingletonT singleton;
index 7adb1a9c3d978024b4539e5ccfbd9f422669c505..194305fc3727622cffd736b37d2cdbc9735e05ff 100644 (file)
@@ -22,9 +22,7 @@
 
 #include <map>
 #include <memory>
-#include <glog/logging.h>
 #include <folly/RWSpinLock.h>
-#include <folly/SingletonThreadLocal.h>
 
 namespace folly {
 
@@ -34,8 +32,8 @@ namespace folly {
 class RequestData {
  public:
   virtual ~RequestData() = default;
-  virtual void onSet(){};
-  virtual void onUnset(){};
+  virtual void onSet() {}
+  virtual void onUnset() {}
 };
 
 class RequestContext;
@@ -66,69 +64,24 @@ class RequestContext {
   // This access is still performance sensitive, so please ask if you need help
   // profiling any use of these functions.
   void setContextData(
-    const std::string& val, std::unique_ptr<RequestData> data) {
-    folly::RWSpinLock::WriteHolder guard(lock);
-    if (data_.find(val) != data_.end()) {
-      LOG_FIRST_N(WARNING, 1) <<
-        "Called RequestContext::setContextData with data already set";
-
-      data_[val] = nullptr;
-    } else {
-      data_[val] = std::move(data);
-    }
-  }
+      const std::string& val,
+      std::unique_ptr<RequestData> data);
 
   // Unlike setContextData, this method does not panic if the key is already
   // present. Returns true iff the new value has been inserted.
-  bool setContextDataIfAbsent(const std::string& val,
-                              std::unique_ptr<RequestData> data) {
-    folly::RWSpinLock::UpgradedHolder guard(lock);
-    if (data_.find(val) != data_.end()) {
-      return false;
-    }
-
-    folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
-    data_[val] = std::move(data);
-    return true;
-  }
-
-  bool hasContextData(const std::string& val) {
-    folly::RWSpinLock::ReadHolder guard(lock);
-    return data_.find(val) != data_.end();
-  }
+  bool setContextDataIfAbsent(
+      const std::string& val,
+      std::unique_ptr<RequestData> data);
 
-  RequestData* getContextData(const std::string& val) {
-    folly::RWSpinLock::ReadHolder guard(lock);
-    auto r = data_.find(val);
-    if (r == data_.end()) {
-      return nullptr;
-    } else {
-      return r->second.get();
-    }
-  }
+  bool hasContextData(const std::string& val) const;
 
-  void onSet() {
-    folly::RWSpinLock::ReadHolder guard(lock);
-    for (auto const& ent : data_) {
-      if (RequestData* data = ent.second.get()) {
-        data->onSet();
-      }
-    }
-  }
+  RequestData* getContextData(const std::string& val);
+  const RequestData* getContextData(const std::string& val) const;
 
-  void onUnset() {
-    folly::RWSpinLock::ReadHolder guard(lock);
-    for (auto const& ent : data_) {
-      if (RequestData* data = ent.second.get()) {
-        data->onUnset();
-      }
-    }
-  }
+  void onSet();
+  void onUnset();
 
-  void clearContextData(const std::string& val) {
-    folly::RWSpinLock::WriteHolder guard(lock);
-    data_.erase(val);
-  }
+  void clearContextData(const std::string& val);
 
   // The following API is used to pass the context through queues / threads.
   // saveContext is called to get a shared_ptr to the context, and
@@ -140,21 +93,8 @@ class RequestContext {
   //
   // A shared_ptr is used, because many request may fan out across
   // multiple threads, or do post-send processing, etc.
-  static std::shared_ptr<RequestContext>
-  setContext(std::shared_ptr<RequestContext> ctx) {
-    auto& prev = getStaticContext();
-    if (ctx != prev) {
-      using std::swap;
-      if (ctx) {
-        ctx->onSet();
-      }
-      if (prev) {
-        prev->onUnset();
-      }
-      swap(ctx, prev);
-    }
-    return ctx;
-  }
+  static std::shared_ptr<RequestContext> setContext(
+      std::shared_ptr<RequestContext> ctx);
 
   static std::shared_ptr<RequestContext> saveContext() {
     return getStaticContext();
@@ -163,7 +103,7 @@ class RequestContext {
  private:
   static std::shared_ptr<RequestContext>& getStaticContext();
 
-  folly::RWSpinLock lock;
+  mutable folly::RWSpinLock lock;
   std::map<std::string, std::unique_ptr<RequestData>> data_;
 };
 
index cdacacada0a75d22c407c03b005446edcf351c63..dc60413099b2e98673a22e4bc7bf63baaf38a4ce 100644 (file)
@@ -35,6 +35,7 @@
 #include <iostream>
 #include <list>
 #include <set>
+#include <thread>
 
 #include <gmock/gmock.h>
 
index 384198d1a48cbae5ea050d768eb8cbd6b72c9206..d9618098f3c4c54d417360809908cad76634505f 100644 (file)
 #include <folly/portability/Sockets.h>
 #include <folly/portability/Unistd.h>
 
+#include <fcntl.h>
 #include <gtest/gtest.h>
+#include <sys/types.h>
+#include <condition_variable>
 #include <iostream>
 #include <list>
-#include <fcntl.h>
-#include <sys/types.h>
 
 namespace folly {
 
index 9efaf571eb7cae6233412efca37428cc03b6b391..f3d6f00a82053983b0852daba9cdf7d31ef8fbcd 100644 (file)
@@ -16,7 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 #include <folly/Memory.h>
+#include <folly/ScopeGuard.h>
 
 #include <folly/io/async/AsyncTimeout.h>
 #include <folly/io/async/EventBase.h>