]>
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
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
18 #elif defined(__WIN32__)
19 #define HAVE_WIN32_THREAD
20 #include "wx/msw/wrapwin.h"
23 #if wxCHECK_GCC_VERSION(3, 3)
24 #define HAVE_COMPILER_THREAD
25 #define wxTHREAD_SPECIFIC __thread
26 #elif wxCHECK_VISUALC_VERSION(7)
27 #define HAVE_COMPILER_THREAD
28 #define wxTHREAD_SPECIFIC __declspec(thread)
31 // uncomment this to also test Boost version (you will also need to link with
33 //#define HAVE_BOOST_THREAD
34 #ifdef HAVE_BOOST_THREAD
35 #include <boost/thread/tss.hpp>
39 static const int NUM_ITER
= 1000;
41 // this is just a baseline
42 BENCHMARK_FUNC(DummyTLS
)
44 static int s_global
= 0;
46 for ( int n
= 0; n
< NUM_ITER
; n
++ )
57 #ifdef HAVE_COMPILER_THREAD
59 BENCHMARK_FUNC(CompilerTLS
)
61 static wxTHREAD_SPECIFIC
int s_global
= 0;
63 for ( int n
= 0; n
< NUM_ITER
; n
++ )
74 #endif // HAVE_COMPILER_THREAD
83 pthread_key_create(&m_key
, NULL
);
88 pthread_key_delete(m_key
);
91 operator pthread_key_t() const { return m_key
; }
96 DECLARE_NO_COPY_CLASS(PthreadKey
)
99 BENCHMARK_FUNC(PosixTLS
)
101 static PthreadKey s_key
;
103 for ( int n
= 0; n
< NUM_ITER
; n
++ )
106 pthread_setspecific(s_key
, 0);
108 pthread_setspecific(s_key
, &n
);
111 return !pthread_getspecific(s_key
);
114 #endif // HAVE_PTHREAD
116 #ifdef HAVE_WIN32_THREAD
123 m_slot
= ::TlsAlloc();
131 operator DWORD() const { return m_slot
; }
136 DECLARE_NO_COPY_CLASS(TlsSlot
)
139 BENCHMARK_FUNC(Win32TLS
)
141 static TlsSlot s_slot
;
143 for ( int n
= 0; n
< NUM_ITER
; n
++ )
146 ::TlsSetValue(s_slot
, 0);
148 ::TlsSetValue(s_slot
, &n
);
151 return !::TlsGetValue(s_slot
);
154 #endif // HAVE_WIN32_THREAD
156 #ifdef HAVE_BOOST_THREAD
158 BENCHMARK_FUNC(BoostTLS
)
160 static boost::thread_specific_ptr
<int> s_ptr
;
162 s_ptr
.reset(new int(0));
164 for ( int n
= 0; n
< NUM_ITER
; n
++ )
175 #endif // HAVE_BOOST_THREAD
177 BENCHMARK_FUNC(wxTLS
)
179 static wxTLS_TYPE(int) s_global
;
181 for ( int n
= 0; n
< NUM_ITER
; n
++ )