<!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>[207480] 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/207480">207480</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-10-18 13:17:10 -0700 (Tue, 18 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>WTF should make it easier to create threads that die automatically after inactivity
https://bugs.webkit.org/show_bug.cgi?id=163576

Reviewed by Andreas Kling.
        
Source/JavaScriptCore:

Added a sleepSeconds() function, which made it easier for me to test this change.
        
The WTF changes in this patch change how the JSC GC manages threads: the GC threads will now
shut down automatically after 1 second of inactivity. Maybe this will save some memory.

* jsc.cpp:
(GlobalObject::finishCreation):
(functionSleepSeconds):

Source/WTF:

For a long time now, I've been adding threads to WTF/JSC and each time I do this, I feel
guilty because those threads don't shut down when they are inactive. For example, in bug
163562, I need to add a new GC thread. There will be one of them per VM. This means that a
JSC API client that starts a lot of VMs will have a lot of threads. I don't think that's
good.
        
A common pattern for all of these threads is that they have some well-defined trigger that
causes them to run. This trigger has a lock, a condition variable, some logic that determines
if there is work to do, and then of course the logic for the thread's actual work. The thread
bodies usually look like this:
        
void Thingy::runThread()
{
    for (;;) {
        Work work;
        {
            LockHolder locker(m_lock);
            while (!hasWork())
                m_cond.wait(m_lock);
            work = takeWork();
        }
        doWork(work);
    }
}
        
If you look at ParallelHelperPool (the GC's threads) and DFG::Worklist (some of the JIT's
threads), you will see this pattern.
        
This change adds a new kind of thread, called AutomaticThread, that lets you write threads to
this pattern while getting automatic thread shutdown for free: instead of just waiting on a
condition variable, AutomaticThread will have a timeout that causes the thread to die. The
condition variable associated with AutomaticThread, called AutomaticThreadCondition, is smart
enough to restart any threads that have decided to stop due to inactivity. The inactivity
threshold is current just 1 second.
        
In this patch I only adopt AutomaticThread for ParallelHelperPool. I plan to adopt it in more
places soon.

