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

Side by Side Diff: content/common/gamepad_seqlock.h

Issue 10785024: Revert 146792 - Lock-free GamepadSeqLock (try 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/gamepad_hardware_buffer.h ('k') | content/common/gamepad_seqlock.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ 5 #ifndef CONTENT_COMMON_GAMEPAD_SEQLOCK_H_
6 #define CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ 6 #define CONTENT_COMMON_GAMEPAD_SEQLOCK_H_
7 7
8 #include "base/atomicops.h" 8 #include "base/atomicops.h"
9 #include "base/basictypes.h" 9 #include "base/threading/platform_thread.h"
10 #include "content/common/content_export.h" 10 #include "content/common/content_export.h"
11 11
12 namespace content { 12 namespace content {
13 13
14 namespace internal { 14 // This SeqLock handles only *one* writer and multiple readers. It may be
15 15 // suitable for low-contention with relatively infrequent writes, and many
16 class CONTENT_EXPORT GamepadSeqLockBase { 16 // readers. See:
17 protected: 17 // http://en.wikipedia.org/wiki/Seqlock
18 static const size_t kEntries = 2; 18 // http://www.concurrencykit.org/doc/ck_sequence.html
19 typedef base::subtle::Atomic32 Atomic32; 19 // This implementation is based on ck_sequence.h from http://concurrencykit.org.
20 struct BaseEntry { 20 //
21 Atomic32 sequence_; 21 // Currently, this is used in only one location. It may make sense to
22 // Both writer and readers work with the array concurrently, 22 // generalize with a higher-level construct that owns both the lock and the
23 // so it must be accessed with atomic operations. 23 // data buffer, if it is to be used more widely.
24 Atomic32 data_[1]; 24 //
25 }; 25 // You must be very careful not to operate on potentially inconsistent read
26 26 // buffers. If the read must be retry'd, the data in the read buffer could
27 GamepadSeqLockBase(BaseEntry* entries, size_t size); 27 // contain any random garbage. e.g., contained pointers might be
28 void ReadTo(Atomic32* obj); 28 // garbage, or indices could be out of range. Probably the only suitable thing
29 void Write(const Atomic32* obj); 29 // to do during the read loop is to make a copy of the data, and operate on it
30 // only after the read was found to be consistent.
31 class CONTENT_EXPORT GamepadSeqLock {
32 public:
33 GamepadSeqLock();
34 base::subtle::Atomic32 ReadBegin();
35 bool ReadRetry(base::subtle::Atomic32 version);
36 void WriteBegin();
37 void WriteEnd();
30 38
31 private: 39 private:
32 const size_t size_; 40 base::subtle::Atomic32 sequence_;
33 Atomic32 current_;
34 BaseEntry* entries_[kEntries];
35 };
36
37 } // namespace internal
38
39 // This SeqLock handles only *one* writer and multiple readers. It may be
40 // suitable for high read frequency scenarios and is especially useful in shared
41 // memory environment. See for the basic idea:
42 // http://en.wikipedia.org/wiki/Seqlock
43 // However, this implementation is an improved lock-free variant.
44 // The SeqLock can hold only POD fully-embed data (no pointers
45 // to satellite data), copies are done with memcpy.
46 template<typename T>
47 class GamepadSeqLock : public internal::GamepadSeqLockBase {
48 public:
49 GamepadSeqLock()
50 : internal::GamepadSeqLockBase(entries_, kSize) {
51 }
52
53 // May be called concurrently with ReadTo and Write.
54 // - If ReadTo occurs in the middle of Write (on another thread),
55 // ReadTo gets the old data (not the new data being written
56 // by the other thread).
57 // - ReadTo never spins unless the writer thread is calling Write
58 // multiple times before ReadTo completes.
59 void ReadTo(T* obj) {
60 // Breaks strict-aliasing rules.
61 Base::ReadTo(reinterpret_cast<Atomic32*>(obj));
62 }
63
64 // Must be called by a single thread at a time. May be called concurrently
65 // with ReadTo.
66 void Write(const T& obj) {
67 // Breaks strict-aliasing rules.
68 Base::Write(reinterpret_cast<const Atomic32*>(&obj));
69 }
70
71 private:
72 typedef internal::GamepadSeqLockBase Base;
73 COMPILE_ASSERT(sizeof(T) % sizeof(Atomic32) == 0, bad_object_size);
74 static size_t const kSize = sizeof(T) / sizeof(Atomic32);
75 struct Entry : Base::BaseEntry {
76 Atomic32 data_[kSize];
77 };
78 Entry entries_[kEntries];
79 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock); 41 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock);
80 }; 42 };
81 43
82 } // namespace content 44 } // namespace content
83 45
84 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ 46 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_
OLDNEW
« no previous file with comments | « content/common/gamepad_hardware_buffer.h ('k') | content/common/gamepad_seqlock.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698