<!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>[199679] trunk/Source/WTF</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/199679">199679</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-04-18 10:30:36 -0700 (Mon, 18 Apr 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Unreviewed, rolling out <a href="http://trac.webkit.org/projects/webkit/changeset/199621">r199621</a>.
https://bugs.webkit.org/show_bug.cgi?id=156708

made windows tests crash (Requested by alexchristensen on
#webkit).

Reverted changeset:

&quot;iTunes crashing JavaScriptCore.dll&quot;
https://bugs.webkit.org/show_bug.cgi?id=156647
http://trac.webkit.org/changeset/199621</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfThreadSpecifich">trunk/Source/WTF/wtf/ThreadSpecific.h</a></li>
<li><a href="#trunkSourceWTFwtfThreadSpecificWincpp">trunk/Source/WTF/wtf/ThreadSpecificWin.cpp</a></li>
<li><a href="#trunkSourceWTFwtfThreadingWincpp">trunk/Source/WTF/wtf/ThreadingWin.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (199678 => 199679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2016-04-18 17:28:40 UTC (rev 199678)
+++ trunk/Source/WTF/ChangeLog        2016-04-18 17:30:36 UTC (rev 199679)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2016-04-18  Commit Queue  &lt;commit-queue@webkit.org&gt;
+
+        Unreviewed, rolling out r199621.
+        https://bugs.webkit.org/show_bug.cgi?id=156708
+
+        made windows tests crash (Requested by alexchristensen on
+        #webkit).
+
+        Reverted changeset:
+
+        &quot;iTunes crashing JavaScriptCore.dll&quot;
+        https://bugs.webkit.org/show_bug.cgi?id=156647
+        http://trac.webkit.org/changeset/199621
+
</ins><span class="cx"> 2016-04-15  Michael Saboff  &lt;msaboff@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         iTunes crashing JavaScriptCore.dll
</span></span></pre></div>
<a id="trunkSourceWTFwtfThreadSpecifich"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ThreadSpecific.h (199678 => 199679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ThreadSpecific.h        2016-04-18 17:28:40 UTC (rev 199678)
+++ trunk/Source/WTF/wtf/ThreadSpecific.h        2016-04-18 17:30:36 UTC (rev 199679)
</span><span class="lines">@@ -94,6 +94,9 @@
</span><span class="cx"> 
</span><span class="cx">         T* value;
</span><span class="cx">         ThreadSpecific&lt;T&gt;* owner;
</span><ins>+#if OS(WINDOWS)
+        void (*destructor)(void*);
+#endif
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx"> #if USE(PTHREADS)
</span><span class="lines">@@ -155,64 +158,47 @@
</span><span class="cx"> 
</span><span class="cx"> #elif OS(WINDOWS)
</span><span class="cx"> 
</span><del>-// The maximum number of FLS keys that can be created. For simplification, we assume that:
</del><ins>+// The maximum number of TLS keys that can be created. For simplification, we assume that:
</ins><span class="cx"> // 1) Once the instance of ThreadSpecific&lt;&gt; is created, it will not be destructed until the program dies.
</span><span class="cx"> // 2) We do not need to hold many instances of ThreadSpecific&lt;&gt; data. This fixed number should be far enough.
</span><del>-const int kMaxFlsKeySize = 100;
</del><ins>+const int kMaxTlsKeySize = 256;
</ins><span class="cx"> 
</span><del>-WTF_EXPORT_PRIVATE long&amp; flsKeyCount();
-WTF_EXPORT_PRIVATE DWORD* flsKeys();
</del><ins>+WTF_EXPORT_PRIVATE long&amp; tlsKeyCount();
+WTF_EXPORT_PRIVATE DWORD* tlsKeys();
</ins><span class="cx"> 
</span><del>-typedef DWORD ThreadSpecificKey;
</del><ins>+class PlatformThreadSpecificKey;
+typedef PlatformThreadSpecificKey* ThreadSpecificKey;
</ins><span class="cx"> 
</span><del>-inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
-{
-    DWORD flsKey = FlsAlloc(reinterpret_cast&lt;PFLS_CALLBACK_FUNCTION&gt;(destructor));
-    if (flsKey == FLS_OUT_OF_INDEXES)
-        CRASH();
</del><ins>+WTF_EXPORT_PRIVATE void threadSpecificKeyCreate(ThreadSpecificKey*, void (*)(void *));
+WTF_EXPORT_PRIVATE void threadSpecificKeyDelete(ThreadSpecificKey);
+WTF_EXPORT_PRIVATE void threadSpecificSet(ThreadSpecificKey, void*);
+WTF_EXPORT_PRIVATE void* threadSpecificGet(ThreadSpecificKey);
</ins><span class="cx"> 
</span><del>-    *key = flsKey;
-}
-
-inline void threadSpecificKeyDelete(ThreadSpecificKey key)
-{
-    FlsFree(key);
-}
-
-inline void threadSpecificSet(ThreadSpecificKey key, void* data)
-{
-    FlsSetValue(key, data);
-}
-
-inline void* threadSpecificGet(ThreadSpecificKey key)
-{
-    return FlsGetValue(key);
-}
-
</del><span class="cx"> template&lt;typename T&gt;
</span><span class="cx"> inline ThreadSpecific&lt;T&gt;::ThreadSpecific()
</span><span class="cx">     : m_index(-1)
</span><span class="cx"> {
</span><del>-    DWORD flsKey = FlsAlloc(reinterpret_cast&lt;PFLS_CALLBACK_FUNCTION&gt;(destroy));
-    if (flsKey == FLS_OUT_OF_INDEXES)
</del><ins>+    DWORD tlsKey = TlsAlloc();
+    if (tlsKey == TLS_OUT_OF_INDEXES)
</ins><span class="cx">         CRASH();
</span><span class="cx"> 
</span><del>-    m_index = InterlockedIncrement(&amp;flsKeyCount()) - 1;
-    if (m_index &gt;= kMaxFlsKeySize)
</del><ins>+    m_index = InterlockedIncrement(&amp;tlsKeyCount()) - 1;
+    if (m_index &gt;= kMaxTlsKeySize)
</ins><span class="cx">         CRASH();
</span><del>-    flsKeys()[m_index] = flsKey;
</del><ins>+    tlsKeys()[m_index] = tlsKey;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;typename T&gt;
</span><span class="cx"> inline ThreadSpecific&lt;T&gt;::~ThreadSpecific()
</span><span class="cx"> {
</span><del>-    FlsFree(flsKeys()[m_index]);
</del><ins>+    // Does not invoke destructor functions. They will be called from ThreadSpecificThreadExit when the thread is detached.
+    TlsFree(tlsKeys()[m_index]);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template&lt;typename T&gt;
</span><span class="cx"> inline T* ThreadSpecific&lt;T&gt;::get()
</span><span class="cx"> {
</span><del>-    Data* data = static_cast&lt;Data*&gt;(FlsGetValue(flsKeys()[m_index]));
</del><ins>+    Data* data = static_cast&lt;Data*&gt;(TlsGetValue(tlsKeys()[m_index]));
</ins><span class="cx">     return data ? data-&gt;value : 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -221,7 +207,8 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!get());
</span><span class="cx">     Data* data = new Data(ptr, this);
</span><del>-    FlsSetValue(flsKeys()[m_index], data);
</del><ins>+    data-&gt;destructor = &amp;ThreadSpecific&lt;T&gt;::destroy;
+    TlsSetValue(tlsKeys()[m_index], data);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #else
</span><span class="lines">@@ -245,7 +232,7 @@
</span><span class="cx"> #if USE(PTHREADS)
</span><span class="cx">     pthread_setspecific(data-&gt;owner-&gt;m_key, 0);
</span><span class="cx"> #elif OS(WINDOWS)
</span><del>-    FlsSetValue(flsKeys()[data-&gt;owner-&gt;m_index], 0);
</del><ins>+    TlsSetValue(tlsKeys()[data-&gt;owner-&gt;m_index], 0);
</ins><span class="cx"> #else
</span><span class="cx"> #error ThreadSpecific is not implemented for this platform.
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWTFwtfThreadSpecificWincpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ThreadSpecificWin.cpp (199678 => 199679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ThreadSpecificWin.cpp        2016-04-18 17:28:40 UTC (rev 199678)
+++ trunk/Source/WTF/wtf/ThreadSpecificWin.cpp        2016-04-18 17:30:36 UTC (rev 199679)
</span><span class="lines">@@ -24,22 +24,117 @@
</span><span class="cx"> 
</span><span class="cx"> #if OS(WINDOWS)
</span><span class="cx"> 
</span><ins>+#include &quot;StdLibExtras.h&quot;
+#include &quot;ThreadingPrimitives.h&quot;
+#include &lt;wtf/DoublyLinkedList.h&gt;
+
</ins><span class="cx"> #if !USE(PTHREADS)
</span><span class="cx"> 
</span><span class="cx"> namespace WTF {
</span><span class="cx"> 
</span><del>-long&amp; flsKeyCount()
</del><ins>+static DoublyLinkedList&lt;PlatformThreadSpecificKey&gt;&amp; destructorsList()
</ins><span class="cx"> {
</span><ins>+    static DoublyLinkedList&lt;PlatformThreadSpecificKey&gt; staticList;
+    return staticList;
+}
+
+static Mutex&amp; destructorsMutex()
+{
+    static Mutex staticMutex;
+    return staticMutex;
+}
+
+class PlatformThreadSpecificKey : public DoublyLinkedListNode&lt;PlatformThreadSpecificKey&gt; {
+public:
+    friend class DoublyLinkedListNode&lt;PlatformThreadSpecificKey&gt;;
+
+    PlatformThreadSpecificKey(void (*destructor)(void *))
+        : m_destructor(destructor)
+    {
+        m_tlsKey = TlsAlloc();
+        if (m_tlsKey == TLS_OUT_OF_INDEXES)
+            CRASH();
+    }
+
+    ~PlatformThreadSpecificKey()
+    {
+        TlsFree(m_tlsKey);
+    }
+
+    void setValue(void* data) { TlsSetValue(m_tlsKey, data); }
+    void* value() { return TlsGetValue(m_tlsKey); }
+
+    void callDestructor()
+    {
+       if (void* data = value())
+            m_destructor(data);
+    }
+
+private:
+    void (*m_destructor)(void *);
+    DWORD m_tlsKey;
+    PlatformThreadSpecificKey* m_prev;
+    PlatformThreadSpecificKey* m_next;
+};
+
+long&amp; tlsKeyCount()
+{
</ins><span class="cx">     static long count;
</span><span class="cx">     return count;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-DWORD* flsKeys()
</del><ins>+DWORD* tlsKeys()
</ins><span class="cx"> {
</span><del>-    static DWORD keys[kMaxFlsKeySize];
</del><ins>+    static DWORD keys[kMaxTlsKeySize];
</ins><span class="cx">     return keys;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
+{
+    // Use the original malloc() instead of fastMalloc() to use this function in FastMalloc code.
+    *key = static_cast&lt;PlatformThreadSpecificKey*&gt;(::malloc(sizeof(PlatformThreadSpecificKey)));
+    new (*key) PlatformThreadSpecificKey(destructor);
+
+    MutexLocker locker(destructorsMutex());
+    destructorsList().push(*key);
+}
+
+void threadSpecificKeyDelete(ThreadSpecificKey key)
+{
+    MutexLocker locker(destructorsMutex());
+    destructorsList().remove(key);
+    key-&gt;~PlatformThreadSpecificKey();
+    ::free(key);
+}
+
+void threadSpecificSet(ThreadSpecificKey key, void* data)
+{
+    key-&gt;setValue(data);
+}
+
+void* threadSpecificGet(ThreadSpecificKey key)
+{
+    return key-&gt;value();
+}
+
+void ThreadSpecificThreadExit()
+{
+    for (long i = 0; i &lt; tlsKeyCount(); i++) {
+        // The layout of ThreadSpecific&lt;T&gt;::Data does not depend on T. So we are safe to do the static cast to ThreadSpecific&lt;int&gt; in order to access its data member.
+        ThreadSpecific&lt;int&gt;::Data* data = static_cast&lt;ThreadSpecific&lt;int&gt;::Data*&gt;(TlsGetValue(tlsKeys()[i]));
+        if (data)
+            data-&gt;destructor(data);
+    }
+
+    MutexLocker locker(destructorsMutex());
+    PlatformThreadSpecificKey* key = destructorsList().head();
+    while (key) {
+        PlatformThreadSpecificKey* nextKey = key-&gt;next();
+        key-&gt;callDestructor();
+        key = nextKey;
+    }
+}
+
</ins><span class="cx"> } // namespace WTF
</span><span class="cx"> 
</span><span class="cx"> #endif // !USE(PTHREADS)
</span></span></pre></div>
<a id="trunkSourceWTFwtfThreadingWincpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ThreadingWin.cpp (199678 => 199679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ThreadingWin.cpp        2016-04-18 17:28:40 UTC (rev 199678)
+++ trunk/Source/WTF/wtf/ThreadingWin.cpp        2016-04-18 17:30:36 UTC (rev 199679)
</span><span class="lines">@@ -102,6 +102,10 @@
</span><span class="cx"> #include &lt;wtf/RandomNumberSeed.h&gt;
</span><span class="cx"> #include &lt;wtf/WTFThreadData.h&gt;
</span><span class="cx"> 
</span><ins>+#if !USE(PTHREADS) &amp;&amp; OS(WINDOWS)
+#include &quot;ThreadSpecific.h&quot;
+#endif
+
</ins><span class="cx"> #if HAVE(ERRNO_H)
</span><span class="cx"> #include &lt;errno.h&gt;
</span><span class="cx"> #endif
</span><span class="lines">@@ -195,6 +199,11 @@
</span><span class="cx">     std::unique_ptr&lt;ThreadFunctionInvocation&gt; invocation(static_cast&lt;ThreadFunctionInvocation*&gt;(param));
</span><span class="cx">     invocation-&gt;function(invocation-&gt;data);
</span><span class="cx"> 
</span><ins>+#if !USE(PTHREADS) &amp;&amp; OS(WINDOWS)
+    // Do the TLS cleanup.
+    ThreadSpecificThreadExit();
+#endif
+
</ins><span class="cx">     return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>