<!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>[188788] 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/188788">188788</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2015-08-21 15:06:25 -0700 (Fri, 21 Aug 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Deprecate the old school callOnMainThread/cancelCallOnMainThread functions
https://bugs.webkit.org/show_bug.cgi?id=148327

Reviewed by Sam Weinig.

* wtf/MainThread.cpp:
(WTF::functionQueue):
(WTF::dispatchFunctionsFromMainThread):
(WTF::callOnMainThread):
Change the function queue to be a queue of std::function&lt;void ()&gt; and get rid of dead code.

* wtf/MainThread.h:
Conditionalize callOnMainThread and cancelCallOnMainThread because Safari on Mavericks still expects to them to be
declared in MainThread.h under 10.9.

* wtf/mac/DeprecatedSymbolsUsedBySafari.mm:
Add a MainThreadFunctionTracker singleton that keeps track of scheduled functions so we can ensure that they're not
called if they're cancelled.

(WTF::MainThreadFunctionTracker::singleton):
Return the singleton.

(WTF::MainThreadFunctionTracker::callOnMainThread):
Add the function/context pair to our map and schedule the function to be run on the main thread.
If the function has been removed, don't call it.

(WTF::MainThreadFunctionTracker::cancelCallOnMainThread):
Remove all entries with the given function/context pair.

(WTF::MainThreadFunctionTracker::addFunction):
Add the function/context pair to the map and return the unique identifier.

(WTF::MainThreadFunctionTracker::removeIdentifier):
Look up the identifier and remove it if it still exists. If it exists we know that it hasn't been canceled and that we can call it.

(WTF::MainThreadFunctionTracker::removeFunctions):
Remove all function identifiers matched by the function/context pair.

(WTF::callOnMainThread):
Implement using MainThreadFunctionTracker.

