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/one_writer_seqlock.h" |
6 | 6 |
7 namespace content { | 7 namespace content { |
8 | 8 |
9 GamepadSeqLock::GamepadSeqLock() | 9 OneWriterSeqLock::OneWriterSeqLock() |
10 : sequence_(0) { | 10 : sequence_(0) { |
11 } | 11 } |
12 | 12 |
13 base::subtle::Atomic32 GamepadSeqLock::ReadBegin() { | 13 base::subtle::Atomic32 OneWriterSeqLock::ReadBegin() { |
14 base::subtle::Atomic32 version; | 14 base::subtle::Atomic32 version; |
15 for (;;) { | 15 for (;;) { |
16 version = base::subtle::NoBarrier_Load(&sequence_); | 16 version = base::subtle::NoBarrier_Load(&sequence_); |
17 | 17 |
18 // If the counter is even, then the associated data might be in a | 18 // If the counter is even, then the associated data might be in a |
19 // consistent state, so we can try to read. | 19 // consistent state, so we can try to read. |
20 if ((version & 1) == 0) | 20 if ((version & 1) == 0) |
21 break; | 21 break; |
22 | 22 |
23 // Otherwise, the writer is in the middle of an update. Retry the read. | 23 // Otherwise, the writer is in the middle of an update. Retry the read. |
24 base::PlatformThread::YieldCurrentThread(); | 24 base::PlatformThread::YieldCurrentThread(); |
25 } | 25 } |
26 return version; | 26 return version; |
27 } | 27 } |
28 | 28 |
29 bool GamepadSeqLock::ReadRetry(base::subtle::Atomic32 version) { | 29 bool OneWriterSeqLock::ReadRetry(base::subtle::Atomic32 version) { |
30 // If the sequence number was updated then a read should be re-attempted. | 30 // If the sequence number was updated then a read should be re-attempted. |
31 // -- Load fence, read membarrier | 31 // -- Load fence, read membarrier |
32 return base::subtle::Release_Load(&sequence_) != version; | 32 return base::subtle::Release_Load(&sequence_) != version; |
33 } | 33 } |
34 | 34 |
35 void GamepadSeqLock::WriteBegin() { | 35 void OneWriterSeqLock::WriteBegin() { |
36 // Increment the sequence number to odd to indicate the beginning of a write | 36 // Increment the sequence number to odd to indicate the beginning of a write |
37 // update. | 37 // update. |
38 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | 38 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); |
39 // -- Store fence, write membarrier | 39 // -- Store fence, write membarrier |
40 } | 40 } |
41 | 41 |
42 void GamepadSeqLock::WriteEnd() { | 42 void OneWriterSeqLock::WriteEnd() { |
43 // Increment the sequence to an even number to indicate the completion of | 43 // Increment the sequence to an even number to indicate the completion of |
44 // a write update. | 44 // a write update. |
45 // -- Store fence, write membarrier | 45 // -- Store fence, write membarrier |
46 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | 46 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); |
47 } | 47 } |
48 | 48 |
49 } // namespace content | 49 } // namespace content |
OLD | NEW |