OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/shared_memory_seqlock_reader.h" | 5 #include "content/renderer/shared_memory_seqlock_reader.h" |
6 | 6 |
7 namespace content { | 7 namespace content { |
8 namespace internal { | 8 namespace internal { |
9 | 9 |
10 SharedMemorySeqLockReaderBase::SharedMemorySeqLockReaderBase() { } | 10 SharedMemorySeqLockReaderBase::SharedMemorySeqLockReaderBase() { } |
11 | 11 |
12 SharedMemorySeqLockReaderBase::~SharedMemorySeqLockReaderBase() { } | 12 SharedMemorySeqLockReaderBase::~SharedMemorySeqLockReaderBase() { } |
13 | 13 |
14 void* | 14 void* |
15 SharedMemorySeqLockReaderBase::InitializeSharedMemory( | 15 SharedMemorySeqLockReaderBase::InitializeSharedMemory( |
16 base::SharedMemoryHandle shared_memory_handle, size_t buffer_size) { | 16 base::SharedMemoryHandle shared_memory_handle, size_t buffer_size) { |
17 renderer_shared_memory_handle_ = shared_memory_handle; | 17 renderer_shared_memory_handle_ = shared_memory_handle; |
18 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 18 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
19 return 0; | 19 return 0; |
20 renderer_shared_memory_.reset(new base::SharedMemory( | 20 renderer_shared_memory_.reset(new base::SharedMemory( |
21 renderer_shared_memory_handle_, true)); | 21 renderer_shared_memory_handle_, true)); |
22 | 22 |
23 return (renderer_shared_memory_->Map(buffer_size)) | 23 return (renderer_shared_memory_->Map(buffer_size)) |
24 ? renderer_shared_memory_->memory() | 24 ? renderer_shared_memory_->memory() |
25 : 0; | 25 : 0; |
26 } | 26 } |
27 | 27 |
28 bool SharedMemorySeqLockReaderBase::FetchFromBuffer( | 28 bool SharedMemorySeqLockReaderBase::FetchFromBuffer( |
29 content::OneWriterSeqLock* seqlock, void* final, void* temp, void* from, | 29 device::OneWriterSeqLock* seqlock, |
| 30 void* final, |
| 31 void* temp, |
| 32 void* from, |
30 size_t size) { | 33 size_t size) { |
31 | |
32 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 34 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
33 return false; | 35 return false; |
34 | 36 |
35 // Only try to read this many times before failing to avoid waiting here | 37 // Only try to read this many times before failing to avoid waiting here |
36 // very long in case of contention with the writer. | 38 // very long in case of contention with the writer. |
37 int contention_count = -1; | 39 int contention_count = -1; |
38 base::subtle::Atomic32 version; | 40 base::subtle::Atomic32 version; |
39 do { | 41 do { |
40 version = seqlock->ReadBegin(); | 42 version = seqlock->ReadBegin(); |
41 memcpy(temp, from, size); | 43 memcpy(temp, from, size); |
42 ++contention_count; | 44 ++contention_count; |
43 if (contention_count == kMaximumContentionCount) | 45 if (contention_count == kMaximumContentionCount) |
44 break; | 46 break; |
45 } while (seqlock->ReadRetry(version)); | 47 } while (seqlock->ReadRetry(version)); |
46 | 48 |
47 if (contention_count >= kMaximumContentionCount) { | 49 if (contention_count >= kMaximumContentionCount) { |
48 // We failed to successfully read, presumably because the hardware | 50 // We failed to successfully read, presumably because the hardware |
49 // thread was taking unusually long. Don't copy the data to the output | 51 // thread was taking unusually long. Don't copy the data to the output |
50 // buffer, and simply leave what was there before. | 52 // buffer, and simply leave what was there before. |
51 return false; | 53 return false; |
52 } | 54 } |
53 | 55 |
54 // New data was read successfully, copy it into the output buffer. | 56 // New data was read successfully, copy it into the output buffer. |
55 memcpy(final, temp, size); | 57 memcpy(final, temp, size); |
56 return true; | 58 return true; |
57 } | 59 } |
58 | 60 |
59 } // namespace internal | 61 } // namespace internal |
60 } // namespace content | 62 } // namespace content |
OLD | NEW |