| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 7 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/shared_memory.h" | 7 #include "base/shared_memory.h" |
| 9 #include "base/test/multiprocess_test.h" | 8 #include "base/test/multiprocess_test.h" |
| 10 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 11 #include "base/time.h" | 10 #include "base/time.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/multiprocess_func_list.h" | 12 #include "testing/multiprocess_func_list.h" |
| 14 | 13 |
| 14 #if defined(OS_MACOSX) |
| 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 16 #endif |
| 17 |
| 15 static const int kNumThreads = 5; | 18 static const int kNumThreads = 5; |
| 16 static const int kNumTasks = 5; | 19 static const int kNumTasks = 5; |
| 17 | 20 |
| 18 namespace base { | 21 namespace base { |
| 19 | 22 |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 // Each thread will open the shared memory. Each thread will take a different 4 | 25 // Each thread will open the shared memory. Each thread will take a different 4 |
| 23 // byte int pointer, and keep changing it, with some small pauses in between. | 26 // byte int pointer, and keep changing it, with some small pauses in between. |
| 24 // Verify that each thread's value in the shared memory is always correct. | 27 // Verify that each thread's value in the shared memory is always correct. |
| 25 class MultipleThreadMain : public PlatformThread::Delegate { | 28 class MultipleThreadMain : public PlatformThread::Delegate { |
| 26 public: | 29 public: |
| 27 explicit MultipleThreadMain(int16 id) : id_(id) {} | 30 explicit MultipleThreadMain(int16 id) : id_(id) {} |
| 28 ~MultipleThreadMain() {} | 31 ~MultipleThreadMain() {} |
| 29 | 32 |
| 30 static void CleanUp() { | 33 static void CleanUp() { |
| 31 SharedMemory memory; | 34 SharedMemory memory; |
| 32 memory.Delete(s_test_name_); | 35 memory.Delete(s_test_name_); |
| 33 } | 36 } |
| 34 | 37 |
| 35 // PlatformThread::Delegate interface. | 38 // PlatformThread::Delegate interface. |
| 36 void ThreadMain() { | 39 void ThreadMain() { |
| 37 mac::ScopedNSAutoreleasePool pool; // noop if not OSX | 40 #if defined(OS_MACOSX) |
| 41 mac::ScopedNSAutoreleasePool pool; |
| 42 #endif |
| 38 const uint32 kDataSize = 1024; | 43 const uint32 kDataSize = 1024; |
| 39 SharedMemory memory; | 44 SharedMemory memory; |
| 40 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); | 45 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); |
| 41 EXPECT_TRUE(rv); | 46 EXPECT_TRUE(rv); |
| 42 rv = memory.Map(kDataSize); | 47 rv = memory.Map(kDataSize); |
| 43 EXPECT_TRUE(rv); | 48 EXPECT_TRUE(rv); |
| 44 int *ptr = static_cast<int*>(memory.memory()) + id_; | 49 int *ptr = static_cast<int*>(memory.memory()) + id_; |
| 45 EXPECT_EQ(0, *ptr); | 50 EXPECT_EQ(0, *ptr); |
| 46 | 51 |
| 47 for (int idx = 0; idx < 100; idx++) { | 52 for (int idx = 0; idx < 100; idx++) { |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 class SharedMemoryProcessTest : public MultiProcessTest { | 337 class SharedMemoryProcessTest : public MultiProcessTest { |
| 333 public: | 338 public: |
| 334 | 339 |
| 335 static void CleanUp() { | 340 static void CleanUp() { |
| 336 SharedMemory memory; | 341 SharedMemory memory; |
| 337 memory.Delete(s_test_name_); | 342 memory.Delete(s_test_name_); |
| 338 } | 343 } |
| 339 | 344 |
| 340 static int TaskTestMain() { | 345 static int TaskTestMain() { |
| 341 int errors = 0; | 346 int errors = 0; |
| 342 mac::ScopedNSAutoreleasePool pool; // noop if not OSX | 347 #if defined(OS_MACOSX) |
| 348 mac::ScopedNSAutoreleasePool pool; |
| 349 #endif |
| 343 const uint32 kDataSize = 1024; | 350 const uint32 kDataSize = 1024; |
| 344 SharedMemory memory; | 351 SharedMemory memory; |
| 345 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); | 352 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); |
| 346 EXPECT_TRUE(rv); | 353 EXPECT_TRUE(rv); |
| 347 if (rv != true) | 354 if (rv != true) |
| 348 errors++; | 355 errors++; |
| 349 rv = memory.Map(kDataSize); | 356 rv = memory.Map(kDataSize); |
| 350 EXPECT_TRUE(rv); | 357 EXPECT_TRUE(rv); |
| 351 if (rv != true) | 358 if (rv != true) |
| 352 errors++; | 359 errors++; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 } | 402 } |
| 396 | 403 |
| 397 SharedMemoryProcessTest::CleanUp(); | 404 SharedMemoryProcessTest::CleanUp(); |
| 398 } | 405 } |
| 399 | 406 |
| 400 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 407 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 401 return SharedMemoryProcessTest::TaskTestMain(); | 408 return SharedMemoryProcessTest::TaskTestMain(); |
| 402 } | 409 } |
| 403 | 410 |
| 404 } // namespace base | 411 } // namespace base |
| OLD | NEW |