Changeset 248586 in webkit


Ignore:
Timestamp:
Aug 12, 2019 8:01:11 PM (5 years ago)
Author:
commit-queue@webkit.org
Message:

[WTF] Thread::removeFromThreadGroup leaks weak pointers.
https://bugs.webkit.org/show_bug.cgi?id=199857

Patch by Takashi Komori <Takashi.Komori@sony.com> on 2019-08-12
Reviewed by Yusuke Suzuki.

Source/WTF:

Fix leaking of ThreadGroup's weak pointers.

Tests: WTF.ThreadGroupRemove API tests

  • wtf/Threading.cpp:

(WTF::Thread::didExit):
(WTF::Thread::addToThreadGroup):
(WTF::Thread::removeFromThreadGroup):
(WTF::Thread::numberOfThreadGroups):

  • wtf/Threading.h:

Tools:

  • TestWebKitAPI/Tests/WTF/ThreadGroup.cpp:

(TestWebKitAPI::countThreadGroups):
(TestWebKitAPI::TEST):

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r248552 r248586  
     12019-08-12  Takashi Komori  <Takashi.Komori@sony.com>
     2
     3        [WTF] Thread::removeFromThreadGroup leaks weak pointers.
     4        https://bugs.webkit.org/show_bug.cgi?id=199857
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        Fix leaking of ThreadGroup's weak pointers.
     9
     10        Tests: WTF.ThreadGroupRemove API tests
     11
     12        * wtf/Threading.cpp:
     13        (WTF::Thread::didExit):
     14        (WTF::Thread::addToThreadGroup):
     15        (WTF::Thread::removeFromThreadGroup):
     16        (WTF::Thread::numberOfThreadGroups):
     17        * wtf/Threading.h:
     18
    1192019-08-12  Sam Weinig  <weinig@apple.com>
    220
  • trunk/Source/WTF/wtf/Threading.cpp

    r246490 r248586  
    212212            {
    213213                auto locker = holdLock(m_mutex);
    214                 for (auto& threadGroup : m_threadGroups) {
     214                for (auto& threadGroupPointerPair : m_threadGroupMap) {
    215215                    // If ThreadGroup is just being destroyed,
    216216                    // we do not need to perform unregistering.
    217                     if (auto retained = threadGroup.lock())
     217                    if (auto retained = threadGroupPointerPair.value.lock())
    218218                        threadGroups.append(WTFMove(retained));
    219219                }
     
    241241        return ThreadGroupAddResult::NotAdded;
    242242    if (threadGroup.m_threads.add(*this).isNewEntry) {
    243         m_threadGroups.append(threadGroup.weakFromThis());
     243        m_threadGroupMap.add(&threadGroup, threadGroup.weakFromThis());
    244244        return ThreadGroupAddResult::NewlyAdded;
    245245    }
     
    253253    if (m_isShuttingDown)
    254254        return;
    255     m_threadGroups.removeFirstMatching([&] (auto weakPtr) {
    256         if (auto sharedPtr = weakPtr.lock())
    257             return sharedPtr.get() == &threadGroup;
    258         return false;
    259     });
     255    m_threadGroupMap.remove(&threadGroup);
     256}
     257
     258unsigned Thread::numberOfThreadGroups()
     259{
     260    auto locker = holdLock(m_mutex);
     261    return m_threadGroupMap.size();
    260262}
    261263
  • trunk/Source/WTF/wtf/Threading.h

    r246490 r248586  
    3737#include <wtf/FastTLS.h>
    3838#include <wtf/Function.h>
     39#include <wtf/HashMap.h>
    3940#include <wtf/HashSet.h>
    4041#include <wtf/PlatformRegisters.h>
     
    9697    WTF_EXPORT_PRIVATE static Lock& allThreadsMutex();
    9798
     99    WTF_EXPORT_PRIVATE unsigned numberOfThreadGroups();
     100
    98101#if OS(WINDOWS)
    99102    // Returns ThreadIdentifier directly. It is useful if the user only cares about identity
     
    291294    WordLock m_mutex;
    292295    StackBounds m_stack { StackBounds::emptyBounds() };
    293     Vector<std::weak_ptr<ThreadGroup>> m_threadGroups;
     296    HashMap<ThreadGroup*, std::weak_ptr<ThreadGroup>> m_threadGroupMap;
    294297    PlatformThreadHandle m_handle;
    295298#if OS(WINDOWS)
  • trunk/Tools/ChangeLog

    r248584 r248586  
     12019-08-12  Takashi Komori  <Takashi.Komori@sony.com>
     2
     3        [WTF] Thread::removeFromThreadGroup leaks weak pointers.
     4        https://bugs.webkit.org/show_bug.cgi?id=199857
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        * TestWebKitAPI/Tests/WTF/ThreadGroup.cpp:
     9        (TestWebKitAPI::countThreadGroups):
     10        (TestWebKitAPI::TEST):
     11
    1122019-08-12  Alexey Shvayka  <shvaikalesh@gmail.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WTF/ThreadGroup.cpp

    r229209 r248586  
    143143}
    144144
     145static unsigned countThreadGroups(Vector<Ref<Thread>>& threads)
     146{
     147    unsigned count = 0;
     148    for (auto& thread : threads)
     149        count += thread->numberOfThreadGroups();
     150
     151    return count;
     152}
     153
     154TEST(WTF, ThreadGroupRemove)
     155{
     156    const unsigned NumberOfThreads = 64;
     157
     158    Lock lock;
     159    Condition threadRunningCondition;
     160    bool threadRunning = true;
     161
     162    Vector<Ref<Thread>> threads;
     163
     164    auto threadGroup = ThreadGroup::create();
     165    for (unsigned i = 0; i < NumberOfThreads; i++) {
     166        auto thread = Thread::create("ThreadGroupWorker", [&]() {
     167            auto locker = holdLock(lock);
     168            threadRunningCondition.wait(lock, [&]() {
     169                return !threadRunning;
     170            });
     171        });
     172        threadGroup->add(thread.get());
     173        threads.append(WTFMove(thread));
     174    }
     175
     176    EXPECT_EQ(NumberOfThreads, countThreadGroups(threads));
     177
     178    threadGroup = nullptr;
     179
     180    EXPECT_EQ(0U, countThreadGroups(threads));
     181
     182    threadRunning = false;
     183    threadRunningCondition.notifyAll();
     184    for (unsigned i = 0; i < NumberOfThreads; i++)
     185        threads[i]->waitForCompletion();
     186}
     187
    145188} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.