<!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>[207566] trunk/Source</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/207566">207566</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-10-19 13:37:20 -0700 (Wed, 19 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Baseline JIT should use AutomaticThread
https://bugs.webkit.org/show_bug.cgi?id=163686

Reviewed by Geoffrey Garen.
        
Source/JavaScriptCore:

Change the JITWorklist to use AutomaticThread, so that the Baseline JIT's concurrent
compiler thread shuts down automatically after inactivity.
        
With this change, all of JSC's threads shut down automatically. If you run splay for a few
seconds (which fires up all threads - compiler and GC) and then go to sleep for a second,
you'll see that the only threads left are the main thread and the bmalloc thread.

* jit/JITWorklist.cpp:
(JSC::JITWorklist::Thread::Thread):
(JSC::JITWorklist::JITWorklist):
(JSC::JITWorklist::completeAllForVM):
(JSC::JITWorklist::poll):
(JSC::JITWorklist::compileLater):
(JSC::JITWorklist::compileNow):
(JSC::JITWorklist::finalizePlans):
(JSC::JITWorklist::runThread): Deleted.
* jit/JITWorklist.h:

Source/WTF:

Added a AutomaticThreadCondition::wait() method, so that if you really want to use one
common condition for your thread and something else, you can do it. This trivially works
if you only use notifyAll(), and behaves as you'd expect for notifyOne() (i.e. it's
dangerous, since you don't know who will wake up).
        
The Baseline JIT used the one-true-Condition idiom because it used notifyAll() in an
optimal way: there are just two threads talking to each other, so it wakes up at most one
thread and that thread is exactly the one you want woken up. Adding wait() means that I did
not have to change that code.

