diff options
author | ACSYS <cvc4-devel@cs.nyu.edu> | 2010-09-27 22:26:46 +0000 |
---|---|---|
committer | ACSYS <cvc4-devel@cs.nyu.edu> | 2010-09-27 22:26:46 +0000 |
commit | 753a072c542c1c254d7c6adbf10e091ba585ede5 (patch) | |
tree | 5ab9157fe0d3f265f82a94f7f4e5940697b54ee5 /src/util/tls.h.in | |
parent | 595751a1814cc9375318c9c158caf6426eeda791 (diff) |
add workaround for systems (i.e., Mac OS X) that don't support __thread; also configure script auto-detection of __thread support and syntax
Diffstat (limited to 'src/util/tls.h.in')
-rw-r--r-- | src/util/tls.h.in | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/util/tls.h.in b/src/util/tls.h.in new file mode 100644 index 000000000..29a52497a --- /dev/null +++ b/src/util/tls.h.in @@ -0,0 +1,90 @@ +/********************* */ +/*! \file tls.h.in + ** \verbatim + ** Original author: mdeters + ** Major contributors: none + ** Minor contributors (to current version): none + ** This file is part of the CVC4 prototype. + ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys) + ** Courant Institute of Mathematical Sciences + ** New York University + ** See the file COPYING in the top-level source directory for licensing + ** information.\endverbatim + ** + ** \brief Header to define CVC4_THREAD whether or not TLS is + ** supported by the compiler/runtime platform + ** + ** Header to define CVC4_THREAD whether or not TLS is supported by + ** the compiler/runtime platform. If not, an implementation based on + ** pthread_getspecific() / pthread_setspecific() is given. + **/ + +#include "cvc4_public.h" + +#ifndef __CVC4__TLS_H +#define __CVC4__TLS_H + +#if @CVC4_TLS_SUPPORTED@ +# define CVC4_THREADLOCAL(__type) @CVC4_TLS@ __type +# define CVC4_THREADLOCAL_PUBLIC(__type) @CVC4_TLS@ CVC4_PUBLIC __type +#else +# include <pthread.h> +# define CVC4_THREADLOCAL(__type) ::CVC4::ThreadLocal< __type > +# define CVC4_THREADLOCAL_PUBLIC(__type) CVC4_PUBLIC ::CVC4::ThreadLocal< __type > + +namespace CVC4 { + +template <class T, bool small> +class ThreadLocalImpl; + +template <class T> +class ThreadLocalImpl<T, true> { + pthread_key_t d_key; + + static void cleanup(void*) { + } + +public: + ThreadLocalImpl() { + pthread_key_create(&d_key, ThreadLocalImpl::cleanup); + } + + ThreadLocalImpl(T t) { + pthread_key_create(&d_key, ThreadLocalImpl::cleanup); + pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t))); + } + + ThreadLocalImpl(const ThreadLocalImpl& tl) { + pthread_key_create(&d_key, ThreadLocalImpl::cleanup); + pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T&>(tl)))); + } + + ThreadLocalImpl& operator=(const T& t) { + pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t))); + return *this; + } + ThreadLocalImpl& operator=(const ThreadLocalImpl& tl) { + pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T&>(tl)))); + return *this; + } + + operator T() const { + return reinterpret_cast<T>(pthread_getspecific(d_key)); + } +};/* class ThreadLocalImpl<T, true> */ + +template <class T> +class ThreadLocal : public ThreadLocalImpl<T, sizeof(T) <= sizeof(void*)> { + typedef ThreadLocalImpl<T, sizeof(T) <= sizeof(void*)> super; + +public: + ThreadLocal() : super() {} + ThreadLocal(const T& t) : super(t) {} + ThreadLocal(const ThreadLocal<T>& tl) : super(tl) {} +};/* class ThreadLocal<T> */ + +}/* CVC4 namespace */ + +#endif /* @CVC4_TLS_SUPPORTED@ */ + +#endif /* _CVC4__TLS_H */ |