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

Unified Diff: device/generic_sensor/platform_sensor_provider_linux.cc

Issue 2569763004: [sensors](Linux) Fix tsan data race in sensor reader (Closed)
Patch Set: change threading model. SensorReader now belongs to a polling thread 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 side-by-side diff with in-line comments
Download patch
Index: device/generic_sensor/platform_sensor_provider_linux.cc
diff --git a/device/generic_sensor/platform_sensor_provider_linux.cc b/device/generic_sensor/platform_sensor_provider_linux.cc
index 736c82ff55ab9dbfbf79dc45af2c28867d78915f..724d2ba2d8631e2f747a3fd065d36224c533a957 100644
--- a/device/generic_sensor/platform_sensor_provider_linux.cc
+++ b/device/generic_sensor/platform_sensor_provider_linux.cc
@@ -5,9 +5,11 @@
#include "device/generic_sensor/platform_sensor_provider_linux.h"
#include "base/memory/singleton.h"
+#include "base/task_runner_util.h"
#include "base/threading/thread.h"
#include "device/generic_sensor/linux/sensor_data_linux.h"
#include "device/generic_sensor/platform_sensor_linux.h"
+#include "device/generic_sensor/platform_sensor_reader_linux.h"
namespace device {
@@ -45,31 +47,36 @@ void PlatformSensorProviderLinux::CreateSensorInternal(
}
SensorInfoLinux* sensor_device = GetSensorDevice(type);
- if (!sensor_device) {
+ if (!sensor_device || !StartPollingThread()) {
// If there are no sensors, stop polling thread.
if (!HasSensors())
AllSensorsRemoved();
callback.Run(nullptr);
return;
}
- SensorDeviceFound(type, std::move(mapping), callback, sensor_device);
+
+ base::PostTaskAndReplyWithResult(
+ polling_thread_->task_runner().get(), FROM_HERE,
+ base::Bind(SensorReader::Create, sensor_device,
+ base::ThreadTaskRunnerHandle::Get()),
+ base::Bind(&PlatformSensorProviderLinux::SensorReaderCreated,
+ base::Unretained(this), type, base::Passed(&mapping), callback,
+ sensor_device));
}
-void PlatformSensorProviderLinux::SensorDeviceFound(
+void PlatformSensorProviderLinux::SensorReaderCreated(
mojom::SensorType type,
mojo::ScopedSharedBufferMapping mapping,
const PlatformSensorProviderBase::CreateSensorCallback& callback,
- SensorInfoLinux* sensor_device) {
+ SensorInfoLinux* const sensor_device,
+ std::unique_ptr<SensorReader> sensor_reader) {
DCHECK(CalledOnValidThread());
+ DCHECK(sensor_reader);
+ DCHECK(sensor_device);
- if (!StartPollingThread()) {
- callback.Run(nullptr);
- return;
- }
-
- scoped_refptr<PlatformSensorLinux> sensor =
- new PlatformSensorLinux(type, std::move(mapping), this, sensor_device,
- polling_thread_->task_runner());
+ scoped_refptr<PlatformSensorLinux> sensor = new PlatformSensorLinux(
+ type, std::move(mapping), this, sensor_device, std::move(sensor_reader),
+ polling_thread_->task_runner());
callback.Run(sensor);
}
@@ -154,27 +161,16 @@ void PlatformSensorProviderLinux::ProcessStoredRequests() {
if (request_types.empty())
return;
+ mojom::SensorType previous_type;
for (auto const& type : request_types) {
- SensorInfoLinux* device = nullptr;
- auto device_entry = sensor_devices_by_type_.find(type);
- if (device_entry != sensor_devices_by_type_.end())
- device = device_entry->second.get();
- CreateSensorAndNotify(type, device);
- }
-}
-
-void PlatformSensorProviderLinux::CreateSensorAndNotify(
- mojom::SensorType type,
- SensorInfoLinux* sensor_device) {
- DCHECK(CalledOnValidThread());
- scoped_refptr<PlatformSensorLinux> sensor;
- mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
- if (sensor_device && mapping && StartPollingThread()) {
- sensor =
- new PlatformSensorLinux(type, std::move(mapping), this, sensor_device,
- polling_thread_->task_runner());
+ DCHECK_NE(type, previous_type);
+ mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
+ CreateSensorInternal(
+ type, std::move(mapping),
+ base::Bind(&PlatformSensorProviderLinux::NotifySensorCreated,
+ base::Unretained(this), type));
+ previous_type = type;
}
- NotifySensorCreated(type, sensor);
}
void PlatformSensorProviderLinux::OnSensorNodesEnumerated() {

Powered by Google App Engine
This is Rietveld 408576698