Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put ThreadGlobalData in Thread #521

Merged
merged 0 commits into from May 15, 2022

Conversation

Constellation
Copy link
Member

@Constellation Constellation commented May 5, 2022

04f2a2b

Put ThreadGlobalData in Thread
https://bugs.webkit.org/show_bug.cgi?id=240116

Reviewed by Darin Adler and Mark Lam.

This patch puts ThreadGlobalData into Thread's m_clientData field.
Thread is stored in fast TLS, so accessing to this field is quite fast
compared to the current ThreadSpecific one.

At the same time, this change can remove a hack in ThreadGlobalData.
Previously worker thread needs to tear down ThreadGlobalData explicitly
because Thread::current() can be destroyed earlier than ThreadGlobalData.
In that case, EventNames etc., which accesses to AtomString's destruction
can have problem because of lack of Thread::current() access. But since
we now move it to Thread, we can control how it is destroyed with Thread::current()
precisely, and we can explicitly destroy it before Thread::current() is fully
cleared. So we do not need to call it explicitly anymore.
Currently, we are calling it just to make ThreadGlobalData destroyed for debugging.

* Source/WebCore/PAL/pal/ThreadGlobalData.cpp:
(PAL::ThreadGlobalData::ThreadGlobalData):
(PAL::ThreadGlobalData::destroy): Deleted.
* Source/WebCore/PAL/pal/ThreadGlobalData.h:
(PAL::ThreadGlobalData::ThreadGlobalData::cachedConverterICU): Deleted.
* Source/WTF/wtf/Threading.h:
(WTF::Thread::Thread):
* Source/WTF/wtf/posix/ThreadingPOSIX.cpp:
(WTF::Thread::destructTLS):
* Source/WTF/wtf/win/ThreadingWin.cpp:
(WTF::Thread::ThreadHolder::~ThreadHolder):
* Source/WebCore/platform/ThreadGlobalData.cpp:
(WebCore::ThreadGlobalData::destroy):
(WebCore::ThreadGlobalData::setWebCoreThreadData):
(WebCore::threadGlobalData):

Canonical link: https://commits.webkit.org/250571@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294213 268f45cc-cd09-0410-ab3c-d52691b4dbfc

@Constellation Constellation self-assigned this May 5, 2022
@Constellation Constellation added New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 5, 2022
@Constellation Constellation requested a review from cdumez May 5, 2022 11:05
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 5, 2022
@cdumez
Copy link
Contributor

cdumez commented May 5, 2022

EWS failures look real:

stderr:
ASSERTION FAILED: The string being removed is an atom in the string table of an other thread!
iterator != atomStringTable.end()
/Volumes/Data/worker/macOS-AppleSilicon-Big-Sur-Debug-Build-EWS/build/Source/WTF/wtf/text/AtomStringImpl.cpp(451) : static void WTF::AtomStringImpl::remove(WTF::AtomStringImpl *)
1   0x14231dbf8 WTFCrash
2   0x1423261dc WTF::AtomStringImpl::remove(WTF::AtomStringImpl*)
3   0x1423faf8c WTF::StringImpl::~StringImpl()
4   0x1423fb330 WTF::StringImpl::~StringImpl()
5   0x1423fb35c WTF::StringImpl::destroy(WTF::StringImpl*)
6   0x11bf28a34 WTF::StringImpl::deref()
7   0x11c398018 WTF::DefaultRefDerefTraits<WTF::StringImpl>::derefIfNotNull(WTF::StringImpl*)
8   0x11bf28a7c WTF::RefPtr<WTF::StringImpl, WTF::RawPtrTraits<WTF::StringImpl>, WTF::DefaultRefDerefTraits<WTF::StringImpl> >::~RefPtr()
9   0x11c0115a0 WTF::RefPtr<WTF::StringImpl, WTF::RawPtrTraits<WTF::StringImpl>, WTF::DefaultRefDerefTraits<WTF::StringImpl> >::~RefPtr()
10  0x11bf28c00 WTF::String::~String()
11  0x11bfc7c90 WTF::String::~String()
12  0x11be30d20 WTF::AtomString::~AtomString()
13  0x11bebf470 WTF::AtomString::~AtomString()

@Constellation
Copy link
Member Author

Yup, this looks like a real issue.

@Constellation Constellation marked this pull request as draft May 6, 2022 00:07
@Constellation Constellation removed merging-blocked Applied to prevent a change from being merged New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 6, 2022
@Constellation Constellation added New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 6, 2022
@Constellation Constellation marked this pull request as ready for review May 6, 2022 00:29
@Constellation Constellation marked this pull request as draft May 6, 2022 04:45
@Constellation
Copy link
Member Author

Interesting, we are still having some crashes, looking.

@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 6, 2022
@Constellation Constellation removed merging-blocked Applied to prevent a change from being merged New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 7, 2022
@Constellation Constellation added New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 7, 2022
@Constellation Constellation marked this pull request as ready for review May 7, 2022 22:16
Copy link
Member

