OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "shared_memory_seqlock_reader.h" | |
6 | |
7 namespace internal { | |
8 | |
9 content::SharedMemorySeqLockBufferBase* | |
10 SharedMemorySeqLockReaderBase::InitializeSharedMemory( | |
11 base::SharedMemoryHandle shared_memory_handle, size_t buffer_size) { | |
12 renderer_shared_memory_handle_ = shared_memory_handle; | |
13 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | |
14 return 0; | |
15 renderer_shared_memory_.reset(new base::SharedMemory( | |
16 renderer_shared_memory_handle_, true)); | |
17 if (renderer_shared_memory_->Map(buffer_size)) { | |
18 base_buffer_ = static_cast<content::SharedMemorySeqLockBufferBase*>( | |
19 renderer_shared_memory_->memory()); | |
20 return base_buffer_; | |
21 } | |
22 return 0; | |
23 } | |
24 | |
25 bool SharedMemorySeqLockReaderBase::FetchFromBuffer( | |
darin (slow to review)
2013/06/27 17:33:26
Is there a path to changing GamepadSharedMemoryRea
timvolodine
2013/06/27 21:08:43
Yes, I think this is best done in a separate CL. I
| |
26 void* final, void* temp, void* from, size_t size) { | |
27 | |
28 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | |
29 return false; | |
30 | |
31 // Only try to read this many times before failing to avoid waiting here | |
32 // very long in case of contention with the writer. | |
33 int contention_count = -1; | |
34 base::subtle::Atomic32 version; | |
35 do { | |
36 version = base_buffer_->seqlock.ReadBegin(); | |
37 memcpy(temp, from, size); | |
38 ++contention_count; | |
39 if (contention_count == kMaximumContentionCount) | |
40 break; | |
41 } while (base_buffer_->seqlock.ReadRetry(version)); | |
42 | |
43 if (contention_count >= kMaximumContentionCount) { | |
44 // We failed to successfully read, presumably because the hardware | |
45 // thread was taking unusually long. Don't copy the data to the output | |
46 // buffer, and simply leave what was there before. | |
47 return false; | |
48 } | |
49 | |
50 // New data was read successfully, copy it into the output buffer. | |
51 memcpy(final, temp, size); | |
52 return true; | |
53 } | |
54 | |
55 } // namespace internal | |
OLD | NEW |