* WTF.xcodeproj/project.pbxproj:
* wtf/AutomaticThread.cpp: Added.
(WTF::AutomaticThreadCondition::create):
(WTF::AutomaticThreadCondition::AutomaticThreadCondition):
(WTF::AutomaticThreadCondition::~AutomaticThreadCondition):
(WTF::AutomaticThreadCondition::notifyAll):
(WTF::AutomaticThreadCondition::add):
(WTF::AutomaticThreadCondition::remove):
(WTF::AutomaticThreadCondition::contains):
(WTF::AutomaticThread::AutomaticThread):
(WTF::AutomaticThread::~AutomaticThread):
(WTF::AutomaticThread::join):
(WTF::AutomaticThread::start):
* wtf/AutomaticThread.h: Added.
* wtf/CMakeLists.txt:
* wtf/ParallelHelperPool.cpp:
(WTF::ParallelHelperClient::ParallelHelperClient):
(WTF::ParallelHelperClient::~ParallelHelperClient):
(WTF::ParallelHelperClient::setTask):
(WTF::ParallelHelperClient::finish):
(WTF::ParallelHelperClient::doSomeHelping):
(WTF::ParallelHelperClient::runTask):
(WTF::ParallelHelperPool::ParallelHelperPool):
(WTF::ParallelHelperPool::~ParallelHelperPool):
(WTF::ParallelHelperPool::ensureThreads):
(WTF::ParallelHelperPool::doSomeHelping):
(WTF::ParallelHelperPool::Thread::Thread):
(WTF::ParallelHelperPool::didMakeWorkAvailable):
(WTF::ParallelHelperPool::helperThreadBody): Deleted.
(WTF::ParallelHelperPool::waitForClientWithTask): Deleted.
* wtf/ParallelHelperPool.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejsccpp">trunk/Source/JavaScriptCore/jsc.cpp</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFWTFxcodeprojprojectpbxproj">trunk/Source/WTF/WTF.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWTFwtfCMakeListstxt">trunk/Source/WTF/wtf/CMakeLists.txt</a></li>
<li><a href="#trunkSourceWTFwtfParallelHelperPoolcpp">trunk/Source/WTF/wtf/ParallelHelperPool.cpp</a></li>
<li><a href="#trunkSourceWTFwtfParallelHelperPoolh">trunk/Source/WTF/wtf/ParallelHelperPool.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<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 (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2016-10-18  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        WTF should make it easier to create threads that die automatically after inactivity
+        https://bugs.webkit.org/show_bug.cgi?id=163576
+
+        Reviewed by Andreas Kling.
+        
+        Added a sleepSeconds() function, which made it easier for me to test this change.
+        
+        The WTF changes in this patch change how the JSC GC manages threads: the GC threads will now
+        shut down automatically after 1 second of inactivity. Maybe this will save some memory.
+
+        * jsc.cpp:
+        (GlobalObject::finishCreation):
+        (functionSleepSeconds):
+
</ins><span class="cx"> 2016-10-18  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Cleanup Wasm memory.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -843,6 +843,7 @@
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionDebug(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionDescribe(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionDescribeArray(ExecState*);
</span><ins>+static EncodedJSValue JSC_HOST_CALL functionSleepSeconds(ExecState*);
</ins><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionJSCStack(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionGCAndSweep(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionFullGC(ExecState*);
</span><span class="lines">@@ -1067,6 +1068,7 @@
</span><span class="cx">         addFunction(vm, &quot;readFile&quot;, functionReadFile, 2);
</span><span class="cx">         addFunction(vm, &quot;read&quot;, functionReadFile, 2);
</span><span class="cx">         addFunction(vm, &quot;checkSyntax&quot;, functionCheckSyntax, 1);
</span><ins>+        addFunction(vm, &quot;sleepSeconds&quot;, functionSleepSeconds, 1);
</ins><span class="cx">         addFunction(vm, &quot;jscStack&quot;, functionJSCStack, 1);
</span><span class="cx">         addFunction(vm, &quot;readline&quot;, functionReadline, 0);
</span><span class="cx">         addFunction(vm, &quot;preciseTime&quot;, functionPreciseTime, 0);
</span><span class="lines">@@ -1493,6 +1495,13 @@
</span><span class="cx">     return JSValue::encode(jsNontrivialString(exec, toString(&quot;&lt;Butterfly: &quot;, RawPointer(object-&gt;butterfly()), &quot;; public length: &quot;, object-&gt;getArrayLength(), &quot;; vector length: &quot;, object-&gt;getVectorLength(), &quot;&gt;&quot;)));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+EncodedJSValue JSC_HOST_CALL functionSleepSeconds(ExecState* exec)
+{
+    if (exec-&gt;argumentCount() &gt;= 1)
+        sleep(exec-&gt;argument(0).toNumber(exec));
+    return JSValue::encode(jsUndefined());
+}
+
</ins><span class="cx"> class FunctionJSCStackFunctor {
</span><span class="cx"> public:
</span><span class="cx">     FunctionJSCStackFunctor(StringBuilder&amp; trace)
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/WTF/ChangeLog        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -1,3 +1,80 @@
</span><ins>+2016-10-18  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        WTF should make it easier to create threads that die automatically after inactivity
+        https://bugs.webkit.org/show_bug.cgi?id=163576
+
+        Reviewed by Andreas Kling.
+        
+        For a long time now, I've been adding threads to WTF/JSC and each time I do this, I feel
+        guilty because those threads don't shut down when they are inactive. For example, in bug
+        163562, I need to add a new GC thread. There will be one of them per VM. This means that a
+        JSC API client that starts a lot of VMs will have a lot of threads. I don't think that's
+        good.
+        
+        A common pattern for all of these threads is that they have some well-defined trigger that
+        causes them to run. This trigger has a lock, a condition variable, some logic that determines
+        if there is work to do, and then of course the logic for the thread's actual work. The thread
+        bodies usually look like this:
+        
+        void Thingy::runThread()
+        {
+            for (;;) {
+                Work work;
+                {
+                    LockHolder locker(m_lock);
+                    while (!hasWork())
+                        m_cond.wait(m_lock);
+                    work = takeWork();
+                }
+                doWork(work);
+            }
+        }
+        
+        If you look at ParallelHelperPool (the GC's threads) and DFG::Worklist (some of the JIT's
+        threads), you will see this pattern.
+        
+        This change adds a new kind of thread, called AutomaticThread, that lets you write threads to
+        this pattern while getting automatic thread shutdown for free: instead of just waiting on a
+        condition variable, AutomaticThread will have a timeout that causes the thread to die. The
+        condition variable associated with AutomaticThread, called AutomaticThreadCondition, is smart
+        enough to restart any threads that have decided to stop due to inactivity. The inactivity
+        threshold is current just 1 second.
+        
+        In this patch I only adopt AutomaticThread for ParallelHelperPool. I plan to adopt it in more
+        places soon.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/AutomaticThread.cpp: Added.
+        (WTF::AutomaticThreadCondition::create):
+        (WTF::AutomaticThreadCondition::AutomaticThreadCondition):
+        (WTF::AutomaticThreadCondition::~AutomaticThreadCondition):
+        (WTF::AutomaticThreadCondition::notifyAll):
+        (WTF::AutomaticThreadCondition::add):
+        (WTF::AutomaticThreadCondition::remove):
+        (WTF::AutomaticThreadCondition::contains):
+        (WTF::AutomaticThread::AutomaticThread):
+        (WTF::AutomaticThread::~AutomaticThread):
+        (WTF::AutomaticThread::join):
+        (WTF::AutomaticThread::start):
+        * wtf/AutomaticThread.h: Added.
+        * wtf/CMakeLists.txt:
+        * wtf/ParallelHelperPool.cpp:
+        (WTF::ParallelHelperClient::ParallelHelperClient):
+        (WTF::ParallelHelperClient::~ParallelHelperClient):
+        (WTF::ParallelHelperClient::setTask):
+        (WTF::ParallelHelperClient::finish):
+        (WTF::ParallelHelperClient::doSomeHelping):
+        (WTF::ParallelHelperClient::runTask):
+        (WTF::ParallelHelperPool::ParallelHelperPool):
+        (WTF::ParallelHelperPool::~ParallelHelperPool):
+        (WTF::ParallelHelperPool::ensureThreads):
+        (WTF::ParallelHelperPool::doSomeHelping):
+        (WTF::ParallelHelperPool::Thread::Thread):
+        (WTF::ParallelHelperPool::didMakeWorkAvailable):
+        (WTF::ParallelHelperPool::helperThreadBody): Deleted.
+        (WTF::ParallelHelperPool::waitForClientWithTask): Deleted.
+        * wtf/ParallelHelperPool.h:
+
</ins><span class="cx"> 2016-10-18  Said Abou-Hallawa  &lt;sabouhallawa@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         SVGCSSParser: m_implicitShorthand value is not reset after adding the shorthand property
</span></span></pre></div>
<a id="trunkSourceWTFWTFxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -25,6 +25,8 @@
</span><span class="cx">                 0F2B66A617B6B4FB00A7AE3F /* DeferrableRefCounted.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66A417B6B4F700A7AE3F /* DeferrableRefCounted.h */; };
</span><span class="cx">                 0F2B66A717B6B4FD00A7AE3F /* FlipBytes.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66A517B6B4F700A7AE3F /* FlipBytes.h */; };
</span><span class="cx">                 0F3501641BB258D500F0A2A3 /* WeakRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F3501631BB258C800F0A2A3 /* WeakRandom.h */; };
</span><ins>+                0F43D8F11DB5ADDC00108FB6 /* AutomaticThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F43D8EF1DB5ADDC00108FB6 /* AutomaticThread.cpp */; };
+                0F43D8F21DB5ADDC00108FB6 /* AutomaticThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F43D8F01DB5ADDC00108FB6 /* AutomaticThread.h */; };
</ins><span class="cx">                 0F4570431BE5B58F0062A629 /* Dominators.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F4570421BE5B58F0062A629 /* Dominators.h */; };
</span><span class="cx">                 0F4570451BE834410062A629 /* BubbleSort.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F4570441BE834410062A629 /* BubbleSort.h */; };
</span><span class="cx">                 0F725CAC1C50461600AD943A /* RangeSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F725CAB1C50461600AD943A /* RangeSet.h */; };
</span><span class="lines">@@ -366,6 +368,8 @@
</span><span class="cx">                 0F2B66A517B6B4F700A7AE3F /* FlipBytes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FlipBytes.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F300B7D18AB48B400A6D72E /* HashMethod.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HashMethod.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F3501631BB258C800F0A2A3 /* WeakRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WeakRandom.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                0F43D8EF1DB5ADDC00108FB6 /* AutomaticThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AutomaticThread.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
+                0F43D8F01DB5ADDC00108FB6 /* AutomaticThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutomaticThread.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 0F4570421BE5B58F0062A629 /* Dominators.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dominators.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F4570441BE834410062A629 /* BubbleSort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BubbleSort.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F725CAB1C50461600AD943A /* RangeSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RangeSet.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -839,6 +843,8 @@
</span><span class="cx">                                 A8A4725D151A825A004123FF /* Atomics.h */,
</span><span class="cx">                                 1469419A16EAB10A0024E146 /* AutodrainedPool.h */,
</span><span class="cx">                                 1469419B16EAB10A0024E146 /* AutodrainedPoolMac.mm */,
</span><ins>+                                0F43D8EF1DB5ADDC00108FB6 /* AutomaticThread.cpp */,
+                                0F43D8F01DB5ADDC00108FB6 /* AutomaticThread.h */,
</ins><span class="cx">                                 DCEE22041CEB9869000C2396 /* BackwardsGraph.h */,
</span><span class="cx">                                 0FB14E18180FA218009B6B4D /* Bag.h */,
</span><span class="cx">                                 0FB14E1A1810E1DA009B6B4D /* BagToHashMap.h */,
</span><span class="lines">@@ -1330,6 +1336,7 @@
</span><span class="cx">                                 26147B0A15DDCCDC00DDB907 /* IntegerToStringConversion.h in Headers */,
</span><span class="cx">                                 7CDD7FF8186D291E007433CD /* IteratorAdaptors.h in Headers */,
</span><span class="cx">                                 7CDD7FFA186D2A54007433CD /* IteratorRange.h in Headers */,
</span><ins>+                                0F43D8F21DB5ADDC00108FB6 /* AutomaticThread.h in Headers */,
</ins><span class="cx">                                 93AC91A818942FC400244939 /* LChar.h in Headers */,
</span><span class="cx">                                 539EB0631D55284200C82EF7 /* LEBDecoder.h in Headers */,
</span><span class="cx">                                 A70DA0851799F04D00529A9B /* ListDump.h in Headers */,
</span><span class="lines">@@ -1634,6 +1641,7 @@
</span><span class="cx">                                 A5BA15F3182433A900A82E69 /* StringMac.mm in Sources */,
</span><span class="cx">                                 0FDDBFA71666DFA300C55FEF /* StringPrintStream.cpp in Sources */,
</span><span class="cx">                                 A8A47443151A825B004123FF /* StringStatics.cpp in Sources */,
</span><ins>+                                0F43D8F11DB5ADDC00108FB6 /* AutomaticThread.cpp in Sources */,
</ins><span class="cx">                                 93F1993E19D7958D00C2390B /* StringView.cpp in Sources */,
</span><span class="cx">                                 93934BD518A1F16900D0D6A1 /* StringViewCF.cpp in Sources */,
</span><span class="cx">                                 93934BD318A1E8C300D0D6A1 /* StringViewObjC.mm in Sources */,
</span></span></pre></div>
<a id="trunkSourceWTFwtfAutomaticThreadcpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WTF/wtf/AutomaticThread.cpp (0 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/AutomaticThread.cpp                                (rev 0)
+++ trunk/Source/WTF/wtf/AutomaticThread.cpp        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -0,0 +1,154 @@
</span><ins>+/*
+ * Copyright (C) 2016 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 INC. OR
+ * 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. 
+ */
+
+#include &quot;config.h&quot;
+#include &quot;AutomaticThread.h&quot;
+
+#include &quot;DataLog.h&quot;
+
+namespace WTF {
+
+static const bool verbose = false;
+
+RefPtr&lt;AutomaticThreadCondition&gt; AutomaticThreadCondition::create()
+{
+    return adoptRef(new AutomaticThreadCondition());
+}
+
+AutomaticThreadCondition::AutomaticThreadCondition()
+{
+}
+
+AutomaticThreadCondition::~AutomaticThreadCondition()
+{
+}
+
+void AutomaticThreadCondition::notifyAll(const LockHolder&amp; locker)
+{
+    m_condition.notifyAll();
+    
+    for (AutomaticThread* thread : m_threads)
+        thread-&gt;start(locker);
+    m_threads.clear();
+}
+
+void AutomaticThreadCondition::add(const LockHolder&amp;, AutomaticThread* thread)
+{
+    ASSERT(!m_threads.contains(thread));
+    m_threads.append(thread);
+}
+
+void AutomaticThreadCondition::remove(const LockHolder&amp;, AutomaticThread* thread)
+{
+    ASSERT(m_threads.contains(thread));
+    m_threads.removeFirst(thread);
+    ASSERT(!m_threads.contains(thread));
+}
+
+bool AutomaticThreadCondition::contains(const LockHolder&amp;, AutomaticThread* thread)
+{
+    return m_threads.contains(thread);
+}
+
+AutomaticThread::AutomaticThread(const LockHolder&amp; locker, Box&lt;Lock&gt; lock, RefPtr&lt;AutomaticThreadCondition&gt; condition)
+    : m_lock(lock)
+    , m_condition(condition)
+{
+    m_condition-&gt;add(locker, this);
+}
+
+AutomaticThread::~AutomaticThread()
+{
+    LockHolder locker(*m_lock);
+    
+    // It's possible that we're in a waiting state with the thread shut down. This is a goofy way to
+    // die, but it could happen.
+    m_condition-&gt;remove(locker, this);
+}
+
+void AutomaticThread::join()
+{
+    LockHolder locker(*m_lock);
+    while (m_isRunning)
+        m_isRunningCondition.wait(*m_lock);
+}
+
+void AutomaticThread::start(const LockHolder&amp;)
+{
+    RefPtr&lt;AutomaticThread&gt; preserveThisForThread = this;
+    
+    ThreadIdentifier thread = createThread(
+        &quot;WTF::AutomaticThread&quot;,
+        [=] () {
+            if (verbose)
+                dataLog(&quot;Running automatic thread!\n&quot;);
+            RefPtr&lt;AutomaticThread&gt; preserveThisInThread = preserveThisForThread;
+            
+            {
+                LockHolder locker(*m_lock);
+                ASSERT(!m_condition-&gt;contains(locker, this));
+            }
+            
+            auto stop = [&amp;] (const LockHolder&amp;) {
+                m_isRunning = false;
+                m_isRunningCondition.notifyAll();
+            };
+            
+            for (;;) {
+                {
+                    LockHolder locker(*m_lock);
+                    for (;;) {
+                        PollResult result = poll(locker);
+                        if (result == PollResult::Work)
+                            break;
+                        if (result == PollResult::Stop)
+                            return stop(locker);
+                        RELEASE_ASSERT(result == PollResult::Wait);
+                        // Shut the thread down after one second.
+                        double timeout = monotonicallyIncreasingTime() + 1;
+                        bool awokenByNotify =
+                            m_condition-&gt;m_condition.waitUntilMonotonicClockSeconds(*m_lock, timeout);
+                        if (!awokenByNotify) {
+                            if (verbose)
+                                dataLog(&quot;Going to sleep!\n&quot;);
+                            m_condition-&gt;add(locker, this);
+                            return;
+                        }
+                    }
+                }
+                
+                WorkResult result = work();
+                if (result == WorkResult::Stop) {
+                    LockHolder locker(*m_lock);
+                    return stop(locker);
+                }
+                RELEASE_ASSERT(result == WorkResult::Continue);
+            }
+        });
+    detachThread(thread);
+}
+
+} // namespace WTF
+
</ins></span></pre></div>
<a id="trunkSourceWTFwtfAutomaticThreadh"></a>
<div class="addfile"><h4>Added: trunk/Source/WTF/wtf/AutomaticThread.h (0 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/AutomaticThread.h                                (rev 0)
+++ trunk/Source/WTF/wtf/AutomaticThread.h        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -0,0 +1,162 @@
</span><ins>+/*
+ * Copyright (C) 2016 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 INC. OR
+ * 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 WTF_AutomaticThread_h
+#define WTF_AutomaticThread_h
+
+#include &lt;wtf/Box.h&gt;
+#include &lt;wtf/Condition.h&gt;
+#include &lt;wtf/Lock.h&gt;
+#include &lt;wtf/Ref.h&gt;
+#include &lt;wtf/ThreadSafeRefCounted.h&gt;
+#include &lt;wtf/Threading.h&gt;
+#include &lt;wtf/Vector.h&gt;
+
+namespace WTF {
+
+// Often, we create threads that have this as their body:
+//
+//     for (;;) {
+//         {
+//             LockHolder locker(m_lock);
+//             for (;;) {
+//  [1]            stuff that could break, return, or fall through;
+//                 m_condition.wait(m_lock);
+//             }
+//         }
+//         
+//  [2]    do work;
+//     }
+//
+// When we do this, we don't always do a good job of managing this thread's lifetime, which may lead
+// to this thread sitting around even when it is not needed.
+//
+// AutomaticThread is here to help you in these situations. It encapsulates a lock, a condition
+// variable, and a thread. It will automatically shut the thread down after 1 second of inactivity.
+// You use AutomaticThread by subclassing it, and put any state that is needed between [1] and [2]
+// in the subclass.
+//
+// The terminology we use is:
+//
+// [1] PollResult AutomaticThread::poll()
+// [2] WordResult AutomaticThread::work()
+//
+// Note that poll() and work() may not be called on the same thread every time, since this will shut
+// down the thread as necessary. This is legal since m_condition.wait(m_lock) can drop the lock, and
+// so there is no reason to keep the thread around.
+
+class AutomaticThread;
+
+class AutomaticThreadCondition : public ThreadSafeRefCounted&lt;AutomaticThread&gt; {
+public:
+    static WTF_EXPORT_PRIVATE RefPtr&lt;AutomaticThreadCondition&gt; create();
+    
+    WTF_EXPORT_PRIVATE ~AutomaticThreadCondition();
+    
+    WTF_EXPORT_PRIVATE void notifyAll(const LockHolder&amp;);
+    
+private:
+    friend class AutomaticThread;
+    
+    WTF_EXPORT_PRIVATE AutomaticThreadCondition();
+
+    void add(const LockHolder&amp;, AutomaticThread*);
+    void remove(const LockHolder&amp;, AutomaticThread*);
+    bool contains(const LockHolder&amp;, AutomaticThread*);
+    
+    Condition m_condition;
+    Vector&lt;AutomaticThread*&gt; m_threads;
+};
+
+class WTF_EXPORT_PRIVATE AutomaticThread : public ThreadSafeRefCounted&lt;AutomaticThread&gt; {
+public:
+    // Note that if you drop all of your references to an AutomaticThread then as soon as there is a
+    // second during which it doesn't get woken up, it will simply die on its own. This is a
+    // permanent kind of death where the AutomaticThread object goes away, rather than the temporary
+    // kind of death where AutomaticThread lives but its underlying thread dies. All you have to do
+    // to prevent permanent death is keep a ref to AutomaticThread. At time of writing, every user of
+    // AutomaticThread keeps a ref to it and does join() as part of the shutdown process, so only the
+    // temporary kind of automatic death happens in practice. We keep the permanent death feature
+    // because it leads to an easy-to-understand reference counting discipline (AutomaticThread holds
+    // strong ref to AutomaticThreadCondition and the underlying thread holds a strong ref to
+    // AutomaticThread).
+    virtual ~AutomaticThread();
+    
+    void join();
+    
+protected:
+    // This logically creates the thread, but in reality the thread won't be created until someone
+    // calls AutomaticThreadCondition::notifyAll().
+    AutomaticThread(const LockHolder&amp;, Box&lt;Lock&gt;, RefPtr&lt;AutomaticThreadCondition&gt;);
+    
+    // To understand PollResult and WorkResult, imagine that poll() and work() are being called like
+    // so:
+    //
+    // void AutomaticThread::runThread()
+    // {
+    //     for (;;) {
+    //         {
+    //             LockHolder locker(m_lock);
+    //             for (;;) {
+    //                 PollResult result = poll();
+    //                 if (result == PollResult::Work)
+    //                     break;
+    //                 if (result == PollResult::Stop)
+    //                     return;
+    //                 RELEASE_ASSERT(result == PollResult::Wait);
+    //                 m_condition.wait(m_lock);
+    //             }
+    //         }
+    //         
+    //         WorkResult result = work();
+    //         if (result == WorkResult::Stop)
+    //             return;
+    //         RELEASE_ASSERT(result == WorkResult::Continue);
+    //     }
+    // }
+    
+    enum class PollResult { Work, Stop, Wait };
+    virtual PollResult poll(const LockHolder&amp;) = 0;
+    
+    enum class WorkResult { Continue, Stop };
+    virtual WorkResult work() = 0;
+    
+private:
+    friend class AutomaticThreadCondition;
+    
+    void start(const LockHolder&amp;);
+    
+    Box&lt;Lock&gt; m_lock;
+    RefPtr&lt;AutomaticThreadCondition&gt; m_condition;
+    bool m_isRunning { true };
+    Condition m_isRunningCondition;
+};
+
+} // namespace WTF
+
+using WTF::AutomaticThread;
+
+#endif // WTF_AutomaticThread_h
+
</ins></span></pre></div>
<a id="trunkSourceWTFwtfCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/CMakeLists.txt (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/CMakeLists.txt        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/WTF/wtf/CMakeLists.txt        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -2,6 +2,7 @@
</span><span class="cx">     ASCIICType.h
</span><span class="cx">     Assertions.h
</span><span class="cx">     Atomics.h
</span><ins>+    AutomaticThread.h
</ins><span class="cx">     BackwardsGraph.h
</span><span class="cx">     Bag.h
</span><span class="cx">     BagToHashMap.h
</span><span class="lines">@@ -173,6 +174,7 @@
</span><span class="cx"> set(WTF_SOURCES
</span><span class="cx">     Assertions.cpp
</span><span class="cx">     Atomics.cpp
</span><ins>+    AutomaticThread.cpp
</ins><span class="cx">     BitVector.cpp
</span><span class="cx">     CompilationThread.cpp
</span><span class="cx">     CrossThreadCopier.cpp
</span></span></pre></div>
<a id="trunkSourceWTFwtfParallelHelperPoolcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ParallelHelperPool.cpp (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ParallelHelperPool.cpp        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/WTF/wtf/ParallelHelperPool.cpp        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> #include &quot;ParallelHelperPool.h&quot;
</span><span class="cx"> 
</span><ins>+#include &quot;AutomaticThread.h&quot;
</ins><span class="cx"> #include &quot;DataLog.h&quot;
</span><span class="cx"> #include &quot;StringPrintStream.h&quot;
</span><span class="cx"> 
</span><span class="lines">@@ -34,7 +35,7 @@
</span><span class="cx"> ParallelHelperClient::ParallelHelperClient(RefPtr&lt;ParallelHelperPool&gt; pool)
</span><span class="cx">     : m_pool(pool)
</span><span class="cx"> {
</span><del>-    LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+    LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">     RELEASE_ASSERT(!m_pool-&gt;m_isDying);
</span><span class="cx">     m_pool-&gt;m_clients.append(this);
</span><span class="cx"> }
</span><span class="lines">@@ -41,7 +42,7 @@
</span><span class="cx"> 
</span><span class="cx"> ParallelHelperClient::~ParallelHelperClient()
</span><span class="cx"> {
</span><del>-    LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+    LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">     finish(locker);
</span><span class="cx"> 
</span><span class="cx">     for (size_t i = 0; i &lt; m_pool-&gt;m_clients.size(); ++i) {
</span><span class="lines">@@ -55,7 +56,7 @@
</span><span class="cx"> 
</span><span class="cx"> void ParallelHelperClient::setTask(RefPtr&lt;SharedTask&lt;void ()&gt;&gt; task)
</span><span class="cx"> {
</span><del>-    LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+    LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">     RELEASE_ASSERT(!m_task);
</span><span class="cx">     m_task = task;
</span><span class="cx">     m_pool-&gt;didMakeWorkAvailable(locker);
</span><span class="lines">@@ -63,7 +64,7 @@
</span><span class="cx"> 
</span><span class="cx"> void ParallelHelperClient::finish()
</span><span class="cx"> {
</span><del>-    LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+    LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">     finish(locker);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -71,7 +72,7 @@
</span><span class="cx"> {
</span><span class="cx">     RefPtr&lt;SharedTask&lt;void ()&gt;&gt; task;
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+        LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">         task = claimTask(locker);
</span><span class="cx">         if (!task)
</span><span class="cx">             return;
</span><span class="lines">@@ -91,7 +92,7 @@
</span><span class="cx"> {
</span><span class="cx">     m_task = nullptr;
</span><span class="cx">     while (m_numActive)
</span><del>-        m_pool-&gt;m_workCompleteCondition.wait(m_pool-&gt;m_lock);
</del><ins>+        m_pool-&gt;m_workCompleteCondition.wait(*m_pool-&gt;m_lock);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RefPtr&lt;SharedTask&lt;void ()&gt;&gt; ParallelHelperClient::claimTask(const LockHolder&amp;)
</span><span class="lines">@@ -111,7 +112,7 @@
</span><span class="cx">     task-&gt;run();
</span><span class="cx"> 
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_pool-&gt;m_lock);
</del><ins>+        LockHolder locker(*m_pool-&gt;m_lock);
</ins><span class="cx">         RELEASE_ASSERT(m_numActive);
</span><span class="cx">         // No new task could have been installed, since we were still active.
</span><span class="cx">         RELEASE_ASSERT(!m_task || m_task == task);
</span><span class="lines">@@ -123,6 +124,8 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> ParallelHelperPool::ParallelHelperPool()
</span><ins>+    : m_lock(Box&lt;Lock&gt;::create())
+    , m_workAvailableCondition(AutomaticThreadCondition::create())
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -131,18 +134,18 @@
</span><span class="cx">     RELEASE_ASSERT(m_clients.isEmpty());
</span><span class="cx">     
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         m_isDying = true;
</span><del>-        m_workAvailableCondition.notifyAll();
</del><ins>+        m_workAvailableCondition-&gt;notifyAll(locker);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    for (ThreadIdentifier threadIdentifier : m_threads)
-        waitForThreadCompletion(threadIdentifier);
</del><ins>+    for (RefPtr&lt;AutomaticThread&gt;&amp; thread : m_threads)
+        thread-&gt;join();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ParallelHelperPool::ensureThreads(unsigned numThreads)
</span><span class="cx"> {
</span><del>-    LockHolder locker(m_lock);
</del><ins>+    LockHolder locker(*m_lock);
</ins><span class="cx">     if (numThreads &lt; m_numThreads)
</span><span class="cx">         return;
</span><span class="cx">     m_numThreads = numThreads;
</span><span class="lines">@@ -155,7 +158,7 @@
</span><span class="cx">     ParallelHelperClient* client;
</span><span class="cx">     RefPtr&lt;SharedTask&lt;void ()&gt;&gt; task;
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_lock);
</del><ins>+        LockHolder locker(*m_lock);
</ins><span class="cx">         client = getClientWithTask(locker);
</span><span class="cx">         if (!client)
</span><span class="cx">             return;
</span><span class="lines">@@ -165,38 +168,46 @@
</span><span class="cx">     client-&gt;runTask(task);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void ParallelHelperPool::didMakeWorkAvailable(const LockHolder&amp;)
-{
-    while (m_numThreads &gt; m_threads.size()) {
-        ThreadIdentifier threadIdentifier = createThread(
-            &quot;WTF Parallel Helper Thread&quot;,
-            [this] () {
-                helperThreadBody();
-            });
-        m_threads.append(threadIdentifier);
</del><ins>+class ParallelHelperPool::Thread : public AutomaticThread {
+public:
+    Thread(const LockHolder&amp; locker, ParallelHelperPool&amp; pool)
+        : AutomaticThread(locker, pool.m_lock, pool.m_workAvailableCondition)
+        , m_pool(pool)
+    {
</ins><span class="cx">     }
</span><del>-    m_workAvailableCondition.notifyAll();
-}
</del><ins>+    
+protected:
+    PollResult poll(const LockHolder&amp; locker) override
+    {
+        if (m_pool.m_isDying)
+            return PollResult::Stop;
+        m_client = m_pool.getClientWithTask(locker);
+        if (m_client) {
+            m_task = m_client-&gt;claimTask(locker);
+            return PollResult::Work;
+        }
+        return PollResult::Wait;
+    }
+    
+    WorkResult work() override
+    {
+        m_client-&gt;runTask(m_task);
+        m_client = nullptr;
+        m_task = nullptr;
+        return WorkResult::Continue;
+    }
+    
+private:
+    ParallelHelperPool&amp; m_pool;
+    ParallelHelperClient* m_client { nullptr };
+    RefPtr&lt;SharedTask&lt;void ()&gt;&gt; m_task;
+};
</ins><span class="cx"> 
</span><del>-void ParallelHelperPool::helperThreadBody()
</del><ins>+void ParallelHelperPool::didMakeWorkAvailable(const LockHolder&amp; locker)
</ins><span class="cx"> {
</span><del>-    for (;;) {
-        ParallelHelperClient* client;
-        RefPtr&lt;SharedTask&lt;void ()&gt;&gt; task;
-
-        {
-            LockHolder locker(m_lock);
-            client = waitForClientWithTask(locker);
-            if (!client) {
-                RELEASE_ASSERT(m_isDying);
-                return;
-            }
-
-            task = client-&gt;claimTask(locker);
-        }
-
-        client-&gt;runTask(task);
-    }
</del><ins>+    while (m_numThreads &gt; m_threads.size())
+        m_threads.append(adoptRef(new Thread(locker, *this)));
+    m_workAvailableCondition-&gt;notifyAll(locker);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool ParallelHelperPool::hasClientWithTask(const LockHolder&amp; locker)
</span><span class="lines">@@ -222,20 +233,5 @@
</span><span class="cx">     return nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-ParallelHelperClient* ParallelHelperPool::waitForClientWithTask(const LockHolder&amp; locker)
-{
-    for (;;) {
-        // It might be quittin' time.
-        if (m_isDying)
-            return nullptr;
-
-        if (ParallelHelperClient* result = getClientWithTask(locker))
-            return result;
-
-        // Wait until work becomes available.
-        m_workAvailableCondition.wait(m_lock);
-    }
-}
-
</del><span class="cx"> } // namespace WTF
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWTFwtfParallelHelperPoolh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ParallelHelperPool.h (207479 => 207480)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ParallelHelperPool.h        2016-10-18 20:03:18 UTC (rev 207479)
+++ trunk/Source/WTF/wtf/ParallelHelperPool.h        2016-10-18 20:17:10 UTC (rev 207480)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #ifndef ParallelHelperPool_h
</span><span class="cx"> #define ParallelHelperPool_h
</span><span class="cx"> 
</span><ins>+#include &lt;wtf/Box.h&gt;
</ins><span class="cx"> #include &lt;wtf/Condition.h&gt;
</span><span class="cx"> #include &lt;wtf/Lock.h&gt;
</span><span class="cx"> #include &lt;wtf/RefPtr.h&gt;
</span><span class="lines">@@ -37,6 +38,9 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WTF {
</span><span class="cx"> 
</span><ins>+class AutomaticThread;
+class AutomaticThreadCondition;
+
</ins><span class="cx"> // A ParallelHelperPool is a shared pool of threads that can be asked to help with some finite-time
</span><span class="cx"> // parallel activity. It's designed to work well when there are multiple concurrent tasks that may
</span><span class="cx"> // all want parallel help. In that case, we don't want each task to start its own thread pool. It's
</span><span class="lines">@@ -186,22 +190,23 @@
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     friend class ParallelHelperClient;
</span><ins>+    class Thread;
+    friend class Thread;
</ins><span class="cx"> 
</span><span class="cx">     void didMakeWorkAvailable(const LockHolder&amp;);
</span><del>-    void helperThreadBody();
</del><span class="cx"> 
</span><span class="cx">     bool hasClientWithTask(const LockHolder&amp;);
</span><span class="cx">     ParallelHelperClient* getClientWithTask(const LockHolder&amp;);
</span><span class="cx">     ParallelHelperClient* waitForClientWithTask(const LockHolder&amp;);
</span><span class="cx">     
</span><del>-    Lock m_lock;
-    Condition m_workAvailableCondition;
</del><ins>+    Box&lt;Lock&gt; m_lock; // AutomaticThread wants this in a box for safety.
+    RefPtr&lt;AutomaticThreadCondition&gt; m_workAvailableCondition;
</ins><span class="cx">     Condition m_workCompleteCondition;
</span><span class="cx"> 
</span><span class="cx">     WeakRandom m_random;
</span><span class="cx">     
</span><span class="cx">     Vector&lt;ParallelHelperClient*&gt; m_clients;
</span><del>-    Vector&lt;ThreadIdentifier&gt; m_threads;
</del><ins>+    Vector&lt;RefPtr&lt;AutomaticThread&gt;&gt; m_threads;
</ins><span class="cx">     unsigned m_numThreads { 0 }; // This can be larger than m_threads.size() because we start threads only once there is work.
</span><span class="cx">     bool m_isDying { false };
</span><span class="cx"> };
</span></span></pre>
</div>
</div>

</body>
</html>