OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #if defined(OS_WIN) | 5 #if defined(OS_WIN) |
6 #include <windows.h> | 6 #include <windows.h> |
7 #include <process.h> | 7 #include <process.h> |
8 #endif | 8 #endif |
9 | 9 |
10 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
11 #include "base/threading/thread_local_storage.h" | 11 #include "base/threading/thread_local_storage.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
15 // Ignore warnings about ptr->int conversions that we use when | 15 // Ignore warnings about ptr->int conversions that we use when |
16 // storing ints into ThreadLocalStorage. | 16 // storing ints into ThreadLocalStorage. |
17 #pragma warning(disable : 4311 4312) | 17 #pragma warning(disable : 4311 4312) |
18 #endif | 18 #endif |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const int kInitialTlsValue = 0x5555; | 24 const int kInitialTlsValue = 0x5555; |
25 const int kFinalTlsValue = 0x7777; | |
26 // How many times must a destructor be called before we really are done. | |
27 const int kNumberDestructorCallRepetitions = 8; | |
28 | |
25 static ThreadLocalStorage::Slot tls_slot(LINKER_INITIALIZED); | 29 static ThreadLocalStorage::Slot tls_slot(LINKER_INITIALIZED); |
26 | 30 |
27 class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate { | 31 class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate { |
28 public: | 32 public: |
29 explicit ThreadLocalStorageRunner(int* tls_value_ptr) | 33 explicit ThreadLocalStorageRunner(int* tls_value_ptr) |
30 : tls_value_ptr_(tls_value_ptr) {} | 34 : tls_value_ptr_(tls_value_ptr) {} |
31 | 35 |
32 virtual ~ThreadLocalStorageRunner() {} | 36 virtual ~ThreadLocalStorageRunner() {} |
33 | 37 |
34 virtual void Run() { | 38 virtual void Run() { |
35 *tls_value_ptr_ = kInitialTlsValue; | 39 *tls_value_ptr_ = kInitialTlsValue; |
36 tls_slot.Set(tls_value_ptr_); | 40 tls_slot.Set(tls_value_ptr_); |
37 | 41 |
38 int *ptr = static_cast<int*>(tls_slot.Get()); | 42 int *ptr = static_cast<int*>(tls_slot.Get()); |
39 EXPECT_EQ(ptr, tls_value_ptr_); | 43 EXPECT_EQ(ptr, tls_value_ptr_); |
40 EXPECT_EQ(*ptr, kInitialTlsValue); | 44 EXPECT_EQ(*ptr, kInitialTlsValue); |
41 *tls_value_ptr_ = 0; | 45 *tls_value_ptr_ = 0; |
42 | 46 |
43 ptr = static_cast<int*>(tls_slot.Get()); | 47 ptr = static_cast<int*>(tls_slot.Get()); |
44 EXPECT_EQ(ptr, tls_value_ptr_); | 48 EXPECT_EQ(ptr, tls_value_ptr_); |
45 EXPECT_EQ(*ptr, 0); | 49 EXPECT_EQ(*ptr, 0); |
50 | |
51 *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions; | |
46 } | 52 } |
47 | 53 |
48 private: | 54 private: |
49 int* tls_value_ptr_; | 55 int* tls_value_ptr_; |
50 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner); | 56 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner); |
51 }; | 57 }; |
52 | 58 |
53 | 59 |
54 void ThreadLocalStorageCleanup(void *value) { | 60 void ThreadLocalStorageCleanup(void *value) { |
55 int *ptr = reinterpret_cast<int*>(value); | 61 int *ptr = reinterpret_cast<int*>(value); |
56 if (ptr) | 62 // Destructors should never be called wit a NULL. |
willchan no longer on Chromium
2011/11/29 01:46:49
s/wit/with/
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
57 *ptr = kInitialTlsValue; | 63 ASSERT_NE(ptr, reinterpret_cast<int*>(NULL)); |
willchan no longer on Chromium
2011/11/29 01:46:49
Should be ASSERT_OP(expected, actual). You have it
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
64 if (*ptr == kFinalTlsValue) | |
65 return; // We've been called enough times. | |
66 ASSERT_GT(*ptr, kFinalTlsValue); | |
67 ASSERT_LE(*ptr, kFinalTlsValue + kNumberDestructorCallRepetitions); | |
68 --*ptr; // Move closer to our target. | |
69 // Tell tls that we're not done with this thread, and still need destruction. | |
70 tls_slot.Set(value); | |
58 } | 71 } |
59 | 72 |
60 } // namespace | 73 } // namespace |
61 | 74 |
62 TEST(ThreadLocalStorageTest, Basics) { | 75 TEST(ThreadLocalStorageTest, Basics) { |
63 ThreadLocalStorage::Slot slot; | 76 ThreadLocalStorage::Slot slot; |
64 slot.Set(reinterpret_cast<void*>(123)); | 77 slot.Set(reinterpret_cast<void*>(123)); |
65 int value = reinterpret_cast<intptr_t>(slot.Get()); | 78 int value = reinterpret_cast<intptr_t>(slot.Get()); |
66 EXPECT_EQ(value, 123); | 79 EXPECT_EQ(value, 123); |
67 } | 80 } |
(...skipping 18 matching lines...) Expand all Loading... | |
86 threads[index]->Start(); | 99 threads[index]->Start(); |
87 } | 100 } |
88 | 101 |
89 // Wait for the threads to finish. | 102 // Wait for the threads to finish. |
90 for (int index = 0; index < kNumThreads; index++) { | 103 for (int index = 0; index < kNumThreads; index++) { |
91 threads[index]->Join(); | 104 threads[index]->Join(); |
92 delete threads[index]; | 105 delete threads[index]; |
93 delete thread_delegates[index]; | 106 delete thread_delegates[index]; |
94 | 107 |
95 // Verify that the destructor was called and that we reset. | 108 // Verify that the destructor was called and that we reset. |
96 EXPECT_EQ(values[index], kInitialTlsValue); | 109 EXPECT_EQ(values[index], kFinalTlsValue); |
willchan no longer on Chromium
2011/11/29 01:46:49
Looks like this one was wrong too, can you fix the
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
97 } | 110 } |
98 tls_slot.Free(); // Stop doing callbacks to cleanup threads. | 111 tls_slot.Free(); // Stop doing callbacks to cleanup threads. |
99 } | 112 } |
100 | 113 |
101 } // namespace base | 114 } // namespace base |
OLD | NEW |