Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: content/renderer/shared_memory_seqlock_reader_base.cc

Issue 14678012: Implement the content/renderer and content/browser part of the Device Motion API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: factored out as much code as possible from the templated reader Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_base.h"
6
7 namespace content {
8
9 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<SharedMemorySeqLockBufferBase*>(
19 renderer_shared_memory_->memory());
20 return base_buffer_;
21 }
22 return 0;
23 }
24
25 bool SharedMemorySeqLockReaderBase::FetchFromBuffer(
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 bool is_ready = false;
34 int contention_count = -1;
35 base::subtle::Atomic32 version;
36 do {
37 version = base_buffer_->seqlock.ReadBegin();
38 is_ready = base_buffer_->is_ready_for_read;
39 memcpy(temp, from, size);
40 ++contention_count;
41 if (contention_count == kMaximumContentionCount)
42 break;
43 } while (base_buffer_->seqlock.ReadRetry(version));
44
45 if (contention_count >= kMaximumContentionCount) {
46 // We failed to successfully read, presumably because the hardware
47 // thread was taking unusually long. Don't copy the data to the output
48 // buffer, and simply leave what was there before.
49 return false;
50 }
51
52 // New data was read successfully, copy it into the output buffer.
53 memcpy(final, temp, size);
54 return is_ready;
55 }
56
57 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698