]>
git.saurik.com Git - wxWidgets.git/blob - tests/thread/tls.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/tls.cpp
3 // Purpose: wxTlsValue unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
22 #include "wx/thread.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
32 // NB: this struct must be a POD, so don't use wxString as its member
39 wxTLS_TYPE(PerThreadData
) gs_threadDataVar
;
40 #define gs_threadData wxTLS_VALUE(gs_threadDataVar)
42 wxTLS_TYPE(int) gs_threadIntVar
;
43 #define gs_threadInt wxTLS_VALUE(gs_threadIntVar)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // this thread arbitrarily modifies all global thread-specific variables to
50 // make sure that the changes in it are not visible from the main thread
51 class TLSTestThread
: public wxThread
54 // ctor both creates and starts the thread
55 TLSTestThread() : wxThread(wxTHREAD_JOINABLE
) { Create(); Run(); }
61 gs_threadData
.name
= "worker";
62 gs_threadData
.number
= 2;
64 CPPUNIT_ASSERT_EQUAL( "worker", gs_threadData
.name
);
65 CPPUNIT_ASSERT_EQUAL( 2, gs_threadData
.number
);
71 } // anonymous namespace
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 class TLSTestCase
: public CppUnit::TestCase
83 CPPUNIT_TEST_SUITE( TLSTestCase
);
84 CPPUNIT_TEST( TestInt
);
85 CPPUNIT_TEST( TestStruct
);
86 CPPUNIT_TEST_SUITE_END();
91 DECLARE_NO_COPY_CLASS(TLSTestCase
)
94 // register in the unnamed registry so that these tests are run by default
95 CPPUNIT_TEST_SUITE_REGISTRATION( TLSTestCase
);
97 // also include in its own registry so that these tests can be run alone
98 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TLSTestCase
, "TLSTestCase" );
100 void TLSTestCase::TestInt()
102 CPPUNIT_ASSERT_EQUAL( 0, gs_threadInt
);
105 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt
);
107 TLSTestThread().Wait();
109 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt
);
112 void TLSTestCase::TestStruct()
114 CPPUNIT_ASSERT_EQUAL( "", gs_threadData
.name
);
115 CPPUNIT_ASSERT_EQUAL( 0, gs_threadData
.number
);
117 gs_threadData
.name
= "main";
118 gs_threadData
.number
= 1;
120 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData
.number
);
122 TLSTestThread().Wait();
124 CPPUNIT_ASSERT_EQUAL( "main", gs_threadData
.name
);
125 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData
.number
);