OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 6480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6491 class ThreadedClearOnShutdownTester : public ThreadedTesterBase { | 6491 class ThreadedClearOnShutdownTester : public ThreadedTesterBase { |
6492 public: | 6492 public: |
6493 static void test() | 6493 static void test() |
6494 { | 6494 { |
6495 IntWrapper::s_destructorCalls = 0; | 6495 IntWrapper::s_destructorCalls = 0; |
6496 ThreadedTesterBase::test(new ThreadedClearOnShutdownTester); | 6496 ThreadedTesterBase::test(new ThreadedClearOnShutdownTester); |
6497 EXPECT_EQ(numberOfThreads, IntWrapper::s_destructorCalls); | 6497 EXPECT_EQ(numberOfThreads, IntWrapper::s_destructorCalls); |
6498 } | 6498 } |
6499 | 6499 |
6500 private: | 6500 private: |
| 6501 void runWhileAttached(); |
| 6502 |
6501 void runThread() override | 6503 void runThread() override |
6502 { | 6504 { |
6503 ThreadState::attachCurrentThread(false); | 6505 ThreadState::attachCurrentThread(false); |
6504 EXPECT_EQ(42, threadSpecificIntWrapper().value()); | 6506 EXPECT_EQ(42, threadSpecificIntWrapper().value()); |
| 6507 runWhileAttached(); |
6505 ThreadState::detachCurrentThread(); | 6508 ThreadState::detachCurrentThread(); |
6506 atomicDecrement(&m_threadsToFinish); | 6509 atomicDecrement(&m_threadsToFinish); |
6507 } | 6510 } |
6508 | 6511 |
| 6512 class HeapObject; |
| 6513 friend class HeapObject; |
| 6514 |
| 6515 using WeakHeapObjectSet = PersistentHeapHashSet<WeakMember<HeapObject>>; |
| 6516 |
| 6517 static WeakHeapObjectSet& weakHeapObjectSet(); |
| 6518 |
| 6519 using HeapObjectSet = PersistentHeapHashSet<Member<HeapObject>>; |
| 6520 static HeapObjectSet& heapObjectSet(); |
| 6521 |
6509 static IntWrapper& threadSpecificIntWrapper() | 6522 static IntWrapper& threadSpecificIntWrapper() |
6510 { | 6523 { |
6511 DEFINE_THREAD_SAFE_STATIC_LOCAL( | 6524 DEFINE_THREAD_SAFE_STATIC_LOCAL( |
6512 ThreadSpecific<Persistent<IntWrapper>>, intWrapper, | 6525 ThreadSpecific<Persistent<IntWrapper>>, intWrapper, |
6513 new ThreadSpecific<Persistent<IntWrapper>>); | 6526 new ThreadSpecific<Persistent<IntWrapper>>); |
6514 Persistent<IntWrapper>& handle = *intWrapper; | 6527 Persistent<IntWrapper>& handle = *intWrapper; |
6515 if (!handle) { | 6528 if (!handle) { |
6516 handle = new IntWrapper(42); | 6529 handle = new IntWrapper(42); |
6517 handle.registerAsStaticReference(); | 6530 handle.registerAsStaticReference(); |
6518 } | 6531 } |
6519 return *handle; | 6532 return *handle; |
6520 } | 6533 } |
6521 }; | 6534 }; |
6522 | 6535 |
| 6536 class ThreadedClearOnShutdownTester::HeapObject final : public GarbageCollectedF
inalized<ThreadedClearOnShutdownTester::HeapObject> { |
| 6537 public: |
| 6538 static HeapObject* create(bool testDestructor) |
| 6539 { |
| 6540 return new HeapObject(testDestructor); |
| 6541 } |
| 6542 |
| 6543 ~HeapObject() |
| 6544 { |
| 6545 if (!m_testDestructor) |
| 6546 return; |
| 6547 |
| 6548 // Verify that the weak reference is gone. |
| 6549 EXPECT_FALSE(weakHeapObjectSet().contains(this)); |
| 6550 |
| 6551 // Add a new member to the static singleton; this will |
| 6552 // re-initializes the persistent node of the collection |
| 6553 // object. Done while terminating the test thread, so |
| 6554 // verify that this brings about the release of the |
| 6555 // persistent also. |
| 6556 heapObjectSet().add(create(false)); |
| 6557 } |
| 6558 |
| 6559 DEFINE_INLINE_TRACE() { } |
| 6560 |
| 6561 private: |
| 6562 explicit HeapObject(bool testDestructor) |
| 6563 : m_testDestructor(testDestructor) |
| 6564 { |
| 6565 } |
| 6566 |
| 6567 bool m_testDestructor; |
| 6568 }; |
| 6569 |
| 6570 ThreadedClearOnShutdownTester::WeakHeapObjectSet& ThreadedClearOnShutdownTester:
:weakHeapObjectSet() |
| 6571 { |
| 6572 DEFINE_THREAD_SAFE_STATIC_LOCAL( |
| 6573 ThreadSpecific<WeakHeapObjectSet>, singleton, |
| 6574 new ThreadSpecific<WeakHeapObjectSet>); |
| 6575 if (!singleton.isSet()) |
| 6576 singleton->registerAsStaticReference(); |
| 6577 |
| 6578 return *singleton; |
| 6579 } |
| 6580 |
| 6581 ThreadedClearOnShutdownTester::HeapObjectSet& ThreadedClearOnShutdownTester::hea
pObjectSet() |
| 6582 { |
| 6583 DEFINE_THREAD_SAFE_STATIC_LOCAL( |
| 6584 ThreadSpecific<HeapObjectSet>, singleton, |
| 6585 new ThreadSpecific<HeapObjectSet>); |
| 6586 if (!singleton.isSet()) |
| 6587 singleton->registerAsStaticReference(); |
| 6588 |
| 6589 return *singleton; |
| 6590 } |
| 6591 |
| 6592 void ThreadedClearOnShutdownTester::runWhileAttached() |
| 6593 { |
| 6594 EXPECT_EQ(42, threadSpecificIntWrapper().value()); |
| 6595 // Creates a thread-specific singleton to a weakly held object. |
| 6596 weakHeapObjectSet().add(HeapObject::create(true)); |
| 6597 } |
| 6598 |
6523 } // namespace | 6599 } // namespace |
6524 | 6600 |
6525 TEST(HeapTest, TestClearOnShutdown) | 6601 TEST(HeapTest, TestClearOnShutdown) |
6526 { | 6602 { |
6527 ThreadedClearOnShutdownTester::test(); | 6603 ThreadedClearOnShutdownTester::test(); |
6528 } | 6604 } |
6529 | 6605 |
6530 // Verify that WeakMember<const T> compiles and behaves as expected. | 6606 // Verify that WeakMember<const T> compiles and behaves as expected. |
6531 class WithWeakConstObject final : public GarbageCollected<WithWeakConstObject> { | 6607 class WithWeakConstObject final : public GarbageCollected<WithWeakConstObject> { |
6532 public: | 6608 public: |
(...skipping 27 matching lines...) Expand all Loading... |
6560 conservativelyCollectGarbage(); | 6636 conservativelyCollectGarbage(); |
6561 EXPECT_EQ(wrapper, weakWrapper->value()); | 6637 EXPECT_EQ(wrapper, weakWrapper->value()); |
6562 // Stub out any stack reference. | 6638 // Stub out any stack reference. |
6563 wrapper = nullptr; | 6639 wrapper = nullptr; |
6564 } | 6640 } |
6565 preciselyCollectGarbage(); | 6641 preciselyCollectGarbage(); |
6566 EXPECT_EQ(nullptr, weakWrapper->value()); | 6642 EXPECT_EQ(nullptr, weakWrapper->value()); |
6567 } | 6643 } |
6568 | 6644 |
6569 } // namespace blink | 6645 } // namespace blink |
OLD | NEW |