]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/tls.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tests/benchmarks/strings.cpp
3 // Purpose: String-related benchmarks
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
17 #elif defined(__WIN32__)
18 #define HAVE_WIN32_THREAD
19 #include "wx/msw/wrapwin.h"
22 #if wxCHECK_GCC_VERSION(3, 3)
23 #define HAVE_COMPILER_THREAD
24 #define wxTHREAD_SPECIFIC __thread
25 #elif wxCHECK_VISUALC_VERSION(7)
26 #define HAVE_COMPILER_THREAD
27 #define wxTHREAD_SPECIFIC __declspec(thread)
30 // uncomment this to also test Boost version (you will also need to link with
32 //#define HAVE_BOOST_THREAD
33 #ifdef HAVE_BOOST_THREAD
34 #include <boost/thread/tss.hpp>
38 static const int NUM_ITER
= 1000;
40 // this is just a baseline
41 BENCHMARK_FUNC(DummyTLS
)
43 static int s_global
= 0;
45 for ( int n
= 0; n
< NUM_ITER
; n
++ )
56 #ifdef HAVE_COMPILER_THREAD
58 BENCHMARK_FUNC(CompilerTLS
)
60 static wxTHREAD_SPECIFIC
int s_global
= 0;
62 for ( int n
= 0; n
< NUM_ITER
; n
++ )
73 #endif // HAVE_COMPILER_THREAD
82 pthread_key_create(&m_key
, NULL
);
87 pthread_key_delete(m_key
);
90 operator pthread_key_t() const { return m_key
; }
95 DECLARE_NO_COPY_CLASS(PthreadKey
)
98 BENCHMARK_FUNC(PosixTLS
)
100 static PthreadKey s_key
;
102 for ( int n
= 0; n
< NUM_ITER
; n
++ )
105 pthread_setspecific(s_key
, 0);
107 pthread_setspecific(s_key
, &n
);
110 return !pthread_getspecific(s_key
);
113 #endif // HAVE_PTHREAD
115 #ifdef HAVE_WIN32_THREAD
122 m_slot
= ::TlsAlloc();
130 operator DWORD() const { return m_slot
; }
135 DECLARE_NO_COPY_CLASS(TlsSlot
)
138 BENCHMARK_FUNC(Win32TLS
)
140 static TlsSlot s_slot
;
142 for ( int n
= 0; n
< NUM_ITER
; n
++ )
145 ::TlsSetValue(s_slot
, 0);
147 ::TlsSetValue(s_slot
, &n
);
150 return !::TlsGetValue(s_slot
);
153 #endif // HAVE_WIN32_THREAD
155 #ifdef HAVE_BOOST_THREAD
157 BENCHMARK_FUNC(BoostTLS
)
159 static boost::thread_specific_ptr
<int> s_ptr
;
161 s_ptr
.reset(new int(0));
163 for ( int n
= 0; n
< NUM_ITER
; n
++ )
174 #endif // HAVE_BOOST_THREAD
176 BENCHMARK_FUNC(wxTLS
)
178 static wxTLS_TYPE(int) s_globalVar
;
179 #define s_global wxTLS_VALUE(s_globalVar)
181 for ( int n
= 0; n
< NUM_ITER
; n
++ )