Commit 4a03236d authored by Marcelo Juchem's avatar Marcelo Juchem Committed by Pavlo Kushnir

cleaning up RequestContext

Test Plan: built folly

Reviewed By: mshneer@fb.com

Subscribers: davejwatson, mshneer, trunkagent, alandau, bmatheny, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2096146

Tasks: 6337419

Signature: t1:2096146:1432658670:4ff00e4274412519dc3b02d2a849b7ff289ac979
parent ddfdba13
...@@ -102,7 +102,7 @@ void SingletonVault::reenableInstances() { ...@@ -102,7 +102,7 @@ void SingletonVault::reenableInstances() {
} }
void SingletonVault::scheduleDestroyInstances() { void SingletonVault::scheduleDestroyInstances() {
RequestContext::getStaticContext(); RequestContext::saveContext();
class SingletonVaultDestructor { class SingletonVaultDestructor {
public: public:
......
...@@ -306,7 +306,7 @@ class SingletonVault { ...@@ -306,7 +306,7 @@ class SingletonVault {
// Mark registration is complete; no more singletons can be // Mark registration is complete; no more singletons can be
// registered at this point. // registered at this point.
void registrationComplete() { void registrationComplete() {
RequestContext::getStaticContext(); RequestContext::saveContext();
std::atexit([](){ SingletonVault::singleton()->destroyInstances(); }); std::atexit([](){ SingletonVault::singleton()->destroyInstances(); });
RWSpinLock::WriteHolder wh(&stateMutex_); RWSpinLock::WriteHolder wh(&stateMutex_);
......
...@@ -36,7 +36,7 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager) ...@@ -36,7 +36,7 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager)
timeoutManager_->attachTimeoutManager( timeoutManager_->attachTimeoutManager(
this, this,
TimeoutManager::InternalEnum::NORMAL); TimeoutManager::InternalEnum::NORMAL);
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
AsyncTimeout::AsyncTimeout(EventBase* eventBase) AsyncTimeout::AsyncTimeout(EventBase* eventBase)
...@@ -49,7 +49,7 @@ AsyncTimeout::AsyncTimeout(EventBase* eventBase) ...@@ -49,7 +49,7 @@ AsyncTimeout::AsyncTimeout(EventBase* eventBase)
this, this,
TimeoutManager::InternalEnum::NORMAL); TimeoutManager::InternalEnum::NORMAL);
} }
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager, AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager,
...@@ -59,7 +59,7 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager, ...@@ -59,7 +59,7 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager,
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr; event_.ev_base = nullptr;
timeoutManager_->attachTimeoutManager(this, internal); timeoutManager_->attachTimeoutManager(this, internal);
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal) AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal)
...@@ -68,13 +68,13 @@ AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal) ...@@ -68,13 +68,13 @@ AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal)
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr; event_.ev_base = nullptr;
timeoutManager_->attachTimeoutManager(this, internal); timeoutManager_->attachTimeoutManager(this, internal);
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
AsyncTimeout::AsyncTimeout(): timeoutManager_(nullptr) { AsyncTimeout::AsyncTimeout(): timeoutManager_(nullptr) {
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr; event_.ev_base = nullptr;
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
AsyncTimeout::~AsyncTimeout() { AsyncTimeout::~AsyncTimeout() {
......
...@@ -174,7 +174,7 @@ EventBase::EventBase(bool enableTimeMeasurement) ...@@ -174,7 +174,7 @@ EventBase::EventBase(bool enableTimeMeasurement)
} }
VLOG(5) << "EventBase(): Created."; VLOG(5) << "EventBase(): Created.";
initNotificationQueue(); initNotificationQueue();
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
// takes ownership of the event_base // takes ownership of the event_base
...@@ -199,7 +199,7 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement) ...@@ -199,7 +199,7 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
throw std::invalid_argument("EventBase(): event base cannot be nullptr"); throw std::invalid_argument("EventBase(): event base cannot be nullptr");
} }
initNotificationQueue(); initNotificationQueue();
RequestContext::getStaticContext(); RequestContext::saveContext();
} }
EventBase::~EventBase() { EventBase::~EventBase() {
......
...@@ -228,7 +228,7 @@ class NotificationQueue { ...@@ -228,7 +228,7 @@ class NotificationQueue {
pid_(getpid()), pid_(getpid()),
queue_() { queue_() {
RequestContext::getStaticContext(); RequestContext::saveContext();
#ifdef FOLLY_HAVE_EVENTFD #ifdef FOLLY_HAVE_EVENTFD
if (fdType == FdType::EVENTFD) { if (fdType == FdType::EVENTFD) {
......
/*
* Copyright 2015 Facebook, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <folly/io/async/Request.h>
namespace folly {
RequestContext* defaultContext;
}
...@@ -41,8 +41,6 @@ class RequestContext; ...@@ -41,8 +41,6 @@ class RequestContext;
// If you do not call create() to create a unique request context, // If you do not call create() to create a unique request context,
// this default request context will always be returned, and is never // this default request context will always be returned, and is never
// copied between threads. // copied between threads.
extern RequestContext* defaultContext;
class RequestContext { class RequestContext {
public: public:
// Create a unique requext context for this request. // Create a unique requext context for this request.
...@@ -54,13 +52,12 @@ class RequestContext { ...@@ -54,13 +52,12 @@ class RequestContext {
// Get the current context. // Get the current context.
static RequestContext* get() { static RequestContext* get() {
if (getStaticContext() == nullptr) { auto context = getStaticContext();
if (defaultContext == nullptr) { if (!context) {
defaultContext = new RequestContext; static RequestContext defaultContext;
} return std::addressof(defaultContext);
return defaultContext;
} }
return getStaticContext().get(); return context.get();
} }
// The following API may be used to set per-request data in a thread-safe way. // The following API may be used to set per-request data in a thread-safe way.
...@@ -108,31 +105,27 @@ class RequestContext { ...@@ -108,31 +105,27 @@ class RequestContext {
static std::shared_ptr<RequestContext> static std::shared_ptr<RequestContext>
setContext(std::shared_ptr<RequestContext> ctx) { setContext(std::shared_ptr<RequestContext> ctx) {
std::shared_ptr<RequestContext> old_ctx; using std::swap;
if (getStaticContext()) { swap(ctx, getStaticContext());
old_ctx = getStaticContext(); return ctx;
}
getStaticContext() = ctx;
return old_ctx;
} }
static std::shared_ptr<RequestContext> saveContext() { static std::shared_ptr<RequestContext> saveContext() {
return getStaticContext(); return getStaticContext();
} }
private:
// Used to solve static destruction ordering issue. Any static object // Used to solve static destruction ordering issue. Any static object
// that uses RequestContext must call this function in its constructor. // that uses RequestContext must call this function in its constructor.
// //
// See below link for more details. // See below link for more details.
// http://stackoverflow.com/questions/335369/ // http://stackoverflow.com/questions/335369/
// finding-c-static-initialization-order-problems#335746 // finding-c-static-initialization-order-problems#335746
static std::shared_ptr<RequestContext>& static std::shared_ptr<RequestContext> &getStaticContext() {
getStaticContext() {
static folly::ThreadLocal<std::shared_ptr<RequestContext> > context; static folly::ThreadLocal<std::shared_ptr<RequestContext> > context;
return *context; return *context;
} }
private:
folly::RWSpinLock lock; folly::RWSpinLock lock;
std::map<std::string, std::unique_ptr<RequestData>> data_; std::map<std::string, std::unique_ptr<RequestData>> data_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment