OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sensors/data_fetcher_shared_memory_base.h" | 5 #include "device/sensors/data_fetcher_shared_memory_base.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 105 } |
106 | 106 |
107 // --- end of PollingThread methods | 107 // --- end of PollingThread methods |
108 | 108 |
109 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() | 109 DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() |
110 : started_consumers_(0) {} | 110 : started_consumers_(0) {} |
111 | 111 |
112 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { | 112 DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { |
113 DCHECK_EQ(0u, started_consumers_); | 113 DCHECK_EQ(0u, started_consumers_); |
114 | 114 |
115 // make sure polling thread stops asap. | 115 // By this point the polling thread should have already been stopped (it's not |
116 if (polling_thread_) | 116 // safe for it to be running in this class's destructor as tasks are posted to |
117 polling_thread_->Stop(); | 117 // it that call virtual methods of this class). |
| 118 DCHECK(!polling_thread_ || !polling_thread_->IsRunning()); |
118 } | 119 } |
119 | 120 |
120 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( | 121 bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( |
121 ConsumerType consumer_type) { | 122 ConsumerType consumer_type) { |
122 if (started_consumers_ & consumer_type) | 123 if (started_consumers_ & consumer_type) |
123 return true; | 124 return true; |
124 | 125 |
125 void* buffer = GetSharedMemoryBuffer(consumer_type); | 126 void* buffer = GetSharedMemoryBuffer(consumer_type); |
126 if (!buffer) | 127 if (!buffer) |
127 return false; | 128 return false; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 started_consumers_ ^= consumer_type; | 168 started_consumers_ ^= consumer_type; |
168 | 169 |
169 return true; | 170 return true; |
170 } | 171 } |
171 | 172 |
172 void DataFetcherSharedMemoryBase::Shutdown() { | 173 void DataFetcherSharedMemoryBase::Shutdown() { |
173 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); | 174 StopFetchingDeviceData(CONSUMER_TYPE_MOTION); |
174 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); | 175 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); |
175 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 176 StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
176 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); | 177 StopFetchingDeviceData(CONSUMER_TYPE_LIGHT); |
| 178 |
| 179 // Ensure that the polling thread stops before entering the destructor of the |
| 180 // subclass, as the stopping of the polling thread causes tasks to execute |
| 181 // that call virtual methods of this class, which can cause crashes if they |
| 182 // execute while (or after) the subclass is being torn down. |
| 183 if (polling_thread_) |
| 184 polling_thread_->Stop(); |
177 } | 185 } |
178 | 186 |
179 mojo::ScopedSharedBufferHandle | 187 mojo::ScopedSharedBufferHandle |
180 DataFetcherSharedMemoryBase::GetSharedMemoryHandle(ConsumerType consumer_type) { | 188 DataFetcherSharedMemoryBase::GetSharedMemoryHandle(ConsumerType consumer_type) { |
181 auto it = shared_memory_map_.find(consumer_type); | 189 auto it = shared_memory_map_.find(consumer_type); |
182 DCHECK(it != shared_memory_map_.end()); | 190 DCHECK(it != shared_memory_map_.end()); |
183 return it->second.first->Clone(); | 191 return it->second.first->Clone(); |
184 } | 192 } |
185 | 193 |
186 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { | 194 bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 241 |
234 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { | 242 base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { |
235 return polling_thread_ ? polling_thread_->message_loop() : nullptr; | 243 return polling_thread_ ? polling_thread_->message_loop() : nullptr; |
236 } | 244 } |
237 | 245 |
238 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { | 246 bool DataFetcherSharedMemoryBase::IsPollingTimerRunningForTesting() const { |
239 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; | 247 return polling_thread_ ? polling_thread_->IsTimerRunning() : false; |
240 } | 248 } |
241 | 249 |
242 } // namespace device | 250 } // namespace device |
OLD | NEW |