chanegd wxTlsValue to be pointer-like instead of value-like which doesn't work for...
[wxWidgets.git] / include / wx / tls.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/tls.h
3 // Purpose: Implementation of thread local storage
4 // Author: Vadim Zeitlin
5 // Created: 2008-08-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TLS_H_
12 #define _WX_TLS_H_
13
14 #include "wx/defs.h"
15
16 // ----------------------------------------------------------------------------
17 // check for compiler support of thread-specific variables
18 // ----------------------------------------------------------------------------
19
20 // when not using threads at all, there is no need for thread-specific
21 // values to be really thread-specific
22 #if !wxUSE_THREADS
23 #define wxHAS_COMPILER_TLS
24 #define wxTHREAD_SPECIFIC_DECL
25 // __thread keyword is supported if configure detected it or when using mingw32
26 // >= 4.3 which is known to have it too
27 #elif defined(HAVE___THREAD_KEYWORD) || \
28 (defined(__MINGW32__) && wxCHECK_GCC_VERSION(4, 3))
29 #define wxHAS_COMPILER_TLS
30 #define wxTHREAD_SPECIFIC_DECL __thread
31 // MSVC has its own version which might be supported by some other Windows
32 // compilers, to be tested
33 #elif wxCHECK_VISUALC_VERSION(7)
34 #define wxHAS_COMPILER_TLS
35 #define wxTHREAD_SPECIFIC_DECL __declspec(thread)
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // define wxTLS_TYPE()
40 // ----------------------------------------------------------------------------
41
42 #ifdef wxHAS_COMPILER_TLS
43 #define wxTLS_TYPE(T) wxTHREAD_SPECIFIC_DECL T
44 #define wxTLS_PTR(var) (&(var))
45 #define wxTLS_VALUE(var) (var)
46 #else // !wxHAS_COMPILER_TLS
47 #ifdef __WXMSW__
48 #include "wx/msw/tls.h"
49 #elif defined(__UNIX__)
50 #include "wx/unix/tls.h"
51 #else
52 // TODO: we could emulate TLS for such platforms...
53 #error Neither compiler nor OS support thread-specific variables.
54 #endif
55
56 #include <stdlib.h> // for calloc()
57
58 // wxTlsValue<T> represents a thread-specific value of type T but, unlike
59 // with native compiler thread-specific variables, it behaves like a
60 // (never NULL) pointer to T and so needs to be dereferenced before use
61 template <typename T>
62 class wxTlsValue
63 {
64 public:
65 typedef T ValueType;
66
67 // ctor doesn't do anything, the object is created on first access
68 //
69 // FIXME: the thread-specific values are currently not freed under
70 // Windows, resulting in memory leaks, this must be implemented
71 // there somehow (probably by keeping a list of all TLS objects
72 // and cleaning them up in wxThread cleanup)
73 wxTlsValue()
74 #ifdef __UNIX__
75 : m_key(free)
76 #endif
77 {
78 }
79
80 // dtor is only called in the main thread context and so is not enough
81 // to free memory allocated by us for the other threads, we use
82 // destructor function when using Pthreads for this (which is not
83 // called for the main thread as it doesn't call pthread_exit() but
84 // just to be safe we also reset the key anyhow) and simply leak the
85 // memory under Windows (see the FIXME above)
86 ~wxTlsValue()
87 {
88 void * const value = m_key.Get();
89 if ( value)
90 {
91 free(value);
92 m_key.Set(NULL);
93 }
94 }
95
96 // access the object creating it on demand
97 ValueType *Get()
98 {
99 void *value = m_key.Get();
100 if ( !value )
101 {
102 // ValueType must be POD to be used in wxHAS_COMPILER_TLS case
103 // anyhow (at least gcc doesn't accept non-POD values being
104 // declared with __thread) so initialize it as a POD too
105 value = calloc(1, sizeof(ValueType));
106
107 if ( !m_key.Set(value) )
108 {
109 free(value);
110
111 // this will probably result in a crash in the caller but
112 // it's arguably better to crash immediately instead of
113 // slowly dying from out-of-memory errors which would
114 // happen as the next access to this object would allocate
115 // another ValueType instance and so on forever
116 value = NULL;
117 }
118 }
119
120 return static_cast<ValueType *>(value);
121 }
122
123 // pointer-like accessors
124 ValueType *operator->() { return Get(); }
125 ValueType& operator*() { return *Get(); }
126
127 private:
128 wxTlsKey m_key;
129
130 DECLARE_NO_COPY_TEMPLATE_CLASS(wxTlsValue, T)
131 };
132
133 #define wxTLS_TYPE(T) wxTlsValue<T>
134 #define wxTLS_PTR(var) (var)
135 #define wxTLS_VALUE(var) (*(var))
136 #endif // wxHAS_COMPILER_TLS/!wxHAS_COMPILER_TLS
137
138 #endif // _WX_TLS_H_
139