Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/atomicops.h" | |
| 5 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 8 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
| 9 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 12 #include "base/test/multiprocess_test.h" | 13 #include "base/test/multiprocess_test.h" |
| 13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 int16 id_; | 84 int16 id_; |
| 84 | 85 |
| 85 static const char* const s_test_name_; | 86 static const char* const s_test_name_; |
| 86 | 87 |
| 87 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); | 88 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 const char* const MultipleThreadMain::s_test_name_ = | 91 const char* const MultipleThreadMain::s_test_name_ = |
| 91 "SharedMemoryOpenThreadTest"; | 92 "SharedMemoryOpenThreadTest"; |
| 92 | 93 |
| 93 // TODO(port): | |
| 94 // This test requires the ability to pass file descriptors between processes. | |
| 95 // We haven't done that yet in Chrome for POSIX. | |
| 96 #if defined(OS_WIN) | |
| 97 // Each thread will open the shared memory. Each thread will take the memory, | |
| 98 // and keep changing it while trying to lock it, with some small pauses in | |
| 99 // between. Verify that each thread's value in the shared memory is always | |
| 100 // correct. | |
| 101 class MultipleLockThread : public PlatformThread::Delegate { | |
| 102 public: | |
| 103 explicit MultipleLockThread(int id) : id_(id) {} | |
| 104 ~MultipleLockThread() override {} | |
| 105 | |
| 106 // PlatformThread::Delegate interface. | |
| 107 void ThreadMain() override { | |
| 108 const uint32 kDataSize = sizeof(int); | |
| 109 SharedMemoryHandle handle = NULL; | |
| 110 { | |
| 111 SharedMemory memory1; | |
| 112 EXPECT_TRUE(memory1.CreateNamedDeprecated( | |
| 113 "SharedMemoryMultipleLockThreadTest", true, kDataSize)); | |
| 114 EXPECT_TRUE(memory1.ShareToProcess(GetCurrentProcess(), &handle)); | |
| 115 // TODO(paulg): Implement this once we have a posix version of | |
| 116 // SharedMemory::ShareToProcess. | |
| 117 EXPECT_TRUE(true); | |
| 118 } | |
| 119 | |
| 120 SharedMemory memory2(handle, false); | |
| 121 EXPECT_TRUE(memory2.Map(kDataSize)); | |
| 122 volatile int* const ptr = static_cast<int*>(memory2.memory()); | |
| 123 | |
| 124 for (int idx = 0; idx < 20; idx++) { | |
| 125 memory2.LockDeprecated(); | |
| 126 int i = (id_ << 16) + idx; | |
| 127 *ptr = i; | |
| 128 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); | |
| 129 EXPECT_EQ(*ptr, i); | |
| 130 memory2.UnlockDeprecated(); | |
| 131 } | |
| 132 | |
| 133 memory2.Close(); | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 int id_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(MultipleLockThread); | |
| 140 }; | |
| 141 #endif | |
| 142 | |
| 143 } // namespace | 94 } // namespace |
| 144 | 95 |
| 145 // Android doesn't support SharedMemory::Open/Delete/ | 96 // Android doesn't support SharedMemory::Open/Delete/ |
| 146 // CreateNamedDeprecated(openExisting=true) | 97 // CreateNamedDeprecated(openExisting=true) |
| 147 #if !defined(OS_ANDROID) | 98 #if !defined(OS_ANDROID) |
| 148 TEST(SharedMemoryTest, OpenClose) { | 99 TEST(SharedMemoryTest, OpenClose) { |
| 149 const uint32 kDataSize = 1024; | 100 const uint32 kDataSize = 1024; |
| 150 std::string test_name = "SharedMemoryOpenCloseTest"; | 101 std::string test_name = "SharedMemoryOpenCloseTest"; |
| 151 | 102 |
| 152 // Open two handles to a memory segment, confirm that they are mapped | 103 // Open two handles to a memory segment, confirm that they are mapped |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 | 264 |
| 314 // Wait for the threads to finish. | 265 // Wait for the threads to finish. |
| 315 for (int index = 0; index < numthreads; index++) { | 266 for (int index = 0; index < numthreads; index++) { |
| 316 PlatformThread::Join(thread_handles[index]); | 267 PlatformThread::Join(thread_handles[index]); |
| 317 delete thread_delegates[index]; | 268 delete thread_delegates[index]; |
| 318 } | 269 } |
| 319 } | 270 } |
| 320 MultipleThreadMain::CleanUp(); | 271 MultipleThreadMain::CleanUp(); |
| 321 } | 272 } |
| 322 | 273 |
| 323 // TODO(port): this test requires the MultipleLockThread class | |
| 324 // (defined above), which requires the ability to pass file | |
| 325 // descriptors between processes. We haven't done that yet in Chrome | |
| 326 // for POSIX. | |
| 327 #if defined(OS_WIN) | |
| 328 // Create a set of threads to each open a shared memory segment and write to it | |
| 329 // with the lock held. Verify that they are always reading/writing consistent | |
| 330 // data. | |
| 331 TEST(SharedMemoryTest, Lock) { | |
| 332 PlatformThreadHandle thread_handles[kNumThreads]; | |
| 333 MultipleLockThread* thread_delegates[kNumThreads]; | |
| 334 | |
| 335 // Spawn the threads. | |
| 336 for (int index = 0; index < kNumThreads; ++index) { | |
| 337 PlatformThreadHandle pth; | |
| 338 thread_delegates[index] = new MultipleLockThread(index); | |
| 339 EXPECT_TRUE(PlatformThread::Create(0, thread_delegates[index], &pth)); | |
| 340 thread_handles[index] = pth; | |
| 341 } | |
| 342 | |
| 343 // Wait for the threads to finish. | |
| 344 for (int index = 0; index < kNumThreads; ++index) { | |
| 345 PlatformThread::Join(thread_handles[index]); | |
| 346 delete thread_delegates[index]; | |
| 347 } | |
| 348 } | |
| 349 #endif | |
| 350 | |
| 351 // Allocate private (unique) shared memory with an empty string for a | 274 // Allocate private (unique) shared memory with an empty string for a |
| 352 // name. Make sure several of them don't point to the same thing as | 275 // name. Make sure several of them don't point to the same thing as |
| 353 // we might expect if the names are equal. | 276 // we might expect if the names are equal. |
| 354 TEST(SharedMemoryTest, AnonymousPrivate) { | 277 TEST(SharedMemoryTest, AnonymousPrivate) { |
| 355 int i, j; | 278 int i, j; |
| 356 int count = 4; | 279 int count = 4; |
| 357 bool rv; | 280 bool rv; |
| 358 const uint32 kDataSize = 8192; | 281 const uint32 kDataSize = 8192; |
| 359 | 282 |
| 360 scoped_ptr<SharedMemory[]> memories(new SharedMemory[count]); | 283 scoped_ptr<SharedMemory[]> memories(new SharedMemory[count]); |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 static void CleanUp() { | 580 static void CleanUp() { |
| 658 SharedMemory memory; | 581 SharedMemory memory; |
| 659 memory.Delete(s_test_name_); | 582 memory.Delete(s_test_name_); |
| 660 } | 583 } |
| 661 | 584 |
| 662 static int TaskTestMain() { | 585 static int TaskTestMain() { |
| 663 int errors = 0; | 586 int errors = 0; |
| 664 #if defined(OS_MACOSX) | 587 #if defined(OS_MACOSX) |
| 665 mac::ScopedNSAutoreleasePool pool; | 588 mac::ScopedNSAutoreleasePool pool; |
| 666 #endif | 589 #endif |
| 667 const uint32 kDataSize = 1024; | |
| 668 SharedMemory memory; | 590 SharedMemory memory; |
| 669 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, kDataSize); | 591 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, data_size_); |
| 670 EXPECT_TRUE(rv); | 592 EXPECT_TRUE(rv); |
| 671 if (rv != true) | 593 if (rv != true) |
| 672 errors++; | 594 errors++; |
| 673 rv = memory.Map(kDataSize); | 595 rv = memory.Map(data_size_); |
| 674 EXPECT_TRUE(rv); | 596 EXPECT_TRUE(rv); |
| 675 if (rv != true) | 597 if (rv != true) |
| 676 errors++; | 598 errors++; |
| 677 int *ptr = static_cast<int*>(memory.memory()); | 599 int *ptr = static_cast<int*>(memory.memory()); |
| 678 | 600 |
| 679 for (int idx = 0; idx < 20; idx++) { | 601 // Atomically increment the value at *ptr by 1. |
| 680 memory.LockDeprecated(); | 602 while (true) { |
| 681 int i = (1 << 16) + idx; | 603 int j = *ptr; |
| 682 *ptr = i; | 604 int old_value = base::subtle::NoBarrier_CompareAndSwap(ptr, j, j + 1); |
|
Nico
2015/06/11 15:39:18
can you use a lock instead?
erikchen
2015/06/11 17:36:30
This code needs to be atomic across multiple proce
Nico
2015/06/11 17:46:40
Ah, right. Maybe say "// This runs concurrently in
erikchen
2015/06/11 17:55:18
Done.
That's weird. I thought I did use an atomic
| |
| 683 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10)); | 605 if (old_value == j) |
| 684 if (*ptr != i) | 606 break; |
| 685 errors++; | |
| 686 memory.UnlockDeprecated(); | |
| 687 } | 607 } |
| 688 | |
| 689 memory.Close(); | 608 memory.Close(); |
| 690 return errors; | 609 return errors; |
| 691 } | 610 } |
| 692 | 611 |
| 693 private: | |
| 694 static const char* const s_test_name_; | 612 static const char* const s_test_name_; |
| 613 static const uint32 data_size_; | |
|
Nico
2015/06/11 15:39:18
s_data_size for consistency
erikchen
2015/06/11 17:36:30
Done.
| |
| 695 }; | 614 }; |
| 696 | 615 |
| 697 const char* const SharedMemoryProcessTest::s_test_name_ = "MPMem"; | 616 const char* const SharedMemoryProcessTest::s_test_name_ = "MPMem"; |
| 617 const uint32 SharedMemoryProcessTest::data_size_ = 1024; | |
| 698 | 618 |
| 699 TEST_F(SharedMemoryProcessTest, Tasks) { | 619 TEST_F(SharedMemoryProcessTest, SharedMemoryAcrossProcesses) { |
| 700 SharedMemoryProcessTest::CleanUp(); | 620 SharedMemoryProcessTest::CleanUp(); |
| 701 | 621 |
| 622 // Create a shared memory region. Set the first word to 0. | |
| 623 SharedMemory memory; | |
| 624 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, data_size_); | |
| 625 ASSERT_TRUE(rv); | |
| 626 rv = memory.Map(data_size_); | |
| 627 ASSERT_TRUE(rv); | |
| 628 int* ptr = static_cast<int*>(memory.memory()); | |
| 629 *ptr = 0; | |
| 630 | |
| 631 // Start |kNumTasks| processes, each of which atomically increments the first | |
| 632 // word by 1. | |
| 702 Process processes[kNumTasks]; | 633 Process processes[kNumTasks]; |
| 703 for (int index = 0; index < kNumTasks; ++index) { | 634 for (int index = 0; index < kNumTasks; ++index) { |
| 704 processes[index] = SpawnChild("SharedMemoryTestMain"); | 635 processes[index] = SpawnChild("SharedMemoryTestMain"); |
| 705 ASSERT_TRUE(processes[index].IsValid()); | 636 ASSERT_TRUE(processes[index].IsValid()); |
| 706 } | 637 } |
| 707 | 638 |
| 639 // Check that each process exited correctly. | |
| 708 int exit_code = 0; | 640 int exit_code = 0; |
| 709 for (int index = 0; index < kNumTasks; ++index) { | 641 for (int index = 0; index < kNumTasks; ++index) { |
| 710 EXPECT_TRUE(processes[index].WaitForExit(&exit_code)); | 642 EXPECT_TRUE(processes[index].WaitForExit(&exit_code)); |
| 711 EXPECT_EQ(0, exit_code); | 643 EXPECT_EQ(0, exit_code); |
| 712 } | 644 } |
| 713 | 645 |
| 646 // Check that the shared memory region reflects |kNumTasks| increments. | |
| 647 ASSERT_EQ(kNumTasks, *ptr); | |
| 648 | |
| 649 memory.Close(); | |
| 714 SharedMemoryProcessTest::CleanUp(); | 650 SharedMemoryProcessTest::CleanUp(); |
| 715 } | 651 } |
| 716 | 652 |
| 717 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 653 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 718 return SharedMemoryProcessTest::TaskTestMain(); | 654 return SharedMemoryProcessTest::TaskTestMain(); |
| 719 } | 655 } |
| 720 | 656 |
| 721 #endif // !OS_IOS | 657 #endif // !OS_IOS |
| 722 | 658 |
| 723 } // namespace base | 659 } // namespace base |
| OLD | NEW |