]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/tls.cpp
213d3e87df6301ced8169a8bab39e8e7e2549ae5
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 /////////////////////////////////////////////////////////////////////////////
16 #elif defined(__WIN32__)
17 #define HAVE_WIN32_THREAD
18 #include "wx/msw/wrapwin.h"
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)
29 // uncomment this to also test Boost version (you will also need to link with
31 #define HAVE_BOOST_THREAD
32 #ifdef HAVE_BOOST_THREAD
33 #include <boost/thread/tss.hpp>
37 static const int NUM_ITER
= 1000;
39 // this is just a baseline
40 BENCHMARK_FUNC(DummyTLS
)
42 static int s_global
= 0;
44 for ( int n
= 0; n
< NUM_ITER
; n
++ )
55 #ifdef HAVE_COMPILER_THREAD
57 BENCHMARK_FUNC(CompilerTLS
)
59 static wxTHREAD_SPECIFIC
int s_global
= 0;
61 for ( int n
= 0; n
< NUM_ITER
; n
++ )
72 #endif // HAVE_COMPILER_THREAD
81 pthread_key_create(&m_key
, NULL
);
86 pthread_key_delete(m_key
);
89 operator pthread_key_t() const { return m_key
; }
94 DECLARE_NO_COPY_CLASS(PthreadKey
)
97 BENCHMARK_FUNC(PosixTLS
)
99 static PthreadKey s_key
;
101 for ( int n
= 0; n
< NUM_ITER
; n
++ )
104 pthread_setspecific(s_key
, 0);
106 pthread_setspecific(s_key
, &n
);
109 return !pthread_getspecific(s_key
);
112 #endif // HAVE_PTHREAD
114 #ifdef HAVE_WIN32_THREAD
121 m_slot
= ::TlsAlloc();
129 operator DWORD() const { return m_slot
; }
134 DECLARE_NO_COPY_CLASS(TlsSlot
)
137 BENCHMARK_FUNC(Win32TLS
)
139 static TlsSlot s_slot
;
141 for ( int n
= 0; n
< NUM_ITER
; n
++ )
144 ::TlsSetValue(s_slot
, 0);
146 ::TlsSetValue(s_slot
, &n
);
149 return !::TlsGetValue(s_slot
);
152 #endif // HAVE_WIN32_THREAD
154 #ifdef HAVE_BOOST_THREAD
156 BENCHMARK_FUNC(BoostTLS
)
158 static boost::thread_specific_ptr
<int> s_ptr
;
160 s_ptr
.reset(new int(0));
162 for ( int n
= 0; n
< NUM_ITER
; n
++ )
173 #endif // HAVE_BOOST_THREAD