@darinadler darinadler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be even better to not have to dereference a second pointer at all, and store all this inside the Thread object, allocated with an appropriate size.

I’m not sure the reference counting is needed here. This is a nice improvement.

@@ -109,6 +109,11 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCounted<Thread>
friend class ThreadGroup;
friend WTF_EXPORT_PRIVATE void initialize();

class ApplicationData : public ThreadSafeRefCounted<ApplicationData> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think application is a great term to use here. I understand we need some abstract way to refer to WebCore from JavaScriptCore, but application has a pretty specific meaning as a term of art on platforms like iOS, and WebCore is not one of those things.

Why does this need to be reference counted? What is the second owner besides the Thread?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ClientData?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClientData sounds good.

Why does this need to be reference counted? What is the second owner besides the Thread?

Because WebThread is sharing it with the main thread. So, in iOS, WebKitLegacy, it is shared by multiple threads (WebThread and OS main thread).

if (LIKELY(applicationData))
return *static_cast<ThreadGlobalData*>(applicationData);

auto data = adoptRef(*new ThreadGlobalData());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add the () here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped.

if (LIKELY(applicationData))
return *static_cast<ThreadGlobalData*>(applicationData);

auto data = adoptRef(*new ThreadGlobalData());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add the () here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, dropped.

@@ -109,6 +109,11 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCounted<Thread>
friend class ThreadGroup;
friend WTF_EXPORT_PRIVATE void initialize();

class ApplicationData : public ThreadSafeRefCounted<ApplicationData> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ClientData?

@@ -76,42 +63,42 @@ void ThreadGlobalData::setWebCoreThreadData()
ASSERT(&threadGlobalData() != sharedMainThreadStaticData);

// Set WebThread's ThreadGlobalData object to be the same as the main UI thread.
**staticData = adoptRef(sharedMainThreadStaticData);
Thread::current().m_applicationData = adoptRef(sharedMainThreadStaticData);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASSERT(&threadGlobalData() != sharedMainThreadStaticData); implies that Thread::current().m_applicationData should already be pointing to sharedMainThreadStaticData. Do we still need this ThreadGlobalData::setWebCoreThreadData function?

Maybe remove this assignment, and rename this function to assertWebCoreThreadData() instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no. ASSERT(&threadGlobalData() != sharedMainThreadStaticData); means that Thread::current().m_applicationData is not pointing to sharedMainThreadStaticData.

Copy link
Member Author

@Constellation Constellation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

if (LIKELY(applicationData))
return *static_cast<ThreadGlobalData*>(applicationData);

auto data = adoptRef(*new ThreadGlobalData());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, dropped.

@@ -76,42 +63,42 @@ void ThreadGlobalData::setWebCoreThreadData()
ASSERT(&threadGlobalData() != sharedMainThreadStaticData);

// Set WebThread's ThreadGlobalData object to be the same as the main UI thread.
**staticData = adoptRef(sharedMainThreadStaticData);
Thread::current().m_applicationData = adoptRef(sharedMainThreadStaticData);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no. ASSERT(&threadGlobalData() != sharedMainThreadStaticData); means that Thread::current().m_applicationData is not pointing to sharedMainThreadStaticData.

if (LIKELY(applicationData))
return *static_cast<ThreadGlobalData*>(applicationData);

auto data = adoptRef(*new ThreadGlobalData());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped.

Copy link
Member Author

@Constellation Constellation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TESTING (Oops, I pushed while testing some GitHub UI).

@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 11, 2022
@Constellation Constellation added merge-queue Applied to send a pull request to merge-queue and removed merging-blocked Applied to prevent a change from being merged labels May 14, 2022
@webkit-early-warning-system
Copy link
Collaborator

Commit message contains (OOPS!), blocking PR #521

@webkit-early-warning-system webkit-early-warning-system added merging-blocked Applied to prevent a change from being merged and removed merge-queue Applied to send a pull request to merge-queue labels May 14, 2022
@Constellation Constellation removed merging-blocked Applied to prevent a change from being merged New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build labels May 14, 2022
@Constellation Constellation added New Bugs Unclassified bugs are placed in this component until the correct component can be determined. WebKit Nightly Build merge-queue Applied to send a pull request to merge-queue labels May 14, 2022
@webkit-early-warning-system webkit-early-warning-system merged commit 04f2a2b into WebKit:main May 15, 2022
@webkit-early-warning-system
Copy link
Collaborator

Committed r294213 (250571@main): https://commits.webkit.org/250571@main

Reviewed commits have been landed. Closing PR #521 and removing active labels.

@webkit-early-warning-system webkit-early-warning-system removed the merge-queue Applied to send a pull request to merge-queue label May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Bugs Unclassified bugs are placed in this component until the correct component can be determined.
Projects
None yet
5 participants