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

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

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

Powered by Google App Engine
This is Rietveld 408576698