X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=include%2Ftls.h;fp=include%2Ftls.h;h=865e6da26bed635a94493b53d9c72007856e7259;hp=0000000000000000000000000000000000000000;hb=052506ff082f134f00834f666653819e18d9bddf;hpb=9aa406ee734cc162cfbc44e4c4d5e6b0993c1ac7 diff --git a/include/tls.h b/include/tls.h new file mode 100644 index 0000000..865e6da --- /dev/null +++ b/include/tls.h @@ -0,0 +1,33 @@ +/* + * Written by Josh Dybnis and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * + * A platform independant wrapper around thread-local storage. On platforms that don't support + * __thread variables (e.g. Mac OS X), we have to use the pthreads library for thread-local storage + */ +#ifndef TLS_H +#define TLS_H + +#ifdef __ELF__ // use gcc thread-local storage (i.e. __thread variables) +#define DECLARE_THREAD_LOCAL (name, type) type name +#define INIT_THREAD_LOCAL (name, value) name = value +#define SET_THREAD_LOCAL (name, value) name = value +#define LOCALIZE_THREAD_LOCAL(name, type) + +#else//!__ELF__ + +#include + +#define DECLARE_THREAD_LOCAL(name, type) pthread_key_t name##_KEY + +#define INIT_THREAD_LOCAL(name) \ + do { \ + if (pthread_key_create(&name##_KEY, NULL) != 0) { assert(FALSE); } \ + } while (0) + +#define SET_THREAD_LOCAL(name, value) pthread_setspecific(name##_KEY, (void *)(size_t)value); + +#define LOCALIZE_THREAD_LOCAL(name, type) type name = (type)(size_t)pthread_getspecific(name##_KEY) + +#endif//__ELF__ +#endif//TLS_H