<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[184218] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/184218">184218</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2015-05-12 14:20:26 -0700 (Tue, 12 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor MachineStackMarker.cpp so that it's easier to reason about MachineThreads::Thread.
https://bugs.webkit.org/show_bug.cgi?id=144925

Reviewed by Michael Saboff.

Currently, the code in MachineStackMarker.cpp is written as a bunch of functions that
operate on the platformThread value in the MachineThreads::Thread struct.  Instead, we
can apply better OO encapsulation and convert all these functions into methods of the
MachineThreads::Thread struct.

This will also make it easier to reason about the fix for
https://bugs.webkit.org/show_bug.cgi?id=144924 later.

* heap/MachineStackMarker.cpp:
(JSC::getCurrentPlatformThread):
(JSC::MachineThreads::Thread::createForCurrentThread):
(JSC::MachineThreads::Thread::operator!=):
(JSC::MachineThreads::Thread::operator==):
(JSC::MachineThreads::addCurrentThread):
(JSC::MachineThreads::removeThreadIfFound):
(JSC::MachineThreads::Thread::suspend):
(JSC::MachineThreads::Thread::resume):
(JSC::MachineThreads::Thread::getRegisters):
(JSC::MachineThreads::Thread::Registers::stackPointer):
(JSC::MachineThreads::Thread::freeRegisters):
(JSC::MachineThreads::Thread::captureStack):
(JSC::MachineThreads::tryCopyOtherThreadStack):
(JSC::MachineThreads::tryCopyOtherThreadStacks):
(JSC::equalThread): Deleted.
(JSC::suspendThread): Deleted.
(JSC::resumeThread): Deleted.
(JSC::getPlatformThreadRegisters): Deleted.
(JSC::otherThreadStackPointer): Deleted.
(JSC::freePlatformThreadRegisters): Deleted.
(JSC::otherThreadStack): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapMachineStackMarkercpp">trunk/Source/JavaScriptCore/heap/MachineStackMarker.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (184217 => 184218)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-05-12 21:04:10 UTC (rev 184217)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-05-12 21:20:26 UTC (rev 184218)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2015-05-12  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        Refactor MachineStackMarker.cpp so that it's easier to reason about MachineThreads::Thread.
+        https://bugs.webkit.org/show_bug.cgi?id=144925
+
+        Reviewed by Michael Saboff.
+
+        Currently, the code in MachineStackMarker.cpp is written as a bunch of functions that
+        operate on the platformThread value in the MachineThreads::Thread struct.  Instead, we
+        can apply better OO encapsulation and convert all these functions into methods of the
+        MachineThreads::Thread struct.
+
+        This will also make it easier to reason about the fix for
+        https://bugs.webkit.org/show_bug.cgi?id=144924 later.
+
+        * heap/MachineStackMarker.cpp:
+        (JSC::getCurrentPlatformThread):
+        (JSC::MachineThreads::Thread::createForCurrentThread):
+        (JSC::MachineThreads::Thread::operator!=):
+        (JSC::MachineThreads::Thread::operator==):
+        (JSC::MachineThreads::addCurrentThread):
+        (JSC::MachineThreads::removeThreadIfFound):
+        (JSC::MachineThreads::Thread::suspend):
+        (JSC::MachineThreads::Thread::resume):
+        (JSC::MachineThreads::Thread::getRegisters):
+        (JSC::MachineThreads::Thread::Registers::stackPointer):
+        (JSC::MachineThreads::Thread::freeRegisters):
+        (JSC::MachineThreads::Thread::captureStack):
+        (JSC::MachineThreads::tryCopyOtherThreadStack):
+        (JSC::MachineThreads::tryCopyOtherThreadStacks):
+        (JSC::equalThread): Deleted.
+        (JSC::suspendThread): Deleted.
+        (JSC::resumeThread): Deleted.
+        (JSC::getPlatformThreadRegisters): Deleted.
+        (JSC::otherThreadStackPointer): Deleted.
+        (JSC::freePlatformThreadRegisters): Deleted.
+        (JSC::otherThreadStack): Deleted.
+
</ins><span class="cx"> 2015-05-12  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Array.slice should have a fast path like Array.splice
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapMachineStackMarkercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/MachineStackMarker.cpp (184217 => 184218)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/MachineStackMarker.cpp        2015-05-12 21:04:10 UTC (rev 184217)
+++ trunk/Source/JavaScriptCore/heap/MachineStackMarker.cpp        2015-05-12 21:20:26 UTC (rev 184218)
</span><span class="lines">@@ -146,10 +146,20 @@
</span><span class="cx">     return *manager;
</span><span class="cx"> }
</span><span class="cx">     
</span><ins>+static inline PlatformThread getCurrentPlatformThread()
+{
+#if OS(DARWIN)
+    return pthread_mach_thread_np(pthread_self());
+#elif OS(WINDOWS)
+    return GetCurrentThread();
+#elif USE(PTHREADS)
+    return pthread_self();
+#endif
+}
</ins><span class="cx"> 
</span><span class="cx"> class MachineThreads::Thread {
</span><span class="cx">     WTF_MAKE_FAST_ALLOCATED;
</span><del>-public:
</del><ins>+
</ins><span class="cx">     Thread(const PlatformThread&amp; platThread, void* base)
</span><span class="cx">         : platformThread(platThread)
</span><span class="cx">         , stackBase(base)
</span><span class="lines">@@ -169,6 +179,52 @@
</span><span class="cx"> #endif
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+public:
+    static Thread* createForCurrentThread()
+    {
+        return new Thread(getCurrentPlatformThread(), wtfThreadData().stack().origin());
+    }
+
+    struct Registers {
+        inline void* stackPointer() const;
+        
+#if OS(DARWIN)
+#if CPU(X86)
+        typedef i386_thread_state_t PlatformRegisters;
+#elif CPU(X86_64)
+        typedef x86_thread_state64_t PlatformRegisters;
+#elif CPU(PPC)
+        typedef ppc_thread_state_t PlatformRegisters;
+#elif CPU(PPC64)
+        typedef ppc_thread_state64_t PlatformRegisters;
+#elif CPU(ARM)
+        typedef arm_thread_state_t PlatformRegisters;
+#elif CPU(ARM64)
+        typedef arm_thread_state64_t PlatformRegisters;
+#else
+#error Unknown Architecture
+#endif
+        
+#elif OS(WINDOWS)
+        typedef CONTEXT PlatformRegisters;
+#elif USE(PTHREADS)
+        typedef pthread_attr_t PlatformRegisters;
+#else
+#error Need a thread register struct for this platform
+#endif
+        
+        PlatformRegisters regs;
+    };
+    
+    inline bool operator==(const PlatformThread&amp; other) const;
+    inline bool operator!=(const PlatformThread&amp; other) const { return !(*this == other); }
+
+    inline bool suspend();
+    inline void resume();
+    size_t getRegisters(Registers&amp;);
+    void freeRegisters(Registers&amp;);
+    std::pair&lt;void*, size_t&gt; captureStack(void* stackTop);
+
</ins><span class="cx">     Thread* next;
</span><span class="cx">     PlatformThread platformThread;
</span><span class="cx">     void* stackBase;
</span><span class="lines">@@ -199,23 +255,12 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static inline PlatformThread getCurrentPlatformThread()
</del><ins>+inline bool MachineThreads::Thread::operator==(const PlatformThread&amp; other) const
</ins><span class="cx"> {
</span><del>-#if OS(DARWIN)
-    return pthread_mach_thread_np(pthread_self());
-#elif OS(WINDOWS)
-    return GetCurrentThread();
-#elif USE(PTHREADS)
-    return pthread_self();
-#endif
-}
-
-static inline bool equalThread(const PlatformThread&amp; first, const PlatformThread&amp; second)
-{
</del><span class="cx"> #if OS(DARWIN) || OS(WINDOWS)
</span><del>-    return first == second;
</del><ins>+    return platformThread == other;
</ins><span class="cx"> #elif USE(PTHREADS)
</span><del>-    return !!pthread_equal(first, second);
</del><ins>+    return !!pthread_equal(platformThread, other);
</ins><span class="cx"> #else
</span><span class="cx"> #error Need a way to compare threads on this platform
</span><span class="cx"> #endif
</span><span class="lines">@@ -231,7 +276,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     threadSpecificSet(m_threadSpecific, this);
</span><del>-    Thread* thread = new Thread(getCurrentPlatformThread(), wtfThreadData().stack().origin());
</del><ins>+    Thread* thread = Thread::createForCurrentThread();
</ins><span class="cx"> 
</span><span class="cx">     MutexLocker lock(m_registeredThreadsMutex);
</span><span class="cx"> 
</span><span class="lines">@@ -258,15 +303,14 @@
</span><span class="cx"> void MachineThreads::removeThreadIfFound(PlatformThread platformThread)
</span><span class="cx"> {
</span><span class="cx">     MutexLocker lock(m_registeredThreadsMutex);
</span><del>-    if (equalThread(platformThread, m_registeredThreads-&gt;platformThread)) {
-        Thread* t = m_registeredThreads;
</del><ins>+    Thread* t = m_registeredThreads;
+    if (*t == platformThread) {
</ins><span class="cx">         m_registeredThreads = m_registeredThreads-&gt;next;
</span><span class="cx">         delete t;
</span><span class="cx">     } else {
</span><span class="cx">         Thread* last = m_registeredThreads;
</span><del>-        Thread* t;
</del><span class="cx">         for (t = m_registeredThreads-&gt;next; t; t = t-&gt;next) {
</span><del>-            if (equalThread(t-&gt;platformThread, platformThread)) {
</del><ins>+            if (*t == platformThread) {
</ins><span class="cx">                 last-&gt;next = t-&gt;next;
</span><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="lines">@@ -285,7 +329,7 @@
</span><span class="cx">     conservativeRoots.add(stackTop, stackOrigin, jitStubRoutines, codeBlocks);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static inline bool suspendThread(const PlatformThread&amp; platformThread)
</del><ins>+inline bool MachineThreads::Thread::suspend()
</ins><span class="cx"> {
</span><span class="cx"> #if OS(DARWIN)
</span><span class="cx">     kern_return_t result = thread_suspend(platformThread);
</span><span class="lines">@@ -302,7 +346,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static inline void resumeThread(const PlatformThread&amp; platformThread)
</del><ins>+inline void MachineThreads::Thread::resume()
</ins><span class="cx"> {
</span><span class="cx"> #if OS(DARWIN)
</span><span class="cx">     thread_resume(platformThread);
</span><span class="lines">@@ -315,38 +359,10 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-typedef unsigned long usword_t; // word size, assumed to be either 32 or 64 bit
-
-#if OS(DARWIN)
-
-#if CPU(X86)
-typedef i386_thread_state_t PlatformThreadRegisters;
-#elif CPU(X86_64)
-typedef x86_thread_state64_t PlatformThreadRegisters;
-#elif CPU(PPC)
-typedef ppc_thread_state_t PlatformThreadRegisters;
-#elif CPU(PPC64)
-typedef ppc_thread_state64_t PlatformThreadRegisters;
-#elif CPU(ARM)
-typedef arm_thread_state_t PlatformThreadRegisters;
-#elif CPU(ARM64)
-typedef arm_thread_state64_t PlatformThreadRegisters;
-#else
-#error Unknown Architecture
-#endif
-
-#elif OS(WINDOWS)
-typedef CONTEXT PlatformThreadRegisters;
-#elif USE(PTHREADS)
-typedef pthread_attr_t PlatformThreadRegisters;
-#else
-#error Need a thread register struct for this platform
-#endif
-
-static size_t getPlatformThreadRegisters(const PlatformThread&amp; platformThread, PlatformThreadRegisters&amp; regs)
</del><ins>+size_t MachineThreads::Thread::getRegisters(MachineThreads::Thread::Registers&amp; registers)
</ins><span class="cx"> {
</span><ins>+    Thread::Registers::PlatformRegisters&amp; regs = registers.regs;
</ins><span class="cx"> #if OS(DARWIN)
</span><del>-
</del><span class="cx"> #if CPU(X86)
</span><span class="cx">     unsigned user_count = sizeof(regs)/sizeof(int);
</span><span class="cx">     thread_state_flavor_t flavor = i386_THREAD_STATE;
</span><span class="lines">@@ -375,7 +391,7 @@
</span><span class="cx">                             &quot;JavaScript garbage collection failed because thread_get_state returned an error (%d). This is probably the result of running inside Rosetta, which is not supported.&quot;, result);
</span><span class="cx">         CRASH();
</span><span class="cx">     }
</span><del>-    return user_count * sizeof(usword_t);
</del><ins>+    return user_count * sizeof(uintptr_t);
</ins><span class="cx"> // end OS(DARWIN)
</span><span class="cx"> 
</span><span class="cx"> #elif OS(WINDOWS)
</span><span class="lines">@@ -399,7 +415,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static inline void* otherThreadStackPointer(const PlatformThreadRegisters&amp; regs)
</del><ins>+inline void* MachineThreads::Thread::Registers::stackPointer() const
</ins><span class="cx"> {
</span><span class="cx"> #if OS(DARWIN)
</span><span class="cx"> 
</span><span class="lines">@@ -467,8 +483,9 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void freePlatformThreadRegisters(PlatformThreadRegisters&amp; regs)
</del><ins>+void MachineThreads::Thread::freeRegisters(MachineThreads::Thread::Registers&amp; registers)
</ins><span class="cx"> {
</span><ins>+    Thread::Registers::PlatformRegisters&amp; regs = registers.regs;
</ins><span class="cx"> #if USE(PTHREADS) &amp;&amp; !OS(WINDOWS) &amp;&amp; !OS(DARWIN)
</span><span class="cx">     pthread_attr_destroy(&amp;regs);
</span><span class="cx"> #else
</span><span class="lines">@@ -476,13 +493,11 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static std::pair&lt;void*, size_t&gt; otherThreadStack(void* stackBase, const PlatformThreadRegisters&amp; registers)
</del><ins>+std::pair&lt;void*, size_t&gt; MachineThreads::Thread::captureStack(void* stackTop)
</ins><span class="cx"> {
</span><span class="cx">     void* begin = stackBase;
</span><span class="cx">     void* end = reinterpret_cast&lt;void*&gt;(
</span><del>-        WTF::roundUpToMultipleOf&lt;sizeof(void*)&gt;(
-            reinterpret_cast&lt;uintptr_t&gt;(
-                otherThreadStackPointer(registers))));
</del><ins>+        WTF::roundUpToMultipleOf&lt;sizeof(void*)&gt;(reinterpret_cast&lt;uintptr_t&gt;(stackTop)));
</ins><span class="cx">     if (begin &gt; end)
</span><span class="cx">         std::swap(begin, end);
</span><span class="cx">     return std::make_pair(begin, static_cast&lt;char*&gt;(end) - static_cast&lt;char*&gt;(begin));
</span><span class="lines">@@ -513,9 +528,9 @@
</span><span class="cx"> // will deadlock if 'thread' holds that lock.
</span><span class="cx"> void MachineThreads::tryCopyOtherThreadStack(Thread* thread, void* buffer, size_t capacity, size_t* size)
</span><span class="cx"> {
</span><del>-    PlatformThreadRegisters registers;
-    size_t registersSize = getPlatformThreadRegisters(thread-&gt;platformThread, registers);
-    std::pair&lt;void*, size_t&gt; stack = otherThreadStack(thread-&gt;stackBase, registers);
</del><ins>+    Thread::Registers registers;
+    size_t registersSize = thread-&gt;getRegisters(registers);
+    std::pair&lt;void*, size_t&gt; stack = thread-&gt;captureStack(registers.stackPointer());
</ins><span class="cx"> 
</span><span class="cx">     bool canCopy = *size + registersSize + stack.second &lt;= capacity;
</span><span class="cx"> 
</span><span class="lines">@@ -527,7 +542,7 @@
</span><span class="cx">         memcpy(static_cast&lt;char*&gt;(buffer) + *size, stack.first, stack.second);
</span><span class="cx">     *size += stack.second;
</span><span class="cx"> 
</span><del>-    freePlatformThreadRegisters(registers);
</del><ins>+    thread-&gt;freeRegisters(registers);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool MachineThreads::tryCopyOtherThreadStacks(MutexLocker&amp;, void* buffer, size_t capacity, size_t* size)
</span><span class="lines">@@ -541,8 +556,8 @@
</span><span class="cx"> 
</span><span class="cx">     Thread* previousThread = nullptr;
</span><span class="cx">     for (Thread* thread = m_registeredThreads; thread; index++) {
</span><del>-        if (!equalThread(thread-&gt;platformThread, currentPlatformThread)) {
-            bool success = suspendThread(thread-&gt;platformThread);
</del><ins>+        if (*thread != currentPlatformThread) {
+            bool success = thread-&gt;suspend();
</ins><span class="cx"> #if OS(DARWIN)
</span><span class="cx">             if (!success) {
</span><span class="cx">                 if (!numberOfThreads) {
</span><span class="lines">@@ -585,13 +600,13 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     for (Thread* thread = m_registeredThreads; thread; thread = thread-&gt;next) {
</span><del>-        if (!equalThread(thread-&gt;platformThread, currentPlatformThread))
</del><ins>+        if (*thread != currentPlatformThread)
</ins><span class="cx">             tryCopyOtherThreadStack(thread, buffer, capacity, size);
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     for (Thread* thread = m_registeredThreads; thread; thread = thread-&gt;next) {
</span><del>-        if (!equalThread(thread-&gt;platformThread, currentPlatformThread))
-            resumeThread(thread-&gt;platformThread);
</del><ins>+        if (*thread != currentPlatformThread)
+            thread-&gt;resume();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     for (Thread* thread = threadsToBeDeleted; thread; ) {
</span></span></pre>
</div>
</div>

</body>
</html>