proxygen
folly::SingletonThreadLocal< T, Tag, Make, TLTag > Class Template Reference

#include <SingletonThreadLocal.h>

Classes

struct  Node
 
struct  Wrapper
 

Static Public Member Functions

FOLLY_EXPORT static FOLLY_ALWAYS_INLINE Tget ()
 
static WrapperTL::Accessor accessAllThreads ()
 

Private Types

using NodeBase = boost::intrusive::list_base_hook< boost::intrusive::link_mode< boost::intrusive::auto_unlink >>
 
using List = boost::intrusive::list< Node, boost::intrusive::constant_time_size< false >>
 
using WrapperTL = ThreadLocal< Wrapper, TLTag >
 

Private Member Functions

 SingletonThreadLocal ()=delete
 

Static Private Member Functions

FOLLY_EXPORT static FOLLY_NOINLINE WrapperTLgetWrapperTL ()
 
static FOLLY_NOINLINE WrappergetWrapper ()
 

Detailed Description

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
class folly::SingletonThreadLocal< T, Tag, Make, TLTag >

SingletonThreadLocal

Useful for a per-thread leaky-singleton model in libraries and applications.

By "leaky" it is meant that the T instances held by the instantiation SingletonThreadLocal<T> will survive until their owning thread exits. Therefore, they can safely be used before main() begins and after main() ends, and they can also safely be used in an application that spawns many temporary threads throughout its life.

Example:

struct UsefulButHasExpensiveCtor { UsefulButHasExpensiveCtor(); // this is expensive Result operator()(Arg arg); };

Result useful(Arg arg) { using Useful = UsefulButHasExpensiveCtor; auto& useful = folly::SingletonThreadLocal<Useful>::get(); return useful(arg); }

As an example use-case, the random generators in <random> are expensive to construct. And their constructors are deterministic, but many cases require that they be randomly seeded. So folly::Random makes good canonical uses of folly::SingletonThreadLocal so that a seed is computed from the secure random device once per thread, and the random generator is constructed with the seed once per thread.

Keywords to help people find this class in search: Thread Local Singleton ThreadLocalSingleton

Definition at line 68 of file SingletonThreadLocal.h.

Member Typedef Documentation

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
using folly::SingletonThreadLocal< T, Tag, Make, TLTag >::List = boost::intrusive::list<Node, boost::intrusive::constant_time_size<false>>
private

Definition at line 95 of file SingletonThreadLocal.h.

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
using folly::SingletonThreadLocal< T, Tag, Make, TLTag >::NodeBase = boost::intrusive::list_base_hook< boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
private

Definition at line 73 of file SingletonThreadLocal.h.

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
using folly::SingletonThreadLocal< T, Tag, Make, TLTag >::WrapperTL = ThreadLocal<Wrapper, TLTag>
private

Definition at line 131 of file SingletonThreadLocal.h.

Constructor & Destructor Documentation

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
folly::SingletonThreadLocal< T, Tag, Make, TLTag >::SingletonThreadLocal ( )
privatedelete

Member Function Documentation

template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
static WrapperTL::Accessor folly::SingletonThreadLocal< T, Tag, Make, TLTag >::accessAllThreads ( )
inlinestatic

Definition at line 165 of file SingletonThreadLocal.h.

References folly::ThreadLocal< T, Tag, AccessMode >::accessAllThreads(), and folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapperTL().

165  {
166  return getWrapperTL().accessAllThreads();
167  }
Accessor accessAllThreads() const
Definition: ThreadLocal.h:87
FOLLY_EXPORT static FOLLY_NOINLINE WrapperTL & getWrapperTL()
template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE T& folly::SingletonThreadLocal< T, Tag, Make, TLTag >::get ( )
inlinestatic

Definition at line 155 of file SingletonThreadLocal.h.

References folly::SingletonThreadLocal< T, Tag, Make, TLTag >::Node::cache, FOLLY_LIKELY, folly::settings::detail::getSlow(), and folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapper().

Referenced by proxygen::HTTPMessage::formatDateHeader(), folly::fibers::FiberManager::registerAlternateSignalStack(), and TEST().

155  {
156 #ifdef FOLLY_TLS
157  static thread_local Wrapper* cache;
158  return FOLLY_LIKELY(!!cache) ? *cache : getSlow(cache);
159 #else
160  return getWrapper();
161 #endif
162  }
static FOLLY_NOINLINE Wrapper & getWrapper()
#define FOLLY_LIKELY(x)
Definition: Likely.h:35
template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
static FOLLY_NOINLINE Wrapper& folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapper ( )
inlinestaticprivate
template<typename T, typename Tag = detail::DefaultTag, typename Make = detail::DefaultMake<T>, typename TLTag = _t<std::conditional< std::is_same<Tag, detail::DefaultTag>::value, void, Tag>>>
FOLLY_EXPORT static FOLLY_NOINLINE WrapperTL& folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapperTL ( )
inlinestaticprivate

Definition at line 135 of file SingletonThreadLocal.h.

Referenced by folly::SingletonThreadLocal< T, Tag, Make, TLTag >::accessAllThreads(), and folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapper().

135  {
136  static auto& entry = *detail::createGlobal<WrapperTL, Tag>();
137  return entry;
138  }

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