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

Side by Side 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 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 #ifndef CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
6 #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.
7
8 #include "base/shared_memory.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/common/shared_memory_seqlock_buffer.h"
11
12 namespace content {
13
14 // Template argument Data should be a pod-like structure only containing
15 // data fields, such that it is copyable by memcpy method.
16 template<typename Data>
17 class SharedMemorySeqLockReader {
18 public:
19 SharedMemorySeqLockReader() : buffer_(0) { }
20 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
21
22 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.
23 Data read_into;
24
25 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
26 return false;
27
28 // Only try to read this many times before failing to avoid waiting here
29 // very long in case of contention with the writer.
30 bool is_ready_for_read = false;
31 int contention_count = -1;
32 base::subtle::Atomic32 version;
33 do {
34 version = buffer_->sequence.ReadBegin();
35 is_ready_for_read = buffer_->is_ready_for_read;
36 memcpy(&read_into, &buffer_->buffer, sizeof(read_into));
37 ++contention_count;
38 if (contention_count == kMaximumContentionCount)
39 break;
40 } while (buffer_->sequence.ReadRetry(version));
41
42 if (contention_count >= kMaximumContentionCount) {
43 // We failed to successfully read, presumably because the hardware
44 // thread was taking unusually long. Don't copy the data to the output
45 // buffer, and simply leave what was there before.
46 return false;
47 }
48
49 // New data was read successfully, copy it into the output buffer.
50 memcpy(data, &read_into, sizeof(*data));
51 return is_ready_for_read;
52 }
53
54 virtual bool Initialize(base::SharedMemoryHandle shared_memory_handle) {
55 renderer_shared_memory_handle_ = shared_memory_handle;
56 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
57 return false;
58 renderer_shared_memory_.reset(new base::SharedMemory(
59 renderer_shared_memory_handle_, true));
60
61 if (renderer_shared_memory_->Map(sizeof(SharedMemorySeqLockBuffer<Data>))) {
62 if (void *memory = renderer_shared_memory_->memory()) {
63 buffer_ = static_cast<SharedMemorySeqLockBuffer<Data>*>(memory);
64 return true;
65 }
66 }
67 return false;
68 }
69
70 private:
71 static const int kMaximumContentionCount = 10;
72 base::SharedMemoryHandle renderer_shared_memory_handle_;
73 scoped_ptr<base::SharedMemory> renderer_shared_memory_;
74 SharedMemorySeqLockBuffer<Data>* buffer_;
75 };
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.
76
77 } // namespace content
78
79 #endif // CONTENT_RENDERER_SHARED_MEMORY_SEQLOCK_READER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698