Definition in file tls.h.
Go to the source code of this file.
Namespaces | |
namespace | tls_tricks |
Classes | |
class | tls_tricks::tls_manager |
A management class for non-POD thread-local storage. More... | |
struct | tls_tricks::tls_manager_schwarz |
Static struct to make sure tls_manager's global init() and fini() are called. More... | |
struct | tls_tricks::tls_blob< T > |
Wrapper for a type, used by TLS_STRUCT helper macro. More... | |
Defines | |
#define | TLS_STRUCT(Type, Name, InitFn) |
Helper macro for DECLARE_TLS. Do not use directly. | |
#define | DECLARE_TLS(Type, Name) |
Cause non-POD TLS object of Type to be created and initialized. | |
#define | DECLARE_TLS_SCHWARZ(Name) |
Cause a Schwarz counter to be declared (for use in header files). | |
#define | DEFINE_TLS_SCHWARZ(Type, Name) |
Cause a Schwarz counter to be defined (for use in .cpp files). |
#define TLS_STRUCT | ( | Type, | |||
Name, | |||||
InitFn | ) |
Value:
struct Name { \ typedef tls_tricks::tls_blob< Type > Wrapper; \ Type &operator*() { return *get(); } \ Type* operator->() { return get(); } \ operator Type*() { return get(); } \ static Wrapper* get_wrapper() { \ static __thread Wrapper val; \ return &val; \ } \ static Type* get() { return get_wrapper()->get(); } \ static void init() { get_wrapper()->init(); } \ static void fini() { get_wrapper()->fini(); } \ InitFn() { \ static bool initialized = false; \ if(initialized) \ return; \ tls_tricks::tls_manager::register_tls(&init, &fini); \ initialized = true; \ } \ }
A helper macro for DECLARE_TLS.
#define DECLARE_TLS | ( | Type, | |||
Name | ) |
Value:
static \ TLS_STRUCT(Type, Name##_tls_wrapper, Name##_tls_wrapper) Name
This macro declares a static "smart pointer" named * Name_tls_wrapper to a thread-local variable of the given Type. When this static struct get initialized at static-init time, it registers with the tls manager its init and fini methods. Those methods invoke the init and fini methods of the item to which this "smart pointer" points, which is the actual TLS entity: a tls_tricks::tls_blobType.
#define DECLARE_TLS_SCHWARZ | ( | Name | ) |
Value:
static struct Name##_tls_wrapper_schwarz { \ Name##_tls_wrapper_schwarz(); \ } Name##_schwarz
Make a Schwarz counter (in a .h) to force initialization of the TLS defined in a .cpp by DEFINE_TLS_SCHWARZ. This is useful if there is a dependency between 2+ TLS variables so the correct one is initialized first. The only way you can control their order is to make sure their DECLARE_TLS_SCHWARZ macros are in the correct order b/c C++ guarantees static init order only for objects in the same translation unit. Note that the counter is really in the tls wrapper.
#define DEFINE_TLS_SCHWARZ | ( | Type, | |||
Name | ) |
Value:
static TLS_STRUCT(Type, Name##_tls_wrapper, static void init_wrapper) Name; \ Name##_tls_wrapper_schwarz::Name##_tls_wrapper_schwarz() { \ Name##_tls_wrapper::init_wrapper(); \ }
Define the TLS struct that DECLARE_TLS_SCHWARZ expects to initialize.