0

Port the thread local storage unittest to Mac and linux.

Review URL: http://codereview.chromium.org/1963

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2101 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
paulg@google.com
2008-09-11 23:37:54 +00:00
parent 3ecfa2f79b
commit fe236c9406
3 changed files with 57 additions and 44 deletions

@ -258,6 +258,7 @@ test_files = [
'string_tokenizer_unittest.cc',
'string_util_unittest.cc',
'thread_local_unittest.cc',
'thread_local_storage_unittest.cc',
'thread_unittest.cc',
'time_unittest.cc',
'timer_unittest.cc',
@ -286,7 +287,6 @@ if env['PLATFORM'] == 'win32':
'run_all_unittests.cc',
'shared_event_unittest.cc',
'stats_table_unittest.cc',
'thread_local_storage_unittest.cc',
'watchdog_unittest.cc',
'gfx/native_theme_unittest.cc',
'gfx/uniscribe_unittest.cc',

@ -152,6 +152,7 @@
ABF4B9BE0DC2BD1500A6E319 /* sha512.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403700D92D2840006B936 /* sha512.cc */; };
ABF4B9C30DC2BD6C00A6E319 /* values.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403880D92D2CF0006B936 /* values.cc */; };
ABFBD3E60DC793C600E164CB /* md5.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403290D92D2090006B936 /* md5.cc */; };
BA0F69870E79D7980079A8A1 /* thread_local_storage_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */; };
BA5CC5840E788093004EDD45 /* shared_memory_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */; };
BA739A020E5E3242009842A7 /* tracked_objects_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA739A000E5E3242009842A7 /* tracked_objects_unittest.cc */; };
BA739A030E5E3242009842A7 /* timer_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA739A010E5E3242009842A7 /* timer_unittest.cc */; };
@ -570,6 +571,7 @@
ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = base_paths_mac.mm; sourceTree = "<group>"; };
ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard_mac.mm; sourceTree = "<group>"; };
ABF4B9B40DC2BC9F00A6E319 /* path_service.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = path_service.cc; sourceTree = "<group>"; };
BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_local_storage_unittest.cc; sourceTree = "<group>"; };
BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shared_memory_unittest.cc; sourceTree = "<group>"; };
BA739A000E5E3242009842A7 /* tracked_objects_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tracked_objects_unittest.cc; sourceTree = "<group>"; };
BA739A010E5E3242009842A7 /* timer_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = timer_unittest.cc; sourceTree = "<group>"; };
@ -738,6 +740,7 @@
825402B60D92D0E20006B936 /* base */ = {
isa = PBXGroup;
children = (
BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */,
825403B40D92D2EC0006B936 /* gfx */,
E49115EC0E47B461001EE8C3 /* at_exit.cc */,
E49115EB0E47B461001EE8C3 /* at_exit.h */,
@ -1358,6 +1361,7 @@
7B78D3A00E54FE0100609465 /* string_tokenizer_unittest.cc in Sources */,
7B78D3A10E54FE0100609465 /* string_util_unittest.cc in Sources */,
7BAE38AF0E6EFDC300C3F750 /* thread_local_unittest.cc in Sources */,
BA0F69870E79D7980079A8A1 /* thread_local_storage_unittest.cc in Sources */,
93E7031B0E5D64390046259B /* thread_unittest.cc in Sources */,
7B78D3A20E54FE0100609465 /* time_unittest.cc in Sources */,
BA739A030E5E3242009842A7 /* timer_unittest.cc in Sources */,

@ -2,19 +2,56 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if defined(OS_WIN)
#include <windows.h>
#include <process.h>
#endif
#include "base/simple_thread.h"
#include "base/thread_local_storage.h"
#include "testing/gtest/include/gtest/gtest.h"
// ignore warnings about ptr->int conversions that we use when
#if defined(OS_WIN)
// Ignore warnings about ptr->int conversions that we use when
// storing ints into ThreadLocalStorage.
#pragma warning(disable : 4311 4312)
#endif
namespace {
class ThreadLocalStorageTest : public testing::Test {
};
const int kInitialTlsValue = 0x5555;
static ThreadLocalStorage::Slot tls_slot(base::LINKER_INITIALIZED);
class ThreadLocalStorageRunner : public base::DelegateSimpleThread::Delegate {
public:
explicit ThreadLocalStorageRunner(int* tls_value_ptr)
: tls_value_ptr_(tls_value_ptr) {}
virtual ~ThreadLocalStorageRunner() {}
virtual void Run() {
*tls_value_ptr_ = kInitialTlsValue;
tls_slot.Set(tls_value_ptr_);
int *ptr = static_cast<int*>(tls_slot.Get());
EXPECT_EQ(ptr, tls_value_ptr_);
EXPECT_EQ(*ptr, kInitialTlsValue);
*tls_value_ptr_ = 0;
ptr = static_cast<int*>(tls_slot.Get());
EXPECT_EQ(ptr, tls_value_ptr_);
EXPECT_EQ(*ptr, 0);
}
private:
int* tls_value_ptr_;
DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner);
};
void ThreadLocalStorageCleanup(void *value) {
int *ptr = reinterpret_cast<int*>(value);
if (ptr)
*ptr = kInitialTlsValue;
}
@ -25,61 +62,33 @@ TEST(ThreadLocalStorageTest, Basics) {
EXPECT_EQ(value, 123);
}
const int kInitialTlsValue = 0x5555;
static ThreadLocalStorage::Slot tls_slot(base::LINKER_INITIALIZED);
unsigned __stdcall TLSTestThreadMain(void* param) {
// param contains the thread local storage index.
int *index = reinterpret_cast<int*>(param);
*index = kInitialTlsValue;
tls_slot.Set(index);
int *ptr = static_cast<int*>(tls_slot.Get());
EXPECT_EQ(ptr, index);
EXPECT_EQ(*ptr, kInitialTlsValue);
*index = 0;
ptr = static_cast<int*>(tls_slot.Get());
EXPECT_EQ(ptr, index);
EXPECT_EQ(*ptr, 0);
return 0;
}
void ThreadLocalStorageCleanup(void *value) {
int *ptr = reinterpret_cast<int*>(value);
if (ptr)
*ptr = kInitialTlsValue;
}
TEST(ThreadLocalStorageTest, TLSDestructors) {
// Create a TLS index with a destructor. Create a set of
// threads that set the TLS, while the destructor cleans it up.
// After the threads finish, verify that the value is cleaned up.
const int kNumThreads = 5;
HANDLE threads[kNumThreads];
int values[kNumThreads];
ThreadLocalStorageRunner* thread_delegates[kNumThreads];
base::DelegateSimpleThread* threads[kNumThreads];
tls_slot.Initialize(ThreadLocalStorageCleanup);
// Spawn the threads.
for (int16 index = 0; index < kNumThreads; index++) {
for (int index = 0; index < kNumThreads; index++) {
values[index] = kInitialTlsValue;
void *argument = static_cast<void*>(&(values[index]));
unsigned thread_id;
threads[index] = reinterpret_cast<HANDLE>(
_beginthreadex(NULL, 0, TLSTestThreadMain, argument, 0, &thread_id));
EXPECT_NE(threads[index], (HANDLE)NULL);
thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]);
threads[index] = new base::DelegateSimpleThread(thread_delegates[index],
"tls thread");
threads[index]->Start();
}
// Wait for the threads to finish.
for (int index = 0; index < kNumThreads; index++) {
DWORD rv = WaitForSingleObject(threads[index], 60*1000);
EXPECT_EQ(rv, WAIT_OBJECT_0); // verify all threads finished
threads[index]->Join();
delete threads[index];
delete thread_delegates[index];
// verify that the destructor was called and that we reset.
// Verify that the destructor was called and that we reset.
EXPECT_EQ(values[index], kInitialTlsValue);
}
}