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