]> git.saurik.com Git - wxWidgets.git/blame - tests/benchmarks/tls.cpp
No changes, just fix a typo in a comment in docview event handling code.
[wxWidgets.git] / tests / benchmarks / tls.cpp
CommitLineData
49f11e84
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: tests/benchmarks/strings.cpp
3// Purpose: String-related benchmarks
4// Author: Vadim Zeitlin
5// Created: 2008-07-19
49f11e84 6// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
526954c5 7// Licence: wxWindows licence
49f11e84
VZ
8/////////////////////////////////////////////////////////////////////////////
9
10#include "bench.h"
11
64a044d5
VZ
12#include "wx/tls.h"
13
c585ccef 14#if defined(__UNIX__)
49f11e84
VZ
15 #define HAVE_PTHREAD
16 #include <pthread.h>
c585ccef
VZ
17#elif defined(__WIN32__)
18 #define HAVE_WIN32_THREAD
19 #include "wx/msw/wrapwin.h"
49f11e84
VZ
20#endif
21
22#if wxCHECK_GCC_VERSION(3, 3)
23 #define HAVE_COMPILER_THREAD
24 #define wxTHREAD_SPECIFIC __thread
c585ccef
VZ
25#elif wxCHECK_VISUALC_VERSION(7)
26 #define HAVE_COMPILER_THREAD
27 #define wxTHREAD_SPECIFIC __declspec(thread)
49f11e84
VZ
28#endif
29
30// uncomment this to also test Boost version (you will also need to link with
31// libboost_threads)
4c25eacc 32//#define HAVE_BOOST_THREAD
49f11e84
VZ
33#ifdef HAVE_BOOST_THREAD
34 #include <boost/thread/tss.hpp>
35#endif
36
37
38static const int NUM_ITER = 1000;
39
40// this is just a baseline
41BENCHMARK_FUNC(DummyTLS)
42{
43 static int s_global = 0;
44
45 for ( int n = 0; n < NUM_ITER; n++ )
46 {
47 if ( n % 2 )
48 s_global = 0;
49 else
50 s_global = n;
51 }
52
53 return !s_global;
54}
55
56#ifdef HAVE_COMPILER_THREAD
57
58BENCHMARK_FUNC(CompilerTLS)
59{
60 static wxTHREAD_SPECIFIC int s_global = 0;
61
62 for ( int n = 0; n < NUM_ITER; n++ )
63 {
64 if ( n % 2 )
65 s_global = 0;
66 else
67 s_global = n;
68 }
69
70 return !s_global;
71}
72
73#endif // HAVE_COMPILER_THREAD
74
75#ifdef HAVE_PTHREAD
76
77class PthreadKey
78{
79public:
80 PthreadKey()
81 {
82 pthread_key_create(&m_key, NULL);
83 }
84
85 ~PthreadKey()
86 {
87 pthread_key_delete(m_key);
88 }
89
90 operator pthread_key_t() const { return m_key; }
91
92private:
93 pthread_key_t m_key;
94
95 DECLARE_NO_COPY_CLASS(PthreadKey)
96};
97
98BENCHMARK_FUNC(PosixTLS)
99{
100 static PthreadKey s_key;
101
102 for ( int n = 0; n < NUM_ITER; n++ )
103 {
104 if ( n % 2 )
105 pthread_setspecific(s_key, 0);
106 else
107 pthread_setspecific(s_key, &n);
108 }
109
110 return !pthread_getspecific(s_key);
111}
112
113#endif // HAVE_PTHREAD
114
c585ccef
VZ
115#ifdef HAVE_WIN32_THREAD
116
117class TlsSlot
118{
119public:
120 TlsSlot()
121 {
122 m_slot = ::TlsAlloc();
123 }
124
125 ~TlsSlot()
126 {
127 ::TlsFree(m_slot);
128 }
129
130 operator DWORD() const { return m_slot; }
131
132private:
133 DWORD m_slot;
134
135 DECLARE_NO_COPY_CLASS(TlsSlot)
136};
137
138BENCHMARK_FUNC(Win32TLS)
139{
140 static TlsSlot s_slot;
141
142 for ( int n = 0; n < NUM_ITER; n++ )
143 {
144 if ( n % 2 )
145 ::TlsSetValue(s_slot, 0);
146 else
147 ::TlsSetValue(s_slot, &n);
148 }
149
150 return !::TlsGetValue(s_slot);
151}
152
153#endif // HAVE_WIN32_THREAD
154
49f11e84
VZ
155#ifdef HAVE_BOOST_THREAD
156
157BENCHMARK_FUNC(BoostTLS)
158{
159 static boost::thread_specific_ptr<int> s_ptr;
160 if ( !s_ptr.get() )
161 s_ptr.reset(new int(0));
162
163 for ( int n = 0; n < NUM_ITER; n++ )
164 {
165 if ( n % 2 )
166 *s_ptr = 0;
167 else
168 *s_ptr = n;
169 }
170
171 return !*s_ptr;
172}
173
174#endif // HAVE_BOOST_THREAD
64a044d5
VZ
175
176BENCHMARK_FUNC(wxTLS)
177{
8b73c531
VZ
178 static wxTLS_TYPE(int) s_globalVar;
179 #define s_global wxTLS_VALUE(s_globalVar)
64a044d5
VZ
180
181 for ( int n = 0; n < NUM_ITER; n++ )
182 {
183 if ( n % 2 )
184 s_global = 0;
185 else
186 s_global = n;
187 }
188
189 return !s_global;
190}