tls.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__TLS_H
#define __CVC4__TLS_H
// A bit obnoxious: we have to take varargs to support multi-argument
// template types in the threadlocals.
// E.g. "CVC4_THREADLOCAL(hash_set<type, hasher>*)" fails otherwise,
// due to the embedded comma.
#if 0
# define CVC4_THREADLOCAL(__type...) __type
# define CVC4_THREADLOCAL_PUBLIC(__type...) CVC4_PUBLIC __type
# define CVC4_THREADLOCAL_TYPE(__type...) __type
#else
# include <pthread.h>
# define CVC4_THREADLOCAL(__type...) ::CVC4::ThreadLocal< __type >
# define CVC4_THREADLOCAL_PUBLIC(__type...) CVC4_PUBLIC ::CVC4::ThreadLocal< __type >
# define CVC4_THREADLOCAL_TYPE(__type...) ::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(const 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 static_cast<T>(reinterpret_cast<size_t>(pthread_getspecific(d_key)));
}
};/* class ThreadLocalImpl<T, true> */
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(const 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 static_cast<T*>(pthread_getspecific(d_key));
}
T operator*() {
return *static_cast<T*>(pthread_getspecific(d_key));
}
T* operator->() {
return static_cast<T*>(pthread_getspecific(d_key));
}
};/* class ThreadLocalImpl<T*, true> */
template <class T>
class ThreadLocalImpl<T, false> {
};/* class ThreadLocalImpl<T, false> */
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) {}
ThreadLocal<T>& operator=(const T& t) {
return static_cast< ThreadLocal<T>& >(super::operator=(t));
}
ThreadLocal<T>& operator=(const ThreadLocal<T>& tl) {
return static_cast< ThreadLocal<T>& >(super::operator=(tl));
}
};/* class ThreadLocal<T> */
template <class T>
class ThreadLocal<T*> : public ThreadLocalImpl<T*, sizeof(T*) <= sizeof(void*)> {
typedef ThreadLocalImpl<T*, sizeof(T*) <= sizeof(void*)> super;
public:
ThreadLocal() : super() {}
ThreadLocal(T* t) : super(t) {}
ThreadLocal(const ThreadLocal<T*>& tl) : super(tl) {}
ThreadLocal<T*>& operator=(T* t) {
return static_cast< ThreadLocal<T*>& >(super::operator=(t));
}
ThreadLocal<T*>& operator=(const ThreadLocal<T*>& tl) {
return static_cast< ThreadLocal<T*>& >(super::operator=(tl));
}
// special operators for pointers
T& operator*() {
return *static_cast<T*>(*this);
}
const T& operator*() const {
return *static_cast<const T*>(*this);
}
T* operator->() {
return static_cast<T*>(*this);
}
const T* operator->() const {
return static_cast<const T*>(*this);
}
T* operator++() {
T* p = *this;
*this = ++p;
return p;
}
T* operator++(int) {
T* p = *this;
*this = p + 1;
return p;
}
T* operator--() {
T* p = *this;
*this = --p;
return p;
}
T* operator--(int) {
T* p = *this;
*this = p - 1;
return p;
}
};/* class ThreadLocal<T*> */
}/* CVC4 namespace */
#endif /* 0 */
#endif /* __CVC4__TLS_H */
CVC4::ThreadLocalImpl< T, true >::ThreadLocalImpl
ThreadLocalImpl(const ThreadLocalImpl &tl)
Definition tls.h:61
CVC4::ThreadLocalImpl< T, true >::ThreadLocalImpl
ThreadLocalImpl()
Definition tls.h:52
CVC4::ThreadLocalImpl< T, true >::operator=
ThreadLocalImpl & operator=(const ThreadLocalImpl &tl)
Definition tls.h:70
CVC4::ThreadLocalImpl< T, true >::ThreadLocalImpl
ThreadLocalImpl(const T &t)
Definition tls.h:56
CVC4::ThreadLocalImpl< T, true >::operator=
ThreadLocalImpl & operator=(const T &t)
Definition tls.h:66
CVC4::ThreadLocalImpl< T *, true >::operator->
T * operator->()
Definition tls.h:118
CVC4::ThreadLocalImpl< T *, true >::ThreadLocalImpl
ThreadLocalImpl(const ThreadLocalImpl &tl)
Definition tls.h:97
CVC4::ThreadLocalImpl< T *, true >::ThreadLocalImpl
ThreadLocalImpl()
Definition tls.h:88
CVC4::ThreadLocalImpl< T *, true >::ThreadLocalImpl
ThreadLocalImpl(const T *t)
Definition tls.h:92
CVC4::ThreadLocalImpl< T *, true >::operator=
ThreadLocalImpl & operator=(const ThreadLocalImpl &tl)
Definition tls.h:106
CVC4::ThreadLocalImpl< T *, true >::operator*
T operator*()
Definition tls.h:115
CVC4::ThreadLocalImpl< T *, true >::operator=
ThreadLocalImpl & operator=(const T *t)
Definition tls.h:102
CVC4::ThreadLocalImpl
Definition tls.h:42
cvc4_public.h
Macros that should be defined everywhere during the building of the libraries and driver binary,...
CVC4
Definition expr.h:106