thread_local_storage_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #if defined(OS_WIN)
  5. #include <windows.h>
  6. #include <process.h>
  7. #endif
  8. #include "butil/threading/simple_thread.h"
  9. #include "butil/threading/thread_local_storage.h"
  10. #include <gtest/gtest.h>
  11. #if defined(OS_WIN)
  12. // Ignore warnings about ptr->int conversions that we use when
  13. // storing ints into ThreadLocalStorage.
  14. #pragma warning(disable : 4311 4312)
  15. #endif
  16. namespace butil {
  17. namespace {
  18. const int kInitialTlsValue = 0x5555;
  19. const int kFinalTlsValue = 0x7777;
  20. // How many times must a destructor be called before we really are done.
  21. const int kNumberDestructorCallRepetitions = 3;
  22. static ThreadLocalStorage::StaticSlot tls_slot = TLS_INITIALIZER;
  23. class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate {
  24. public:
  25. explicit ThreadLocalStorageRunner(int* tls_value_ptr)
  26. : tls_value_ptr_(tls_value_ptr) {}
  27. virtual ~ThreadLocalStorageRunner() {}
  28. virtual void Run() OVERRIDE {
  29. *tls_value_ptr_ = kInitialTlsValue;
  30. tls_slot.Set(tls_value_ptr_);
  31. int *ptr = static_cast<int*>(tls_slot.Get());
  32. EXPECT_EQ(ptr, tls_value_ptr_);
  33. EXPECT_EQ(*ptr, kInitialTlsValue);
  34. *tls_value_ptr_ = 0;
  35. ptr = static_cast<int*>(tls_slot.Get());
  36. EXPECT_EQ(ptr, tls_value_ptr_);
  37. EXPECT_EQ(*ptr, 0);
  38. *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions;
  39. }
  40. private:
  41. int* tls_value_ptr_;
  42. DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner);
  43. };
  44. void ThreadLocalStorageCleanup(void *value) {
  45. int *ptr = reinterpret_cast<int*>(value);
  46. // Destructors should never be called with a NULL.
  47. ASSERT_NE(reinterpret_cast<int*>(NULL), ptr);
  48. if (*ptr == kFinalTlsValue)
  49. return; // We've been called enough times.
  50. ASSERT_LT(kFinalTlsValue, *ptr);
  51. ASSERT_GE(kFinalTlsValue + kNumberDestructorCallRepetitions, *ptr);
  52. --*ptr; // Move closer to our target.
  53. // Tell tls that we're not done with this thread, and still need destruction.
  54. tls_slot.Set(value);
  55. }
  56. } // namespace
  57. TEST(ThreadLocalStorageTest, Basics) {
  58. ThreadLocalStorage::Slot slot;
  59. slot.Set(reinterpret_cast<void*>(123));
  60. int value = reinterpret_cast<intptr_t>(slot.Get());
  61. EXPECT_EQ(value, 123);
  62. }
  63. #if defined(THREAD_SANITIZER)
  64. // Do not run the test under ThreadSanitizer. Because this test iterates its
  65. // own TSD destructor for the maximum possible number of times, TSan can't jump
  66. // in after the last destructor invocation, therefore the destructor remains
  67. // unsynchronized with the following users of the same TSD slot. This results
  68. // in race reports between the destructor and functions in other tests.
  69. #define MAYBE_TLSDestructors DISABLED_TLSDestructors
  70. #else
  71. #define MAYBE_TLSDestructors TLSDestructors
  72. #endif
  73. TEST(ThreadLocalStorageTest, MAYBE_TLSDestructors) {
  74. // Create a TLS index with a destructor. Create a set of
  75. // threads that set the TLS, while the destructor cleans it up.
  76. // After the threads finish, verify that the value is cleaned up.
  77. const int kNumThreads = 5;
  78. int values[kNumThreads];
  79. ThreadLocalStorageRunner* thread_delegates[kNumThreads];
  80. DelegateSimpleThread* threads[kNumThreads];
  81. tls_slot.Initialize(ThreadLocalStorageCleanup);
  82. // Spawn the threads.
  83. for (int index = 0; index < kNumThreads; index++) {
  84. values[index] = kInitialTlsValue;
  85. thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]);
  86. threads[index] = new DelegateSimpleThread(thread_delegates[index],
  87. "tls thread");
  88. threads[index]->Start();
  89. }
  90. // Wait for the threads to finish.
  91. for (int index = 0; index < kNumThreads; index++) {
  92. threads[index]->Join();
  93. delete threads[index];
  94. delete thread_delegates[index];
  95. // Verify that the destructor was called and that we reset.
  96. EXPECT_EQ(values[index], kFinalTlsValue);
  97. }
  98. tls_slot.Free(); // Stop doing callbacks to cleanup threads.
  99. }
  100. } // namespace butil