From: Yedidya Feldblum Date: Sat, 23 Jul 2016 22:44:57 +0000 (-0700) Subject: Move RequestContext definitions to source files X-Git-Tag: 2016.07.26~7 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=4fde70969e903ab0d8419e4371a23bb3fed48e89;p=folly.git Move RequestContext definitions to source files 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 --- diff --git a/folly/fibers/Fiber.h b/folly/fibers/Fiber.h index f543321b..29269b28 100644 --- a/folly/fibers/Fiber.h +++ b/folly/fibers/Fiber.h @@ -16,6 +16,7 @@ #pragma once #include +#include #include #include diff --git a/folly/futures/test/WindowTest.cpp b/folly/futures/test/WindowTest.cpp index ddd43d97..6eecedc1 100644 --- a/folly/futures/test/WindowTest.cpp +++ b/folly/futures/test/WindowTest.cpp @@ -18,6 +18,7 @@ #include +#include #include #include diff --git a/folly/io/async/HHWheelTimer.cpp b/folly/io/async/HHWheelTimer.cpp index e16e4b39..55d489b5 100644 --- a/folly/io/async/HHWheelTimer.cpp +++ b/folly/io/async/HHWheelTimer.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include diff --git a/folly/io/async/Request.cpp b/folly/io/async/Request.cpp index d2d17ca7..939ac2e0 100644 --- a/folly/io/async/Request.cpp +++ b/folly/io/async/Request.cpp @@ -18,10 +18,107 @@ * specific language governing permissions and limitations * under the License. */ + #include +#include + +#include + namespace folly { +void RequestContext::setContextData( + const std::string& val, + std::unique_ptr 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 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::setContext( + std::shared_ptr 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::getStaticContext() { using SingletonT = SingletonThreadLocal>; static SingletonT singleton; diff --git a/folly/io/async/Request.h b/folly/io/async/Request.h index 7adb1a9c..194305fc 100644 --- a/folly/io/async/Request.h +++ b/folly/io/async/Request.h @@ -22,9 +22,7 @@ #include #include -#include #include -#include 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 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 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 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 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 - setContext(std::shared_ptr 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 setContext( + std::shared_ptr ctx); static std::shared_ptr saveContext() { return getStaticContext(); @@ -163,7 +103,7 @@ class RequestContext { private: static std::shared_ptr& getStaticContext(); - folly::RWSpinLock lock; + mutable folly::RWSpinLock lock; std::map> data_; }; diff --git a/folly/io/async/test/AsyncSSLSocketTest.cpp b/folly/io/async/test/AsyncSSLSocketTest.cpp index cdacacad..dc604130 100644 --- a/folly/io/async/test/AsyncSSLSocketTest.cpp +++ b/folly/io/async/test/AsyncSSLSocketTest.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include diff --git a/folly/io/async/test/AsyncSSLSocketTest.h b/folly/io/async/test/AsyncSSLSocketTest.h index 384198d1..d9618098 100644 --- a/folly/io/async/test/AsyncSSLSocketTest.h +++ b/folly/io/async/test/AsyncSSLSocketTest.h @@ -30,11 +30,12 @@ #include #include +#include #include +#include +#include #include #include -#include -#include namespace folly { diff --git a/folly/io/async/test/EventBaseTest.cpp b/folly/io/async/test/EventBaseTest.cpp index 9efaf571..f3d6f00a 100644 --- a/folly/io/async/test/EventBaseTest.cpp +++ b/folly/io/async/test/EventBaseTest.cpp @@ -16,7 +16,9 @@ * specific language governing permissions and limitations * under the License. */ + #include +#include #include #include