* wtf/AutomaticThread.cpp:
(WTF::AutomaticThreadCondition::wait):
* wtf/AutomaticThread.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejitJITWorklistcpp">trunk/Source/JavaScriptCore/jit/JITWorklist.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitJITWorklisth">trunk/Source/JavaScriptCore/jit/JITWorklist.h</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfAutomaticThreadcpp">trunk/Source/WTF/wtf/AutomaticThread.cpp</a></li>
<li><a href="#trunkSourceWTFwtfAutomaticThreadh">trunk/Source/WTF/wtf/AutomaticThread.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+2016-10-19  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        Baseline JIT should use AutomaticThread
+        https://bugs.webkit.org/show_bug.cgi?id=163686
+
+        Reviewed by Geoffrey Garen.
+        
+        Change the JITWorklist to use AutomaticThread, so that the Baseline JIT's concurrent
+        compiler thread shuts down automatically after inactivity.
+        
+        With this change, all of JSC's threads shut down automatically. If you run splay for a few
+        seconds (which fires up all threads - compiler and GC) and then go to sleep for a second,
+        you'll see that the only threads left are the main thread and the bmalloc thread.
+
+        * jit/JITWorklist.cpp:
+        (JSC::JITWorklist::Thread::Thread):
+        (JSC::JITWorklist::JITWorklist):
+        (JSC::JITWorklist::completeAllForVM):
+        (JSC::JITWorklist::poll):
+        (JSC::JITWorklist::compileLater):
+        (JSC::JITWorklist::compileNow):
+        (JSC::JITWorklist::finalizePlans):
+        (JSC::JITWorklist::runThread): Deleted.
+        * jit/JITWorklist.h:
+
</ins><span class="cx"> 2016-10-19  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [macOS] [iOS] Disable variation fonts on macOS El Capitan and iOS 9
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitJITWorklistcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/JITWorklist.cpp (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/JITWorklist.cpp        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/JavaScriptCore/jit/JITWorklist.cpp        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -97,9 +97,60 @@
</span><span class="cx">     bool m_isFinishedCompiling { false };
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+class JITWorklist::Thread : public AutomaticThread {
+public:
+    Thread(const LockHolder&amp; locker, JITWorklist&amp; worklist)
+        : AutomaticThread(locker, worklist.m_lock, worklist.m_condition)
+        , m_worklist(worklist)
+    {
+        m_worklist.m_numAvailableThreads++;
+    }
+    
+protected:
+    PollResult poll(const LockHolder&amp;) override
+    {
+        RELEASE_ASSERT(m_worklist.m_numAvailableThreads);
+        
+        if (m_worklist.m_queue.isEmpty())
+            return PollResult::Wait;
+        
+        m_myPlans = WTFMove(m_worklist.m_queue);
+        m_worklist.m_numAvailableThreads--;
+        return PollResult::Work;
+    }
+    
+    WorkResult work() override
+    {
+        RELEASE_ASSERT(!m_myPlans.isEmpty());
+        
+        for (RefPtr&lt;Plan&gt;&amp; plan : m_myPlans) {
+            plan-&gt;compileInThread();
+            plan = nullptr;
+            
+            // Make sure that the main thread realizes that we just compiled something. Notifying
+            // a condition is basically free if nobody is waiting.
+            LockHolder locker(*m_worklist.m_lock);
+            m_worklist.m_condition-&gt;notifyAll(locker);
+        }
+        
+        m_myPlans.clear();
+        
+        LockHolder locker(*m_worklist.m_lock);
+        m_worklist.m_numAvailableThreads++;
+        return WorkResult::Continue;
+    }
+    
+private:
+    JITWorklist&amp; m_worklist;
+    Plans m_myPlans;
+};
+
</ins><span class="cx"> JITWorklist::JITWorklist()
</span><ins>+    : m_lock(Box&lt;Lock&gt;::create())
+    , m_condition(AutomaticThreadCondition::create())
</ins><span class="cx"> {
</span><del>-    createThread(&quot;JIT Worklist Worker Thread&quot;, [this] () { runThread(); });
</del><ins>+    LockHolder locker(*m_lock);
+    m_thread = new Thread(locker, *this);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> JITWorklist::~JITWorklist()
</span><span class="lines">@@ -113,7 +164,7 @@
</span><span class="cx">     for (;;) {
</span><span class="cx">         Vector&lt;RefPtr&lt;Plan&gt;, 32&gt; myPlans;
</span><span class="cx">         {
</span><del>-            LockHolder locker(m_lock);
</del><ins>+            LockHolder locker(*m_lock);
</ins><span class="cx">             for (;;) {
</span><span class="cx">                 bool didFindUnfinishedPlan = false;
</span><span class="cx">                 m_plans.removeAllMatching(
</span><span class="lines">@@ -137,7 +188,7 @@
</span><span class="cx">                 if (!didFindUnfinishedPlan)
</span><span class="cx">                     return;
</span><span class="cx">                 
</span><del>-                m_condition.wait(m_lock);
</del><ins>+                m_condition-&gt;wait(*m_lock);
</ins><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="lines">@@ -150,7 +201,7 @@
</span><span class="cx">     DeferGC deferGC(vm.heap);
</span><span class="cx">     Plans myPlans;
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         m_plans.removeAllMatching(
</span><span class="cx">             [&amp;] (RefPtr&lt;Plan&gt;&amp; plan) {
</span><span class="cx">                 if (plan-&gt;vm() != &amp;vm)
</span><span class="lines">@@ -183,7 +234,7 @@
</span><span class="cx">     codeBlock-&gt;jitSoon();
</span><span class="cx"> 
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         
</span><span class="cx">         if (m_planned.contains(codeBlock))
</span><span class="cx">             return;
</span><span class="lines">@@ -193,7 +244,7 @@
</span><span class="cx">             RefPtr&lt;Plan&gt; plan = adoptRef(new Plan(codeBlock));
</span><span class="cx">             m_plans.append(plan);
</span><span class="cx">             m_queue.append(plan);
</span><del>-            m_condition.notifyAll();
</del><ins>+            m_condition-&gt;notifyAll(locker);
</ins><span class="cx">             return;
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="lines">@@ -225,7 +276,7 @@
</span><span class="cx">     
</span><span class="cx">     bool isPlanned;
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         isPlanned = m_planned.contains(codeBlock);
</span><span class="cx">     }
</span><span class="cx">     
</span><span class="lines">@@ -248,42 +299,12 @@
</span><span class="cx">     codeBlock-&gt;ownerScriptExecutable()-&gt;installCode(codeBlock);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void JITWorklist::runThread()
-{
-    for (;;) {
-        Plans myPlans;
-        {
-            LockHolder locker(m_lock);
-            m_numAvailableThreads++;
-            while (m_queue.isEmpty())
-                m_condition.wait(m_lock);
-            m_numAvailableThreads--;
-            
-            // This is a fun way to dequeue. I don't know if it's any better or worse than dequeuing
-            // one thing at a time.
-            myPlans = WTFMove(m_queue);
-        }
-        
-        RELEASE_ASSERT(!myPlans.isEmpty());
-        
-        for (RefPtr&lt;Plan&gt;&amp; plan : myPlans) {
-            plan-&gt;compileInThread();
-            plan = nullptr;
-            
-            // Make sure that the main thread realizes that we just compiled something. Notifying
-            // a WTF condition is basically free if nobody is waiting.
-            LockHolder locker(m_lock);
-            m_condition.notifyAll();
-        }
-    }
-}
-
</del><span class="cx"> void JITWorklist::finalizePlans(Plans&amp; myPlans)
</span><span class="cx"> {
</span><span class="cx">     for (RefPtr&lt;Plan&gt;&amp; plan : myPlans) {
</span><span class="cx">         plan-&gt;finalize();
</span><span class="cx">         
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         m_planned.remove(plan-&gt;codeBlock());
</span><span class="cx">     }
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitJITWorklisth"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/JITWorklist.h (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/JITWorklist.h        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/JavaScriptCore/jit/JITWorklist.h        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -27,7 +27,7 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(JIT)
</span><span class="cx"> 
</span><del>-#include &lt;wtf/Condition.h&gt;
</del><ins>+#include &lt;wtf/AutomaticThread.h&gt;
</ins><span class="cx"> #include &lt;wtf/FastMalloc.h&gt;
</span><span class="cx"> #include &lt;wtf/HashSet.h&gt;
</span><span class="cx"> #include &lt;wtf/Lock.h&gt;
</span><span class="lines">@@ -62,7 +62,8 @@
</span><span class="cx"> private:
</span><span class="cx">     JITWorklist();
</span><span class="cx">     
</span><del>-    NO_RETURN void runThread();
</del><ins>+    class Thread;
+    friend class Thread;
</ins><span class="cx">     
</span><span class="cx">     void finalizePlans(Plans&amp;);
</span><span class="cx">     
</span><span class="lines">@@ -70,8 +71,9 @@
</span><span class="cx">     Plans m_plans;
</span><span class="cx">     HashSet&lt;CodeBlock*&gt; m_planned;
</span><span class="cx">     
</span><del>-    Lock m_lock;
-    Condition m_condition; // We use One True Condition for everything because that's easier.
</del><ins>+    Box&lt;Lock&gt; m_lock;
+    RefPtr&lt;AutomaticThreadCondition&gt; m_condition; // We use One True Condition for everything because that's easier.
+    RefPtr&lt;AutomaticThread&gt; m_thread;
</ins><span class="cx">     
</span><span class="cx">     unsigned m_numAvailableThreads { 0 };
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/ChangeLog        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2016-10-19  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        Baseline JIT should use AutomaticThread
+        https://bugs.webkit.org/show_bug.cgi?id=163686
+
+        Reviewed by Geoffrey Garen.
+        
+        Added a AutomaticThreadCondition::wait() method, so that if you really want to use one
+        common condition for your thread and something else, you can do it. This trivially works
+        if you only use notifyAll(), and behaves as you'd expect for notifyOne() (i.e. it's
+        dangerous, since you don't know who will wake up).
+        
+        The Baseline JIT used the one-true-Condition idiom because it used notifyAll() in an
+        optimal way: there are just two threads talking to each other, so it wakes up at most one
+        thread and that thread is exactly the one you want woken up. Adding wait() means that I did
+        not have to change that code.
+
+        * wtf/AutomaticThread.cpp:
+        (WTF::AutomaticThreadCondition::wait):
+        * wtf/AutomaticThread.h:
+
</ins><span class="cx"> 2016-10-18  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         DFG worklist should use AutomaticThread
</span></span></pre></div>
<a id="trunkSourceWTFwtfAutomaticThreadcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/AutomaticThread.cpp (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/AutomaticThread.cpp        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/wtf/AutomaticThread.cpp        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -65,6 +65,11 @@
</span><span class="cx">     m_threads.clear();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void AutomaticThreadCondition::wait(Lock&amp; lock)
+{
+    m_condition.wait(lock);
+}
+
</ins><span class="cx"> void AutomaticThreadCondition::add(const LockHolder&amp;, AutomaticThread* thread)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!m_threads.contains(thread));
</span></span></pre></div>
<a id="trunkSourceWTFwtfAutomaticThreadh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/AutomaticThread.h (207565 => 207566)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/AutomaticThread.h        2016-10-19 20:34:14 UTC (rev 207565)
+++ trunk/Source/WTF/wtf/AutomaticThread.h        2016-10-19 20:37:20 UTC (rev 207566)
</span><span class="lines">@@ -78,6 +78,13 @@
</span><span class="cx">     WTF_EXPORT_PRIVATE void notifyOne(const LockHolder&amp;);
</span><span class="cx">     WTF_EXPORT_PRIVATE void notifyAll(const LockHolder&amp;);
</span><span class="cx">     
</span><ins>+    // You can reuse this condition for other things, just as you would any other condition.
+    // However, since conflating conditions could lead to thundering herd, it's best to avoid it.
+    // One known-good case for one-true-condition is when the communication involves just two
+    // threads. In such cases, the thread doing the notifyAll() can wake up at most one thread -
+    // its partner.
+    WTF_EXPORT_PRIVATE void wait(Lock&amp;);
+    
</ins><span class="cx"> private:
</span><span class="cx">     friend class AutomaticThread;
</span><span class="cx">     
</span></span></pre>
</div>
</div>

</body>
</html>