2 * Copyright 2016 Facebook, Inc.
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 #include <folly/io/async/Request.h>
24 #include <glog/logging.h>
26 #include <folly/SingletonThreadLocal.h>
30 void RequestContext::setContextData(
31 const std::string& val,
32 std::unique_ptr<RequestData> data) {
33 folly::RWSpinLock::WriteHolder guard(lock);
34 if (data_.find(val) != data_.end()) {
35 LOG_FIRST_N(WARNING, 1)
36 << "Called RequestContext::setContextData with data already set";
40 data_[val] = std::move(data);
44 bool RequestContext::setContextDataIfAbsent(
45 const std::string& val,
46 std::unique_ptr<RequestData> data) {
47 folly::RWSpinLock::UpgradedHolder guard(lock);
48 if (data_.find(val) != data_.end()) {
52 folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
53 data_[val] = std::move(data);
57 bool RequestContext::hasContextData(const std::string& val) const {
58 folly::RWSpinLock::ReadHolder guard(lock);
59 return data_.find(val) != data_.end();
62 RequestData* RequestContext::getContextData(const std::string& val) {
63 folly::RWSpinLock::ReadHolder guard(lock);
64 auto r = data_.find(val);
65 if (r == data_.end()) {
68 return r->second.get();
72 const RequestData* RequestContext::getContextData(
73 const std::string& val) const {
74 folly::RWSpinLock::ReadHolder guard(lock);
75 auto r = data_.find(val);
76 if (r == data_.end()) {
79 return r->second.get();
83 void RequestContext::onSet() {
84 folly::RWSpinLock::ReadHolder guard(lock);
85 for (auto const& ent : data_) {
86 if (RequestData* data = ent.second.get()) {
92 void RequestContext::onUnset() {
93 folly::RWSpinLock::ReadHolder guard(lock);
94 for (auto const& ent : data_) {
95 if (RequestData* data = ent.second.get()) {
101 void RequestContext::clearContextData(const std::string& val) {
102 folly::RWSpinLock::WriteHolder guard(lock);
106 std::shared_ptr<RequestContext> RequestContext::setContext(
107 std::shared_ptr<RequestContext> ctx) {
108 auto& curCtx = getStaticContext();
122 std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
123 using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
124 static SingletonT singleton;
126 return singleton.get();