]>
git.saurik.com Git - wxWidgets.git/blob - tests/thread/tls.cpp
f88e68f72c420e4f0559c719ec612209af4bbc22
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/tls.cpp
3 // Purpose: wxTlsValue unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/thread.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
33 // NB: this struct must be a POD, so don't use wxString as its member
40 wxTLS_TYPE(PerThreadData
) gs_threadDataVar
;
41 #define gs_threadData wxTLS_VALUE(gs_threadDataVar)
43 wxTLS_TYPE(int) gs_threadIntVar
;
44 #define gs_threadInt wxTLS_VALUE(gs_threadIntVar)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // this thread arbitrarily modifies all global thread-specific variables to
51 // make sure that the changes in it are not visible from the main thread
52 class TLSTestThread
: public wxThread
55 // ctor both creates and starts the thread
56 TLSTestThread() : wxThread(wxTHREAD_JOINABLE
) { Create(); Run(); }
62 gs_threadData
.name
= "worker";
63 gs_threadData
.number
= 2;
65 CPPUNIT_ASSERT_EQUAL( "worker", gs_threadData
.name
);
66 CPPUNIT_ASSERT_EQUAL( 2, gs_threadData
.number
);
72 } // anonymous namespace
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 class TLSTestCase
: public CppUnit::TestCase
84 CPPUNIT_TEST_SUITE( TLSTestCase
);
85 CPPUNIT_TEST( TestInt
);
86 CPPUNIT_TEST( TestStruct
);
87 CPPUNIT_TEST_SUITE_END();
92 DECLARE_NO_COPY_CLASS(TLSTestCase
)
95 // register in the unnamed registry so that these tests are run by default
96 CPPUNIT_TEST_SUITE_REGISTRATION( TLSTestCase
);
98 // also include in its own registry so that these tests can be run alone
99 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TLSTestCase
, "TLSTestCase" );
101 void TLSTestCase::TestInt()
103 CPPUNIT_ASSERT_EQUAL( 0, gs_threadInt
);
106 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt
);
108 TLSTestThread().Wait();
110 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt
);
113 void TLSTestCase::TestStruct()
115 CPPUNIT_ASSERT_EQUAL( "", gs_threadData
.name
);
116 CPPUNIT_ASSERT_EQUAL( 0, gs_threadData
.number
);
118 gs_threadData
.name
= "main";
119 gs_threadData
.number
= 1;
121 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData
.number
);
123 TLSTestThread().Wait();
125 CPPUNIT_ASSERT_EQUAL( "main", gs_threadData
.name
);
126 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData
.number
);