proxygen
folly::ParkingLot< Data > Class Template Reference

#include <ParkingLot.h>

Classes

struct  WaitNode
 

Public Member Functions

 ParkingLot ()
 
template<typename Key , typename D , typename ToPark , typename PreWait >
ParkResult park (const Key key, D &&data, ToPark &&toPark, PreWait &&preWait)
 
template<typename Key , typename D , typename ToPark , typename PreWait , typename Clock , typename Duration >
ParkResult park_until (const Key key, D &&data, ToPark &&toPark, PreWait &&preWait, std::chrono::time_point< Clock, Duration > deadline)
 
template<typename Key , typename D , typename ToPark , typename PreWait , typename Rep , typename Period >
ParkResult park_for (const Key key, D &&data, ToPark &&toPark, PreWait &&preWait, std::chrono::duration< Rep, Period > &timeout)
 
template<typename Key , typename Unparker >
void unpark (const Key key, Unparker &&func)
 
template<typename Key , typename Func >
void unpark (const Key bits, Func &&func)
 

Private Member Functions

 ParkingLot (const ParkingLot &)=delete
 

Private Attributes

const uint64_t lotid_
 

Detailed Description

template<typename Data = Unit>
class folly::ParkingLot< Data >

Definition at line 162 of file ParkingLot.h.

Constructor & Destructor Documentation

template<typename Data = Unit>
folly::ParkingLot< Data >::ParkingLot ( const ParkingLot< Data > &  )
privatedelete
template<typename Data = Unit>
folly::ParkingLot< Data >::ParkingLot ( )
inline

Definition at line 175 of file ParkingLot.h.

std::atomic< uint64_t > idallocator
Definition: ParkingLot.cpp:33
const uint64_t lotid_
Definition: ParkingLot.h:163

Member Function Documentation

template<typename Data = Unit>
template<typename Key , typename D , typename ToPark , typename PreWait >
ParkResult folly::ParkingLot< Data >::park ( const Key  key,
D &&  data,
ToPark &&  toPark,
PreWait &&  preWait 
)
inline

Definition at line 189 of file ParkingLot.h.

References D, folly::data(), testing::Key(), and max.

Referenced by folly::detail::atomic_notification::atomic_wait_impl(), BENCHMARK_RELATIVE(), TEST(), and WaitableMutex::wait().

