[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 4324e61 | 2011-12-01 00:01:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 5 | #ifndef CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ |
6 | #define CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ | ||||
[email protected] | 4324e61 | 2011-12-01 00:01:38 | [diff] [blame] | 7 | |
8 | #include "base/atomicops.h" | ||||
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 9 | #include "base/macros.h" |
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 10 | #include "base/threading/platform_thread.h" |
[email protected] | 8d85a4b | 2011-12-01 00:37:16 | [diff] [blame] | 11 | #include "content/common/content_export.h" |
[email protected] | 4324e61 | 2011-12-01 00:01:38 | [diff] [blame] | 12 | |
13 | namespace content { | ||||
14 | |||||
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 15 | // This SeqLock handles only *one* writer and multiple readers. It may be |
16 | // suitable for low-contention with relatively infrequent writes, and many | ||||
17 | // readers. See: | ||||
18 | // http://en.wikipedia.org/wiki/Seqlock | ||||
19 | // http://www.concurrencykit.org/doc/ck_sequence.html | ||||
20 | // This implementation is based on ck_sequence.h from http://concurrencykit.org. | ||||
21 | // | ||||
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 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). | ||||
24 | // It may make sense to generalize this lock to multiple writers. | ||||
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 25 | // |
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 | ||||
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 | ||||
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. | ||||
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 32 | class CONTENT_EXPORT OneWriterSeqLock { |
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 33 | public: |
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 34 | OneWriterSeqLock(); |
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 35 | base::subtle::Atomic32 ReadBegin(); |
36 | bool ReadRetry(base::subtle::Atomic32 version); | ||||
37 | void WriteBegin(); | ||||
38 | void WriteEnd(); | ||||
[email protected] | 4324e61 | 2011-12-01 00:01:38 | [diff] [blame] | 39 | |
40 | private: | ||||
[email protected] | 501f968 | 2012-07-16 21:45:27 | [diff] [blame] | 41 | base::subtle::Atomic32 sequence_; |
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 42 | DISALLOW_COPY_AND_ASSIGN(OneWriterSeqLock); |
[email protected] | 4324e61 | 2011-12-01 00:01:38 | [diff] [blame] | 43 | }; |
44 | |||||
45 | } // namespace content | ||||
46 | |||||
[email protected] | 876526aa | 2013-07-11 08:00:57 | [diff] [blame] | 47 | #endif // CONTENT_COMMON_ONE_WRITER_SEQLOCK_H_ |