proxygen
folly::ThreadLocalPtr< T, Tag, AccessMode > Class Template Reference

#include <ThreadLocal.h>

Classes

class  Accessor
 

Public Member Functions

constexpr ThreadLocalPtr ()
 
 ThreadLocalPtr (ThreadLocalPtr &&other) noexcept
 
ThreadLocalPtroperator= (ThreadLocalPtr &&other)
 
 ~ThreadLocalPtr ()
 
Tget () const
 
Toperator-> () const
 
Toperator* () const
 
Trelease ()
 
void reset (T *newPtr=nullptr)
 
 operator bool () const
 
template<typename SourceT , typename Deleter , typename = typename std::enable_if< std::is_convertible<SourceT*, T*>::value>::type>
void reset (std::unique_ptr< SourceT, Deleter > source)
 
template<typename SourceT , typename = typename std::enable_if< std::is_convertible<SourceT*, T*>::value>::type>
void reset (std::unique_ptr< SourceT > source)
 
template<class Deleter >
void reset (T *newPtr, const Deleter &deleter)
 
Accessor accessAllThreads () const
 

Private Types

typedef threadlocal_detail::StaticMeta< Tag, AccessMode > StaticMeta
 

Private Member Functions

void destroy ()
 
 ThreadLocalPtr (const ThreadLocalPtr &)=delete
 
ThreadLocalPtroperator= (const ThreadLocalPtr &)=delete
 

Private Attributes

StaticMeta::EntryID id_
 

Detailed Description

template<class T, class Tag = void, class AccessMode = void>
class folly::ThreadLocalPtr< T, Tag, AccessMode >

Definition at line 56 of file ThreadLocal.h.

Member Typedef Documentation

template<class T, class Tag = void, class AccessMode = void>
typedef threadlocal_detail::StaticMeta<Tag, AccessMode> folly::ThreadLocalPtr< T, Tag, AccessMode >::StaticMeta
private

Definition at line 139 of file ThreadLocal.h.

Constructor & Destructor Documentation

template<class T, class Tag = void, class AccessMode = void>
constexpr folly::ThreadLocalPtr< T, Tag, AccessMode >::ThreadLocalPtr ( )
inline

Definition at line 142 of file ThreadLocal.h.

142 : id_() {}
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
folly::ThreadLocalPtr< T, Tag, AccessMode >::ThreadLocalPtr ( ThreadLocalPtr< T, Tag, AccessMode > &&  other)
inlinenoexcept

Definition at line 144 of file ThreadLocal.h.

144 : id_(std::move(other.id_)) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
folly::ThreadLocalPtr< T, Tag, AccessMode >::~ThreadLocalPtr ( )
inline

Definition at line 153 of file ThreadLocal.h.

153  {
154  destroy();
155  }
template<class T, class Tag = void, class AccessMode = void>
folly::ThreadLocalPtr< T, Tag, AccessMode >::ThreadLocalPtr ( const ThreadLocalPtr< T, Tag, AccessMode > &  )
privatedelete

Member Function Documentation

template<class T, class Tag = void, class AccessMode = void>
Accessor folly::ThreadLocalPtr< T, Tag, AccessMode >::accessAllThreads ( ) const
inline

Definition at line 437 of file ThreadLocal.h.

Referenced by folly::detail::ThreadCachedInts< folly::detail::folly::detail::Tag >::readFull().

437  {
438  static_assert(
440  "Must use a unique Tag to use the accessAllThreads feature");
441  return Accessor(id_.getOrAllocate(StaticMeta::instance()));
442  }
static StaticMeta< Tag, AccessMode > & instance()
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
static const char *const value
Definition: Conv.cpp:50
template<class T, class Tag = void, class AccessMode = void>
void folly::ThreadLocalPtr< T, Tag, AccessMode >::destroy ( )
inlineprivate

Definition at line 445 of file ThreadLocal.h.

445  {
446  StaticMeta::instance().destroy(&id_);
447  }
static StaticMeta< Tag, AccessMode > & instance()
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
T* folly::ThreadLocalPtr< T, Tag, AccessMode >::get ( ) const
inline

Definition at line 157 of file ThreadLocal.h.

Referenced by TEST().

157  {
158  threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
159  return static_cast<T*>(w.ptr);
160  }
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE ElementWrapper & get(EntryID *ent)
folly::std T
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
folly::ThreadLocalPtr< T, Tag, AccessMode >::operator bool ( ) const
inlineexplicit

Definition at line 189 of file ThreadLocal.h.

189  {
190  return get() != nullptr;
191  }
template<class T, class Tag = void, class AccessMode = void>
T& folly::ThreadLocalPtr< T, Tag, AccessMode >::operator* ( ) const
inline

Definition at line 166 of file ThreadLocal.h.

166  {
167  return *get();
168  }
template<class T, class Tag = void, class AccessMode = void>
T* folly::ThreadLocalPtr< T, Tag, AccessMode >::operator-> ( ) const
inline

