proxygen
folly::RequestContext Class Reference

#include <Request.h>

Classes

struct  State
 

Public Member Functions

void setContextData (const RequestToken &val, std::unique_ptr< RequestData > data)
 
void setContextData (const std::string &val, std::unique_ptr< RequestData > data)
 
bool setContextDataIfAbsent (const RequestToken &val, std::unique_ptr< RequestData > data)
 
bool setContextDataIfAbsent (const std::string &val, std::unique_ptr< RequestData > data)
 
void clearContextData (const RequestToken &val)
 
void clearContextData (const std::string &val)
 
bool hasContextData (const RequestToken &val) const
 
bool hasContextData (const std::string &val) const
 
RequestDatagetContextData (const RequestToken &val)
 
const RequestDatagetContextData (const RequestToken &val) const
 
RequestDatagetContextData (const std::string &val)
 
const RequestDatagetContextData (const std::string &val) const
 
void onSet ()
 
void onUnset ()
 

Static Public Member Functions

static void create ()
 
static RequestContextget ()
 
static std::shared_ptr< RequestContextsetContext (std::shared_ptr< RequestContext > ctx)
 
static std::shared_ptr< RequestContextsaveContext ()
 

Private Types

enum  DoSetBehaviour { DoSetBehaviour::SET, DoSetBehaviour::SET_IF_ABSENT, DoSetBehaviour::OVERWRITE }
 

Private Member Functions

void overwriteContextData (const RequestToken &val, std::unique_ptr< RequestData > data)
 
void overwriteContextData (const std::string &val, std::unique_ptr< RequestData > data)
 
