Index: base/synchronization/seqlock.h |
diff --git a/base/synchronization/seqlock.h b/base/synchronization/seqlock.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b740da07795627037868e156319161bde5d54161 |
--- /dev/null |
+++ b/base/synchronization/seqlock.h |
@@ -0,0 +1,67 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_SYNCHRONIZATION_SEQLOCK_H_ |
+#define BASE_SYNCHRONIZATION_SEQLOCK_H_ |
+#pragma once |
+ |
+#include "base/base_export.h" |
+#include "base/atomicops.h" |
+ |
+namespace base { |
+ |
+// This SeqLock handles only *one* writer and multiple readers. It may be |
+// suitable for low-contention with relatively infrequent writes, and many |
+// readers. See: |
+// http://en.wikipedia.org/wiki/Seqlock |
+// http://www.concurrencykit.org/doc/ck_sequence.html |
+class BASE_EXPORT SeqLock { |
+ public: |
+ SeqLock() : sequence_(0) {} |
+ |
+ subtle::Atomic32 ReadBegin() { |
+ base::subtle::Atomic32 version; |
+ for (;;) { |
+ version = subtle::Acquire_Load(&sequence_); |
+ |
+ // If the a sequence is even, then the associated data may be in a |
+ // consistent state. |
+ if ((version & 1) == 0) |
+ break; |
+ |
+ // Otherwise, a thread is in the middle of an update. Retry the read. |
+ PlatformThread::YieldCurrentThread(); |
+ } |
+ return version; |
+ } |
+ |
+ bool ReadRetry(subtle::Atomic32 version) { |
+ // If the sequence number was updated then a read should be re-attempted. |
+ // -- Load fence, read membarrier |
+ bool ret = subtle::Release_Load(&sequence_) != version; |
+ return ret; |
+ } |
+ |
+ void WriteBegin() { |
+ // Increment the sequence number to odd to indicate the beginning of a |
+ // write update. |
+ subtle::Barrier_AtomicIncrement(&sequence_, 1); |
jbates
2011/11/29 00:05:26
I'm not sure why there's BarrierAtomicIncrement an
|
+ // -- Store fence, write membarrier |
+ } |
+ |
+ void WriteEnd() { |
+ // Increment the sequence to an even number to indicate the completion of |
+ // a write update. |
+ // -- Store fence, write membarrier |
+ subtle::Barrier_AtomicIncrement(&sequence_, 1); |
jbates
2011/11/29 00:05:26
And this:
subtle::Release_Store(&sequence_, sequen
|
+ } |
+ |
+ private: |
+ base::subtle::Atomic32 sequence_; |
+ DISALLOW_COPY_AND_ASSIGN(SeqLock); |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_SYNCHRONIZATION_SEQLOCK_H_ |