OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "device/generic_sensor/public/cpp/sensor_reading.h" | 5 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
6 | 6 |
7 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" | |
8 | |
9 namespace { | |
timvolodine
2017/06/21 18:22:11
looks like this is a merge from a different patch
juncai
2017/07/20 00:22:16
This buffer reading functionality is moved to Sens
| |
10 | |
11 constexpr int kMaxReadAttemptsCount = 10; | |
12 | |
13 bool TryReadFromBuffer(const device::SensorReadingSharedBuffer* buffer, | |
14 device::SensorReading* result) { | |
15 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | |
16 auto version = seqlock.ReadBegin(); | |
17 auto reading_data = buffer->reading; | |
18 if (seqlock.ReadRetry(version)) | |
19 return false; | |
20 *result = reading_data; | |
21 return true; | |
22 } | |
23 | |
24 } // namespace | |
25 | |
7 namespace device { | 26 namespace device { |
8 | 27 |
9 SensorReading::SensorReading() = default; | 28 SensorReading::SensorReading() = default; |
10 SensorReading::SensorReading(const SensorReading& other) = default; | 29 SensorReading::SensorReading(const SensorReading& other) = default; |
11 SensorReading::~SensorReading() = default; | 30 SensorReading::~SensorReading() = default; |
12 | 31 |
13 SensorReadingSharedBuffer::SensorReadingSharedBuffer() = default; | 32 SensorReadingSharedBuffer::SensorReadingSharedBuffer() = default; |
14 SensorReadingSharedBuffer::~SensorReadingSharedBuffer() = default; | 33 SensorReadingSharedBuffer::~SensorReadingSharedBuffer() = default; |
15 | 34 |
16 // static | 35 // static |
17 uint64_t SensorReadingSharedBuffer::GetOffset(mojom::SensorType type) { | 36 uint64_t SensorReadingSharedBuffer::GetOffset(mojom::SensorType type) { |
18 return (static_cast<uint64_t>(mojom::SensorType::LAST) - | 37 return (static_cast<uint64_t>(mojom::SensorType::LAST) - |
19 static_cast<uint64_t>(type)) * | 38 static_cast<uint64_t>(type)) * |
20 sizeof(SensorReadingSharedBuffer); | 39 sizeof(SensorReadingSharedBuffer); |
21 } | 40 } |
22 | 41 |
42 bool GetSensorReadingFromBuffer(const SensorReadingSharedBuffer* buffer, | |
43 SensorReading* result) { | |
44 int read_attempts = 0; | |
45 while (!TryReadFromBuffer(buffer, result)) { | |
46 if (++read_attempts == kMaxReadAttemptsCount) | |
47 return false; | |
48 } | |
49 | |
50 return true; | |
51 } | |
52 | |
23 } // namespace device | 53 } // namespace device |
OLD | NEW |