OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <windows.h> | 6 #include <windows.h> |
6 #include <process.h> | 7 #include <process.h> |
| 8 #endif |
7 | 9 |
| 10 #include "base/simple_thread.h" |
8 #include "base/thread_local_storage.h" | 11 #include "base/thread_local_storage.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 // ignore warnings about ptr->int conversions that we use when | 14 #if defined(OS_WIN) |
| 15 // Ignore warnings about ptr->int conversions that we use when |
12 // storing ints into ThreadLocalStorage. | 16 // storing ints into ThreadLocalStorage. |
13 #pragma warning(disable : 4311 4312) | 17 #pragma warning(disable : 4311 4312) |
14 | 18 #endif |
15 namespace { | |
16 class ThreadLocalStorageTest : public testing::Test { | |
17 }; | |
18 } | |
19 | |
20 | |
21 TEST(ThreadLocalStorageTest, Basics) { | |
22 ThreadLocalStorage::Slot slot; | |
23 slot.Set(reinterpret_cast<void*>(123)); | |
24 int value = reinterpret_cast<int>(slot.Get()); | |
25 EXPECT_EQ(value, 123); | |
26 } | |
27 | 19 |
28 const int kInitialTlsValue = 0x5555; | 20 const int kInitialTlsValue = 0x5555; |
29 static ThreadLocalStorage::Slot tls_slot(base::LINKER_INITIALIZED); | 21 static ThreadLocalStorage::Slot tls_slot(base::LINKER_INITIALIZED); |
30 | 22 |
31 unsigned __stdcall TLSTestThreadMain(void* param) { | |
32 // param contains the thread local storage index. | |
33 int *index = reinterpret_cast<int*>(param); | |
34 *index = kInitialTlsValue; | |
35 | 23 |
36 tls_slot.Set(index); | 24 class ThreadLocalStorageRunner : public base::DelegateSimpleThread::Delegate { |
| 25 public: |
| 26 explicit ThreadLocalStorageRunner(int* tls_value_ptr) |
| 27 : tls_value_ptr_(tls_value_ptr) {} |
37 | 28 |
38 int *ptr = static_cast<int*>(tls_slot.Get()); | 29 virtual ~ThreadLocalStorageRunner() {} |
39 EXPECT_EQ(ptr, index); | |
40 EXPECT_EQ(*ptr, kInitialTlsValue); | |
41 *index = 0; | |
42 | 30 |
43 ptr = static_cast<int*>(tls_slot.Get()); | 31 virtual void Run() { |
44 EXPECT_EQ(ptr, index); | 32 *tls_value_ptr_ = kInitialTlsValue; |
45 EXPECT_EQ(*ptr, 0); | 33 tls_slot.Set(tls_value_ptr_); |
46 return 0; | 34 |
47 } | 35 int *ptr = static_cast<int*>(tls_slot.Get()); |
| 36 EXPECT_EQ(ptr, tls_value_ptr_); |
| 37 EXPECT_EQ(*ptr, kInitialTlsValue); |
| 38 *tls_value_ptr_ = 0; |
| 39 |
| 40 ptr = static_cast<int*>(tls_slot.Get()); |
| 41 EXPECT_EQ(ptr, tls_value_ptr_); |
| 42 EXPECT_EQ(*ptr, 0); |
| 43 } |
| 44 |
| 45 private: |
| 46 int* tls_value_ptr_; |
| 47 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner); |
| 48 }; |
| 49 |
48 | 50 |
49 void ThreadLocalStorageCleanup(void *value) { | 51 void ThreadLocalStorageCleanup(void *value) { |
50 int *ptr = reinterpret_cast<int*>(value); | 52 int *ptr = reinterpret_cast<int*>(value); |
51 if (ptr) | 53 if (ptr) |
52 *ptr = kInitialTlsValue; | 54 *ptr = kInitialTlsValue; |
53 } | 55 } |
54 | 56 |
55 | 57 |
| 58 TEST(ThreadLocalStorageTest, Basics) { |
| 59 ThreadLocalStorage::Slot slot; |
| 60 slot.Set(reinterpret_cast<void*>(123)); |
| 61 int value = reinterpret_cast<int>(slot.Get()); |
| 62 EXPECT_EQ(value, 123); |
| 63 } |
| 64 |
56 TEST(ThreadLocalStorageTest, TLSDestructors) { | 65 TEST(ThreadLocalStorageTest, TLSDestructors) { |
57 // Create a TLS index with a destructor. Create a set of | 66 // Create a TLS index with a destructor. Create a set of |
58 // threads that set the TLS, while the destructor cleans it up. | 67 // threads that set the TLS, while the destructor cleans it up. |
59 // After the threads finish, verify that the value is cleaned up. | 68 // After the threads finish, verify that the value is cleaned up. |
60 const int kNumThreads = 5; | 69 const int kNumThreads = 5; |
61 HANDLE threads[kNumThreads]; | |
62 int values[kNumThreads]; | 70 int values[kNumThreads]; |
| 71 ThreadLocalStorageRunner* thread_delegates[kNumThreads]; |
| 72 base::DelegateSimpleThread* threads[kNumThreads]; |
63 | 73 |
64 tls_slot.Initialize(ThreadLocalStorageCleanup); | 74 tls_slot.Initialize(ThreadLocalStorageCleanup); |
65 | 75 |
66 // Spawn the threads. | 76 // Spawn the threads. |
67 for (int16 index = 0; index < kNumThreads; index++) { | 77 for (int index = 0; index < kNumThreads; index++) { |
68 values[index] = kInitialTlsValue; | 78 values[index] = kInitialTlsValue; |
69 void *argument = static_cast<void*>(&(values[index])); | 79 thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]); |
70 unsigned thread_id; | 80 threads[index] = new base::DelegateSimpleThread(thread_delegates[index], |
71 threads[index] = reinterpret_cast<HANDLE>( | 81 "tls thread"); |
72 _beginthreadex(NULL, 0, TLSTestThreadMain, argument, 0, &thread_id)); | 82 threads[index]->Start(); |
73 EXPECT_NE(threads[index], (HANDLE)NULL); | |
74 } | 83 } |
75 | 84 |
76 // Wait for the threads to finish. | 85 // Wait for the threads to finish. |
77 for (int index = 0; index < kNumThreads; index++) { | 86 for (int index = 0; index < kNumThreads; index++) { |
78 DWORD rv = WaitForSingleObject(threads[index], 60*1000); | 87 threads[index]->Join(); |
79 EXPECT_EQ(rv, WAIT_OBJECT_0); // verify all threads finished | 88 delete threads[index]; |
| 89 delete thread_delegates[index]; |
80 | 90 |
81 // verify that the destructor was called and that we reset. | 91 // Verify that the destructor was called and that we reset. |
82 EXPECT_EQ(values[index], kInitialTlsValue); | 92 EXPECT_EQ(values[index], kInitialTlsValue); |
83 } | 93 } |
84 } | 94 } |
85 | |
OLD | NEW |