Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1101)

Side by Side Diff: device/generic_sensor/platform_sensor_linux.cc

Issue 2569763004: [sensors](Linux) Fix tsan data race in sensor reader (Closed)
Patch Set: don't use BindToCurrentLoop, but rather pass a task runner and callbacks Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/platform_sensor_linux.h" 5 #include "device/generic_sensor/platform_sensor_linux.h"
6 6
7 #include "device/generic_sensor/linux/sensor_data_linux.h" 7 #include "device/generic_sensor/linux/sensor_data_linux.h"
8 #include "device/generic_sensor/platform_sensor_reader_linux.h" 8 #include "device/generic_sensor/platform_sensor_reader_linux.h"
9 9
10 namespace device { 10 namespace device {
(...skipping 11 matching lines...) Expand all
22 PlatformSensorLinux::PlatformSensorLinux( 22 PlatformSensorLinux::PlatformSensorLinux(
23 mojom::SensorType type, 23 mojom::SensorType type,
24 mojo::ScopedSharedBufferMapping mapping, 24 mojo::ScopedSharedBufferMapping mapping,
25 PlatformSensorProvider* provider, 25 PlatformSensorProvider* provider,
26 const SensorInfoLinux* sensor_device, 26 const SensorInfoLinux* sensor_device,
27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) 27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner)
28 : PlatformSensor(type, std::move(mapping), provider), 28 : PlatformSensor(type, std::move(mapping), provider),
29 default_configuration_( 29 default_configuration_(
30 PlatformSensorConfiguration(sensor_device->device_frequency)), 30 PlatformSensorConfiguration(sensor_device->device_frequency)),
31 reporting_mode_(sensor_device->reporting_mode), 31 reporting_mode_(sensor_device->reporting_mode),
32 polling_thread_task_runner_(std::move(polling_thread_task_runner)),
32 weak_factory_(this) { 33 weak_factory_(this) {
33 sensor_reader_ = 34 SensorReader::UpdateSensorReadingCallback update_reading_cb =
34 SensorReader::Create(sensor_device, this, polling_thread_task_runner); 35 base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading,
36 weak_factory_.GetWeakPtr());
37 SensorReader::NotifyReadErrorCallback notify_read_error_cb =
38 base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError,
39 weak_factory_.GetWeakPtr());
40
41 sensor_reader_ = SensorReader::Create(
42 sensor_device, task_runner_, update_reading_cb, notify_read_error_cb);
Reilly Grant (use Gerrit) 2016/12/16 21:34:42 Now you've negated the benefit of passing callback
maksims (do not use this acc) 2016/12/19 09:46:22 Done.
35 } 43 }
36 44
37 PlatformSensorLinux::~PlatformSensorLinux() = default; 45 PlatformSensorLinux::~PlatformSensorLinux() {
46 DCHECK(task_runner_->BelongsToCurrentThread());
47 polling_thread_task_runner_->DeleteSoon(FROM_HERE, sensor_reader_.release());
48 }
38 49
39 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { 50 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() {
40 DCHECK(task_runner_->BelongsToCurrentThread()); 51 DCHECK(task_runner_->BelongsToCurrentThread());
41 return reporting_mode_; 52 return reporting_mode_;
42 } 53 }
43 54
44 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { 55 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) {
45 DCHECK(task_runner_->BelongsToCurrentThread()); 56 DCHECK(task_runner_->BelongsToCurrentThread());
46 bool notifyNeeded = false; 57 bool notifyNeeded = false;
47 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { 58 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) {
48 if (!HaveValuesChanged(reading, old_values_)) 59 if (!HaveValuesChanged(reading, old_values_))
49 return; 60 return;
50 notifyNeeded = true; 61 notifyNeeded = true;
51 } 62 }
52 old_values_ = reading; 63 old_values_ = reading;
53 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); 64 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
54 UpdateSensorReading(reading, notifyNeeded); 65 UpdateSensorReading(reading, notifyNeeded);
55 } 66 }
56 67
57 void PlatformSensorLinux::NotifyPlatformSensorError() { 68 void PlatformSensorLinux::NotifyPlatformSensorError() {
58 DCHECK(task_runner_->BelongsToCurrentThread()); 69 DCHECK(task_runner_->BelongsToCurrentThread());
59 NotifySensorError(); 70 NotifySensorError();
60 } 71 }
61 72
62 bool PlatformSensorLinux::StartSensor( 73 bool PlatformSensorLinux::StartSensor(
63 const PlatformSensorConfiguration& configuration) { 74 const PlatformSensorConfiguration& configuration) {
64 DCHECK(task_runner_->BelongsToCurrentThread()); 75 DCHECK(task_runner_->BelongsToCurrentThread());
65 if (!sensor_reader_) 76 polling_thread_task_runner_->PostTask(
66 return false; 77 FROM_HERE,
67 return sensor_reader_->StartFetchingData(configuration); 78 base::Bind(&SensorReader::StartFetchingData,
79 base::Unretained(sensor_reader_.get()), configuration));
80 return true;
68 } 81 }
69 82
70 void PlatformSensorLinux::StopSensor() { 83 void PlatformSensorLinux::StopSensor() {
71 DCHECK(task_runner_->BelongsToCurrentThread()); 84 DCHECK(task_runner_->BelongsToCurrentThread());
72 DCHECK(sensor_reader_); 85 polling_thread_task_runner_->PostTask(
73 sensor_reader_->StopFetchingData(); 86 FROM_HERE, base::Bind(&SensorReader::StopFetchingData,
87 base::Unretained(sensor_reader_.get())));
74 } 88 }
75 89
76 bool PlatformSensorLinux::CheckSensorConfiguration( 90 bool PlatformSensorLinux::CheckSensorConfiguration(
77 const PlatformSensorConfiguration& configuration) { 91 const PlatformSensorConfiguration& configuration) {
78 DCHECK(task_runner_->BelongsToCurrentThread()); 92 DCHECK(task_runner_->BelongsToCurrentThread());
79 return configuration.frequency() > 0 && 93 return configuration.frequency() > 0 &&
80 configuration.frequency() <= default_configuration_.frequency(); 94 configuration.frequency() <= default_configuration_.frequency();
81 } 95 }
82 96
83 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { 97 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() {
84 DCHECK(task_runner_->BelongsToCurrentThread()); 98 DCHECK(task_runner_->BelongsToCurrentThread());
85 return default_configuration_; 99 return default_configuration_;
86 } 100 }
87 101
88 } // namespace device 102 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698