cvc4-1.4
tls.h
Go to the documentation of this file.
1/********************* */
20#include "cvc4_public.h"
21
22#ifndef __CVC4__TLS_H
23#define __CVC4__TLS_H
24
25// A bit obnoxious: we have to take varargs to support multi-argument
26// template types in the threadlocals.
27// E.g. "CVC4_THREADLOCAL(hash_set<type, hasher>*)" fails otherwise,
28// due to the embedded comma.
29#if 0
30# define CVC4_THREADLOCAL(__type...) __type
31# define CVC4_THREADLOCAL_PUBLIC(__type...) CVC4_PUBLIC __type
32# define CVC4_THREADLOCAL_TYPE(__type...) __type
33#else
34# include <pthread.h>
35# define CVC4_THREADLOCAL(__type...) ::CVC4::ThreadLocal< __type >
36# define CVC4_THREADLOCAL_PUBLIC(__type...) CVC4_PUBLIC ::CVC4::ThreadLocal< __type >
37# define CVC4_THREADLOCAL_TYPE(__type...) ::CVC4::ThreadLocal< __type >
38
39namespace CVC4 {
40
41template <class T, bool small>
43
44template <class T>
45class ThreadLocalImpl<T, true> {
46 pthread_key_t d_key;
47
48 static void cleanup(void*) {
49 }
50
51public:
53 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
54 }
55
56 ThreadLocalImpl(const T& t) {
57 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
58 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t)));
59 }
60
62 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
63 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T&>(tl))));
64 }
65
67 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t)));
68 return *this;
69 }
71 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T&>(tl))));
72 return *this;
73 }
74
75 operator T() const {
76 return static_cast<T>(reinterpret_cast<size_t>(pthread_getspecific(d_key)));
77 }
78};/* class ThreadLocalImpl<T, true> */
79
80template <class T>
81class ThreadLocalImpl<T*, true> {
82 pthread_key_t d_key;
83
84 static void cleanup(void*) {
85 }
86
87public:
89 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
90 }
91
92 ThreadLocalImpl(const T* t) {
93 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
94 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t)));
95 }
96
98 pthread_key_create(&d_key, ThreadLocalImpl::cleanup);
99 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T*>(tl))));
100 }
101
103 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(t)));
104 return *this;
105 }
107 pthread_setspecific(d_key, const_cast<void*>(reinterpret_cast<const void*>(static_cast<const T*>(tl))));
108 return *this;
109 }
110
111 operator T*() const {
112 return static_cast<T*>(pthread_getspecific(d_key));
113 }
114
116 return *static_cast<T*>(pthread_getspecific(d_key));
117 }
119 return static_cast<T*>(pthread_getspecific(d_key));
120 }
121};/* class ThreadLocalImpl<T*, true> */
122
123template <class T>
124class ThreadLocalImpl<T, false> {
125};/* class ThreadLocalImpl<T, false> */
126
127template <class T>
128class ThreadLocal : public ThreadLocalImpl<T, sizeof(T) <= sizeof(void*)> {
129 typedef ThreadLocalImpl<T, sizeof(T) <= sizeof(void*)> super;
130
131public:
132 ThreadLocal() : super() {}
133 ThreadLocal(const T& t) : super(t) {}
134 ThreadLocal(const ThreadLocal<T>& tl) : super(tl) {}
135
136 ThreadLocal<T>& operator=(const T& t) {
137 return static_cast< ThreadLocal<T>& >(super::operator=(t));
138 }
139 ThreadLocal<T>& operator=(const ThreadLocal<T>& tl) {
140 return static_cast< ThreadLocal<T>& >(super::operator=(tl));
141 }
142};/* class ThreadLocal<T> */
143
144template <class T>
145class ThreadLocal<T*> : public ThreadLocalImpl<T*, sizeof(T*) <= sizeof(void*)> {
146 typedef ThreadLocalImpl<T*, sizeof(T*) <= sizeof(void*)> super;
147
148public:
149 ThreadLocal() : super() {}
150 ThreadLocal(T* t) : super(t) {}
151 ThreadLocal(const ThreadLocal<T*>& tl) : super(tl) {}
152
153 ThreadLocal<T*>& operator=(T* t) {
154 return static_cast< ThreadLocal<T*>& >(super::operator=(t));
155 }
156 ThreadLocal<T*>& operator=(const ThreadLocal<T*>& tl) {
157 return static_cast< ThreadLocal<T*>& >(super::operator=(tl));
158 }
159 // special operators for pointers
160 T& operator*() {
161 return *static_cast<T*>(*this);
162 }
163 const T& operator*() const {
164 return *static_cast<const T*>(*this);
165 }
166 T* operator->() {
167 return static_cast<T*>(*this);
168 }
169 const T* operator->() const {
170 return static_cast<const T*>(*this);
171 }
172 T* operator++() {
173 T* p = *this;
174 *this = ++p;
175 return p;
176 }
177 T* operator++(int) {
178 T* p = *this;
179 *this = p + 1;
180 return p;
181 }
182 T* operator--() {
183 T* p = *this;
184 *this = --p;
185 return p;
186 }
187 T* operator--(int) {
188 T* p = *this;
189 *this = p - 1;
190 return p;
191 }
192};/* class ThreadLocal<T*> */
193
194}/* CVC4 namespace */
195
196#endif /* 0 */
197
198#endif /* __CVC4__TLS_H */
ThreadLocalImpl(const ThreadLocalImpl &tl)
Definition tls.h:61
ThreadLocalImpl & operator=(const ThreadLocalImpl &tl)
Definition tls.h:70
ThreadLocalImpl(const T &t)
Definition tls.h:56
ThreadLocalImpl & operator=(const T &t)
Definition tls.h:66
ThreadLocalImpl(const ThreadLocalImpl &tl)
Definition tls.h:97
ThreadLocalImpl & operator=(const ThreadLocalImpl &tl)
Definition tls.h:106
ThreadLocalImpl & operator=(const T *t)
Definition tls.h:102
Macros that should be defined everywhere during the building of the libraries and driver binary,...
Definition expr.h:106