Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ | |
| 6 #define CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ | |
| 7 | |
| 8 #include "base/atomicops.h" | |
| 9 #include "base/threading/platform_thread.h" | |
| 10 | |
| 11 namespace gamepad { | |
|
darin (slow to review)
2011/11/30 18:34:37
I think we probably want new code in the content/
scottmg
2011/11/30 19:43:31
Phew, that turned out to be a lot of places (not c
| |
| 12 | |
| 13 // This SeqLock handles only *one* writer and multiple readers. It may be | |
| 14 // suitable for low-contention with relatively infrequent writes, and many | |
| 15 // readers. See: | |
| 16 // http://en.wikipedia.org/wiki/Seqlock | |
| 17 // http://www.concurrencykit.org/doc/ck_sequence.html | |
| 18 // This implementation is based on ck_sequence.h from http://concurrencykit.org. | |
| 19 // | |
| 20 // Currently, this is used in only one location. It may make sense to | |
| 21 // generalize with a higher-level construct that owns both the lock and the | |
| 22 // data buffer, if it is to be used more widely. | |
| 23 // | |
| 24 // You must be very careful not to operate on potentially inconsistent read | |
| 25 // buffers. If the read must be retry'd, the data in the read buffer could | |
| 26 // contain any random garbage. e.g., contained pointers might be | |
| 27 // garbage, or indices could be out of range. Probably the only suitable thing | |
| 28 // to do during the read loop is to make a copy of the data, and operate on it | |
| 29 // only after the read was found to be consistent. | |
| 30 class GamepadSeqLock { | |
| 31 public: | |
| 32 GamepadSeqLock() : sequence_(0) {} | |
| 33 | |
| 34 base::subtle::Atomic32 ReadBegin() { | |
|
darin (slow to review)
2011/11/30 18:34:37
why inline these functions?
scottmg
2011/11/30 19:43:31
4/5 seemed trivial enough (other than ReadBegin),
| |
| 35 base::subtle::Atomic32 version; | |
| 36 for (;;) { | |
| 37 version = base::subtle::NoBarrier_Load(&sequence_); | |
| 38 | |
| 39 // If the counter is even, then the associated data might be in a | |
| 40 // consistent state, so we can try to read. | |
| 41 if ((version & 1) == 0) | |
| 42 break; | |
| 43 | |
| 44 // Otherwise, the writer is in the middle of an update. Retry the read. | |
| 45 base::PlatformThread::YieldCurrentThread(); | |
| 46 } | |
| 47 return version; | |
| 48 } | |
| 49 | |
| 50 bool ReadRetry(base::subtle::Atomic32 version) { | |
| 51 // If the sequence number was updated then a read should be re-attempted. | |
| 52 // -- Load fence, read membarrier | |
| 53 bool ret = base::subtle::Release_Load(&sequence_) != version; | |
| 54 return ret; | |
| 55 } | |
| 56 | |
| 57 void WriteBegin() { | |
| 58 // Increment the sequence number to odd to indicate the beginning of a | |
| 59 // write update. | |
| 60 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | |
| 61 // -- Store fence, write membarrier | |
| 62 } | |
| 63 | |
| 64 void WriteEnd() { | |
| 65 // Increment the sequence to an even number to indicate the completion of | |
| 66 // a write update. | |
| 67 // -- Store fence, write membarrier | |
| 68 base::subtle::Barrier_AtomicIncrement(&sequence_, 1); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 base::subtle::Atomic32 sequence_; | |
| 73 DISALLOW_COPY_AND_ASSIGN(GamepadSeqLock); | |
| 74 }; | |
| 75 | |
| 76 } // namespace gamepad | |
| 77 | |
| 78 #endif // CONTENT_COMMON_GAMEPAD_SEQLOCK_H_ | |
| OLD | NEW |