| 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 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 329 |
| 330 // The new WeakPtr is owned by background thread. | 330 // The new WeakPtr is owned by background thread. |
| 331 EXPECT_EQ(target, background.DeRef(&arrow)); | 331 EXPECT_EQ(target, background.DeRef(&arrow)); |
| 332 } | 332 } |
| 333 | 333 |
| 334 // Target can only be deleted on background thread. | 334 // Target can only be deleted on background thread. |
| 335 background.DeleteTarget(target); | 335 background.DeleteTarget(target); |
| 336 background.DeleteArrow(arrow); | 336 background.DeleteArrow(arrow); |
| 337 } | 337 } |
| 338 | 338 |
| 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) { | 339 TEST(WeakPtrTest, MoveOwnershipExplicitly) { |
| 355 BackgroundThread background; | 340 BackgroundThread background; |
| 356 background.Start(); | 341 background.Start(); |
| 357 | 342 |
| 358 Arrow* arrow; | 343 Arrow* arrow; |
| 359 { | 344 { |
| 360 Target target; | 345 Target target; |
| 361 // Background thread creates WeakPtr(and implicitly owns the object). | 346 // Background thread creates WeakPtr. |
| 362 background.CreateArrowFromTarget(&arrow, &target); | 347 background.CreateArrowFromTarget(&arrow, &target); |
| 363 EXPECT_EQ(&target, background.DeRef(arrow)); | |
| 364 | 348 |
| 365 // Detach from background thread. | 349 // Bind to main thread. |
| 366 target.DetachFromThreadHack(); | |
| 367 | |
| 368 // Re-bind to main thread. | |
| 369 EXPECT_EQ(&target, arrow->target.get()); | 350 EXPECT_EQ(&target, arrow->target.get()); |
| 370 | 351 |
| 371 // Main thread can now delete the target. | 352 // Main thread can now delete the target. |
| 372 } | 353 } |
| 373 | 354 |
| 374 // WeakPtr can be deleted on non-owner thread. | 355 // WeakPtr can be deleted on non-owner thread. |
| 375 background.DeleteArrow(arrow); | 356 background.DeleteArrow(arrow); |
| 376 } | 357 } |
| 377 | 358 |
| 378 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { | 359 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 background.Start(); | 563 background.Start(); |
| 583 background.DeleteTarget(target.release()); | 564 background.DeleteTarget(target.release()); |
| 584 | 565 |
| 585 // Main thread attempts to dereference the target, violating thread binding. | 566 // Main thread attempts to dereference the target, violating thread binding. |
| 586 ASSERT_DEATH(arrow.target.get(), ""); | 567 ASSERT_DEATH(arrow.target.get(), ""); |
| 587 } | 568 } |
| 588 | 569 |
| 589 #endif | 570 #endif |
| 590 | 571 |
| 591 } // namespace base | 572 } // namespace base |
| OLD | NEW |