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

Unified Diff: content/renderer/shared_memory_seqlock_reader.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: fix compilation: peer_handle -> PeerHandle() Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/shared_memory_seqlock_reader.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/shared_memory_seqlock_reader.cc
diff --git a/content/renderer/shared_memory_seqlock_reader.cc b/content/renderer/shared_memory_seqlock_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..84dc91c13daf2b37852cc621e1a3c8028a1e4620
--- /dev/null
+++ b/content/renderer/shared_memory_seqlock_reader.cc
@@ -0,0 +1,58 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "shared_memory_seqlock_reader.h"
+
+namespace internal {
+
+SharedMemorySeqLockReaderBase::SharedMemorySeqLockReaderBase() { }
+
+SharedMemorySeqLockReaderBase::~SharedMemorySeqLockReaderBase() { }
+
+void*
+SharedMemorySeqLockReaderBase::InitializeSharedMemory(
+ base::SharedMemoryHandle shared_memory_handle, size_t buffer_size) {
+ renderer_shared_memory_handle_ = shared_memory_handle;
+ if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
+ return 0;
+ renderer_shared_memory_.reset(new base::SharedMemory(
+ renderer_shared_memory_handle_, true));
+
+ return (renderer_shared_memory_->Map(buffer_size))
+ ? renderer_shared_memory_->memory()
+ : 0;
+}
+
+bool SharedMemorySeqLockReaderBase::FetchFromBuffer(
+ content::OneWriterSeqLock* seqlock, void* final, void* temp, void* from,
+ size_t size) {
+
+ if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
+ return false;
+
+ // Only try to read this many times before failing to avoid waiting here
+ // very long in case of contention with the writer.
+ int contention_count = -1;
+ base::subtle::Atomic32 version;
+ do {
+ version = seqlock->ReadBegin();
+ memcpy(temp, from, size);
+ ++contention_count;
+ if (contention_count == kMaximumContentionCount)
+ break;
+ } while (seqlock->ReadRetry(version));
+
+ if (contention_count >= kMaximumContentionCount) {
+ // We failed to successfully read, presumably because the hardware
+ // thread was taking unusually long. Don't copy the data to the output
+ // buffer, and simply leave what was there before.
+ return false;
+ }
+
+ // New data was read successfully, copy it into the output buffer.
+ memcpy(final, temp, size);
+ return true;
+}
+
+} // namespace internal
« no previous file with comments | « content/renderer/shared_memory_seqlock_reader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698