Title: [210313] trunk
Revision
210313
Author
utatane....@gmail.com
Date
2017-01-04 18:53:00 -0800 (Wed, 04 Jan 2017)

Log Message

Limit thread name appropriately
https://bugs.webkit.org/show_bug.cgi?id=166676

Reviewed by Sam Weinig.

Source/WTF:

In some platform, the max length of thread names are limited.
For example, the number of the max length is 32 in Windows and
16 in Linux. But the specified thread name is typically long
in WebKit like "com.apple.CoreIPC.ReceiveQueue"

We port the logic substring the thread name in
generic/WorkQueueGeneric.cpp to Threading. It retrieves the name
"ReceiveQueue" from "com.apple.CoreIPC.ReceiveQueue". And apply
the appropriate the thread name limit and use it on Linux and
Windows environment.

* wtf/Threading.cpp:
(WTF::normalizeThreadName):
(WTF::createThread):
* wtf/Threading.h:
* wtf/ThreadingPthreads.cpp:
(WTF::initializeCurrentThreadInternal):
* wtf/ThreadingWin.cpp:
(WTF::initializeCurrentThreadInternal):
* wtf/generic/WorkQueueGeneric.cpp:
(WorkQueue::platformInitialize):
* wtf/text/StringView.h:
(WTF::StringView::left):
(WTF::StringView::right):
(WTF::StringView::reverseFind):

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp:
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (210312 => 210313)


--- trunk/Source/WTF/ChangeLog	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/ChangeLog	2017-01-05 02:53:00 UTC (rev 210313)
@@ -1,3 +1,36 @@
+2017-01-04  Yusuke Suzuki  <utatane....@gmail.com>
+
+        Limit thread name appropriately
+        https://bugs.webkit.org/show_bug.cgi?id=166676
+
+        Reviewed by Sam Weinig.
+
+        In some platform, the max length of thread names are limited.
+        For example, the number of the max length is 32 in Windows and
+        16 in Linux. But the specified thread name is typically long
+        in WebKit like "com.apple.CoreIPC.ReceiveQueue"
+
+        We port the logic substring the thread name in
+        generic/WorkQueueGeneric.cpp to Threading. It retrieves the name
+        "ReceiveQueue" from "com.apple.CoreIPC.ReceiveQueue". And apply
+        the appropriate the thread name limit and use it on Linux and
+        Windows environment.
+
+        * wtf/Threading.cpp:
+        (WTF::normalizeThreadName):
+        (WTF::createThread):
+        * wtf/Threading.h:
+        * wtf/ThreadingPthreads.cpp:
+        (WTF::initializeCurrentThreadInternal):
+        * wtf/ThreadingWin.cpp:
+        (WTF::initializeCurrentThreadInternal):
+        * wtf/generic/WorkQueueGeneric.cpp:
+        (WorkQueue::platformInitialize):
+        * wtf/text/StringView.h:
+        (WTF::StringView::left):
+        (WTF::StringView::right):
+        (WTF::StringView::reverseFind):
+
 2017-01-04  Sam Weinig  <s...@webkit.org>
 
         REGRESSION (r210257): com.apple.WebKit.WebContent.Development crashed in com.apple.WebCore: WebCore::ExceptionOr<WTF::Ref<WebCore::Database> >::operator= + 14

Modified: trunk/Source/WTF/wtf/Threading.cpp (210312 => 210313)


--- trunk/Source/WTF/wtf/Threading.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/Threading.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -29,6 +29,7 @@
 #include <algorithm>
 #include <cmath>
 #include <cstring>
+#include <wtf/text/StringView.h>
 
 namespace WTF {
 
@@ -40,6 +41,33 @@
     Mutex creationMutex;
 };
 
+const char* normalizeThreadName(const char* threadName)
+{
+#if HAVE(PTHREAD_SETNAME_NP)
+    return threadName;
+#else
+    // This name can be com.apple.WebKit.ProcessLauncher or com.apple.CoreIPC.ReceiveQueue.
+    // We are using those names for the thread name, but both are longer than the limit of
+    // the platform thread name length, 32 for Windows and 16 for Linux.
+    StringView result(threadName);
+    size_t size = result.reverseFind('.');
+    if (size != notFound)
+        result = result.substring(size + 1);
+
+#if OS(WINDOWS)
+    constexpr const size_t kVisualStudioThreadNameLimit = 32 - 1;
+    if (result.length() > kVisualStudioThreadNameLimit)
+        result = result.right(kVisualStudioThreadNameLimit);
+#elif OS(LINUX)
+    constexpr const size_t kLinuxThreadNameLimit = 16 - 1;
+    if (result.length() > kLinuxThreadNameLimit)
+        result = result.right(kLinuxThreadNameLimit);
+#endif
+    ASSERT(result.characters8()[result.length()] == '\0');
+    return reinterpret_cast<const char*>(result.characters8());
+#endif
+}
+
 static void threadEntryPoint(void* contextData)
 {
     NewThreadContext* context = static_cast<NewThreadContext*>(contextData);
@@ -62,13 +90,6 @@
 
 ThreadIdentifier createThread(const char* name, std::function<void()> entryPoint)
 {
-    // Visual Studio has a 31-character limit on thread names. Longer names will
-    // be truncated silently, but we'd like callers to know about the limit.
-#if !LOG_DISABLED && PLATFORM(WIN)
-    if (name && strlen(name) > 31)
-        LOG_ERROR("Thread name \"%s\" is longer than 31 characters and will be truncated by Visual Studio", name);
-#endif
-
     NewThreadContext* context = new NewThreadContext { name, WTFMove(entryPoint), { } };
 
     // Prevent the thread body from executing until we've established the thread identifier.

Modified: trunk/Source/WTF/wtf/Threading.h (210312 => 210313)


--- trunk/Source/WTF/wtf/Threading.h	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/Threading.h	2017-01-05 02:53:00 UTC (rev 210313)
@@ -77,6 +77,8 @@
 // Helpful for platforms where the thread name must be set from within the thread.
 void initializeCurrentThreadInternal(const char* threadName);
 
+const char* normalizeThreadName(const char* threadName);
+
 } // namespace WTF
 
 using WTF::ThreadIdentifier;

Modified: trunk/Source/WTF/wtf/ThreadingPthreads.cpp (210312 => 210313)


--- trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -195,9 +195,9 @@
 void initializeCurrentThreadInternal(const char* threadName)
 {
 #if HAVE(PTHREAD_SETNAME_NP)
-    pthread_setname_np(threadName);
+    pthread_setname_np(normalizeThreadName(threadName));
 #elif OS(LINUX)
-    prctl(PR_SET_NAME, threadName);
+    prctl(PR_SET_NAME, normalizeThreadName(threadName));
 #else
     UNUSED_PARAM(threadName);
 #endif

Modified: trunk/Source/WTF/wtf/ThreadingWin.cpp (210312 => 210313)


--- trunk/Source/WTF/wtf/ThreadingWin.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/ThreadingWin.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -129,7 +129,7 @@
 #else
     THREADNAME_INFO info;
     info.dwType = 0x1000;
-    info.szName = szThreadName;
+    info.szName = normalizeThreadName(szThreadName);
     info.dwThreadID = GetCurrentThreadId();
     info.dwFlags = 0;
 

Modified: trunk/Source/WTF/wtf/generic/WorkQueueGeneric.cpp (210312 => 210313)


--- trunk/Source/WTF/wtf/generic/WorkQueueGeneric.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/generic/WorkQueueGeneric.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -32,24 +32,10 @@
 
 #include <wtf/text/WTFString.h>
 
-static const size_t kVisualStudioThreadNameLimit = 31;
-
 void WorkQueue::platformInitialize(const char* name, Type, QOS)
 {
-    // This name can be com.apple.WebKit.ProcessLauncher or com.apple.CoreIPC.ReceiveQueue.
-    // We are using those names for the thread name, but both are longer than 31 characters,
-    // which is the limit of Visual Studio for thread names.
-    // When log is enabled createThread() will assert instead of truncate the name, so we need
-    // to make sure we don't use a name longer than 31 characters.
-    String threadName(name);
-    size_t size = threadName.reverseFind('.');
-    if (size != notFound)
-        threadName = threadName.substring(size + 1);
-    if (threadName.length() > kVisualStudioThreadNameLimit)
-        threadName = threadName.right(kVisualStudioThreadNameLimit);
-
     LockHolder locker(m_initializeRunLoopConditionMutex);
-    m_workQueueThread = createThread(threadName.ascii().data(), [this] {
+    m_workQueueThread = createThread(name, [this] {
         {
             LockHolder locker(m_initializeRunLoopConditionMutex);
             m_runLoop = &RunLoop::current();

Modified: trunk/Source/WTF/wtf/text/StringView.h (210312 => 210313)


--- trunk/Source/WTF/wtf/text/StringView.h	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Source/WTF/wtf/text/StringView.h	2017-01-05 02:53:00 UTC (rev 210313)
@@ -23,9 +23,9 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef StringView_h
-#define StringView_h
+#pragma once
 
+#include <limits.h>
 #include <unicode/utypes.h>
 #include <wtf/Forward.h>
 #include <wtf/RetainPtr.h>
@@ -114,6 +114,8 @@
     void getCharactersWithUpconvert(UChar*) const;
 
     StringView substring(unsigned start, unsigned length = std::numeric_limits<unsigned>::max()) const;
+    StringView left(unsigned len) const { return substring(0, len); }
+    StringView right(unsigned len) const { return substring(length() - len, len); }
     WTF_EXPORT_STRING_API Vector<StringView> split(UChar);
 
     size_t find(UChar, unsigned start = 0) const;
@@ -121,6 +123,8 @@
 
     WTF_EXPORT_STRING_API size_t find(StringView, unsigned start) const;
 
+    size_t reverseFind(UChar, unsigned index = UINT_MAX) const;
+
     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&) const;
     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&, unsigned startOffset) const;
 
@@ -531,6 +535,13 @@
     return WTF::find(characters16(), m_length, matchFunction, start);
 }
 
+inline size_t StringView::reverseFind(UChar character, unsigned index) const
+{
+    if (is8Bit())
+        return WTF::reverseFind(characters8(), m_length, character, index);
+    return WTF::reverseFind(characters16(), m_length, character, index);
+}
+
 #if !CHECK_STRINGVIEW_LIFETIME
 inline void StringView::invalidate(const StringImpl&)
 {
@@ -837,5 +848,3 @@
 using WTF::append;
 using WTF::equal;
 using WTF::StringView;
-
-#endif // StringView_h

Modified: trunk/Tools/ChangeLog (210312 => 210313)


--- trunk/Tools/ChangeLog	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Tools/ChangeLog	2017-01-05 02:53:00 UTC (rev 210313)
@@ -1,3 +1,15 @@
+2017-01-04  Yusuke Suzuki  <utatane....@gmail.com>
+
+        Limit thread name appropriately
+        https://bugs.webkit.org/show_bug.cgi?id=166676
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp:
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::TEST):
+
 2017-01-04  Andy Estes  <aes...@apple.com>
 
         [Cocoa] Teach SharedBuffer to return an NSArray of data segments to avoid flattening

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp (210312 => 210313)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringView.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -804,4 +804,52 @@
     EXPECT_TRUE(StringView(emptyString().impl()).is8Bit());
 }
 
+TEST(WTF, StringViewRightBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_TRUE(reference.right(0) == stringViewFromLiteral(""));
+    EXPECT_TRUE(reference.right(1) == stringViewFromLiteral("o"));
+    EXPECT_TRUE(reference.right(2) == stringViewFromLiteral("no"));
+    EXPECT_TRUE(reference.right(3) == stringViewFromLiteral("ino"));
+    EXPECT_TRUE(reference.right(4) == stringViewFromLiteral("cino"));
+    EXPECT_TRUE(reference.right(5) == stringViewFromLiteral("ccino"));
+    EXPECT_TRUE(reference.right(6) == stringViewFromLiteral("uccino"));
+    EXPECT_TRUE(reference.right(7) == stringViewFromLiteral("puccino"));
+    EXPECT_TRUE(reference.right(8) == stringViewFromLiteral("ppuccino"));
+    EXPECT_TRUE(reference.right(9) == stringViewFromLiteral("appuccino"));
+    EXPECT_TRUE(reference.right(10) == stringViewFromLiteral("Cappuccino"));
+}
+
+TEST(WTF, StringViewLeftBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_TRUE(reference.left(0) == stringViewFromLiteral(""));
+    EXPECT_TRUE(reference.left(1) == stringViewFromLiteral("C"));
+    EXPECT_TRUE(reference.left(2) == stringViewFromLiteral("Ca"));
+    EXPECT_TRUE(reference.left(3) == stringViewFromLiteral("Cap"));
+    EXPECT_TRUE(reference.left(4) == stringViewFromLiteral("Capp"));
+    EXPECT_TRUE(reference.left(5) == stringViewFromLiteral("Cappu"));
+    EXPECT_TRUE(reference.left(6) == stringViewFromLiteral("Cappuc"));
+    EXPECT_TRUE(reference.left(7) == stringViewFromLiteral("Cappucc"));
+    EXPECT_TRUE(reference.left(8) == stringViewFromLiteral("Cappucci"));
+    EXPECT_TRUE(reference.left(9) == stringViewFromLiteral("Cappuccin"));
+    EXPECT_TRUE(reference.left(10) == stringViewFromLiteral("Cappuccino"));
+}
+
+TEST(WTF, StringViewReverseFindBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_EQ(reference.reverseFind('o'), 9);
+    EXPECT_EQ(reference.reverseFind('n'), 8);
+    EXPECT_EQ(reference.reverseFind('c'), 6);
+    EXPECT_EQ(reference.reverseFind('p'), 3);
+    EXPECT_EQ(reference.reverseFind('k'), notFound);
+
+    EXPECT_EQ(reference.reverseFind('o', 8), notFound);
+    EXPECT_EQ(reference.reverseFind('c', 8), 6);
+    EXPECT_EQ(reference.reverseFind('c', 6), 6);
+    EXPECT_EQ(reference.reverseFind('c', 5), 5);
+    EXPECT_EQ(reference.reverseFind('c', 4), notFound);
+}
+
 } // namespace TestWebKitAPI

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp (210312 => 210313)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2017-01-05 02:49:51 UTC (rev 210312)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2017-01-05 02:53:00 UTC (rev 210313)
@@ -313,4 +313,52 @@
     ASSERT_FALSE(equal(string2, aBc));
 }
 
+TEST(WTF, StringRightBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_TRUE(reference.right(0) == String::fromUTF8(""));
+    EXPECT_TRUE(reference.right(1) == String::fromUTF8("o"));
+    EXPECT_TRUE(reference.right(2) == String::fromUTF8("no"));
+    EXPECT_TRUE(reference.right(3) == String::fromUTF8("ino"));
+    EXPECT_TRUE(reference.right(4) == String::fromUTF8("cino"));
+    EXPECT_TRUE(reference.right(5) == String::fromUTF8("ccino"));
+    EXPECT_TRUE(reference.right(6) == String::fromUTF8("uccino"));
+    EXPECT_TRUE(reference.right(7) == String::fromUTF8("puccino"));
+    EXPECT_TRUE(reference.right(8) == String::fromUTF8("ppuccino"));
+    EXPECT_TRUE(reference.right(9) == String::fromUTF8("appuccino"));
+    EXPECT_TRUE(reference.right(10) == String::fromUTF8("Cappuccino"));
+}
+
+TEST(WTF, StringLeftBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_TRUE(reference.left(0) == String::fromUTF8(""));
+    EXPECT_TRUE(reference.left(1) == String::fromUTF8("C"));
+    EXPECT_TRUE(reference.left(2) == String::fromUTF8("Ca"));
+    EXPECT_TRUE(reference.left(3) == String::fromUTF8("Cap"));
+    EXPECT_TRUE(reference.left(4) == String::fromUTF8("Capp"));
+    EXPECT_TRUE(reference.left(5) == String::fromUTF8("Cappu"));
+    EXPECT_TRUE(reference.left(6) == String::fromUTF8("Cappuc"));
+    EXPECT_TRUE(reference.left(7) == String::fromUTF8("Cappucc"));
+    EXPECT_TRUE(reference.left(8) == String::fromUTF8("Cappucci"));
+    EXPECT_TRUE(reference.left(9) == String::fromUTF8("Cappuccin"));
+    EXPECT_TRUE(reference.left(10) == String::fromUTF8("Cappuccino"));
+}
+
+TEST(WTF, StringReverseFindBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_EQ(reference.reverseFind('o'), 9);
+    EXPECT_EQ(reference.reverseFind('n'), 8);
+    EXPECT_EQ(reference.reverseFind('c'), 6);
+    EXPECT_EQ(reference.reverseFind('p'), 3);
+    EXPECT_EQ(reference.reverseFind('k'), notFound);
+
+    EXPECT_EQ(reference.reverseFind('o', 8), notFound);
+    EXPECT_EQ(reference.reverseFind('c', 8), 6);
+    EXPECT_EQ(reference.reverseFind('c', 6), 6);
+    EXPECT_EQ(reference.reverseFind('c', 5), 5);
+    EXPECT_EQ(reference.reverseFind('c', 4), notFound);
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to