OLD | NEW |
---|---|
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 // This SeqLock handles only *one* writer and multiple readers. It may be |
15 // suitable for low-contention with relatively infrequent writes, and many | 13 // suitable for high read frequency scenarios and is especially useful in shared |
16 // readers. See: | 14 // memory environment. See for the basic idea: |
17 // http://en.wikipedia.org/wiki/Seqlock | 15 // http://en.wikipedia.org/wiki/Seqlock |
18 // http://www.concurrencykit.org/doc/ck_sequence.html | 16 // However, this implementation is an improved lock-free variant. |
19 // This implementation is based on ck_sequence.h from http://concurrencykit.org. | 17 // The SeqLock can hold only POD fully-embed data (no pointers |
20 // | 18 // to satellite data), copies are done with memcpy. |
21 // Currently, this is used in only one location. It may make sense to | 19 template<typename T> |
22 // generalize with a higher-level construct that owns both the lock and the | 20 class GamepadSeqLock { |
23 // data buffer, if it is to be used more widely. | |
24 // | |
25 // You must be very careful not to operate on potentially inconsistent read | |
26 // buffers. If the read must be retry'd, the data in the read buffer could | |
27 // contain any random garbage. e.g., contained pointers might be | |
28 // garbage, or indices could be out of range. Probably the only suitable thing | |
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: | 21 public: |
33 GamepadSeqLock(); | 22 GamepadSeqLock(); |
34 base::subtle::Atomic32 ReadBegin(); | 23 void ReadTo(T* obj); |
35 bool ReadRetry(base::subtle::Atomic32 version); | 24 void Write(T const& obj); |
36 void WriteBegin(); | |
37 void WriteEnd(); | |
38 | 25 |
39 private: | 26 private: |
40 base::subtle::Atomic32 sequence_; | 27 typedef base::subtle::Atomic32 Atomic32; |
28 typedef char static_assert_size[sizeof(T) % sizeof(Atomic32) ? -1 : 1]; | |
29 static size_t const kSize = sizeof(T) / sizeof(Atomic32); | |
30 static size_t const kEntries = 2; | |
31 struct Entry { | |
32 Atomic32 sequence_; | |
33 // Both writer and readers work with the array concurrently, | |
34 // so it must be accessed with atomic operations. | |
35 Atomic32 data_[kSize]; | |
36 }; | |
37 Atomic32 current_; | |
38 Entry entries_[kEntries]; | |
41 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock); | 39 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock); |
42 }; | 40 }; |
43 | 41 |
42 template<typename T> | |
43 GamepadSeqLock<T>::GamepadSeqLock() | |
44 : current_(0) { | |
45 memset(entries_, 0, sizeof(entries_)); | |
46 } | |
47 | |
48 // The algorithm works as follows. | |
49 // The SeqLock contains 2 user objects - a current and a non-current. | |
50 // The object roles can be swapped by incrementing the current_ variable. | |
51 // Initially both objects are consistent, that is, their sequence_%2 == 0. | |
52 // A writer proceeds as follows. First, it marks the non-current object | |
53 // as inconsistent by incrementing it's sequence_ (the sequence_ becomes odd). | |
54 // Then it mutates the object. Then marks it as consistent by incrementing | |
55 // it's sequence_ once again (the sequence_ is even now). And finally swaps | |
56 // the object roles, that is, the object becomes current. | |
57 // Such disipline establishes an important property - the current object | |
58 // is always consistent and contains the most recent data. | |
59 // Readers proceed as follows. First, determine what is the current object, | |
60 // remember it's seqence, check that the sequence is even | |
61 // (the object is consistent), copy out the object, verify that | |
62 // the sequence is not changed. If any of the checks fail, a reader works | |
63 // with a non-current object (current object is always consistent), so it | |
64 // just retries from the beginning. Thus readers are completely lock-free, | |
65 // that is, a reader retries iff a writer has accomplished a write operation | |
66 // during reading (only constant sequence of writes can stall readers, | |
67 // a stalled writer can't block readers). | |
68 | |
69 template<typename T> | |
70 void GamepadSeqLock<T>::ReadTo(T* obj) { | |
darin (slow to review)
2011/12/08 22:10:48
nit: Can't this be a non-template helper function
dvyukov
2011/12/13 13:46:20
Done.
| |
71 using base::subtle::NoBarrier_Load; | |
72 using base::subtle::Acquire_Load; | |
73 using base::subtle::MemoryBarrier; | |
74 for (;;) { | |
75 // Determine the current object. | |
76 Atomic32 cur = Acquire_Load(¤t_); | |
77 Entry* ent = &entries_[cur % kEntries]; | |
78 Atomic32 seq = Acquire_Load(&ent->sequence_); // membar #LoadLoad | |
79 // If the counter is even, then the object is not already current, | |
80 // no need to yield, just retry (the current object is consistent). | |
81 if (seq % 2) | |
82 continue; | |
83 // Breaks strict-aliasing rules. | |
84 Atomic32* dst = reinterpret_cast<Atomic32*>(obj); | |
85 // Copy out the entry. | |
86 for (size_t i = 0; i < kSize; ++i) | |
87 dst[i] = NoBarrier_Load(&ent->data_[i]); | |
88 MemoryBarrier(); // membar #LoadLoad | |
89 Atomic32 seq2 = NoBarrier_Load(&ent->sequence_); | |
90 // If the counter is changed, then we've read inconsistent data, | |
91 // no need to yield, just retry (the current object is consistent). | |
92 if (seq2 != seq) | |
93 continue; | |
94 break; | |
95 } | |
96 } | |
97 | |
98 template<typename T> | |
99 void GamepadSeqLock<T>::Write(T const& obj) { | |
darin (slow to review)
2011/12/08 22:10:48
Ditto.
dvyukov
2011/12/13 13:46:20
Done.
| |
100 using base::subtle::NoBarrier_Store; | |
101 using base::subtle::Release_Store; | |
102 using base::subtle::MemoryBarrier; | |
103 // Get the non current object... | |
104 Entry* ent = &entries_[(current_ + 1) % kEntries]; | |
105 // ... and mark it as inconsistent. | |
106 NoBarrier_Store(&ent->sequence_, ent->sequence_ + 1); | |
107 MemoryBarrier(); // membar #StoreStore | |
108 // Breaks strict-aliasing rules. | |
109 Atomic32 const* src = reinterpret_cast<Atomic32 const*>(&obj); | |
110 // Copy in the entry. | |
111 for (size_t i = 0; i < kSize; ++i) | |
112 NoBarrier_Store(&ent->data_[i], src[i]); | |
113 // Mark the object as consistent again. | |
114 Release_Store(&ent->sequence_, ent->sequence_ + 1); // membar #StoreStore | |
115 // Switch the current object. | |
116 Release_Store(¤t_, current_ + 1); | |
117 } | |
118 | |
44 } // namespace content | 119 } // namespace content |
45 | 120 |
46 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ | 121 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ |
OLD | NEW |