summaryrefslogtreecommitdiff
path: root/src/util/tls.h.in
blob: a5af4a4c5c6927941cd9dfbe95a748b036f0b4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*********************                                                        */
/*! \file tls.h.in
 ** \verbatim
 ** Original author: ACSYS <cvc4-devel@cs.nyu.edu>
 ** Major contributors: Morgan Deters <mdeters@cs.nyu.edu>
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2013  New York University and The University of Iowa
 ** 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

// 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 @CVC4_TLS_SUPPORTED@
#  define CVC4_THREADLOCAL(__type...) @CVC4_TLS@ __type
#  define CVC4_THREADLOCAL_PUBLIC(__type...) @CVC4_TLS@ 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 /* @CVC4_TLS_SUPPORTED@ */

#endif /* _CVC4__TLS_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback