Title: [219654] trunk/Source/WTF
Revision
219654
Author
[email protected]
Date
2017-07-19 03:44:57 -0700 (Wed, 19 Jul 2017)

Log Message

[WTF] Remove unnecessary indirection of WTF::Thread entry point
https://bugs.webkit.org/show_bug.cgi?id=174291

Reviewed by Mark Lam.

Now wtfThreadEntryPoint is almost the same. Only the difference is function signature due to platform APIs.
We remove ThreadFunctionInvocation indirection in ThreadingPthread.cpp and ThreadingWin.cpp.

Also, ThreadFunctionInvocation keeps a RefPtr to the Thread object. This was previously needed to keep the
Thread object alive until the thread itself could install the ThreadHolder into its thread local storage.
The ThreadHolder has a Ref that keeps the Thread object alive for the lifetime of the thread. Since
Thread::create() now waits for the thread to be initialized before returning and Thread::create() hold a Ref
to the Thread object, we are guaranteed that the Thread object will be alive long enough for it to be installed
in the thread's ThreadHolder, and we no longer need ThreadFunctionInvocation.

And we also simplify ThreadHolder::initialize a bit. Now Thread::create waits for the completion of Thread
initialization. So, after establishing thread handle, we can call ThreadHolder::initialize before completing
Thread initialization.

Also we drop errno.h ifdefs in ThreadingWin.cpp. This is introduced to support WinCE. But now WinCE port is removed.

* WTF.xcodeproj/project.pbxproj:
* wtf/ThreadFunctionInvocation.h: Removed.
* wtf/ThreadHolder.h:
* wtf/ThreadHolderWin.cpp:
(WTF::ThreadHolder::initialize):
* wtf/Threading.cpp:
(WTF::Thread::entryPoint):
(WTF::Thread::create):
(WTF::threadEntryPoint): Deleted.
* wtf/Threading.h:
* wtf/ThreadingPthreads.cpp:
(WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
(WTF::wtfThreadEntryPoint):
(WTF::Thread::establishHandle):
(WTF::Thread::initializeCurrentThreadInternal):
(WTF::Thread::current):
(WTF::Thread::establishPlatformSpecificHandle):
(WTF::Thread::createInternal): Deleted.
(WTF::Thread::establish): Deleted.
* wtf/ThreadingWin.cpp:
(WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
(WTF::Thread::initializeCurrentThreadInternal):
(WTF::wtfThreadEntryPoint):
(WTF::Thread::establishHandle):
(WTF::Thread::current):
(WTF::Thread::establishPlatformSpecificHandle):
(WTF::Thread::createInternal): Deleted.
(WTF::Thread::establish): Deleted.
* wtf/win/MainThreadWin.cpp:
(WTF::initializeMainThreadPlatform):

Modified Paths

Removed Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (219653 => 219654)


--- trunk/Source/WTF/ChangeLog	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/ChangeLog	2017-07-19 10:44:57 UTC (rev 219654)
@@ -1,5 +1,59 @@
 2017-07-19  Yusuke Suzuki  <[email protected]>
 
+        [WTF] Remove unnecessary indirection of WTF::Thread entry point
+        https://bugs.webkit.org/show_bug.cgi?id=174291
+
+        Reviewed by Mark Lam.
+
+        Now wtfThreadEntryPoint is almost the same. Only the difference is function signature due to platform APIs.
+        We remove ThreadFunctionInvocation indirection in ThreadingPthread.cpp and ThreadingWin.cpp.
+
+        Also, ThreadFunctionInvocation keeps a RefPtr to the Thread object. This was previously needed to keep the
+        Thread object alive until the thread itself could install the ThreadHolder into its thread local storage.
+        The ThreadHolder has a Ref that keeps the Thread object alive for the lifetime of the thread. Since
+        Thread::create() now waits for the thread to be initialized before returning and Thread::create() hold a Ref
+        to the Thread object, we are guaranteed that the Thread object will be alive long enough for it to be installed
+        in the thread's ThreadHolder, and we no longer need ThreadFunctionInvocation.
+
+        And we also simplify ThreadHolder::initialize a bit. Now Thread::create waits for the completion of Thread
+        initialization. So, after establishing thread handle, we can call ThreadHolder::initialize before completing
+        Thread initialization.
+
+        Also we drop errno.h ifdefs in ThreadingWin.cpp. This is introduced to support WinCE. But now WinCE port is removed.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/ThreadFunctionInvocation.h: Removed.
+        * wtf/ThreadHolder.h:
+        * wtf/ThreadHolderWin.cpp:
+        (WTF::ThreadHolder::initialize):
+        * wtf/Threading.cpp:
+        (WTF::Thread::entryPoint):
+        (WTF::Thread::create):
+        (WTF::threadEntryPoint): Deleted.
+        * wtf/Threading.h:
+        * wtf/ThreadingPthreads.cpp:
+        (WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
+        (WTF::wtfThreadEntryPoint):
+        (WTF::Thread::establishHandle):
+        (WTF::Thread::initializeCurrentThreadInternal):
+        (WTF::Thread::current):
+        (WTF::Thread::establishPlatformSpecificHandle):
+        (WTF::Thread::createInternal): Deleted.
+        (WTF::Thread::establish): Deleted.
+        * wtf/ThreadingWin.cpp:
+        (WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
+        (WTF::Thread::initializeCurrentThreadInternal):
+        (WTF::wtfThreadEntryPoint):
+        (WTF::Thread::establishHandle):
+        (WTF::Thread::current):
+        (WTF::Thread::establishPlatformSpecificHandle):
+        (WTF::Thread::createInternal): Deleted.
+        (WTF::Thread::establish): Deleted.
+        * wtf/win/MainThreadWin.cpp:
+        (WTF::initializeMainThreadPlatform):
+
+2017-07-19  Yusuke Suzuki  <[email protected]>
+
         [WTF] Implement WTF::ThreadGroup
         https://bugs.webkit.org/show_bug.cgi?id=174081
 

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (219653 => 219654)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2017-07-19 10:44:57 UTC (rev 219654)
@@ -492,7 +492,6 @@
 		A8A4732C151A825B004123FF /* TextPosition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextPosition.h; sourceTree = "<group>"; };
 		A8A4732D151A825B004123FF /* WTFString.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WTFString.cpp; sourceTree = "<group>"; };
 		A8A4732E151A825B004123FF /* WTFString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFString.h; sourceTree = "<group>"; };
-		A8A4732F151A825B004123FF /* ThreadFunctionInvocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadFunctionInvocation.h; sourceTree = "<group>"; };
 		A8A47330151A825B004123FF /* ThreadHolderPthreads.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadHolderPthreads.cpp; sourceTree = "<group>"; };
 		A8A47332151A825B004123FF /* Threading.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Threading.cpp; sourceTree = "<group>"; };
 		A8A47333151A825B004123FF /* Threading.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Threading.h; sourceTree = "<group>"; };
@@ -958,7 +957,6 @@
 				5597F82C1D94B9970066BC21 /* SynchronizedFixedQueue.h */,
 				E3E158251EADA53C004A079D /* SystemFree.h */,
 				0FB317C31C488001007E395A /* SystemTracing.h */,
-				A8A4732F151A825B004123FF /* ThreadFunctionInvocation.h */,
 				E311FB151F0A568B003C08DE /* ThreadGroup.cpp */,
 				E311FB161F0A568B003C08DE /* ThreadGroup.h */,
 				E3200AB51E9A536D003B59D2 /* ThreadHolder.cpp */,

Deleted: trunk/Source/WTF/wtf/ThreadFunctionInvocation.h (219653 => 219654)


--- trunk/Source/WTF/wtf/ThreadFunctionInvocation.h	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/ThreadFunctionInvocation.h	2017-07-19 10:44:57 UTC (rev 219654)
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1.  Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer. 
- * 2.  Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution. 
- * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
- *     its contributors may be used to endorse or promote products derived
- *     from this software without specific prior written permission. 
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef ThreadFunctionInvocation_h
-#define ThreadFunctionInvocation_h
-
-#include "Threading.h"
-
-namespace WTF {
-
-typedef void (*ThreadFunction)(void* argument);
-
-struct ThreadFunctionInvocation {
-    WTF_MAKE_FAST_ALLOCATED;
-public:
-    ThreadFunctionInvocation(ThreadFunction function, RefPtr<Thread>&& thread, void* data)
-        : function(function)
-        , thread(WTFMove(thread))
-        , data(data)
-    {
-    }
-
-    ThreadFunction function;
-    RefPtr<Thread> thread;
-    void* data;
-};
-
-} // namespace WTF
-
-#endif // ThreadFunctionInvocation_h

Modified: trunk/Source/WTF/wtf/ThreadHolder.h (219653 => 219654)


--- trunk/Source/WTF/wtf/ThreadHolder.h	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/ThreadHolder.h	2017-07-19 10:44:57 UTC (rev 219654)
@@ -50,11 +50,7 @@
     static void initializeOnce();
 
     // Creates and puts an instance of ThreadHolder into thread-specific storage.
-#if OS(WINDOWS)
-    static void initialize(Thread&, ThreadIdentifier);
-#else
     static void initialize(Thread&);
-#endif
 
     // Returns 0 if thread-specific storage was not initialized.
     static ThreadHolder* current();

Modified: trunk/Source/WTF/wtf/ThreadHolderWin.cpp (219653 => 219654)


--- trunk/Source/WTF/wtf/ThreadHolderWin.cpp	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/ThreadHolderWin.cpp	2017-07-19 10:44:57 UTC (rev 219654)
@@ -80,7 +80,7 @@
     return nullptr;
 }
 
-void ThreadHolder::initialize(Thread& thread, ThreadIdentifier id)
+void ThreadHolder::initialize(Thread& thread)
 {
     if (!current()) {
         // Ideally we'd have this as a release assert everywhere, but that would hurt performance.
@@ -90,11 +90,9 @@
         // FIXME: Remove this workaround code once <rdar://problem/31793213> is fixed.
         auto* holder = new ThreadHolder(thread);
         threadSpecificSet(m_key, holder);
-
-        // Since Thread is not established yet, we use the given id instead of thread->id().
         {
             std::lock_guard<std::mutex> locker(threadMapMutex());
-            threadMap().add(id, holder);
+            threadMap().add(thread.id(), holder);
         }
     }
 }

Modified: trunk/Source/WTF/wtf/Threading.cpp (219653 => 219654)


--- trunk/Source/WTF/wtf/Threading.cpp	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/Threading.cpp	2017-07-19 10:44:57 UTC (rev 219654)
@@ -46,15 +46,16 @@
 namespace WTF {
 
 enum class Stage {
-    Start, Initialized
+    Start, EstablishedHandle, Initialized
 };
 
-struct NewThreadContext {
+struct Thread::NewThreadContext {
     const char* name;
     Function<void()> entryPoint;
     Stage stage;
     Mutex mutex;
     ThreadCondition condition;
+    Thread& thread;
 };
 
 const char* Thread::normalizeThreadName(const char* threadName)
@@ -84,36 +85,39 @@
 #endif
 }
 
-static void threadEntryPoint(void* contextData)
+void Thread::entryPoint(NewThreadContext* context)
 {
-    NewThreadContext* context = static_cast<NewThreadContext*>(contextData);
-    Function<void()> entryPoint;
+    Function<void()> function;
     {
         // Block until our creating thread has completed any extra setup work, including establishing ThreadIdentifier.
         MutexLocker locker(context->mutex);
+        ASSERT(context->stage == Stage::EstablishedHandle);
 
-        Thread::initializeCurrentThreadInternal(context->name);
-        entryPoint = WTFMove(context->entryPoint);
+        // Initialize thread holder with established ID.
+        ThreadHolder::initialize(context->thread);
 
+        Thread::initializeCurrentThreadInternal(context->thread, context->name);
+        function = WTFMove(context->entryPoint);
+
         // Ack completion of initialization to the creating thread.
         context->stage = Stage::Initialized;
         context->condition.signal();
     }
-
-    entryPoint();
+    function();
 }
 
 RefPtr<Thread> Thread::create(const char* name, Function<void()>&& entryPoint)
 {
-    NewThreadContext context { name, WTFMove(entryPoint), Stage::Start, { }, { } };
-
+    Ref<Thread> thread = adoptRef(*new Thread());
+    NewThreadContext context { name, WTFMove(entryPoint), Stage::Start, { }, { }, thread.get() };
     MutexLocker locker(context.mutex);
-    RefPtr<Thread> result = Thread::createInternal(threadEntryPoint, &context, name);
+    if (!thread->establishHandle(&context))
+        return nullptr;
+    context.stage = Stage::EstablishedHandle;
     // After establishing Thread, release the mutex and wait for completion of initialization.
     while (context.stage != Stage::Initialized)
         context.condition.wait(context.mutex);
-
-    return result;
+    return WTFMove(thread);
 }
 
 Thread* Thread::currentMayBeNull()

Modified: trunk/Source/WTF/wtf/Threading.h (219653 => 219654)


--- trunk/Source/WTF/wtf/Threading.h	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/Threading.h	2017-07-19 10:44:57 UTC (rev 219654)
@@ -112,8 +112,8 @@
 
     // Called in the thread during initialization.
     // Helpful for platforms where the thread name must be set from within the thread.
-    static void initializeCurrentThreadInternal(const char* threadName);
-    static void initializeCurrentThreadEvenIfNonWTFCreated();
+    static void initializeCurrentThreadInternal(Thread&, const char* threadName);
+    static void initializeCurrentThreadEvenIfNonWTFCreated(Thread&);
 
     WTF_EXPORT_PRIVATE void dump(PrintStream& out) const;
 
@@ -140,16 +140,18 @@
     mach_port_t machThread() { return m_platformThread; }
 #endif
 
+    struct NewThreadContext;
+    static void entryPoint(NewThreadContext* data);
 protected:
     Thread();
 
-    // Internal platform-specific Thread::create implementation.
-    static RefPtr<Thread> createInternal(ThreadFunction, void*, const char* threadName);
+    // Internal platform-specific Thread establishment implementation.
+    bool establishHandle(NewThreadContext* data);
 
 #if USE(PTHREADS)
-    void establish(pthread_t);
+    void establishPlatformSpecificHandle(pthread_t);
 #else
-    void establish(HANDLE, ThreadIdentifier);
+    void establishPlatformSpecificHandle(HANDLE, ThreadIdentifier);
 #endif
     void initialize();
 

Modified: trunk/Source/WTF/wtf/ThreadingPthreads.cpp (219653 => 219654)


--- trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2017-07-19 10:44:57 UTC (rev 219654)
@@ -39,7 +39,6 @@
 #include <wtf/DataLog.h>
 #include <wtf/RawPointer.h>
 #include <wtf/StdLibExtras.h>
-#include <wtf/ThreadFunctionInvocation.h>
 #include <wtf/ThreadGroup.h>
 #include <wtf/ThreadHolder.h>
 #include <wtf/ThreadingPrimitives.h>
@@ -189,9 +188,9 @@
 #endif
 }
 
-void Thread::initializeCurrentThreadEvenIfNonWTFCreated()
+void Thread::initializeCurrentThreadEvenIfNonWTFCreated(Thread& thread)
 {
-    Thread::current().initialize();
+    thread.initialize();
 #if !OS(DARWIN)
     sigset_t mask;
     sigemptyset(&mask);
@@ -200,22 +199,14 @@
 #endif
 }
 
-static void* wtfThreadEntryPoint(void* param)
+static void* wtfThreadEntryPoint(void* data)
 {
-    // Balanced by .leakPtr() in Thread::createInternal.
-    auto invocation = std::unique_ptr<ThreadFunctionInvocation>(static_cast<ThreadFunctionInvocation*>(param));
-
-    ThreadHolder::initialize(*invocation->thread);
-    invocation->thread = nullptr;
-
-    invocation->function(invocation->data);
+    Thread::entryPoint(reinterpret_cast<Thread::NewThreadContext*>(data));
     return nullptr;
 }
 
-RefPtr<Thread> Thread::createInternal(ThreadFunction entryPoint, void* data, const char*)
+bool Thread::establishHandle(NewThreadContext* data)
 {
-    RefPtr<Thread> thread = adoptRef(new Thread());
-    auto invocation = std::make_unique<ThreadFunctionInvocation>(entryPoint, thread.get(), data);
     pthread_t threadHandle;
     pthread_attr_t attr;
     pthread_attr_init(&attr);
@@ -222,22 +213,17 @@
 #if HAVE(QOS_CLASSES)
     pthread_attr_set_qos_class_np(&attr, adjustedQOSClass(QOS_CLASS_USER_INITIATED), 0);
 #endif
-    int error = pthread_create(&threadHandle, &attr, wtfThreadEntryPoint, invocation.get());
+    int error = pthread_create(&threadHandle, &attr, wtfThreadEntryPoint, data);
     pthread_attr_destroy(&attr);
     if (error) {
-        LOG_ERROR("Failed to create pthread at entry point %p with data %p", wtfThreadEntryPoint, invocation.get());
-        return nullptr;
+        LOG_ERROR("Failed to create pthread at entry point %p with data %p", wtfThreadEntryPoint, data);
+        return false;
     }
-
-    // Balanced by std::unique_ptr constructor in wtfThreadEntryPoint.
-    ThreadFunctionInvocation* leakedInvocation = invocation.release();
-    UNUSED_PARAM(leakedInvocation);
-
-    thread->establish(threadHandle);
-    return thread;
+    establishPlatformSpecificHandle(threadHandle);
+    return true;
 }
 
-void Thread::initializeCurrentThreadInternal(const char* threadName)
+void Thread::initializeCurrentThreadInternal(Thread& thread, const char* threadName)
 {
 #if HAVE(PTHREAD_SETNAME_NP)
     pthread_setname_np(normalizeThreadName(threadName));
@@ -246,7 +232,7 @@
 #else
     UNUSED_PARAM(threadName);
 #endif
-    initializeCurrentThreadEvenIfNonWTFCreated();
+    initializeCurrentThreadEvenIfNonWTFCreated(thread);
 }
 
 void Thread::changePriority(int delta)
@@ -308,9 +294,9 @@
 
     // Not a WTF-created thread, ThreadIdentifier is not established yet.
     Ref<Thread> thread = adoptRef(*new Thread());
-    thread->establish(pthread_self());
+    thread->establishPlatformSpecificHandle(pthread_self());
     ThreadHolder::initialize(thread.get());
-    initializeCurrentThreadEvenIfNonWTFCreated();
+    initializeCurrentThreadEvenIfNonWTFCreated(thread.get());
     return thread.get();
 }
 
@@ -443,7 +429,7 @@
 #endif
 }
 
-void Thread::establish(pthread_t handle)
+void Thread::establishPlatformSpecificHandle(pthread_t handle)
 {
     std::lock_guard<std::mutex> locker(m_mutex);
     m_handle = handle;

Modified: trunk/Source/WTF/wtf/ThreadingWin.cpp (219653 => 219654)


--- trunk/Source/WTF/wtf/ThreadingWin.cpp	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/ThreadingWin.cpp	2017-07-19 10:44:57 UTC (rev 219654)
@@ -88,6 +88,7 @@
 
 #if OS(WINDOWS)
 
+#include <errno.h>
 #include <process.h>
 #include <windows.h>
 #include <wtf/CurrentTime.h>
@@ -95,14 +96,9 @@
 #include <wtf/MainThread.h>
 #include <wtf/MathExtras.h>
 #include <wtf/NeverDestroyed.h>
-#include <wtf/ThreadFunctionInvocation.h>
 #include <wtf/ThreadHolder.h>
 #include <wtf/ThreadingPrimitives.h>
 
-#if HAVE(ERRNO_H)
-#include <errno.h>
-#endif
-
 namespace WTF {
 
 static StaticLock globalSuspendLock;
@@ -119,9 +115,9 @@
         CloseHandle(m_handle);
 }
 
-void Thread::initializeCurrentThreadEvenIfNonWTFCreated()
+void Thread::initializeCurrentThreadEvenIfNonWTFCreated(Thread& thread)
 {
-    Thread::current().initialize();
+    thread.initialize();
 }
 
 // MS_VC_EXCEPTION, THREADNAME_INFO, and setThreadNameInternal all come from <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>.
@@ -136,7 +132,7 @@
 } THREADNAME_INFO;
 #pragma pack(pop)
 
-void Thread::initializeCurrentThreadInternal(const char* szThreadName)
+void Thread::initializeCurrentThreadInternal(Thread& thread, const char* szThreadName)
 {
 #if COMPILER(MINGW)
     // FIXME: Implement thread name setting with MingW.
@@ -153,7 +149,7 @@
     } __except (EXCEPTION_CONTINUE_EXECUTION) {
     }
 #endif
-    initializeCurrentThreadEvenIfNonWTFCreated();
+    initializeCurrentThreadEvenIfNonWTFCreated(thread);
 }
 
 void Thread::initializePlatformThreading()
@@ -160,42 +156,22 @@
 {
 }
 
-static unsigned __stdcall wtfThreadEntryPoint(void* param)
+static unsigned __stdcall wtfThreadEntryPoint(void* data)
 {
-    // Balanced by .leakPtr() in Thread::createInternal.
-    auto invocation = std::unique_ptr<ThreadFunctionInvocation>(static_cast<ThreadFunctionInvocation*>(param));
-
-    ThreadHolder::initialize(*invocation->thread, Thread::currentID());
-    invocation->thread = nullptr;
-
-    invocation->function(invocation->data);
+    Thread::entryPoint(reinterpret_cast<Thread::NewThreadContext*>(data));
     return 0;
 }
 
-RefPtr<Thread> Thread::createInternal(ThreadFunction entryPoint, void* data, const char* threadName)
+bool Thread::establishHandle(NewThreadContext* data)
 {
-    Ref<Thread> thread = adoptRef(*new Thread());
     unsigned threadIdentifier = 0;
-    ThreadIdentifier threadID = 0;
-    auto invocation = std::make_unique<ThreadFunctionInvocation>(entryPoint, thread.ptr(), data);
-    HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, invocation.get(), 0, &threadIdentifier));
+    HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0, wtfThreadEntryPoint, data, 0, &threadIdentifier));
     if (!threadHandle) {
-#if !HAVE(ERRNO_H)
-        LOG_ERROR("Failed to create thread at entry point %p with data %p.", entryPoint, data);
-#else
-        LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", entryPoint, data, errno);
-#endif
-        return 0;
+        LOG_ERROR("Failed to create thread at entry point %p with data %p: %ld", wtfThreadEntryPoint, data, errno);
+        return false;
     }
-
-    // The thread will take ownership of invocation.
-    ThreadFunctionInvocation* leakedInvocation = invocation.release();
-    UNUSED_PARAM(leakedInvocation);
-
-    threadID = static_cast<ThreadIdentifier>(threadIdentifier);
-
-    thread->establish(threadHandle, threadIdentifier);
-    return thread;
+    establishPlatformSpecificHandle(threadHandle, threadIdentifier);
+    return true;
 }
 
 void Thread::changePriority(int delta)
@@ -280,9 +256,9 @@
     bool isSuccessful = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
     RELEASE_ASSERT(isSuccessful);
 
-    thread->establish(handle, currentID());
-    ThreadHolder::initialize(thread.get(), Thread::currentID());
-    initializeCurrentThreadEvenIfNonWTFCreated();
+    thread->establishPlatformSpecificHandle(handle, currentID());
+    ThreadHolder::initialize(thread.get());
+    initializeCurrentThreadEvenIfNonWTFCreated(thread.get());
     return thread.get();
 }
 
@@ -291,7 +267,7 @@
     return static_cast<ThreadIdentifier>(GetCurrentThreadId());
 }
 
-void Thread::establish(HANDLE handle, ThreadIdentifier threadID)
+void Thread::establishPlatformSpecificHandle(HANDLE handle, ThreadIdentifier threadID)
 {
     std::lock_guard<std::mutex> locker(m_mutex);
     m_handle = handle;

Modified: trunk/Source/WTF/wtf/win/MainThreadWin.cpp (219653 => 219654)


--- trunk/Source/WTF/wtf/win/MainThreadWin.cpp	2017-07-19 08:43:57 UTC (rev 219653)
+++ trunk/Source/WTF/wtf/win/MainThreadWin.cpp	2017-07-19 10:44:57 UTC (rev 219654)
@@ -64,7 +64,7 @@
         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, 0, 0, 0);
     threadingFiredMessage = RegisterWindowMessageW(L"com.apple.WebKit.MainThreadFired");
 
-    Thread::initializeCurrentThreadInternal("Main Thread");
+    Thread::initializeCurrentThreadInternal(Thread::current(), "Main Thread");
 }
 
 void scheduleDispatchFunctionsOnMainThread()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to