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

Unified Diff: content/renderer/device_orientation/shared_memory_seqlock_reader.h

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: fixed Darin's comments, added generic reader + rebased 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/device_orientation/shared_memory_seqlock_reader.h
diff --git a/content/renderer/device_orientation/shared_memory_seqlock_reader.h b/content/renderer/device_orientation/shared_memory_seqlock_reader.h
new file mode 100644
index 0000000000000000000000000000000000000000..1725666093ce602f7c076b8bc5c5e279fdd5bf5e
--- /dev/null
+++ b/content/renderer/device_orientation/shared_memory_seqlock_reader.h
@@ -0,0 +1,79 @@
+// 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.
+
+#ifndef CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
+#define CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
darin (slow to review) 2013/06/17 20:23:44 I'm confused to see this file in the device_orient
timvolodine 2013/06/18 16:44:31 Done.
+
+#include "base/shared_memory.h"
+#include "base/memory/scoped_ptr.h"
+#include "content/common/shared_memory_seqlock_buffer.h"
+
+namespace content {
+
+// Template argument Data should be a pod-like structure only containing
+// data fields, such that it is copyable by memcpy method.
+template<typename Data>
+class SharedMemorySeqLockReader {
+ public:
+ SharedMemorySeqLockReader() : buffer_(0) { }
+ virtual ~SharedMemorySeqLockReader() { }
darin (slow to review) 2013/06/17 20:23:44 why the virtual functions?
timvolodine 2013/06/18 16:44:31 in the refactored version there is the base class
+
+ virtual bool GetLatestDeviceMotionData(Data* data) {
darin (slow to review) 2013/06/17 20:23:44 nit: maybe this function should be named GetLatest
timvolodine 2013/06/18 16:44:31 Done.
+ Data read_into;
+
+ if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
darin (slow to review) 2013/06/17 20:23:44 It seems like the guts of this function does not n
timvolodine 2013/06/18 16:44:31 Actually the guts also needs the buffer variable b
+ return false;
+
+ // Only try to read this many times before failing to avoid waiting here
+ // very long in case of contention with the writer.
+ bool is_ready_for_read = false;
+ int contention_count = -1;
+ base::subtle::Atomic32 version;
+ do {
+ version = buffer_->sequence.ReadBegin();
+ is_ready_for_read = buffer_->is_ready_for_read;
+ memcpy(&read_into, &buffer_->buffer, sizeof(read_into));
+ ++contention_count;
+ if (contention_count == kMaximumContentionCount)
+ break;
+ } while (buffer_->sequence.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(data, &read_into, sizeof(*data));
+ return is_ready_for_read;
+ }
+
+ virtual bool Initialize(base::SharedMemoryHandle shared_memory_handle) {
+ renderer_shared_memory_handle_ = shared_memory_handle;
+ if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
+ return false;
+ renderer_shared_memory_.reset(new base::SharedMemory(
+ renderer_shared_memory_handle_, true));
+
+ if (renderer_shared_memory_->Map(sizeof(SharedMemorySeqLockBuffer<Data>))) {
+ if (void *memory = renderer_shared_memory_->memory()) {
+ buffer_ = static_cast<SharedMemorySeqLockBuffer<Data>*>(memory);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private:
+ static const int kMaximumContentionCount = 10;
+ base::SharedMemoryHandle renderer_shared_memory_handle_;
+ scoped_ptr<base::SharedMemory> renderer_shared_memory_;
+ SharedMemorySeqLockBuffer<Data>* buffer_;
+};
darin (slow to review) 2013/06/17 20:23:44 nit: probably want to add DISALLOW_COPY_AND_ASSIGN
timvolodine 2013/06/18 16:44:31 Done.
+
+} // namespace content
+
+#endif // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_

Powered by Google App Engine
This is Rietveld 408576698