OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef PersistentNode_h | 5 #ifndef PersistentNode_h |
6 #define PersistentNode_h | 6 #define PersistentNode_h |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include "platform/PlatformExport.h" | 9 #include "platform/PlatformExport.h" |
10 #include "platform/heap/ThreadState.h" | 10 #include "platform/heap/ThreadState.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 if (!persistent_node) | 195 if (!persistent_node) |
196 return; | 196 return; |
197 persistent_region_->FreePersistentNode(persistent_node); | 197 persistent_region_->FreePersistentNode(persistent_node); |
198 ReleaseStore(reinterpret_cast<void* volatile*>(&persistent_node), nullptr); | 198 ReleaseStore(reinterpret_cast<void* volatile*>(&persistent_node), nullptr); |
199 } | 199 } |
200 | 200 |
201 class LockScope final { | 201 class LockScope final { |
202 STACK_ALLOCATED(); | 202 STACK_ALLOCATED(); |
203 | 203 |
204 public: | 204 public: |
205 LockScope(CrossThreadPersistentRegion& persistent_region) | 205 LockScope(CrossThreadPersistentRegion& persistent_region, |
206 : persistent_region_(persistent_region) { | 206 bool try_lock = false) |
207 persistent_region_.lock(); | 207 : persistent_region_(persistent_region), locked_(true) { |
| 208 if (try_lock) |
| 209 locked_ = persistent_region_.TryLock(); |
| 210 else |
| 211 persistent_region_.lock(); |
208 } | 212 } |
209 ~LockScope() { persistent_region_.unlock(); } | 213 ~LockScope() { |
| 214 if (locked_) |
| 215 persistent_region_.unlock(); |
| 216 } |
| 217 |
| 218 // If the lock scope is set up with |try_lock| set to |true|, caller/user |
| 219 // is responsible for checking whether the GC lock was taken via |
| 220 // |HasLock()|. |
| 221 bool HasLock() const { return locked_; } |
210 | 222 |
211 private: | 223 private: |
212 CrossThreadPersistentRegion& persistent_region_; | 224 CrossThreadPersistentRegion& persistent_region_; |
| 225 bool locked_; |
213 }; | 226 }; |
214 | 227 |
215 void TracePersistentNodes(Visitor* visitor) { | 228 void TracePersistentNodes(Visitor* visitor) { |
216 // If this assert triggers, you're tracing without being in a LockScope. | 229 // If this assert triggers, you're tracing without being in a LockScope. |
217 #if DCHECK_IS_ON() | 230 #if DCHECK_IS_ON() |
218 DCHECK(mutex_.Locked()); | 231 DCHECK(mutex_.Locked()); |
219 #endif | 232 #endif |
220 persistent_region_->TracePersistentNodes( | 233 persistent_region_->TracePersistentNodes( |
221 visitor, CrossThreadPersistentRegion::ShouldTracePersistentNode); | 234 visitor, CrossThreadPersistentRegion::ShouldTracePersistentNode); |
222 } | 235 } |
223 | 236 |
224 void PrepareForThreadStateTermination(ThreadState*); | 237 void PrepareForThreadStateTermination(ThreadState*); |
225 | 238 |
226 NO_SANITIZE_ADDRESS | 239 NO_SANITIZE_ADDRESS |
227 static bool ShouldTracePersistentNode(Visitor*, PersistentNode*); | 240 static bool ShouldTracePersistentNode(Visitor*, PersistentNode*); |
228 | 241 |
229 #if defined(ADDRESS_SANITIZER) | 242 #if defined(ADDRESS_SANITIZER) |
230 void UnpoisonCrossThreadPersistents(); | 243 void UnpoisonCrossThreadPersistents(); |
231 #endif | 244 #endif |
232 | 245 |
233 private: | 246 private: |
234 friend class LockScope; | 247 friend class LockScope; |
235 | 248 |
236 void lock() { mutex_.lock(); } | 249 void lock() { mutex_.lock(); } |
237 | 250 |
238 void unlock() { mutex_.unlock(); } | 251 void unlock() { mutex_.unlock(); } |
239 | 252 |
| 253 bool TryLock() { return mutex_.TryLock(); } |
| 254 |
240 // We don't make CrossThreadPersistentRegion inherit from PersistentRegion | 255 // We don't make CrossThreadPersistentRegion inherit from PersistentRegion |
241 // because we don't want to virtualize performance-sensitive methods | 256 // because we don't want to virtualize performance-sensitive methods |
242 // such as PersistentRegion::allocate/freePersistentNode. | 257 // such as PersistentRegion::allocate/freePersistentNode. |
243 std::unique_ptr<PersistentRegion> persistent_region_; | 258 std::unique_ptr<PersistentRegion> persistent_region_; |
244 | 259 |
245 // Recursive as prepareForThreadStateTermination() clears a PersistentNode's | 260 // Recursive as prepareForThreadStateTermination() clears a PersistentNode's |
246 // associated Persistent<> -- it in turn freeing the PersistentNode. And both | 261 // associated Persistent<> -- it in turn freeing the PersistentNode. And both |
247 // CrossThreadPersistentRegion operations need a lock on the region before | 262 // CrossThreadPersistentRegion operations need a lock on the region before |
248 // mutating. | 263 // mutating. |
249 RecursiveMutex mutex_; | 264 RecursiveMutex mutex_; |
250 }; | 265 }; |
251 | 266 |
252 } // namespace blink | 267 } // namespace blink |
253 | 268 |
254 #endif | 269 #endif |
OLD | NEW |