}
void SingletonVault::scheduleDestroyInstances() {
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
class SingletonVaultDestructor {
public:
// Mark registration is complete; no more singletons can be
// registered at this point.
void registrationComplete() {
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
std::atexit([](){ SingletonVault::singleton()->destroyInstances(); });
RWSpinLock::WriteHolder wh(&stateMutex_);
timeoutManager_->attachTimeoutManager(
this,
TimeoutManager::InternalEnum::NORMAL);
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::AsyncTimeout(EventBase* eventBase)
this,
TimeoutManager::InternalEnum::NORMAL);
}
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager,
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr;
timeoutManager_->attachTimeoutManager(this, internal);
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal)
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr;
timeoutManager_->attachTimeoutManager(this, internal);
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::AsyncTimeout(): timeoutManager_(nullptr) {
event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
event_.ev_base = nullptr;
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::~AsyncTimeout() {
}
VLOG(5) << "EventBase(): Created.";
initNotificationQueue();
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
// takes ownership of the event_base
throw std::invalid_argument("EventBase(): event base cannot be nullptr");
}
initNotificationQueue();
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
EventBase::~EventBase() {
pid_(getpid()),
queue_() {
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
#ifdef FOLLY_HAVE_EVENTFD
if (fdType == FdType::EVENTFD) {
+++ /dev/null
-/*
- * 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;
-
-}
// If you do not call create() to create a unique request context,
// this default request context will always be returned, and is never
// copied between threads.
-extern RequestContext* defaultContext;
-
class RequestContext {
public:
// Create a unique requext context for this request.
// Get the current context.
static RequestContext* get() {
- if (getStaticContext() == nullptr) {
- if (defaultContext == nullptr) {
- defaultContext = new RequestContext;
- }
- return defaultContext;
+ auto context = getStaticContext();
+ if (!context) {
+ static RequestContext defaultContext;
+ return std::addressof(defaultContext);
}
- return getStaticContext().get();
+ return context.get();
}
// The following API may be used to set per-request data in a thread-safe way.
static std::shared_ptr<RequestContext>
setContext(std::shared_ptr<RequestContext> ctx) {
- std::shared_ptr<RequestContext> old_ctx;
- if (getStaticContext()) {
- old_ctx = getStaticContext();
- }
- getStaticContext() = ctx;
- return old_ctx;
+ using std::swap;
+ swap(ctx, getStaticContext());
+ return ctx;
}
static std::shared_ptr<RequestContext> saveContext() {
return getStaticContext();
}
+ private:
// Used to solve static destruction ordering issue. Any static object
// that uses RequestContext must call this function in its constructor.
//
// See below link for more details.
// http://stackoverflow.com/questions/335369/
// finding-c-static-initialization-order-problems#335746
- static std::shared_ptr<RequestContext>&
- getStaticContext() {
+ static std::shared_ptr<RequestContext> &getStaticContext() {
static folly::ThreadLocal<std::shared_ptr<RequestContext> > context;
return *context;
}
- private:
folly::RWSpinLock lock;
std::map<std::string, std::unique_ptr<RequestData>> data_;
};