diff options
author | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 00:58:39 +0000 |
---|---|---|
committer | jrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 00:58:39 +0000 |
commit | 9e51af9018550b6b23802f66469310f5d1790ab9 (patch) | |
tree | 9ab6f3e0532f6d23c2af1d0acc5605ad00876418 /base/shared_memory_unittest.cc | |
parent | 4c7ca4b8c2ceb6f823474744f035672c8501d23b (diff) | |
download | chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.zip chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.tar.gz chromium_src-9e51af9018550b6b23802f66469310f5d1790ab9.tar.bz2 |
Properly honor base::SharedMemory semantics for name="" to mean
new/private shared memory on POSIX. Transition base::SharedMemory
implementation to file/mmap() to prevent leaking of wired kernel
resources and allow easier cleanup. Enable one more shared_memory
unit test for POSIX. Enable stats_table_unittest.cc for Mac, and
modify it so it cleans up.
Review URL: http://codereview.chromium.org/19724
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9114 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/shared_memory_unittest.cc')
-rw-r--r-- | base/shared_memory_unittest.cc | 84 |
1 files changed, 80 insertions, 4 deletions
diff --git a/base/shared_memory_unittest.cc b/base/shared_memory_unittest.cc index 0c3e464..bdb6c86 100644 --- a/base/shared_memory_unittest.cc +++ b/base/shared_memory_unittest.cc @@ -4,6 +4,7 @@ #include "base/basictypes.h" #include "base/platform_thread.h" +#include "base/scoped_nsautorelease_pool.h" #include "base/shared_memory.h" #include "testing/gtest/include/gtest/gtest.h" @@ -21,12 +22,17 @@ class MultipleThreadMain : public PlatformThread::Delegate { explicit MultipleThreadMain(int16 id) : id_(id) {} ~MultipleThreadMain() {} + static void CleanUp() { + SharedMemory memory; + memory.Delete(test_name_); + } + // PlatformThread::Delegate interface. void ThreadMain() { + ScopedNSAutoreleasePool pool; // noop if not OSX const int kDataSize = 1024; - std::wstring test_name = L"SharedMemoryOpenThreadTest"; SharedMemory memory; - bool rv = memory.Create(test_name, false, true, kDataSize); + bool rv = memory.Create(test_name_, false, true, kDataSize); EXPECT_TRUE(rv); rv = memory.Map(kDataSize); EXPECT_TRUE(rv); @@ -45,9 +51,16 @@ class MultipleThreadMain : public PlatformThread::Delegate { private: int16 id_; + static const std::wstring test_name_; + DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); }; +const std::wstring MultipleThreadMain::test_name_ = L"SharedMemoryOpenThreadTest"; + +// TODO(port): +// This test requires the ability to pass file descriptors between processes. +// We haven't done that yet in Chrome for POSIX. #if defined(OS_WIN) // Each thread will open the shared memory. Each thread will take the memory, // and keep changing it while trying to lock it, with some small pauses in @@ -104,7 +117,11 @@ TEST(SharedMemoryTest, OpenClose) { // Open two handles to a memory segment, confirm that they are mapped // separately yet point to the same space. SharedMemory memory1; - bool rv = memory1.Open(test_name, false); + bool rv = memory1.Delete(test_name); + EXPECT_TRUE(rv); + rv = memory1.Delete(test_name); + EXPECT_TRUE(rv); + rv = memory1.Open(test_name, false); EXPECT_FALSE(rv); rv = memory1.Create(test_name, false, false, kDataSize); EXPECT_TRUE(rv); @@ -134,12 +151,17 @@ TEST(SharedMemoryTest, OpenClose) { // Close the second memory segment. memory2.Close(); + + rv = memory1.Delete(test_name); + EXPECT_TRUE(rv); + rv = memory2.Delete(test_name); + EXPECT_TRUE(rv); } -#if defined(OS_WIN) // Create a set of 5 threads to each open a shared memory segment and write to // it. Verify that they are always reading/writing consistent data. TEST(SharedMemoryTest, MultipleThreads) { + MultipleThreadMain::CleanUp(); PlatformThreadHandle thread_handles[kNumThreads]; MultipleThreadMain* thread_delegates[kNumThreads]; @@ -156,8 +178,15 @@ TEST(SharedMemoryTest, MultipleThreads) { PlatformThread::Join(thread_handles[index]); delete thread_delegates[index]; } + + MultipleThreadMain::CleanUp(); } +// TODO(port): this test requires the MultipleLockThread class +// (defined above), which requires the ability to pass file +// descriptors between processes. We haven't done that yet in Chrome +// for POSIX. +#if defined(OS_WIN) // Create a set of threads to each open a shared memory segment and write to it // with the lock held. Verify that they are always reading/writing consistent // data. @@ -181,4 +210,51 @@ TEST(SharedMemoryTest, Lock) { } #endif +// Allocate private (unique) shared memory with an empty string for a +// name. Make sure several of them don't point to the same thing as +// we might expect if the names are equal. +TEST(SharedMemoryTest, AnonymousPrivate) { + int i, j; + int count = 4; + bool rv; + const int kDataSize = 8192; + + SharedMemory* memories = new SharedMemory[count]; + int **pointers = new int*[count]; + ASSERT_TRUE(memories); + ASSERT_TRUE(pointers); + + for (i = 0; i < count; i++) { + rv = memories[i].Create(L"", false, true, kDataSize); + EXPECT_TRUE(rv); + rv = memories[i].Map(kDataSize); + EXPECT_TRUE(rv); + int *ptr = static_cast<int*>(memories[i].memory()); + EXPECT_TRUE(ptr); + pointers[i] = ptr; + } + + for (i = 0; i < count; i++) { + // zero out the first int in each except for i; for that one, make it 100. + for (j = 0; j < count; j++) { + if (i == j) + pointers[j][0] = 100; + else + pointers[j][0] = 0; + } + // make sure there is no bleeding of the 100 into the other pointers + for (j = 0; j < count; j++) { + if (i == j) + EXPECT_EQ(100, pointers[j][0]); + else + EXPECT_EQ(0, pointers[j][0]); + } + } + + for (int i = 0; i < count; i++) { + memories[i].Close(); + } +} + + } // namespace base |