bool doSetContextData (const RequestToken &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
 
bool doSetContextData (const std::string &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
 

Static Private Member Functions

static std::shared_ptr< RequestContext > & getStaticContext ()
 
static std::shared_ptr< RequestContextsetShallowCopyContext ()
 

Private Attributes

folly::Synchronized< Statestate_
 

Friends

struct ShallowCopyRequestContextScopeGuard
 

Detailed Description

Definition at line 113 of file Request.h.

Member Enumeration Documentation

Enumerator
SET 
SET_IF_ABSENT 
OVERWRITE 

Definition at line 223 of file Request.h.

223  {
224  SET,
225  SET_IF_ABSENT,
226  OVERWRITE,
227  };

Member Function Documentation

void folly::RequestContext::clearContextData ( const RequestToken val)

Definition at line 176 of file Request.cpp.

References folly::data(), folly::gen::move, folly::ulock(), and folly::wlock().

Referenced by TEST_F().

176  {
177  RequestData::SharedPtr requestData;
178  // Delete the RequestData after giving up the wlock just in case one of the
179  // RequestData destructors will try to grab the lock again.
180  {
181  auto ulock = state_.ulock();
182  auto it = ulock->requestData_.find(val);
183  if (it == ulock->requestData_.end()) {
184  return;
185  }
186 
187  auto wlock = ulock.moveFromUpgradeToWrite();
188  if (it->second && it->second->hasCallback()) {
189  it->second->onUnset();
190  wlock->callbackData_.erase(it->second.get());
191  }
192 
193  requestData = std::move(it->second);
194  wlock->requestData_.erase(it);
195  }
196 }
auto wlock(Synchronized< D, M > &synchronized, Args &&...args)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
std::unique_ptr< RequestData, DestructPtr > SharedPtr
Definition: Request.h:101
auto ulock(Synchronized< D, M > &synchronized, Args &&...args)
folly::Synchronized< State > state_
Definition: Request.h:249
void folly::RequestContext::clearContextData ( const std::string val)
inline

Definition at line 158 of file Request.h.

References folly::RequestToken::RequestToken().

158  {
159  clearContextData(RequestToken(val));
160  }
double val
Definition: String.cpp:273
void clearContextData(const RequestToken &val)
Definition: Request.cpp:176
static void folly::RequestContext::create ( )
inlinestatic

Definition at line 118 of file Request.h.

References folly::data(), and val.

Referenced by folly::RequestContextScopeGuard::RequestContextScopeGuard(), and TEST_F().

118  {
119  setContext(std::make_shared<RequestContext>());
120  }
static std::shared_ptr< RequestContext > setContext(std::shared_ptr< RequestContext > ctx)
Definition: Request.cpp:227
bool folly::RequestContext::doSetContextData ( const RequestToken val,
std::unique_ptr< RequestData > &  data,
DoSetBehaviour  behaviour 
)
private

Definition at line 87 of file Request.cpp.

References folly::RequestData::constructPtr(), folly::RequestToken::getDebugString(), folly::ulock(), val, folly::WARNING, and folly::wlock().

90  {
91  auto ulock = state_.ulock();
92 
93  bool conflict = false;
94  auto it = ulock->requestData_.find(val);
95  if (it != ulock->requestData_.end()) {
96  if (behaviour == DoSetBehaviour::SET_IF_ABSENT) {
97  return false;
98  } else if (behaviour == DoSetBehaviour::SET) {
99  LOG_FIRST_N(WARNING, 1)
100  << "Calling RequestContext::setContextData for "
101  << val.getDebugString() << " but it is already set";
102  }
103  conflict = true;
104  }
105 
106  auto wlock = ulock.moveFromUpgradeToWrite();
107  if (conflict) {
108  if (it->second) {
109  if (it->second->hasCallback()) {
110  it->second->onUnset();
111  wlock->callbackData_.erase(it->second.get());
112  }
113  it->second.reset(nullptr);
114  }
115  if (behaviour == DoSetBehaviour::SET) {
116  return true;
117  }
118  }
119 
120  if (data && data->hasCallback()) {
121  wlock->callbackData_.insert(data.get());
122  data->onSet();
123  }
124  wlock->requestData_[val] = RequestData::constructPtr(data.release());
125 
126  return true;
127 }
auto wlock(Synchronized< D, M > &synchronized, Args &&...args)
double val
Definition: String.cpp:273
static SharedPtr constructPtr(RequestData *ptr)
Definition: Request.cpp:77
auto ulock(Synchronized< D, M > &synchronized, Args &&...args)
folly::Synchronized< State > state_
Definition: Request.h:249
bool folly::RequestContext::doSetContextData ( const std::string val,
std::unique_ptr< RequestData > &  data,
DoSetBehaviour  behaviour 
)
inlineprivate

Definition at line 233 of file Request.h.

References folly::RequestToken::RequestToken().

236  {
237  return doSetContextData(RequestToken(val), data, behaviour);
238  }
bool doSetContextData(const RequestToken &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
Definition: Request.cpp:87
double val
Definition: String.cpp:273
RequestContext * folly::RequestContext::get ( )
static

Definition at line 290 of file Request.cpp.

References context.

Referenced by folly::RequestEventBase::get(), RequestContextTest::getContext(), folly::RequestEventBase::set(), folly::ShallowCopyRequestContextScopeGuard::ShallowCopyRequestContextScopeGuard(), TEST(), and TEST_F().

290  {
291  auto& context = getStaticContext();
292  if (!context) {
293  static RequestContext defaultContext;
294  return std::addressof(defaultContext);
295  }
296  return context.get();
297 }
context
Definition: CMakeCache.txt:563
static std::shared_ptr< RequestContext > & getStaticContext()
Definition: Request.cpp:263
RequestData * folly::RequestContext::getContextData ( const RequestToken val)

Definition at line 151 of file Request.cpp.

References folly::get_ref_default(), and val.

Referenced by folly::RequestEventBase::get(), TEST(), and TEST_F().

151  {
152  const RequestData::SharedPtr dflt{nullptr};
153  return get_ref_default(state_.rlock()->requestData_, val, dflt).get();
154 }
const Map::mapped_type & get_ref_default(const Map &map, const Key &key, const typename Map::mapped_type &dflt)
Definition: MapUtil.h:119
double val
Definition: String.cpp:273
std::unique_ptr< RequestData, DestructPtr > SharedPtr
Definition: Request.h:101
folly::Synchronized< State > state_
Definition: Request.h:249
const RequestData * folly::RequestContext::getContextData ( const RequestToken val) const

Definition at line 156 of file Request.cpp.

References folly::get_ref_default(), and val.

157  {
158  const RequestData::SharedPtr dflt{nullptr};
159  return get_ref_default(state_.rlock()->requestData_, val, dflt).get();
160 }
const Map::mapped_type & get_ref_default(const Map &map, const Key &key, const typename Map::mapped_type &dflt)
Definition: MapUtil.h:119
double val
Definition: String.cpp:273
std::unique_ptr< RequestData, DestructPtr > SharedPtr
Definition: Request.h:101
folly::Synchronized< State > state_
Definition: Request.h:249
RequestData* folly::RequestContext::getContextData ( const std::string val)
inline

Definition at line 173 of file Request.h.

References folly::RequestToken::RequestToken().

173  {
174  return getContextData(RequestToken(val));
175  }
double val
Definition: String.cpp:273
RequestData * getContextData(const RequestToken &val)
Definition: Request.cpp:151
const RequestData* folly::RequestContext::getContextData ( const std::string val) const
inline

Definition at line 176 of file Request.h.

References folly::RequestToken::RequestToken().

176  {
177  return getContextData(RequestToken(val));
178  }
double val
Definition: String.cpp:273
RequestData * getContextData(const RequestToken &val)
Definition: Request.cpp:151
std::shared_ptr< RequestContext > & folly::RequestContext::getStaticContext ( )
staticprivate

Definition at line 263 of file Request.cpp.

References folly::pushmi::operators::get.

263  {
264  using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
265  return SingletonT::get();
266 }
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
bool folly::RequestContext::hasContextData ( const RequestToken val) const

Definition at line 147 of file Request.cpp.

147  {
148  return state_.rlock()->requestData_.count(val);
149 }
double val
Definition: String.cpp:273
folly::Synchronized< State > state_
Definition: Request.h:249
bool folly::RequestContext::hasContextData ( const std::string val) const
inline

Definition at line 165 of file Request.h.

References folly::RequestToken::RequestToken().

165  {
166  return hasContextData(RequestToken(val));
167  }
double val
Definition: String.cpp:273
bool hasContextData(const RequestToken &val) const
Definition: Request.cpp:147
void folly::RequestContext::onSet ( )

Definition at line 162 of file Request.cpp.

References folly::data(), and folly::rlock().

162  {
163  auto rlock = state_.rlock();
164  for (const auto& data : rlock->callbackData_) {
165  data->onSet();
166  }
167 }
auto rlock(const Synchronized< Data, Mutex > &synchronized, Args &&...args)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
folly::Synchronized< State > state_
Definition: Request.h:249
void folly::RequestContext::onUnset ( )

Definition at line 169 of file Request.cpp.

References folly::data(), and folly::rlock().

169  {
170  auto rlock = state_.rlock();
171  for (const auto& data : rlock->callbackData_) {
172  data->onUnset();
173  }
174 }
auto rlock(const Synchronized< Data, Mutex > &synchronized, Args &&...args)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
folly::Synchronized< State > state_
Definition: Request.h:249
void folly::RequestContext::overwriteContextData ( const RequestToken val,
std::unique_ptr< RequestData data 
)
private

Definition at line 141 of file Request.cpp.

Referenced by folly::ShallowCopyRequestContextScopeGuard::ShallowCopyRequestContextScopeGuard().

143  {
145 }
bool doSetContextData(const RequestToken &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
Definition: Request.cpp:87
double val
Definition: String.cpp:273
void folly::RequestContext::overwriteContextData ( const std::string val,
std::unique_ptr< RequestData data 
)
inlineprivate

Definition at line 216 of file Request.h.

References folly::gen::move, and folly::RequestToken::RequestToken().

218  {
219  overwriteContextData(RequestToken(val), std::move(data));
220  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
void overwriteContextData(const RequestToken &val, std::unique_ptr< RequestData > data)
Definition: Request.cpp:141
std::shared_ptr< RequestContext > folly::RequestContext::setContext ( std::shared_ptr< RequestContext ctx)
static

Definition at line 227 of file Request.cpp.

References folly::acquireLocked(), folly::as_const(), folly::data(), FOLLY_SDT, folly::RequestData::onSet(), and folly::RequestData::onUnset().

Referenced by folly::fibers::FiberManager::loopUntilNoReadyImpl(), folly::fibers::FiberManager::runReadyFiber(), RequestContextTest::SetUp(), TEST(), TEST_F(), proxygen::AsyncTimeoutSet::timeoutExpired(), folly::NotificationQueue< folly::AsyncServerSocket::QueueMessage >::tryConsume(), folly::RequestContextScopeGuard::~RequestContextScopeGuard(), and folly::ShallowCopyRequestContextScopeGuard::~ShallowCopyRequestContextScopeGuard().

228  {
229  auto& staticCtx = getStaticContext();
230  if (newCtx == staticCtx) {
231  return newCtx;
232  }
233 
234  FOLLY_SDT(
235  folly, request_context_switch_before, staticCtx.get(), newCtx.get());
236 
237  auto curCtx = staticCtx;
238  if (newCtx && curCtx) {
239  // Only call set/unset for all request data that differs
240  auto ret = folly::acquireLocked(
241  as_const(newCtx->state_), as_const(curCtx->state_));
242  auto& newLock = std::get<0>(ret);
243  auto& curLock = std::get<1>(ret);
244  auto& newData = newLock->callbackData_;
245  auto& curData = curLock->callbackData_;
246  exec_set_difference(
247  curData, newData, [](RequestData* data) { data->onUnset(); });
248  staticCtx = newCtx;
249  exec_set_difference(
250  newData, curData, [](RequestData* data) { data->onSet(); });
251  } else {
252  if (curCtx) {
253  curCtx->onUnset();
254  }
255  staticCtx = newCtx;
256  if (newCtx) {
257  newCtx->onSet();
258  }
259  }
260  return curCtx;
261 }
#define FOLLY_SDT(provider, name,...)
static std::shared_ptr< RequestContext > & getStaticContext()
Definition: Request.cpp:263
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
std::tuple< detail::LockedPtrType< Sync1 >, detail::LockedPtrType< Sync2 > > acquireLocked(Sync1 &l1, Sync2 &l2)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
void folly::RequestContext::setContextData ( const RequestToken val,
std::unique_ptr< RequestData data 
)

Definition at line 129 of file Request.cpp.

Referenced by folly::RequestEventBase::set(), TEST(), and TEST_F().

131  {
133 }
bool doSetContextData(const RequestToken &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
Definition: Request.cpp:87
double val
Definition: String.cpp:273
void folly::RequestContext::setContextData ( const std::string val,
std::unique_ptr< RequestData data 
)
inline

Definition at line 138 of file Request.h.

References folly::gen::move, and folly::RequestToken::RequestToken().

140  {
141  setContextData(RequestToken(val), std::move(data));
142  }
void setContextData(const RequestToken &val, std::unique_ptr< RequestData > data)
Definition: Request.cpp:129
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
bool folly::RequestContext::setContextDataIfAbsent ( const RequestToken val,
std::unique_ptr< RequestData data 
)

Definition at line 135 of file Request.cpp.

137  {
139 }
bool doSetContextData(const RequestToken &val, std::unique_ptr< RequestData > &data, DoSetBehaviour behaviour)
Definition: Request.cpp:87
double val
Definition: String.cpp:273
bool folly::RequestContext::setContextDataIfAbsent ( const std::string val,
std::unique_ptr< RequestData data 
)
inline

Definition at line 150 of file Request.h.

References folly::gen::move, and folly::RequestToken::RequestToken().

152  {
153  return setContextDataIfAbsent(RequestToken(val), std::move(data));
154  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
bool setContextDataIfAbsent(const RequestToken &val, std::unique_ptr< RequestData > data)
Definition: Request.cpp:135
std::shared_ptr< RequestContext > folly::RequestContext::setShallowCopyContext ( )
staticprivate

Definition at line 269 of file Request.cpp.

References folly::acquireLocked(), folly::as_const(), child, folly::RequestData::constructPtr(), parent, and folly::f14::swap().

269  {
270  auto& parent = getStaticContext();
271  auto child = std::make_shared<RequestContext>();
272 
273  if (parent) {
274  auto ret = folly::acquireLocked(as_const(parent->state_), child->state_);
275  auto& parentLock = std::get<0>(ret);
276  auto& childLock = std::get<1>(ret);
277  childLock->callbackData_ = parentLock->callbackData_;
278  childLock->requestData_.reserve(parentLock->requestData_.size());
279  for (const auto& entry : parentLock->requestData_) {
280  childLock->requestData_.insert(std::make_pair(
281  entry.first, RequestData::constructPtr(entry.second.get())));
282  }
283  }
284 
285  // Do not use setContext to avoid global set/unset
287  return child;
288 }
static std::shared_ptr< RequestContext > & getStaticContext()
Definition: Request.cpp:263
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
std::tuple< detail::LockedPtrType< Sync1 >, detail::LockedPtrType< Sync2 > > acquireLocked(Sync1 &l1, Sync2 &l2)
static SharedPtr constructPtr(RequestData *ptr)
Definition: Request.cpp:77
folly::Function< void()> child
Definition: AtFork.cpp:35
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
folly::Function< void()> parent
Definition: AtFork.cpp:34

Friends And Related Function Documentation

Definition at line 205 of file Request.h.

Member Data Documentation

folly::Synchronized<State> folly::RequestContext::state_
private

Definition at line 249 of file Request.h.


The documentation for this class was generated from the following files: