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" | 6 #include "base/mac/scoped_nsautorelease_pool.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/shared_memory.h" | 8 #include "base/shared_memory.h" |
9 #include "base/test/multiprocess_test.h" | 9 #include "base/test/multiprocess_test.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 // PlatformThread::Delegate interface. | 35 // PlatformThread::Delegate interface. |
36 void ThreadMain() { | 36 void ThreadMain() { |
37 mac::ScopedNSAutoreleasePool pool; // noop if not OSX | 37 mac::ScopedNSAutoreleasePool pool; // noop if not OSX |
38 const uint32 kDataSize = 1024; | 38 const uint32 kDataSize = 1024; |
39 SharedMemory memory; | 39 SharedMemory memory; |
40 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); | 40 bool rv = memory.CreateNamed(s_test_name_, true, kDataSize); |
41 EXPECT_TRUE(rv); | 41 EXPECT_TRUE(rv); |
42 rv = memory.Map(kDataSize); | 42 rv = memory.Map(kDataSize); |
43 EXPECT_TRUE(rv); | 43 EXPECT_TRUE(rv); |
44 int *ptr = static_cast<int*>(memory.memory()) + id_; | 44 int *ptr = static_cast<int*>(memory.memory()) + id_; |
45 EXPECT_EQ(*ptr, 0); | 45 EXPECT_EQ(0, *ptr); |
46 | 46 |
47 for (int idx = 0; idx < 100; idx++) { | 47 for (int idx = 0; idx < 100; idx++) { |
48 *ptr = idx; | 48 *ptr = idx; |
49 PlatformThread::Sleep(1); // Short wait. | 49 PlatformThread::Sleep(1); // Short wait. |
50 EXPECT_EQ(*ptr, idx); | 50 EXPECT_EQ(*ptr, idx); |
51 } | 51 } |
| 52 // Reset back to 0 for the next test that uses the same name. |
| 53 *ptr = 0; |
52 | 54 |
53 memory.Close(); | 55 memory.Close(); |
54 } | 56 } |
55 | 57 |
56 private: | 58 private: |
57 int16 id_; | 59 int16 id_; |
58 | 60 |
59 static const char* const s_test_name_; | 61 static const char* const s_test_name_; |
60 | 62 |
61 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); | 63 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 TEST(SharedMemoryTest, MultipleThreads) { | 223 TEST(SharedMemoryTest, MultipleThreads) { |
222 MultipleThreadMain::CleanUp(); | 224 MultipleThreadMain::CleanUp(); |
223 // On POSIX we have a problem when 2 threads try to create the shmem | 225 // On POSIX we have a problem when 2 threads try to create the shmem |
224 // (a file) at exactly the same time, since create both creates the | 226 // (a file) at exactly the same time, since create both creates the |
225 // file and zerofills it. We solve the problem for this unit test | 227 // file and zerofills it. We solve the problem for this unit test |
226 // (make it not flaky) by starting with 1 thread, then | 228 // (make it not flaky) by starting with 1 thread, then |
227 // intentionally don't clean up its shmem before running with | 229 // intentionally don't clean up its shmem before running with |
228 // kNumThreads. | 230 // kNumThreads. |
229 | 231 |
230 int threadcounts[] = { 1, kNumThreads }; | 232 int threadcounts[] = { 1, kNumThreads }; |
231 for (size_t i = 0; i < sizeof(threadcounts) / sizeof(threadcounts); i++) { | 233 for (size_t i = 0; i < arraysize(threadcounts); i++) { |
232 int numthreads = threadcounts[i]; | 234 int numthreads = threadcounts[i]; |
233 scoped_array<PlatformThreadHandle> thread_handles; | 235 scoped_array<PlatformThreadHandle> thread_handles; |
234 scoped_array<MultipleThreadMain*> thread_delegates; | 236 scoped_array<MultipleThreadMain*> thread_delegates; |
235 | 237 |
236 thread_handles.reset(new PlatformThreadHandle[numthreads]); | 238 thread_handles.reset(new PlatformThreadHandle[numthreads]); |
237 thread_delegates.reset(new MultipleThreadMain*[numthreads]); | 239 thread_delegates.reset(new MultipleThreadMain*[numthreads]); |
238 | 240 |
239 // Spawn the threads. | 241 // Spawn the threads. |
240 for (int16 index = 0; index < numthreads; index++) { | 242 for (int16 index = 0; index < numthreads; index++) { |
241 PlatformThreadHandle pth; | 243 PlatformThreadHandle pth; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } | 395 } |
394 | 396 |
395 SharedMemoryProcessTest::CleanUp(); | 397 SharedMemoryProcessTest::CleanUp(); |
396 } | 398 } |
397 | 399 |
398 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 400 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
399 return SharedMemoryProcessTest::TaskTestMain(); | 401 return SharedMemoryProcessTest::TaskTestMain(); |
400 } | 402 } |
401 | 403 |
402 } // namespace base | 404 } // namespace base |
OLD | NEW |