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); |
348 | |
349 // Bind to background thread. | |
363 EXPECT_EQ(&target, background.DeRef(arrow)); | 350 EXPECT_EQ(&target, background.DeRef(arrow)); |
364 | 351 |
365 // Detach from background thread. | 352 // Invalidating the only weak pointer unbinds it from the background thread. |
366 target.DetachFromThreadHack(); | 353 arrow->target.reset(); |
Wez
2013/07/31 22:46:43
This should crash, since you're invalidating from
| |
354 arrow->target = target.AsWeakPtr(); | |
367 | 355 |
368 // Re-bind to main thread. | 356 // Re-bind to main thread. |
369 EXPECT_EQ(&target, arrow->target.get()); | 357 EXPECT_EQ(&target, arrow->target.get()); |
370 | 358 |
371 // Main thread can now delete the target. | 359 // Main thread can now delete the target. |
372 } | 360 } |
373 | 361 |
374 // WeakPtr can be deleted on non-owner thread. | 362 // WeakPtr can be deleted on non-owner thread. |
375 background.DeleteArrow(arrow); | 363 background.DeleteArrow(arrow); |
376 } | 364 } |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 background.Start(); | 570 background.Start(); |
583 background.DeleteTarget(target.release()); | 571 background.DeleteTarget(target.release()); |
584 | 572 |
585 // Main thread attempts to dereference the target, violating thread binding. | 573 // Main thread attempts to dereference the target, violating thread binding. |
586 ASSERT_DEATH(arrow.target.get(), ""); | 574 ASSERT_DEATH(arrow.target.get(), ""); |
587 } | 575 } |
588 | 576 |
589 #endif | 577 #endif |
590 | 578 |
591 } // namespace base | 579 } // namespace base |
OLD | NEW |