| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/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 { |
| 21 |
| 22 namespace { |
| 23 |
| 20 const int kInitialTlsValue = 0x5555; | 24 const int kInitialTlsValue = 0x5555; |
| 21 static ThreadLocalStorage::Slot tls_slot(base::LINKER_INITIALIZED); | 25 static ThreadLocalStorage::Slot tls_slot(LINKER_INITIALIZED); |
| 22 | 26 |
| 23 | 27 class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate { |
| 24 class ThreadLocalStorageRunner : public base::DelegateSimpleThread::Delegate { | |
| 25 public: | 28 public: |
| 26 explicit ThreadLocalStorageRunner(int* tls_value_ptr) | 29 explicit ThreadLocalStorageRunner(int* tls_value_ptr) |
| 27 : tls_value_ptr_(tls_value_ptr) {} | 30 : tls_value_ptr_(tls_value_ptr) {} |
| 28 | 31 |
| 29 virtual ~ThreadLocalStorageRunner() {} | 32 virtual ~ThreadLocalStorageRunner() {} |
| 30 | 33 |
| 31 virtual void Run() { | 34 virtual void Run() { |
| 32 *tls_value_ptr_ = kInitialTlsValue; | 35 *tls_value_ptr_ = kInitialTlsValue; |
| 33 tls_slot.Set(tls_value_ptr_); | 36 tls_slot.Set(tls_value_ptr_); |
| 34 | 37 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 47 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner); | 50 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner); |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 | 53 |
| 51 void ThreadLocalStorageCleanup(void *value) { | 54 void ThreadLocalStorageCleanup(void *value) { |
| 52 int *ptr = reinterpret_cast<int*>(value); | 55 int *ptr = reinterpret_cast<int*>(value); |
| 53 if (ptr) | 56 if (ptr) |
| 54 *ptr = kInitialTlsValue; | 57 *ptr = kInitialTlsValue; |
| 55 } | 58 } |
| 56 | 59 |
| 60 } // namespace |
| 57 | 61 |
| 58 TEST(ThreadLocalStorageTest, Basics) { | 62 TEST(ThreadLocalStorageTest, Basics) { |
| 59 ThreadLocalStorage::Slot slot; | 63 ThreadLocalStorage::Slot slot; |
| 60 slot.Set(reinterpret_cast<void*>(123)); | 64 slot.Set(reinterpret_cast<void*>(123)); |
| 61 int value = reinterpret_cast<intptr_t>(slot.Get()); | 65 int value = reinterpret_cast<intptr_t>(slot.Get()); |
| 62 EXPECT_EQ(value, 123); | 66 EXPECT_EQ(value, 123); |
| 63 } | 67 } |
| 64 | 68 |
| 65 TEST(ThreadLocalStorageTest, TLSDestructors) { | 69 TEST(ThreadLocalStorageTest, TLSDestructors) { |
| 66 // Create a TLS index with a destructor. Create a set of | 70 // Create a TLS index with a destructor. Create a set of |
| 67 // threads that set the TLS, while the destructor cleans it up. | 71 // threads that set the TLS, while the destructor cleans it up. |
| 68 // After the threads finish, verify that the value is cleaned up. | 72 // After the threads finish, verify that the value is cleaned up. |
| 69 const int kNumThreads = 5; | 73 const int kNumThreads = 5; |
| 70 int values[kNumThreads]; | 74 int values[kNumThreads]; |
| 71 ThreadLocalStorageRunner* thread_delegates[kNumThreads]; | 75 ThreadLocalStorageRunner* thread_delegates[kNumThreads]; |
| 72 base::DelegateSimpleThread* threads[kNumThreads]; | 76 DelegateSimpleThread* threads[kNumThreads]; |
| 73 | 77 |
| 74 tls_slot.Initialize(ThreadLocalStorageCleanup); | 78 tls_slot.Initialize(ThreadLocalStorageCleanup); |
| 75 | 79 |
| 76 // Spawn the threads. | 80 // Spawn the threads. |
| 77 for (int index = 0; index < kNumThreads; index++) { | 81 for (int index = 0; index < kNumThreads; index++) { |
| 78 values[index] = kInitialTlsValue; | 82 values[index] = kInitialTlsValue; |
| 79 thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]); | 83 thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]); |
| 80 threads[index] = new base::DelegateSimpleThread(thread_delegates[index], | 84 threads[index] = new DelegateSimpleThread(thread_delegates[index], |
| 81 "tls thread"); | 85 "tls thread"); |
| 82 threads[index]->Start(); | 86 threads[index]->Start(); |
| 83 } | 87 } |
| 84 | 88 |
| 85 // Wait for the threads to finish. | 89 // Wait for the threads to finish. |
| 86 for (int index = 0; index < kNumThreads; index++) { | 90 for (int index = 0; index < kNumThreads; index++) { |
| 87 threads[index]->Join(); | 91 threads[index]->Join(); |
| 88 delete threads[index]; | 92 delete threads[index]; |
| 89 delete thread_delegates[index]; | 93 delete thread_delegates[index]; |
| 90 | 94 |
| 91 // Verify that the destructor was called and that we reset. | 95 // Verify that the destructor was called and that we reset. |
| 92 EXPECT_EQ(values[index], kInitialTlsValue); | 96 EXPECT_EQ(values[index], kInitialTlsValue); |
| 93 } | 97 } |
| 94 } | 98 } |
| 99 |
| 100 } // namespace base |
| OLD | NEW |