summaryrefslogtreecommitdiff
path: root/src/util/tls.h.in
blob: 80eac93b0ffaab38a4ab7ddac6ab921af21c09a0 (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
/*********************                                                        */
/*! \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

#line 28 "@srcdir@/tls.h.in"

#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(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 reinterpret_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> */

}/* 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