proxygen
|
#include <SingletonThreadLocal.h>
Classes | |
struct | Node |
struct | Wrapper |
Static Public Member Functions | |
FOLLY_EXPORT static FOLLY_ALWAYS_INLINE T & | get () |
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 WrapperTL & | getWrapperTL () |
static FOLLY_NOINLINE Wrapper & | getWrapper () |
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.
|
private |
Definition at line 95 of file SingletonThreadLocal.h.
|
private |
Definition at line 73 of file SingletonThreadLocal.h.
|
private |
Definition at line 131 of file SingletonThreadLocal.h.
|
privatedelete |
|
inlinestatic |
Definition at line 165 of file SingletonThreadLocal.h.
References folly::ThreadLocal< T, Tag, AccessMode >::accessAllThreads(), and folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapperTL().
|
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().
|
inlinestaticprivate |
Definition at line 140 of file SingletonThreadLocal.h.
References folly::SingletonThreadLocal< T, Tag, Make, TLTag >::Node::cache, check(), FOLLY_NOINLINE, folly::settings::detail::getSlow(), folly::SingletonThreadLocal< T, Tag, Make, TLTag >::getWrapperTL(), folly::SingletonThreadLocal< T, Tag, Make, TLTag >::Node::stale, and folly::T.
Referenced by folly::SingletonThreadLocal< T, Tag, Make, TLTag >::get(), and folly::SingletonThreadLocal< T, Tag, Make, TLTag >::Node::Node().
|
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().