189  {
190  return park_until(
191  key,
192  std::forward<D>(data),
193  std::forward<ToPark>(toPark),
194  std::forward<PreWait>(preWait),
196  }
LogLevel max
Definition: LogLevel.cpp:31
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
ParkResult park_until(const Key key, D &&data, ToPark &&toPark, PreWait &&preWait, std::chrono::time_point< Clock, Duration > deadline)
Definition: ParkingLot.h:256
template<typename Data = Unit>
template<typename Key , typename D , typename ToPark , typename PreWait , typename Rep , typename Period >
ParkResult folly::ParkingLot< Data >::park_for ( const Key  key,
D &&  data,
ToPark &&  toPark,
PreWait &&  preWait,
std::chrono::duration< Rep, Period > &  timeout 
)
inline

Definition at line 219 of file ParkingLot.h.

References D, folly::data(), testing::Key(), and now().

224  {
225  return park_until(
226  key,
227  std::forward<D>(data),
228  std::forward<ToPark>(toPark),
229  std::forward<PreWait>(preWait),
230  timeout + std::chrono::steady_clock::now());
231  }
std::chrono::steady_clock::time_point now()
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
ParkResult park_until(const Key key, D &&data, ToPark &&toPark, PreWait &&preWait, std::chrono::time_point< Clock, Duration > deadline)
Definition: ParkingLot.h:256
template<typename Data >
template<typename Key , typename D , typename ToPark , typename PreWait , typename Clock , typename Duration >
ParkResult folly::ParkingLot< Data >::park_until ( const Key  key,
D &&  data,
ToPark &&  toPark,
PreWait &&  preWait,
std::chrono::time_point< Clock, Duration deadline 
)

Definition at line 256 of file ParkingLot.h.

References folly::parking_lot_detail::Bucket::bucketFor(), folly::data(), folly::parking_lot_detail::WaitNodeBase::lotid_, folly::parking_lot_detail::WaitNodeBase::signaled(), folly::Skip, folly::Timeout, folly::hash::twang_mix64(), uint64_t, folly::Unpark, and folly::parking_lot_detail::WaitNodeBase::wait().

Referenced by folly::detail::atomic_notification::atomic_wait_until_impl().

261  {
262  auto key = hash::twang_mix64(uint64_t(bits));
263  auto& bucket = parking_lot_detail::Bucket::bucketFor(key);
264  WaitNode node(key, lotid_, std::forward<D>(data));
265 
266  {
267  // A: Must be seq_cst. Matches B.
268  bucket.count_.fetch_add(1, std::memory_order_seq_cst);
269 
270  std::unique_lock<std::mutex> bucketLock(bucket.mutex_);
271 
272  if (!std::forward<ToPark>(toPark)()) {
273  bucketLock.unlock();
274  bucket.count_.fetch_sub(1, std::memory_order_relaxed);
275  return ParkResult::Skip;
276  }
277 
278  bucket.push_back(&node);
279  } // bucketLock scope
280 
281  std::forward<PreWait>(preWait)();
282 
283  auto status = node.wait(deadline);
284 
285  if (status == std::cv_status::timeout) {
286  // it's not really a timeout until we unlink the unsignaled node
287  std::lock_guard<std::mutex> bucketLock(bucket.mutex_);
288  if (!node.signaled()) {
289  bucket.erase(&node);
290  return ParkResult::Timeout;
291  }
292  }
293 
294  return ParkResult::Unpark;
295 }
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
uint64_t twang_mix64(uint64_t key) noexcept
Definition: Hash.h:49
static Bucket & bucketFor(uint64_t key)
Definition: ParkingLot.cpp:23
const uint64_t lotid_
Definition: ParkingLot.h:163
template<typename Data = Unit>
template<typename Key , typename Unparker >
void folly::ParkingLot< Data >::unpark ( const Key  key,
Unparker &&  func 
)
template<typename Data = Unit>
template<typename Key , typename Func >
void folly::ParkingLot< Data >::unpark ( const Key  bits,
Func &&  func 
)

Definition at line 299 of file ParkingLot.h.

References folly::parking_lot_detail::Bucket::bucketFor(), folly::parking_lot_detail::WaitNodeBase::lotid_, folly::parking_lot_detail::WaitNodeBase::next_, folly::RemoveBreak, folly::RemoveContinue, folly::RetainBreak, folly::hash::twang_mix64(), and uint64_t.

299  {
300  auto key = hash::twang_mix64(uint64_t(bits));
301  auto& bucket = parking_lot_detail::Bucket::bucketFor(key);
302  // B: Must be seq_cst. Matches A. If true, A *must* see in seq_cst
303  // order any atomic updates in toPark() (and matching updates that
304  // happen before unpark is called)
305  if (bucket.count_.load(std::memory_order_seq_cst) == 0) {
306  return;
307  }
308 
309  std::lock_guard<std::mutex> bucketLock(bucket.mutex_);
310 
311  for (auto iter = bucket.head_; iter != nullptr;) {
312  auto node = static_cast<WaitNode*>(iter);
313  iter = iter->next_;
314  if (node->key_ == key && node->lotid_ == lotid_) {
315  auto result = std::forward<Func>(func)(node->data_);
316  if (result == UnparkControl::RemoveBreak ||
317  result == UnparkControl::RemoveContinue) {
318  // we unlink, but waiter destroys the node
319  bucket.erase(node);
320 
321  node->wake();
322  }
323  if (result == UnparkControl::RemoveBreak ||
324  result == UnparkControl::RetainBreak) {
325  return;
326  }
327  }
328  }
329 }
uint64_t twang_mix64(uint64_t key) noexcept
Definition: Hash.h:49
static Bucket & bucketFor(uint64_t key)
Definition: ParkingLot.cpp:23
const uint64_t lotid_
Definition: ParkingLot.h:163

Member Data Documentation

template<typename Data = Unit>
const uint64_t folly::ParkingLot< Data >::lotid_
private

Definition at line 163 of file ParkingLot.h.


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