Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 *result = new T; | 37 *result = new T; |
| 38 } | 38 } |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 struct Base { | 41 struct Base { |
| 42 std::string member; | 42 std::string member; |
| 43 }; | 43 }; |
| 44 struct Derived : public Base {}; | 44 struct Derived : public Base {}; |
| 45 | 45 |
| 46 struct TargetBase {}; | 46 struct TargetBase {}; |
| 47 struct Target : public TargetBase, public SupportsWeakPtr<Target> {}; | 47 struct Target : public TargetBase, public SupportsWeakPtr<Target> { |
| 48 public: | |
| 49 void InvalidateWeakPtrs() { SupportsWeakPtr::InvalidateWeakPtrs(); } | |
| 50 }; | |
| 48 struct DerivedTarget : public Target {}; | 51 struct DerivedTarget : public Target {}; |
| 49 struct Arrow { | 52 struct Arrow { |
| 50 WeakPtr<Target> target; | 53 WeakPtr<Target> target; |
| 51 }; | 54 }; |
| 52 | 55 |
| 53 // Helper class to create and destroy weak pointer copies | 56 // Helper class to create and destroy weak pointer copies |
| 54 // and delete objects on a background thread. | 57 // and delete objects on a background thread. |
| 55 class BackgroundThread : public Thread { | 58 class BackgroundThread : public Thread { |
| 56 public: | 59 public: |
| 57 BackgroundThread() : Thread("owner_thread") {} | 60 BackgroundThread() : Thread("owner_thread") {} |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 Target* DeRef(const Arrow* arrow) { | 118 Target* DeRef(const Arrow* arrow) { |
| 116 WaitableEvent completion(true, false); | 119 WaitableEvent completion(true, false); |
| 117 Target* result = NULL; | 120 Target* result = NULL; |
| 118 message_loop()->PostTask( | 121 message_loop()->PostTask( |
| 119 FROM_HERE, | 122 FROM_HERE, |
| 120 base::Bind(&BackgroundThread::DoDeRef, arrow, &result, &completion)); | 123 base::Bind(&BackgroundThread::DoDeRef, arrow, &result, &completion)); |
| 121 completion.Wait(); | 124 completion.Wait(); |
| 122 return result; | 125 return result; |
| 123 } | 126 } |
| 124 | 127 |
| 128 void InvalidateArrowsForTarget(Target* target) { | |
| 129 WaitableEvent completion(true, false); | |
| 130 message_loop()->PostTask( | |
| 131 FROM_HERE, | |
| 132 base::Bind(&BackgroundThread::DoInvalidateArrowsForTarget, | |
| 133 target, | |
| 134 &completion)); | |
| 135 completion.Wait(); | |
| 136 } | |
| 137 | |
| 125 protected: | 138 protected: |
| 126 static void DoCreateArrowFromArrow(Arrow** arrow, | 139 static void DoCreateArrowFromArrow(Arrow** arrow, |
| 127 const Arrow* other, | 140 const Arrow* other, |
| 128 WaitableEvent* completion) { | 141 WaitableEvent* completion) { |
| 129 *arrow = new Arrow; | 142 *arrow = new Arrow; |
| 130 **arrow = *other; | 143 **arrow = *other; |
| 131 completion->Signal(); | 144 completion->Signal(); |
| 132 } | 145 } |
| 133 | 146 |
| 134 static void DoCreateArrowFromTarget(Arrow** arrow, | 147 static void DoCreateArrowFromTarget(Arrow** arrow, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 // Assignment operator. | 180 // Assignment operator. |
| 168 WeakPtr<TargetBase> c; | 181 WeakPtr<TargetBase> c; |
| 169 c = object->target; | 182 c = object->target; |
| 170 completion->Signal(); | 183 completion->Signal(); |
| 171 } | 184 } |
| 172 | 185 |
| 173 static void DoDeleteArrow(Arrow* object, WaitableEvent* completion) { | 186 static void DoDeleteArrow(Arrow* object, WaitableEvent* completion) { |
| 174 delete object; | 187 delete object; |
| 175 completion->Signal(); | 188 completion->Signal(); |
| 176 } | 189 } |
| 190 | |
| 191 static void DoInvalidateArrowsForTarget(Target* target, WaitableEvent* | |
| 192 completion) { | |
| 193 target->InvalidateWeakPtrs(); | |
| 194 completion->Signal(); | |
| 195 } | |
| 177 }; | 196 }; |
| 178 | 197 |
| 179 } // namespace | 198 } // namespace |
| 180 | 199 |
| 181 TEST(WeakPtrFactoryTest, Basic) { | 200 TEST(WeakPtrFactoryTest, Basic) { |
| 182 int data; | 201 int data; |
| 183 WeakPtrFactory<int> factory(&data); | 202 WeakPtrFactory<int> factory(&data); |
| 184 WeakPtr<int> ptr = factory.GetWeakPtr(); | 203 WeakPtr<int> ptr = factory.GetWeakPtr(); |
| 185 EXPECT_EQ(&data, ptr.get()); | 204 EXPECT_EQ(&data, ptr.get()); |
| 186 } | 205 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 | 348 |
| 330 // The new WeakPtr is owned by background thread. | 349 // The new WeakPtr is owned by background thread. |
| 331 EXPECT_EQ(target, background.DeRef(&arrow)); | 350 EXPECT_EQ(target, background.DeRef(&arrow)); |
| 332 } | 351 } |
| 333 | 352 |
| 334 // Target can only be deleted on background thread. | 353 // Target can only be deleted on background thread. |
| 335 background.DeleteTarget(target); | 354 background.DeleteTarget(target); |
| 336 background.DeleteArrow(arrow); | 355 background.DeleteArrow(arrow); |
| 337 } | 356 } |
| 338 | 357 |
| 339 TEST(WeakPtrTest, MoveOwnershipExplicitlyObjectNotReferenced) { | |
| 340 // Case 1: The target is not bound to any thread yet. So calling | |
| 341 // DetachFromThread() is a no-op. | |
| 342 Target target; | |
| 343 target.DetachFromThreadHack(); | |
| 344 | |
| 345 // Case 2: The target is bound to main thread but no WeakPtr is pointing to | |
| 346 // it. In this case, it will be re-bound to any thread trying to get a | |
| 347 // WeakPtr pointing to it. So detach function call is again no-op. | |
| 348 { | |
| 349 WeakPtr<Target> weak_ptr = target.AsWeakPtr(); | |
| 350 } | |
| 351 target.DetachFromThreadHack(); | |
| 352 } | |
| 353 | |
| 354 TEST(WeakPtrTest, MoveOwnershipExplicitly) { | 358 TEST(WeakPtrTest, MoveOwnershipExplicitly) { |
| 355 BackgroundThread background; | 359 BackgroundThread background; |
| 356 background.Start(); | 360 background.Start(); |
| 357 | 361 |
| 358 Arrow* arrow; | 362 Arrow* arrow; |
| 359 { | 363 { |
| 360 Target target; | 364 Target target; |
| 361 // Background thread creates WeakPtr(and implicitly owns the object). | 365 // Background thread creates WeakPtr. |
| 362 background.CreateArrowFromTarget(&arrow, &target); | 366 background.CreateArrowFromTarget(&arrow, &target); |
| 367 | |
| 368 // Bind to background thread. | |
| 363 EXPECT_EQ(&target, background.DeRef(arrow)); | 369 EXPECT_EQ(&target, background.DeRef(arrow)); |
| 364 | 370 |
| 365 // Detach from background thread. | 371 // Release the only WeakPtr. |
| 366 target.DetachFromThreadHack(); | 372 arrow->target.reset(); |
|
no sievers
2013/08/01 01:12:04
Ok, to follow up on the original discussion:
This
Wez
2013/08/01 18:30:31
Right, so this test is really MoveOwnershipOfUnref
no sievers
2013/08/01 20:57:58
Done.
| |
| 373 | |
| 374 // Now we should be able to create a new reference from this thread. | |
| 375 arrow->target = target.AsWeakPtr(); | |
| 367 | 376 |
| 368 // Re-bind to main thread. | 377 // Re-bind to main thread. |
| 369 EXPECT_EQ(&target, arrow->target.get()); | 378 EXPECT_EQ(&target, arrow->target.get()); |
| 370 | 379 |
| 371 // Main thread can now delete the target. | 380 // And the main thread can now delete the target. |
| 372 } | 381 } |
| 373 | 382 |
| 374 // WeakPtr can be deleted on non-owner thread. | 383 // WeakPtr can be deleted on non-owner thread. |
| 384 background.DeleteArrow(arrow); | |
| 385 } | |
| 386 | |
| 387 TEST(WeakPtrTest, MoveOwnershipExplicitlyWithInvalidate) { | |
| 388 BackgroundThread background; | |
| 389 background.Start(); | |
| 390 | |
| 391 Arrow* arrow; | |
| 392 { | |
| 393 Target target; | |
| 394 // Background thread creates WeakPtr. | |
| 395 background.CreateArrowFromTarget(&arrow, &target); | |
| 396 | |
| 397 // Bind to background thread. | |
| 398 EXPECT_EQ(&target, background.DeRef(arrow)); | |
| 399 | |
| 400 background.InvalidateArrowsForTarget(&target); | |
|
Wez
2013/08/01 18:30:31
If you can rewrite this specific test to use a Wea
| |
| 401 EXPECT_EQ(NULL, background.DeRef(arrow)); | |
|
no sievers
2013/08/01 01:12:04
This is the same test as above, but it invalidates
| |
| 402 | |
| 403 // Release the only WeakPtr. | |
| 404 arrow->target.reset(); | |
|
Wez
2013/08/01 18:30:31
Don't do this here - the test is specifically for
no sievers
2013/08/01 20:57:58
Yea, that's making sense now. Done.
| |
| 405 | |
| 406 // Now we should be able to create a new reference from this thread. | |
| 407 arrow->target = target.AsWeakPtr(); | |
| 408 | |
| 409 // Re-bind to main thread. | |
| 410 EXPECT_EQ(&target, arrow->target.get()); | |
| 411 | |
| 412 // And the main thread can now delete the target. | |
| 413 } | |
| 414 | |
| 415 // WeakPtr can be deleted on non-owner thread. | |
| 375 background.DeleteArrow(arrow); | 416 background.DeleteArrow(arrow); |
|
Wez
2013/08/01 18:30:31
IIRC this aspect of WeakPtr semantics is tested el
| |
| 376 } | 417 } |
| 377 | 418 |
| 378 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { | 419 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { |
| 379 // Originating thread has a WeakPtr that outlives others. | 420 // Originating thread has a WeakPtr that outlives others. |
| 380 // - Main thread creates a WeakPtr | 421 // - Main thread creates a WeakPtr |
| 381 // - Background thread creates a WeakPtr copy from the one in main thread | 422 // - Background thread creates a WeakPtr copy from the one in main thread |
| 382 // - Destruct the WeakPtr on background thread | 423 // - Destruct the WeakPtr on background thread |
| 383 // - Destruct the WeakPtr on main thread | 424 // - Destruct the WeakPtr on main thread |
| 384 BackgroundThread background; | 425 BackgroundThread background; |
| 385 background.Start(); | 426 background.Start(); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 background.Start(); | 623 background.Start(); |
| 583 background.DeleteTarget(target.release()); | 624 background.DeleteTarget(target.release()); |
| 584 | 625 |
| 585 // Main thread attempts to dereference the target, violating thread binding. | 626 // Main thread attempts to dereference the target, violating thread binding. |
| 586 ASSERT_DEATH(arrow.target.get(), ""); | 627 ASSERT_DEATH(arrow.target.get(), ""); |
| 587 } | 628 } |
| 588 | 629 |
| 589 #endif | 630 #endif |
| 590 | 631 |
| 591 } // namespace base | 632 } // namespace base |
| OLD | NEW |