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) { | 339 TEST(WeakPtrTest, MoveOwnershipOfUnreferencedObject) { |
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) { | |
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 // Release the only WeakPtr. |
366 target.DetachFromThreadHack(); | 353 arrow->target.reset(); |
354 | |
355 // Now we should be able to create a new reference from this thread. | |
356 arrow->target = target.AsWeakPtr(); | |
367 | 357 |
368 // Re-bind to main thread. | 358 // Re-bind to main thread. |
369 EXPECT_EQ(&target, arrow->target.get()); | 359 EXPECT_EQ(&target, arrow->target.get()); |
370 | 360 |
371 // Main thread can now delete the target. | 361 // And the main thread can now delete the target. |
372 } | 362 } |
373 | 363 |
374 // WeakPtr can be deleted on non-owner thread. | 364 // WeakPtr can be deleted on non-owner thread. |
375 background.DeleteArrow(arrow); | 365 background.DeleteArrow(arrow); |
Wez
2013/08/01 21:09:10
nit: This doesn't feel like it belongs in this tes
no sievers
2013/08/02 22:24:09
Done.
NonOwnerThreadCanDeleteWeakPtr already cover
| |
376 } | 366 } |
377 | 367 |
368 TEST(WeakPtrTest, MoveOwnershipAfterInvalidate) { | |
369 BackgroundThread background; | |
370 background.Start(); | |
371 | |
372 Arrow arrow; | |
373 { | |
374 Target* target = new Target; | |
no sievers
2013/08/01 20:57:59
I could add some plumbing in Background to avoid i
Wez
2013/08/01 21:09:10
You can create a Target, wrap it with a WeakPtrFac
Wez
2013/08/01 21:09:10
nit: scoped_ptr<Target> target = new Target, and t
no sievers
2013/08/02 22:24:09
Done.
| |
375 WeakPtrFactory<Target> factory(target); | |
376 | |
377 // Bind to main thread. | |
378 arrow.target = factory.GetWeakPtr(); | |
379 EXPECT_EQ(target, arrow.target.get()); | |
380 | |
381 factory.InvalidateWeakPtrs(); | |
382 EXPECT_EQ(NULL, arrow.target.get()); | |
383 | |
384 arrow.target = factory.GetWeakPtr(); | |
385 // Re-bind to background thread. | |
386 EXPECT_EQ(target, background.DeRef(&arrow)); | |
387 | |
388 // And the background thread can now delete the target. | |
389 background.DeleteTarget(target); | |
390 | |
391 // Make sure we can delete the factory on the main thread. | |
Wez
2013/08/01 21:09:10
This code's not deleting the factory, it's droppin
| |
392 arrow.target.reset(); | |
no sievers
2013/08/01 20:57:59
This wouldn't be needed if I made the test the opp
Wez
2013/08/01 21:09:10
Why is it needed at all? It's just clearing the We
no sievers
2013/08/01 21:15:12
Because the factory's flag is now bound to the bac
| |
393 } | |
394 } | |
395 | |
378 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { | 396 TEST(WeakPtrTest, MainThreadRefOutlivesBackgroundThreadRef) { |
379 // Originating thread has a WeakPtr that outlives others. | 397 // Originating thread has a WeakPtr that outlives others. |
380 // - Main thread creates a WeakPtr | 398 // - Main thread creates a WeakPtr |
381 // - Background thread creates a WeakPtr copy from the one in main thread | 399 // - Background thread creates a WeakPtr copy from the one in main thread |
382 // - Destruct the WeakPtr on background thread | 400 // - Destruct the WeakPtr on background thread |
383 // - Destruct the WeakPtr on main thread | 401 // - Destruct the WeakPtr on main thread |
384 BackgroundThread background; | 402 BackgroundThread background; |
385 background.Start(); | 403 background.Start(); |
386 | 404 |
387 Target target; | 405 Target target; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 background.Start(); | 600 background.Start(); |
583 background.DeleteTarget(target.release()); | 601 background.DeleteTarget(target.release()); |
584 | 602 |
585 // Main thread attempts to dereference the target, violating thread binding. | 603 // Main thread attempts to dereference the target, violating thread binding. |
586 ASSERT_DEATH(arrow.target.get(), ""); | 604 ASSERT_DEATH(arrow.target.get(), ""); |
587 } | 605 } |
588 | 606 |
589 #endif | 607 #endif |
590 | 608 |
591 } // namespace base | 609 } // namespace base |
OLD | NEW |