Title: [188788] trunk/Source/WTF
Revision
188788
Author
[email protected]
Date
2015-08-21 15:06:25 -0700 (Fri, 21 Aug 2015)

Log Message

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<void ()> 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.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (188787 => 188788)


--- 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)
@@ -1,5 +1,51 @@
 2015-08-21  Anders Carlsson  <[email protected]>
 
+        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<void ()> 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  <[email protected]>
+
         Get rid of uses of the old callOnMainThread function that takes a function ptr + context
         https://bugs.webkit.org/show_bug.cgi?id=148324
 

Modified: trunk/Source/WTF/wtf/MainThread.cpp (188787 => 188788)


--- 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)
@@ -40,31 +40,6 @@
 
 namespace WTF {
 
-struct FunctionWithContext {
-    MainThreadFunction* function;
-    void* context;
-
-    FunctionWithContext(MainThreadFunction* function = nullptr, void* context = nullptr)
-        : function(function)
-        , context(context)
-    {
-    }
-    bool operator == (const FunctionWithContext& o)
-    {
-        return function == o.function && context == o.context;
-    }
-};
-
-class FunctionWithContextFinder {
-public:
-    FunctionWithContextFinder(const FunctionWithContext& m) : m(m) {}
-    bool operator() (FunctionWithContext& o) { return o == m; }
-    FunctionWithContext m;
-};
-
-
-typedef Deque<FunctionWithContext> FunctionQueue;
-
 static bool callbacksPaused; // This global variable is only accessed from main thread.
 #if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
 static ThreadIdentifier mainThreadIdentifier;
@@ -72,13 +47,12 @@
 
 static StaticLock mainThreadFunctionQueueMutex;
 
-static FunctionQueue& functionQueue()
+static Deque<std::function<void ()>>& functionQueue()
 {
-    static NeverDestroyed<FunctionQueue> functionQueue;
+    static NeverDestroyed<Deque<std::function<void ()>>> functionQueue;
     return functionQueue;
 }
 
-
 #if !OS(DARWIN) || PLATFORM(EFL) || PLATFORM(GTK)
 
 void initializeMainThread()
@@ -146,16 +120,18 @@
 
     auto startTime = std::chrono::steady_clock::now();
 
-    FunctionWithContext invocation;
+    std::function<void ()> function;
+
     while (true) {
         {
             std::lock_guard<StaticLock> lock(mainThreadFunctionQueueMutex);
             if (!functionQueue().size())
                 break;
-            invocation = functionQueue().takeFirst();
+
+            function = functionQueue().takeFirst();
         }
 
-        invocation.function(invocation.context);
+        function();
 
         // If we are running accumulated functions for too long so UI may become unresponsive, we need to
         // yield so the user input can be processed. Otherwise user may not be able to even close the window.
@@ -168,48 +144,22 @@
     }
 }
 
-void callOnMainThread(MainThreadFunction* function, void* context)
+void callOnMainThread(std::function<void ()> function)
 {
     ASSERT(function);
+
     bool needToSchedule = false;
+
     {
         std::lock_guard<StaticLock> lock(mainThreadFunctionQueueMutex);
         needToSchedule = functionQueue().size() == 0;
-        functionQueue().append(FunctionWithContext(function, context));
+        functionQueue().append(WTF::move(function));
     }
+
     if (needToSchedule)
         scheduleDispatchFunctionsOnMainThread();
 }
 
-void cancelCallOnMainThread(MainThreadFunction* function, void* context)
-{
-    ASSERT(function);
-
-    std::lock_guard<StaticLock> 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<std::function<void ()>>(static_cast<std::function<void ()>*>(context));
-    (*function)();
-}
-
-void callOnMainThread(std::function<void ()> function)
-{
-    callOnMainThread(callFunctionObject, std::make_unique<std::function<void ()>>(WTF::move(function)).release());
-}
-
 void setMainThreadCallbacksPaused(bool paused)
 {
     ASSERT(isMainThread());

Modified: trunk/Source/WTF/wtf/MainThread.h (188787 => 188788)


--- 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)
@@ -41,9 +41,6 @@
 // Must be called from the main thread.
 WTF_EXPORT_PRIVATE void initializeMainThread();
 
-WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
-WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
-
 WTF_EXPORT_PRIVATE void callOnMainThread(std::function<void ()>);
 
 #if PLATFORM(COCOA)
@@ -94,10 +91,18 @@
 void initializeMainThreadToProcessMainThreadPlatform();
 #endif
 
+#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1090
+WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
+WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
+#endif
+
 } // namespace WTF
 
+#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1090
+using WTF::cancelCallOnMainThread;
+#endif
+
 using WTF::callOnMainThread;
-using WTF::cancelCallOnMainThread;
 #if PLATFORM(COCOA)
 using WTF::callOnWebThreadOrDispatchAsyncOnMainThread;
 #endif

Modified: trunk/Source/WTF/wtf/mac/DeprecatedSymbolsUsedBySafari.mm (188787 => 188788)


--- 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)
@@ -25,12 +25,16 @@
 
 #include "config.h"
 
+#if PLATFORM(MAC)
+
 #include "Functional.h"
+#include "HashMap.h"
+#include "HashSet.h"
+#include "Lock.h"
 #include "MainThread.h"
 #include "NeverDestroyed.h"
 #include "StdLibExtras.h"
-#include <mutex>
-#include <wtf/Lock.h>
+#include <stdint.h>
 
 // This file contains deprecated symbols that the last released version of Safari uses.
 // Once Safari stops using them, we should remove them.
@@ -58,4 +62,97 @@
     atomicallyInitializedStaticMutex.unlock();
 }
 
+#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 101100
+WTF_EXPORT_PRIVATE void callOnMainThread(MainThreadFunction*, void* context);
+WTF_EXPORT_PRIVATE void cancelCallOnMainThread(MainThreadFunction*, void* context);
+
+class MainThreadFunctionTracker {
+public:
+    static MainThreadFunctionTracker& singleton()
+    {
+        std::once_flag onceFlag;
+
+        static LazyNeverDestroyed<MainThreadFunctionTracker> tracker;
+
+        std::call_once(onceFlag, [&] {
+            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& set = m_functions.add({ function, context }, HashSet<uint64_t> { }).iterator->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& set = it->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<std::pair<void (*)(void*), void*>, HashSet<uint64_t>> 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
+
 } // namespace WTF
+
+#endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to