(WTF::cancelCallOnMainThread):
Implement using MainThreadFunctionTracker.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfMainThreadcpp">trunk/Source/WTF/wtf/MainThread.cpp</a></li>
<li><a href="#trunkSourceWTFwtfMainThreadh">trunk/Source/WTF/wtf/MainThread.h</a></li>
<li><a href="#trunkSourceWTFwtfmacDeprecatedSymbolsUsedBySafarimm">trunk/Source/WTF/wtf/mac/DeprecatedSymbolsUsedBySafari.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (188787 => 188788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-08-21 22:01:02 UTC (rev 188787)
+++ trunk/Source/WTF/ChangeLog        2015-08-21 22:06:25 UTC (rev 188788)
</span><span class="lines">@@ -1,5 +1,51 @@
</span><span class="cx"> 2015-08-21  Anders Carlsson  &lt;andersca@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Deprecate the old school callOnMainThread/cancelCallOnMainThread functions
+        https://bugs.webkit.org/show_bug.cgi?id=148327
+
+        Reviewed by Sam Weinig.
+
+        * wtf/MainThread.cpp:
+        (WTF::functionQueue):
+        (WTF::dispatchFunctionsFromMainThread):
+        (WTF::callOnMainThread):
+        Change the function queue to be a queue of std::function&lt;void ()&gt; and get rid of dead code.
+
+        * wtf/MainThread.h:
+        Conditionalize callOnMainThread and cancelCallOnMainThread because Safari on Mavericks still expects to them to be
+        declared in MainThread.h under 10.9.
+
+        * wtf/mac/DeprecatedSymbolsUsedBySafari.mm:
+        Add a MainThreadFunctionTracker singleton that keeps track of scheduled functions so we can ensure that they're not
+        called if they're cancelled.
+
+        (WTF::MainThreadFunctionTracker::singleton):
+        Return the singleton.
+
+        (WTF::MainThreadFunctionTracker::callOnMainThread):
+        Add the function/context pair to our map and schedule the function to be run on the main thread.
+        If the function has been removed, don't call it.
+
+        (WTF::MainThreadFunctionTracker::cancelCallOnMainThread):
+        Remove all entries with the given function/context pair.
+
+        (WTF::MainThreadFunctionTracker::addFunction):
+        Add the function/context pair to the map and return the unique identifier.
+
+        (WTF::MainThreadFunctionTracker::removeIdentifier):
+        Look up the identifier and remove it if it still exists. If it exists we know that it hasn't been canceled and that we can call it.
+
+        (WTF::MainThreadFunctionTracker::removeFunctions):
+        Remove all function identifiers matched by the function/context pair.
+
+        (WTF::callOnMainThread):
+        Implement using MainThreadFunctionTracker.
+
+        (WTF::cancelCallOnMainThread):
+        Implement using MainThreadFunctionTracker.
+
+2015-08-21  Anders Carlsson  &lt;andersca@apple.com&gt;
+
</ins><span class="cx">         Get rid of uses of the old callOnMainThread function that takes a function ptr + context
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=148324
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWTFwtfMainThreadcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/MainThread.cpp (188787 => 188788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MainThread.cpp        2015-08-21 22:01:02 UTC (rev 188787)
+++ trunk/Source/WTF/wtf/MainThread.cpp        2015-08-21 22:06:25 UTC (rev 188788)
</span><span class="lines">@@ -40,31 +40,6 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WTF {
</span><span class="cx"> 
</span><del>-struct FunctionWithContext {
-    MainThreadFunction* function;
-    void* context;
-
-    FunctionWithContext(MainThreadFunction* function = nullptr, void* context = nullptr)
-        : function(function)
-        , context(context)
-    {
-    }
-    bool operator == (const FunctionWithContext&amp; o)
-    {
-        return function == o.function &amp;&amp; context == o.context;
-    }
-};
-
-class FunctionWithContextFinder {
-public:
-    FunctionWithContextFinder(const FunctionWithContext&amp; m) : m(m) {}
-    bool operator() (FunctionWithContext&amp; o) { return o == m; }
-    FunctionWithContext m;
-};
-
-
-typedef Deque&lt;FunctionWithContext&gt; FunctionQueue;
-
</del><span class="cx"> static bool callbacksPaused; // This global variable is only accessed from main thread.
</span><span class="cx"> #if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
</span><span class="cx"> static ThreadIdentifier mainThreadIdentifier;
</span><span class="lines">@@ -72,13 +47,12 @@
</span><span class="cx"> 
</span><span class="cx"> static StaticLock mainThreadFunctionQueueMutex;
</span><span class="cx"> 
</span><del>-static FunctionQueue&amp; functionQueue()
</del><ins>+static Deque&lt;std::function&lt;void ()&gt;&gt;&amp; functionQueue()
</ins><span class="cx"> {
</span><del>-    static NeverDestroyed&lt;FunctionQueue&gt; functionQueue;
</del><ins>+    static NeverDestroyed&lt;Deque&lt;std::function&lt;void ()&gt;&gt;&gt; functionQueue;
</ins><span class="cx">     return functionQueue;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> #if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
</span><span class="cx"> 
</span><span class="cx"> void initializeMainThread()
</span><span class="lines">@@ -146,16 +120,18 @@
</span><span class="cx"> 
</span><span class="cx">     auto startTime = std::chrono::steady_clock::now();
</span><span class="cx"> 
</span><del>-    FunctionWithContext invocation;
</del><ins>+    std::function&lt;void ()&gt; function;
+
</ins><span class="cx">     while (true) {
</span><span class="cx">         {
</span><span class="cx">             std::lock_guard&lt;StaticLock&gt; lock(mainThreadFunctionQueueMutex);
</span><span class="cx">             if (!functionQueue().size())
</span><span class="cx">                 break;
</span><del>-            invocation = functionQueue().takeFirst();
</del><ins>+
+            function = functionQueue().takeFirst();
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        invocation.function(invocation.context);
</del><ins>+        function();
</ins><span class="cx"> 
</span><span class="cx">         // If we are running accumulated functions for too long so UI may become unresponsive, we need to
</span><span class="cx">         // yield so the user input can be processed. Otherwise user may not be able to even close the window.
</span><span class="lines">@@ -168,48 +144,22 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void callOnMainThread(MainThreadFunction* function, void* context)
</del><ins>+void callOnMainThread(std::function&lt;void ()&gt; function)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(function);
</span><ins>+
</ins><span class="cx">     bool needToSchedule = false;
</span><ins>+
</ins><span class="cx">     {
</span><span class="cx">         std::lock_guard&lt;StaticLock&gt; lock(mainThreadFunctionQueueMutex);
</span><span class="cx">         needToSchedule = functionQueue().size() == 0;
</span><del>-        functionQueue().append(FunctionWithContext(function, context));
</del><ins>+        functionQueue().append(WTF::move(function));
</ins><span class="cx">     }
</span><ins>+
</ins><span class="cx">     if (needToSchedule)
</span><span class="cx">         scheduleDispatchFunctionsOnMainThread();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void cancelCallOnMainThread(MainThreadFunction* function, void* context)
-{
-    ASSERT(function);
-
-    std::lock_guard&lt;StaticLock&gt; lock(mainThreadFunctionQueueMutex);
-
-    FunctionWithContextFinder pred(FunctionWithContext(function, context));
-
-    while (true) {
-        // We must redefine 'i' each pass, because the itererator's operator= 
-        // requires 'this' to be valid, and remove() invalidates all iterators
-        FunctionQueue::iterator i(functionQueue().findIf(pred));
-        if (i == functionQueue().end())
-            break;
-        functionQueue().remove(i);
-    }
-}
-
-static void callFunctionObject(void* context)
-{
-    auto function = std::unique_ptr&lt;std::function&lt;void ()&gt;&gt;(static_cast&lt;std::function&lt;void ()&gt;*&gt;(context));
-    (*function)();
-}
-
-void callOnMainThread(std::function&lt;void ()&gt; function)
-{
-    callOnMainThread(callFunctionObject, std::make_unique&lt;std::function&lt;void ()&gt;&gt;(WTF::move(function)).release());
-}
-
</del><span class="cx"> void setMainThreadCallbacksPaused(bool paused)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span></span></pre></div>
<a id="trunkSourceWTFwtfMainThreadh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/MainThread.h (188787 => 188788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MainThread.h        2015-08-21 22:01:02 UTC (rev 188787)
+++ trunk/Source/WTF/wtf/MainThread.h        2015-08-21 22:06:25 UTC (rev 188788)
</span><span class="lines">@@ -41,9 +41,6 @@
</span><span class="cx"> // Must be called from the main thread.
</span><span class="cx"> WTF_EXPORT_PRIVATE void initializeMainThread();
</span><span class="cx"> 
</span><del>-WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
-WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
-
</del><span class="cx"> WTF_EXPORT_PRIVATE void callOnMainThread(std::function&lt;void ()&gt;);
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(COCOA)
</span><span class="lines">@@ -94,10 +91,18 @@
</span><span class="cx"> void initializeMainThreadToProcessMainThreadPlatform();
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+#if PLATFORM(MAC) &amp;&amp; __MAC_OS_X_VERSION_MIN_REQUIRED == 1090
+WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
+WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
+#endif
+
</ins><span class="cx"> } // namespace WTF
</span><span class="cx"> 
</span><ins>+#if PLATFORM(MAC) &amp;&amp; __MAC_OS_X_VERSION_MIN_REQUIRED == 1090
+using WTF::cancelCallOnMainThread;
+#endif
+
</ins><span class="cx"> using WTF::callOnMainThread;
</span><del>-using WTF::cancelCallOnMainThread;
</del><span class="cx"> #if PLATFORM(COCOA)
</span><span class="cx"> using WTF::callOnWebThreadOrDispatchAsyncOnMainThread;
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWTFwtfmacDeprecatedSymbolsUsedBySafarimm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/mac/DeprecatedSymbolsUsedBySafari.mm (188787 => 188788)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/mac/DeprecatedSymbolsUsedBySafari.mm        2015-08-21 22:01:02 UTC (rev 188787)
+++ trunk/Source/WTF/wtf/mac/DeprecatedSymbolsUsedBySafari.mm        2015-08-21 22:06:25 UTC (rev 188788)
</span><span class="lines">@@ -25,12 +25,16 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> 
</span><ins>+#if PLATFORM(MAC)
+
</ins><span class="cx"> #include &quot;Functional.h&quot;
</span><ins>+#include &quot;HashMap.h&quot;
+#include &quot;HashSet.h&quot;
+#include &quot;Lock.h&quot;
</ins><span class="cx"> #include &quot;MainThread.h&quot;
</span><span class="cx"> #include &quot;NeverDestroyed.h&quot;
</span><span class="cx"> #include &quot;StdLibExtras.h&quot;
</span><del>-#include &lt;mutex&gt;
-#include &lt;wtf/Lock.h&gt;
</del><ins>+#include &lt;stdint.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> // This file contains deprecated symbols that the last released version of Safari uses.
</span><span class="cx"> // Once Safari stops using them, we should remove them.
</span><span class="lines">@@ -58,4 +62,97 @@
</span><span class="cx">     atomicallyInitializedStaticMutex.unlock();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if __MAC_OS_X_VERSION_MIN_REQUIRED &lt;= 101100
+WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
+WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
+
+class MainThreadFunctionTracker {
+public:
+    static MainThreadFunctionTracker&amp; singleton()
+    {
+        std::once_flag onceFlag;
+
+        static LazyNeverDestroyed&lt;MainThreadFunctionTracker&gt; tracker;
+
+        std::call_once(onceFlag, [&amp;] {
+            tracker.construct();
+        });
+
+        return tracker;
+    }
+
+    void callOnMainThread(void (*function)(void*), void* context)
+    {
+        uint64_t identifier = addFunction(function, context);
+
+        WTF::callOnMainThread([this, function, context, identifier] {
+            if (!removeIdentifier(function, context, identifier))
+                return;
+
+            function(context);
+        });
+    }
+
+    void cancelCallOnMainThread(void (*function)(void*), void* context)
+    {
+        removeFunctions(function, context);
+    }
+
+private:
+    uint64_t addFunction(void (*function)(void*), void* context)
+    {
+        LockHolder lockHolder(m_lock);
+        uint64_t identifier = ++m_currentIdentifier;
+
+        auto&amp; set = m_functions.add({ function, context }, HashSet&lt;uint64_t&gt; { }).iterator-&gt;value;
+        set.add(identifier);
+
+        return identifier;
+    }
+
+    bool removeIdentifier(void (*function)(void*), void* context, uint64_t identifier)
+    {
+        LockHolder lockHolder(m_lock);
+
+        auto it = m_functions.find({ function, context });
+        if (it == m_functions.end())
+            return false;
+
+        auto&amp; set = it-&gt;value;
+        if (!set.remove(identifier))
+            return false;
+
+        if (set.isEmpty())
+            m_functions.remove(it);
+
+        return true;
+    }
+
+    void removeFunctions(void (*function)(void*), void* context)
+    {
+        LockHolder lockHolder(m_lock);
+
+        m_functions.remove({ function, context });
+    }
+
+    Lock m_lock;
+    uint64_t m_currentIdentifier;
+    HashMap&lt;std::pair&lt;void (*)(void*), void*&gt;, HashSet&lt;uint64_t&gt;&gt; m_functions;
+};
+
+void callOnMainThread(MainThreadFunction* function, void* context)
+{
+    MainThreadFunctionTracker::singleton().callOnMainThread(function, context);
+}
+
+void cancelCallOnMainThread(MainThreadFunction* function, void* context)
+{
+    ASSERT(function);
+
+    MainThreadFunctionTracker::singleton().cancelCallOnMainThread(function, context);
+}
+#endif
+
</ins><span class="cx"> } // namespace WTF
</span><ins>+
+#endif
</ins></span></pre>
</div>
</div>

</body>
</html>