proxygen
folly::AtomicCoreCachedSharedPtr< T, kNumSlots > Class Template Reference

#include <CoreCachedSharedPtr.h>

Classes

struct  Slots
 

Public Member Functions

 AtomicCoreCachedSharedPtr (const std::shared_ptr< T > &p=nullptr)
 
 ~AtomicCoreCachedSharedPtr ()
 
void reset (const std::shared_ptr< T > &p=nullptr)
 
std::shared_ptr< Tget () const
 

Private Types

using Holder = std::shared_ptr< T >
 

Private Attributes

std::atomic< Slots * > slots_ {nullptr}
 

Detailed Description

template<class T, size_t kNumSlots = 64>
class folly::AtomicCoreCachedSharedPtr< T, kNumSlots >

This class creates core-local caches for a given shared_ptr, to mitigate contention when acquiring/releasing it.

All methods are threadsafe. Hazard pointers are used to avoid use-after-free for concurrent reset() and get() operations.

Concurrent reset()s are sequenced with respect to each other: the sharded shared_ptrs will always all be set to the same value. get()s will never see a newer pointer on one core, and an older pointer on another after a subsequent thread migration.

Definition at line 100 of file CoreCachedSharedPtr.h.

Member Typedef Documentation

template<class T, size_t kNumSlots = 64>
using folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::Holder = std::shared_ptr<T>
private

Definition at line 142 of file CoreCachedSharedPtr.h.

Constructor & Destructor Documentation

template<class T, size_t kNumSlots = 64>
folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::AtomicCoreCachedSharedPtr ( const std::shared_ptr< T > &  p = nullptr)
inlineexplicit

Definition at line 102 of file CoreCachedSharedPtr.h.

References folly::CoreCachedSharedPtr< T, kNumSlots >::reset().

102  {
103  reset(p);
104  }
void reset(const std::shared_ptr< T > &p=nullptr)
template<class T, size_t kNumSlots = 64>
folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::~AtomicCoreCachedSharedPtr ( )
inline

Definition at line 106 of file CoreCachedSharedPtr.h.

References folly::CoreCachedSharedPtr< T, kNumSlots >::slots_.

106  {
107  auto slots = slots_.load(std::memory_order_acquire);
108  // Delete of AtomicCoreCachedSharedPtr must be synchronized, no
109  // need for stlots->retire().
110  if (slots) {
111  delete slots;
112  }
113  }

Member Function Documentation

template<class T, size_t kNumSlots = 64>
std::shared_ptr<T> folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::get ( ) const
inline

Definition at line 132 of file CoreCachedSharedPtr.h.

References folly::AccessSpreader< Atom >::current(), and folly::CoreCachedSharedPtr< T, kNumSlots >::slots_.

132  {
133  folly::hazptr_local<1> hazptr;
134  auto slots = hazptr[0].get_protected(slots_);
135  if (!slots) {
136  return nullptr;
137  }
138  return (slots->slots_)[AccessSpreader<>::current(kNumSlots)];
139  }
static size_t current(size_t numStripes)
template<class T, size_t kNumSlots = 64>
void folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::reset ( const std::shared_ptr< T > &  p = nullptr)
inline

Definition at line 115 of file CoreCachedSharedPtr.h.

References folly::enumerate(), and folly::CoreCachedSharedPtr< T, kNumSlots >::slots_.

115  {
116  auto newslots = folly::make_unique<Slots>();
117  // Allocate each Holder in a different CoreRawAllocator stripe to
118  // prevent false sharing. Their control blocks will be adjacent
119  // thanks to allocate_shared().
120  for (auto slot : folly::enumerate(newslots->slots_)) {
121  auto alloc = getCoreAllocator<Holder, kNumSlots>(slot.index);
122  auto holder = std::allocate_shared<Holder>(alloc, p);
123  *slot = std::shared_ptr<T>(holder, p.get());
124  }
125 
126  auto oldslots = slots_.exchange(newslots.release());
127  if (oldslots) {
128  oldslots->retire();
129  }
130  }
detail::RangeEnumerator< Range > enumerate(Range &&r)
Definition: Enumerate.h:167

Member Data Documentation

template<class T, size_t kNumSlots = 64>
std::atomic<Slots*> folly::AtomicCoreCachedSharedPtr< T, kNumSlots >::slots_ {nullptr}
private

Definition at line 146 of file CoreCachedSharedPtr.h.


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