Definition at line 162 of file ThreadLocal.h.

162  {
163  return get();
164  }
template<class T, class Tag = void, class AccessMode = void>
ThreadLocalPtr& folly::ThreadLocalPtr< T, Tag, AccessMode >::operator= ( ThreadLocalPtr< T, Tag, AccessMode > &&  other)
inline

Definition at line 146 of file ThreadLocal.h.

146  {
147  assert(this != &other);
148  destroy();
149  id_ = std::move(other.id_);
150  return *this;
151  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
ThreadLocalPtr& folly::ThreadLocalPtr< T, Tag, AccessMode >::operator= ( const ThreadLocalPtr< T, Tag, AccessMode > &  )
privatedelete
template<class T, class Tag = void, class AccessMode = void>
T* folly::ThreadLocalPtr< T, Tag, AccessMode >::release ( )
inline

Definition at line 170 of file ThreadLocal.h.

Referenced by TEST().

170  {
171  threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
172 
173  return static_cast<T*>(w.release());
174  }
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE ElementWrapper & get(EntryID *ent)
folly::std T
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
template<class T, class Tag = void, class AccessMode = void>
void folly::ThreadLocalPtr< T, Tag, AccessMode >::reset ( T newPtr = nullptr)
inline

Definition at line 176 of file ThreadLocal.h.

Referenced by wangle::Observable< T >::forEachObserver(), folly::detail::ThreadCachedInts< folly::detail::folly::detail::Tag >::init(), runTestTag(), wangle::Observable< T >::subscribeImpl(), TEST(), and wangle::Observable< T >::unsubscribe().

176  {
177  auto guard = makeGuard([&] { delete newPtr; });
178  threadlocal_detail::ElementWrapper* w = &StaticMeta::get(&id_);
179 
181  // need to get a new ptr since the
182  // ThreadEntry::elements array can be reallocated
183  w = &StaticMeta::get(&id_);
184  w->cleanup();
185  guard.dismiss();
186  w->set(newPtr);
187  }
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE ElementWrapper & get(EntryID *ent)
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
template<class T, class Tag = void, class AccessMode = void>
template<typename SourceT , typename Deleter , typename = typename std::enable_if< std::is_convertible<SourceT*, T*>::value>::type>
void folly::ThreadLocalPtr< T, Tag, AccessMode >::reset ( std::unique_ptr< SourceT, Deleter >  source)
inline

reset() that transfers ownership from a smart pointer

Definition at line 201 of file ThreadLocal.h.

201  {
202  auto deleter = [delegate = source.get_deleter()](
203  T* ptr, TLPDestructionMode) { delegate(ptr); };
204  reset(source.release(), deleter);
205  }
void * ptr
void reset(T *newPtr=nullptr)
Definition: ThreadLocal.h:176
folly::std T
template<class T, class Tag = void, class AccessMode = void>
template<typename SourceT , typename = typename std::enable_if< std::is_convertible<SourceT*, T*>::value>::type>
void folly::ThreadLocalPtr< T, Tag, AccessMode >::reset ( std::unique_ptr< SourceT >  source)
inline

reset() that transfers ownership from a smart pointer with the default deleter

Definition at line 215 of file ThreadLocal.h.

215  {
216  reset(source.release());
217  }
void reset(T *newPtr=nullptr)
Definition: ThreadLocal.h:176
template<class T, class Tag = void, class AccessMode = void>
template<class Deleter >
void folly::ThreadLocalPtr< T, Tag, AccessMode >::reset ( T newPtr,
const Deleter &  deleter 
)
inline

reset() with a custom deleter: deleter(T* ptr, TLPDestructionMode mode) "mode" is ALL_THREADS if we're destructing this ThreadLocalPtr (and thus deleting pointers for all threads), and THIS_THREAD if we're only deleting the member for one thread (because of thread exit or reset()). Invoking the deleter must not throw.

Definition at line 228 of file ThreadLocal.h.

228  {
229  auto guard = makeGuard([&] {
230  if (newPtr) {
231  deleter(newPtr, TLPDestructionMode::THIS_THREAD);
232  }
233  });
234  threadlocal_detail::ElementWrapper* w = &StaticMeta::get(&id_);
236  // need to get a new ptr since the
237  // ThreadEntry::elements array can be reallocated
238  w = &StaticMeta::get(&id_);
239  w->cleanup();
240  guard.dismiss();
241  w->set(newPtr, deleter);
242  }
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE ElementWrapper & get(EntryID *ent)
StaticMeta::EntryID id_
Definition: ThreadLocal.h:453
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184

Member Data Documentation

template<class T, class Tag = void, class AccessMode = void>
StaticMeta::EntryID folly::ThreadLocalPtr< T, Tag, AccessMode >::id_
mutableprivate

Definition at line 453 of file ThreadLocal.h.


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