tls.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/thread/tls.cpp
  3. // Purpose: wxTlsValue unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-08-28
  6. // Copyright: (c) 2008 Vadim Zeitlin
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #endif // WX_PRECOMP
  17. #include "wx/thread.h"
  18. #include "wx/tls.h"
  19. // ----------------------------------------------------------------------------
  20. // globals
  21. // ----------------------------------------------------------------------------
  22. namespace
  23. {
  24. // NB: this struct must be a POD, so don't use wxString as its member
  25. struct PerThreadData
  26. {
  27. const char *name;
  28. int number;
  29. };
  30. wxTLS_TYPE(PerThreadData) gs_threadDataVar;
  31. #define gs_threadData wxTLS_VALUE(gs_threadDataVar)
  32. wxTLS_TYPE(int) gs_threadIntVar;
  33. #define gs_threadInt wxTLS_VALUE(gs_threadIntVar)
  34. // ----------------------------------------------------------------------------
  35. // test thread
  36. // ----------------------------------------------------------------------------
  37. // this thread arbitrarily modifies all global thread-specific variables to
  38. // make sure that the changes in it are not visible from the main thread
  39. class TLSTestThread : public wxThread
  40. {
  41. public:
  42. // ctor both creates and starts the thread
  43. TLSTestThread() : wxThread(wxTHREAD_JOINABLE) { Create(); Run(); }
  44. virtual void *Entry()
  45. {
  46. gs_threadInt = 17;
  47. gs_threadData.name = "worker";
  48. gs_threadData.number = 2;
  49. CPPUNIT_ASSERT_EQUAL( "worker", gs_threadData.name );
  50. CPPUNIT_ASSERT_EQUAL( 2, gs_threadData.number );
  51. return NULL;
  52. }
  53. };
  54. } // anonymous namespace
  55. // ----------------------------------------------------------------------------
  56. // test class
  57. // ----------------------------------------------------------------------------
  58. class TLSTestCase : public CppUnit::TestCase
  59. {
  60. public:
  61. TLSTestCase() { }
  62. private:
  63. CPPUNIT_TEST_SUITE( TLSTestCase );
  64. CPPUNIT_TEST( TestInt );
  65. CPPUNIT_TEST( TestStruct );
  66. CPPUNIT_TEST_SUITE_END();
  67. void TestInt();
  68. void TestStruct();
  69. DECLARE_NO_COPY_CLASS(TLSTestCase)
  70. };
  71. // register in the unnamed registry so that these tests are run by default
  72. CPPUNIT_TEST_SUITE_REGISTRATION( TLSTestCase );
  73. // also include in its own registry so that these tests can be run alone
  74. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TLSTestCase, "TLSTestCase" );
  75. void TLSTestCase::TestInt()
  76. {
  77. CPPUNIT_ASSERT_EQUAL( 0, gs_threadInt );
  78. gs_threadInt++;
  79. CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
  80. TLSTestThread().Wait();
  81. CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
  82. }
  83. void TLSTestCase::TestStruct()
  84. {
  85. CPPUNIT_ASSERT_EQUAL( "", gs_threadData.name );
  86. CPPUNIT_ASSERT_EQUAL( 0, gs_threadData.number );
  87. gs_threadData.name = "main";
  88. gs_threadData.number = 1;
  89. CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
  90. TLSTestThread().Wait();
  91. CPPUNIT_ASSERT_EQUAL( "main", gs_threadData.name );
  92. CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
  93. }