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 #include "content/common/gamepad_seqlock.h" | 5 #include "content/common/gamepad_seqlock.h" |
6 | 6 |
7 namespace content { | 7 namespace content { |
| 8 namespace internal { |
8 | 9 |
9 GamepadSeqLock::GamepadSeqLock() | 10 GamepadSeqLockBase::GamepadSeqLockBase(BaseEntry* entries, size_t size) |
10 : sequence_(0) { | 11 : size_(size) |
| 12 , current_(0) { |
| 13 const size_t ent_size = sizeof(BaseEntry) + sizeof(Atomic32) * (size + 1); |
| 14 memset(entries, 0, kEntries * ent_size); |
| 15 for (size_t i = 0; i < kEntries; ++i) { |
| 16 entries_[i] = reinterpret_cast<BaseEntry*> |
| 17 (reinterpret_cast<char*>(entries) + i * ent_size); |
| 18 } |
11 } | 19 } |
12 | 20 |
13 base::subtle::Atomic32 GamepadSeqLock::ReadBegin() { | 21 // The algorithm works as follows. |
14 base::subtle::Atomic32 version; | 22 // The SeqLock contains 2 user objects - a current and a non-current. |
| 23 // The object roles can be swapped by incrementing the current_ variable. |
| 24 // Initially both objects are consistent, that is, their sequence_%2 == 0. |
| 25 // A writer proceeds as follows. First, it marks the non-current object |
| 26 // as inconsistent by incrementing it's sequence_ (the sequence_ becomes odd). |
| 27 // Then it mutates the object. Then marks it as consistent by incrementing |
| 28 // it's sequence_ once again (the sequence_ is even now). And finally swaps |
| 29 // the object roles, that is, the object becomes current. |
| 30 // Such disipline establishes an important property - the current object |
| 31 // is always consistent and contains the most recent data. |
| 32 // Readers proceed as follows. First, determine what is the current object, |
| 33 // remember it's seqence, check that the sequence is even |
| 34 // (the object is consistent), copy out the object, verify that |
| 35 // the sequence is not changed. If any of the checks fail, a reader works |
| 36 // with a non-current object (current object is always consistent), so it |
| 37 // just retries from the beginning. Thus readers are completely lock-free, |
| 38 // that is, a reader retries iff a writer has accomplished a write operation |
| 39 // during reading (only constant sequence of writes can stall readers, |
| 40 // a stalled writer can't block readers). |
| 41 |
| 42 void GamepadSeqLockBase::ReadTo(Atomic32* obj) { |
| 43 using base::subtle::NoBarrier_Load; |
| 44 using base::subtle::Acquire_Load; |
| 45 using base::subtle::MemoryBarrier; |
15 for (;;) { | 46 for (;;) { |
16 version = base::subtle::NoBarrier_Load(&sequence_); | 47 // Determine the current object. |
17 | 48 Atomic32 cur = Acquire_Load(¤t_); |
18 // If the counter is even, then the associated data might be in a | 49 BaseEntry* ent = entries_[cur % kEntries]; |
19 // consistent state, so we can try to read. | 50 Atomic32 seq = Acquire_Load(&ent->sequence_); // membar #LoadLoad |
20 if ((version & 1) == 0) | 51 // If the counter is even, then the object is not already current, |
21 break; | 52 // no need to yield, just retry (the current object is consistent). |
22 | 53 if (seq % 2) |
23 // Otherwise, the writer is in the middle of an update. Retry the read. | 54 continue; |
24 base::PlatformThread::YieldCurrentThread(); | 55 // Copy out the entry. |
| 56 for (size_t i = 0; i < size_; ++i) |
| 57 obj[i] = NoBarrier_Load(&ent->data_[i]); |
| 58 MemoryBarrier(); // membar #LoadLoad |
| 59 Atomic32 seq2 = NoBarrier_Load(&ent->sequence_); |
| 60 // If the counter is changed, then we've read inconsistent data, |
| 61 // no need to yield, just retry (the current object is consistent). |
| 62 if (seq2 != seq) |
| 63 continue; |
| 64 break; |
25 } | 65 } |
26 return version; | |
27 } | 66 } |
28 | 67 |
29 bool GamepadSeqLock::ReadRetry(base::subtle::Atomic32 version) { | 68 void GamepadSeqLockBase::Write(const Atomic32* obj) { |
30 // If the sequence number was updated then a read should be re-attempted. | 69 using base::subtle::NoBarrier_Store; |
31 // -- Load fence, read membarrier | 70 using base::subtle::Release_Store; |
32 return base::subtle::Release_Load(&sequence_) != version; | 71 using base::subtle::MemoryBarrier; |
| 72 // Get the non current object... |
| 73 BaseEntry* ent = entries_[(current_ + 1) % kEntries]; |
| 74 // ... and mark it as inconsistent. |
| 75 NoBarrier_Store(&ent->sequence_, ent->sequence_ + 1); |
| 76 MemoryBarrier(); // membar #StoreStore |
| 77 // Copy in the entry. |
| 78 for (size_t i = 0; i < size_; ++i) |
| 79 NoBarrier_Store(&ent->data_[i], obj[i]); |
| 80 // Mark the object as consistent again. |
| 81 Release_Store(&ent->sequence_, ent->sequence_ + 1); // membar #StoreStore |
| 82 // Switch the current object. |
| 83 Release_Store(¤t_, current_ + 1); |
33 } | 84 } |
34 | 85 |
35 void GamepadSeqLock::WriteBegin() { | 86 } // namespace internal |
36 // Increment the sequence number to odd to indicate the beginning of a write | 87 } // namespace content |
37 // update. | |
38 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | |
39 // -- Store fence, write membarrier | |
40 } | |
41 | |
42 void GamepadSeqLock::WriteEnd() { | |
43 // Increment the sequence to an even number to indicate the completion of | |
44 // a write update. | |
45 // -- Store fence, write membarrier | |
46 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | |
47 } | |
48 | |
49 } // namespace content | |
OLD | NEW |