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

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

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_seqlock.h ('k') | content/common/gamepad_seqlock_unittest.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 #include "content/common/gamepad_seqlock.h" 5 #include "content/common/gamepad_seqlock.h"
6 6
7 namespace content { 7 namespace content {
8 namespace internal {
9 8
10 GamepadSeqLockBase::GamepadSeqLockBase(BaseEntry* entries, size_t size) 9 GamepadSeqLock::GamepadSeqLock()
11 : size_(size) 10 : sequence_(0) {
12 , current_(0) {
13 const size_t ent_size = sizeof(BaseEntry) + sizeof(Atomic32) * size;
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 }
19 } 11 }
20 12
21 // The algorithm works as follows. 13 base::subtle::Atomic32 GamepadSeqLock::ReadBegin() {
22 // The SeqLock contains 2 user objects - a current and a non-current. 14 base::subtle::Atomic32 version;
23 // The object roles can be swapped by incrementing the current_ variable. 15 for (;;) {
24 // Initially both objects are consistent, that is, their sequence_%2 == 0. 16 version = base::subtle::NoBarrier_Load(&sequence_);
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 17
42 void GamepadSeqLockBase::ReadTo(Atomic32* obj) { 18 // If the counter is even, then the associated data might be in a
43 using base::subtle::NoBarrier_Load; 19 // consistent state, so we can try to read.
44 using base::subtle::Acquire_Load; 20 if ((version & 1) == 0)
45 using base::subtle::MemoryBarrier; 21 break;
46 for (;;) { 22
47 // Determine the current object. 23 // Otherwise, the writer is in the middle of an update. Retry the read.
48 Atomic32 cur = Acquire_Load(&current_); 24 base::PlatformThread::YieldCurrentThread();
49 BaseEntry* ent = entries_[cur % kEntries];
50 Atomic32 seq = Acquire_Load(&ent->sequence_); // membar #LoadLoad
51 // If the counter is even, then the object is not already current,
52 // no need to yield, just retry (the current object is consistent).
53 if (seq % 2)
54 continue;
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;
65 } 25 }
26 return version;
66 } 27 }
67 28
68 void GamepadSeqLockBase::Write(const Atomic32* obj) { 29 bool GamepadSeqLock::ReadRetry(base::subtle::Atomic32 version) {
69 using base::subtle::NoBarrier_Store; 30 // If the sequence number was updated then a read should be re-attempted.
70 using base::subtle::Release_Store; 31 // -- Load fence, read membarrier
71 using base::subtle::MemoryBarrier; 32 return base::subtle::Release_Load(&sequence_) != version;
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(&current_, current_ + 1);
84 } 33 }
85 34
86 } // namespace internal 35 void GamepadSeqLock::WriteBegin() {
87 } // namespace content 36 // Increment the sequence number to odd to indicate the beginning of a write
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
OLDNEW
« no previous file with comments | « content/common/gamepad_seqlock.h ('k') | content/common/gamepad_seqlock_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698