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

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, 11 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"
10 #include "content/common/content_export.h"
11 9
12 namespace content { 10 namespace content {
13 11
14 // This SeqLock handles only *one* writer and multiple readers. It may be 12 namespace internal {
15 // suitable for low-contention with relatively infrequent writes, and many 13
16 // readers. See: 14 class GamepadSeqLockBase {
17 // http://en.wikipedia.org/wiki/Seqlock 15 protected:
18 // http://www.concurrencykit.org/doc/ck_sequence.html 16 static const size_t kEntries = 2;
19 // This implementation is based on ck_sequence.h from http://concurrencykit.org. 17 typedef base::subtle::Atomic32 Atomic32;
20 // 18 struct Entry {
21 // Currently, this is used in only one location. It may make sense to 19 Atomic32 sequence_;
22 // generalize with a higher-level construct that owns both the lock and the 20 // Both writer and readers work with the array concurrently,
23 // data buffer, if it is to be used more widely. 21 // so it must be accessed with atomic operations.
24 // 22 Atomic32 data_[];
25 // You must be very careful not to operate on potentially inconsistent read 23 };
26 // buffers. If the read must be retry'd, the data in the read buffer could 24
27 // contain any random garbage. e.g., contained pointers might be 25 GamepadSeqLockBase(Entry* entries, size_t size);
28 // garbage, or indices could be out of range. Probably the only suitable thing 26 void ReadTo(Atomic32* obj);
29 // to do during the read loop is to make a copy of the data, and operate on it 27 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 28
39 private: 29 private:
40 base::subtle::Atomic32 sequence_; 30 const size_t size_;
31 Atomic32 current_;
32 Entry* entries_[kEntries];
33 };
34
35 } // namespace internal
36
37 // This SeqLock handles only *one* writer and multiple readers. It may be
38 // suitable for high read frequency scenarios and is especially useful in shared
39 // memory environment. See for the basic idea:
40 // http://en.wikipedia.org/wiki/Seqlock
41 // However, this implementation is an improved lock-free variant.
42 // The SeqLock can hold only POD fully-embed data (no pointers
43 // to satellite data), copies are done with memcpy.
44 template<typename T>
45 class GamepadSeqLock : public internal::GamepadSeqLockBase {
46 public:
47 GamepadSeqLock()
48 : Base(entries_, kSize) {
jbates 2012/02/17 23:29:52 hrmm, so I see you haven't tried to compile this y
dvyukov 2012/02/21 15:18:04 Compile?.. What for? :) Done... hope I guessed cor
49 }
50
51 void ReadTo(T* obj) {
jbates 2012/02/17 23:29:52 Would be helpful to see the properties and guarant
dvyukov 2012/02/21 15:18:04 Done.
52 // Breaks strict-aliasing rules.
53 Base::ReadTo(reinterpret_cast<Atomic32*>(obj));
54 }
55
56 void Write(const T& obj) {
57 // Breaks strict-aliasing rules.
58 Base::Write(reinterpret_cast<const Atomic32*>(&obj));
59 }
60
61 private:
62 typedef internal::GamepadSeqLockBase Base;
63 typedef char static_assert_size[sizeof(T) % sizeof(Atomic32) ? -1 : 1];
jbates 2012/02/17 23:29:52 could use the macro for this in base/basictypes.h:
dvyukov 2012/02/21 15:18:04 Done.
64 static size_t const kSize = sizeof(T) / sizeof(Atomic32);
65 struct Entry : Base::Entry{
jbates 2012/02/17 23:29:52 I was confused by "Entry" since it is the same nam
dvyukov 2012/02/21 15:18:04 Done.
66 Atomic32 data_[kSize];
67 };
68 Entry entries_[kEntries];
41 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock); 69 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock);
42 }; 70 };
43 71
44 } // namespace content 72 } // namespace content
45 73
46 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ 74 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698