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

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

Issue 2569763004: [sensors](Linux) Fix tsan data race in sensor reader (Closed)
Patch Set: modify code comments 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_provider_linux.h" 5 #include "device/generic_sensor/platform_sensor_provider_linux.h"
6 6
7 #include "base/memory/singleton.h" 7 #include "base/memory/singleton.h"
8 #include "base/task_runner_util.h"
8 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
9 #include "device/generic_sensor/linux/sensor_data_linux.h" 10 #include "device/generic_sensor/linux/sensor_data_linux.h"
10 #include "device/generic_sensor/platform_sensor_linux.h" 11 #include "device/generic_sensor/platform_sensor_linux.h"
12 #include "device/generic_sensor/platform_sensor_reader_linux.h"
11 13
12 namespace device { 14 namespace device {
13 15
14 // static 16 // static
15 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() { 17 PlatformSensorProviderLinux* PlatformSensorProviderLinux::GetInstance() {
16 return base::Singleton< 18 return base::Singleton<
17 PlatformSensorProviderLinux, 19 PlatformSensorProviderLinux,
18 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get(); 20 base::LeakySingletonTraits<PlatformSensorProviderLinux>>::get();
19 } 21 }
20 22
(...skipping 17 matching lines...) Expand all
38 if (!sensor_nodes_enumeration_started_) { 40 if (!sensor_nodes_enumeration_started_) {
39 sensor_nodes_enumeration_started_ = file_task_runner_->PostTask( 41 sensor_nodes_enumeration_started_ = file_task_runner_->PostTask(
40 FROM_HERE, 42 FROM_HERE,
41 base::Bind(&SensorDeviceManager::Start, 43 base::Bind(&SensorDeviceManager::Start,
42 base::Unretained(sensor_device_manager_.get()), this)); 44 base::Unretained(sensor_device_manager_.get()), this));
43 } 45 }
44 return; 46 return;
45 } 47 }
46 48
47 SensorInfoLinux* sensor_device = GetSensorDevice(type); 49 SensorInfoLinux* sensor_device = GetSensorDevice(type);
48 if (!sensor_device) { 50 if (!sensor_device || !StartPollingThread()) {
49 // If there are no sensors, stop polling thread. 51 // If there are no sensors, stop polling thread.
50 if (!HasSensors()) 52 if (!HasSensors())
51 AllSensorsRemoved(); 53 AllSensorsRemoved();
52 callback.Run(nullptr); 54 callback.Run(nullptr);
53 return; 55 return;
54 } 56 }
55 SensorDeviceFound(type, std::move(mapping), callback, sensor_device); 57
58 base::PostTaskAndReplyWithResult(
59 polling_thread_->task_runner().get(), FROM_HERE,
60 base::Bind(SensorReader::Create, sensor_device,
Mikhail 2016/12/15 10:57:48 Let's delegate SensorReader creation to the sensor
maksims (do not use this acc) 2016/12/16 08:39:22 Done.
61 base::ThreadTaskRunnerHandle::Get()),
62 base::Bind(&PlatformSensorProviderLinux::SensorReaderCreated,
63 base::Unretained(this), type, base::Passed(&mapping), callback,
64 sensor_device));
56 } 65 }
57 66
58 void PlatformSensorProviderLinux::SensorDeviceFound( 67 void PlatformSensorProviderLinux::SensorReaderCreated(
59 mojom::SensorType type, 68 mojom::SensorType type,
60 mojo::ScopedSharedBufferMapping mapping, 69 mojo::ScopedSharedBufferMapping mapping,
61 const PlatformSensorProviderBase::CreateSensorCallback& callback, 70 const PlatformSensorProviderBase::CreateSensorCallback& callback,
62 SensorInfoLinux* sensor_device) { 71 SensorInfoLinux* const sensor_device,
72 std::unique_ptr<SensorReader> sensor_reader) {
63 DCHECK(CalledOnValidThread()); 73 DCHECK(CalledOnValidThread());
74 DCHECK(sensor_reader);
75 DCHECK(sensor_device);
64 76
65 if (!StartPollingThread()) { 77 scoped_refptr<PlatformSensorLinux> sensor = new PlatformSensorLinux(
66 callback.Run(nullptr); 78 type, std::move(mapping), this, sensor_device, std::move(sensor_reader),
67 return; 79 polling_thread_->task_runner());
68 }
69
70 scoped_refptr<PlatformSensorLinux> sensor =
71 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device,
72 polling_thread_->task_runner());
73 callback.Run(sensor); 80 callback.Run(sensor);
74 } 81 }
75 82
76 void PlatformSensorProviderLinux::SetFileTaskRunner( 83 void PlatformSensorProviderLinux::SetFileTaskRunner(
77 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { 84 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
78 DCHECK(CalledOnValidThread()); 85 DCHECK(CalledOnValidThread());
79 if (!file_task_runner_) 86 if (!file_task_runner_)
80 file_task_runner_ = file_task_runner; 87 file_task_runner_ = file_task_runner;
81 } 88 }
82 89
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 DCHECK(CalledOnValidThread()); 154 DCHECK(CalledOnValidThread());
148 file_task_runner_ = std::move(task_runner); 155 file_task_runner_ = std::move(task_runner);
149 } 156 }
150 157
151 void PlatformSensorProviderLinux::ProcessStoredRequests() { 158 void PlatformSensorProviderLinux::ProcessStoredRequests() {
152 DCHECK(CalledOnValidThread()); 159 DCHECK(CalledOnValidThread());
153 std::vector<mojom::SensorType> request_types = GetPendingRequestTypes(); 160 std::vector<mojom::SensorType> request_types = GetPendingRequestTypes();
154 if (request_types.empty()) 161 if (request_types.empty())
155 return; 162 return;
156 163
164 mojom::SensorType previous_type;
157 for (auto const& type : request_types) { 165 for (auto const& type : request_types) {
158 SensorInfoLinux* device = nullptr; 166 DCHECK_NE(type, previous_type);
159 auto device_entry = sensor_devices_by_type_.find(type); 167 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
160 if (device_entry != sensor_devices_by_type_.end()) 168 CreateSensorInternal(
161 device = device_entry->second.get(); 169 type, std::move(mapping),
162 CreateSensorAndNotify(type, device); 170 base::Bind(&PlatformSensorProviderLinux::NotifySensorCreated,
171 base::Unretained(this), type));
Reilly Grant (use Gerrit) 2016/12/14 18:56:08 It doesn't seem necessary to pass this as a callba
maksims (do not use this acc) 2016/12/16 08:39:22 Done.
172 previous_type = type;
163 } 173 }
164 } 174 }
165 175
166 void PlatformSensorProviderLinux::CreateSensorAndNotify(
167 mojom::SensorType type,
168 SensorInfoLinux* sensor_device) {
169 DCHECK(CalledOnValidThread());
170 scoped_refptr<PlatformSensorLinux> sensor;
171 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
172 if (sensor_device && mapping && StartPollingThread()) {
173 sensor =
174 new PlatformSensorLinux(type, std::move(mapping), this, sensor_device,
175 polling_thread_->task_runner());
176 }
177 NotifySensorCreated(type, sensor);
178 }
179
180 void PlatformSensorProviderLinux::OnSensorNodesEnumerated() { 176 void PlatformSensorProviderLinux::OnSensorNodesEnumerated() {
181 DCHECK(CalledOnValidThread()); 177 DCHECK(CalledOnValidThread());
182 DCHECK(!sensor_nodes_enumerated_); 178 DCHECK(!sensor_nodes_enumerated_);
183 sensor_nodes_enumerated_ = true; 179 sensor_nodes_enumerated_ = true;
184 ProcessStoredRequests(); 180 ProcessStoredRequests();
185 } 181 }
186 182
187 void PlatformSensorProviderLinux::OnDeviceAdded( 183 void PlatformSensorProviderLinux::OnDeviceAdded(
188 mojom::SensorType type, 184 mojom::SensorType type,
189 std::unique_ptr<SensorInfoLinux> sensor_device) { 185 std::unique_ptr<SensorInfoLinux> sensor_device) {
(...skipping 11 matching lines...) Expand all
201 mojom::SensorType type, 197 mojom::SensorType type,
202 const std::string& device_node) { 198 const std::string& device_node) {
203 DCHECK(CalledOnValidThread()); 199 DCHECK(CalledOnValidThread());
204 auto it = sensor_devices_by_type_.find(type); 200 auto it = sensor_devices_by_type_.find(type);
205 if (it != sensor_devices_by_type_.end() && 201 if (it != sensor_devices_by_type_.end() &&
206 it->second->device_node == device_node) 202 it->second->device_node == device_node)
207 sensor_devices_by_type_.erase(it); 203 sensor_devices_by_type_.erase(it);
208 } 204 }
209 205
210 } // namespace device 206 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698