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