#include <glog/logging.h>
+#include <folly/MapUtil.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()) {
+ auto wlock = data_.wlock();
+ if (wlock->count(val)) {
LOG_FIRST_N(WARNING, 1)
<< "Called RequestContext::setContextData with data already set";
- data_[val] = nullptr;
+ (*wlock)[val] = nullptr;
} else {
- data_[val] = std::move(data);
+ (*wlock)[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()) {
+ auto ulock = data_.ulock();
+ if (ulock->count(val)) {
return false;
}
- folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
- data_[val] = std::move(data);
+ auto wlock = ulock.moveFromUpgradeToWrite();
+ (*wlock)[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();
+ return data_.rlock()->count(val);
}
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();
- }
+ return get_ref_default(*data_.rlock(), val, nullptr).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();
- }
+ return get_ref_default(*data_.rlock(), val, nullptr).get();
}
void RequestContext::onSet() {
- folly::RWSpinLock::ReadHolder guard(lock);
- for (auto const& ent : data_) {
- if (RequestData* data = ent.second.get()) {
+ auto rlock = data_.rlock();
+ for (auto const& ent : *rlock) {
+ if (auto& data = ent.second) {
data->onSet();
}
}
}
void RequestContext::onUnset() {
- folly::RWSpinLock::ReadHolder guard(lock);
- for (auto const& ent : data_) {
- if (RequestData* data = ent.second.get()) {
+ auto rlock = data_.rlock();
+ for (auto const& ent : *rlock) {
+ if (auto& data = ent.second) {
data->onUnset();
}
}
}
void RequestContext::clearContextData(const std::string& val) {
- folly::RWSpinLock::WriteHolder guard(lock);
- data_.erase(val);
+ data_.wlock()->erase(val);
}
std::shared_ptr<RequestContext> RequestContext::setContext(
#include <gtest/gtest.h>
#include <thread>
+#include <folly/Memory.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/Request.h>
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
RequestContext::get()->setContextData(
- "test",
- std::unique_ptr<TestData>(new TestData(10)));
+ "test", folly::make_unique<TestData>(10));
base.runInEventBaseThread([&](){
EXPECT_TRUE(RequestContext::get() != nullptr);
auto data = dynamic_cast<TestData*>(
EXPECT_TRUE(RequestContext::get() != nullptr);
RequestContext::get()->setContextData(
- "test", std::unique_ptr<TestData>(new TestData(10)));
+ "test", folly::make_unique<TestData>(10));
EXPECT_FALSE(RequestContext::get()->setContextDataIfAbsent(
- "test", std::unique_ptr<TestData>(new TestData(20))));
+ "test", folly::make_unique<TestData>(20)));
EXPECT_EQ(10,
dynamic_cast<TestData*>(
RequestContext::get()->getContextData("test"))->data_);
EXPECT_TRUE(RequestContext::get()->setContextDataIfAbsent(
- "test2", std::unique_ptr<TestData>(new TestData(20))));
+ "test2", folly::make_unique<TestData>(20)));
EXPECT_EQ(20,
dynamic_cast<TestData*>(
RequestContext::get()->getContextData("test2"))->data_);
TEST(RequestContext, testSetUnset) {
RequestContext::create();
auto ctx1 = RequestContext::saveContext();
- ctx1->setContextData("test", std::unique_ptr<TestData>(new TestData(10)));
+ ctx1->setContextData("test", folly::make_unique<TestData>(10));
auto testData1 = dynamic_cast<TestData*>(ctx1->getContextData("test"));
// Override RequestContext
RequestContext::create();
auto ctx2 = RequestContext::saveContext();
- ctx2->setContextData("test", std::unique_ptr<TestData>(new TestData(20)));
+ ctx2->setContextData("test", folly::make_unique<TestData>(20));
auto testData2 = dynamic_cast<TestData*>(ctx2->getContextData("test"));
// Check ctx1->onUnset was called
EXPECT_EQ(1, testData2->set_);
EXPECT_EQ(1, testData2->unset_);
}
-
-int main(int argc, char** argv) {
- testing::InitGoogleTest(&argc, argv);
- google::InitGoogleLogging(argv[0]);
- google::ParseCommandLineFlags(&argc, &argv, true);
-
- return RUN_ALL_TESTS();
-}