| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ | 5 #ifndef BASE_SYNCHRONIZATION_ONE_WRITER_SEQLOCK_H_ |
| 6 #define CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ | 6 #define BASE_SYNCHRONIZATION_ONE_WRITER_SEQLOCK_H_ |
| 7 | 7 |
| 8 #include "base/atomicops.h" | 8 #include "base/atomicops.h" |
| 9 #include "base/base_export.h" |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 11 #include "content/common/content_export.h" | |
| 12 | 12 |
| 13 namespace content { | 13 namespace base { |
| 14 | 14 |
| 15 // This SeqLock handles only *one* writer and multiple readers. It may be | 15 // This SeqLock handles only *one* writer and multiple readers. It may be |
| 16 // suitable for low-contention with relatively infrequent writes, and many | 16 // suitable for low-contention with relatively infrequent writes, and many |
| 17 // readers. See: | 17 // readers. See: |
| 18 // http://en.wikipedia.org/wiki/Seqlock | 18 // http://en.wikipedia.org/wiki/Seqlock |
| 19 // http://www.concurrencykit.org/doc/ck_sequence.html | 19 // http://www.concurrencykit.org/doc/ck_sequence.html |
| 20 // This implementation is based on ck_sequence.h from http://concurrencykit.org. | 20 // This implementation is based on ck_sequence.h from http://concurrencykit.org. |
| 21 // | 21 // |
| 22 // Currently this type of lock is used in two implementations (gamepad and | 22 // Currently this type of lock is used in two implementations (gamepad and |
| 23 // device motion, in particular see e.g. shared_memory_seqlock_buffer.h). | 23 // device motion, in particular see e.g. shared_memory_seqlock_buffer.h). |
| 24 // It may make sense to generalize this lock to multiple writers. | 24 // It may make sense to generalize this lock to multiple writers. |
| 25 // | 25 // |
| 26 // You must be very careful not to operate on potentially inconsistent read | 26 // You must be very careful not to operate on potentially inconsistent read |
| 27 // buffers. If the read must be retry'd, the data in the read buffer could | 27 // buffers. If the read must be retry'd, the data in the read buffer could |
| 28 // contain any random garbage. e.g., contained pointers might be | 28 // contain any random garbage. e.g., contained pointers might be |
| 29 // garbage, or indices could be out of range. Probably the only suitable thing | 29 // garbage, or indices could be out of range. Probably the only suitable thing |
| 30 // to do during the read loop is to make a copy of the data, and operate on it | 30 // to do during the read loop is to make a copy of the data, and operate on it |
| 31 // only after the read was found to be consistent. | 31 // only after the read was found to be consistent. |
| 32 class CONTENT_EXPORT OneWriterSeqLock { | 32 class BASE_EXPORT OneWriterSeqLock { |
| 33 public: | 33 public: |
| 34 OneWriterSeqLock(); | 34 OneWriterSeqLock(); |
| 35 base::subtle::Atomic32 ReadBegin(); | 35 base::subtle::Atomic32 ReadBegin(); |
| 36 bool ReadRetry(base::subtle::Atomic32 version); | 36 bool ReadRetry(base::subtle::Atomic32 version); |
| 37 void WriteBegin(); | 37 void WriteBegin(); |
| 38 void WriteEnd(); | 38 void WriteEnd(); |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 base::subtle::Atomic32 sequence_; | 41 base::subtle::Atomic32 sequence_; |
| 42 DISALLOW_COPY_AND_ASSIGN(OneWriterSeqLock); | 42 DISALLOW_COPY_AND_ASSIGN(OneWriterSeqLock); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 } // namespace content | 45 } // namespace base |
| 46 | 46 |
| 47 #endif // CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ | 47 #endif // BASE_SYNCHRONIZATION_ONE_WRITER_SEQLOCK_H_ |
| OLD | NEW |