Templatize NoncopyableFunction class similarly to std::function
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 29 May 2016 04:20:06 +0000 (04:20 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 29 May 2016 04:20:06 +0000 (04:20 +0000)
https://bugs.webkit.org/show_bug.cgi?id=158185

Reviewed by Darin Adler.

Templatize NoncopyableFunction class similarly to std::function, so
that it can be used as a std::function replacement in more places.

Previously, NoncopyableFunction could only support "void()" lambdas.

Source/WebCore:

* Modules/mediastream/MediaEndpointPeerConnection.cpp:
(WebCore::MediaEndpointPeerConnection::runTask):
* Modules/mediastream/MediaEndpointPeerConnection.h:
* fileapi/AsyncFileStream.cpp:
(WebCore::callOnFileThread):
(WebCore::AsyncFileStream::perform):
(WebCore::AsyncFileStream::getSize):
(WebCore::AsyncFileStream::openForRead):
(WebCore::AsyncFileStream::openForWrite):
(WebCore::AsyncFileStream::write):
* fileapi/AsyncFileStream.h:
* page/scrolling/ScrollingThread.cpp:
(WebCore::ScrollingThread::dispatch):
(WebCore::ScrollingThread::dispatchBarrier):
(WebCore::ScrollingThread::dispatchFunctionsFromScrollingThread):
* page/scrolling/ScrollingThread.h:
* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.h:
* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.mm:
(WebCore::MediaPlayerPrivateMediaStreamAVFObjC::scheduleDeferredTask):
* platform/mediastream/MediaStreamPrivate.cpp:
(WebCore::MediaStreamPrivate::scheduleDeferredTask):
* platform/mediastream/MediaStreamPrivate.h:
* platform/mediastream/mac/AVMediaCaptureSource.h:
* platform/mediastream/mac/AVMediaCaptureSource.mm:
(WebCore::AVMediaCaptureSource::scheduleDeferredTask):

Source/WebKit:

* Storage/StorageSyncManager.cpp:
(WebCore::StorageSyncManager::dispatch):
* Storage/StorageSyncManager.h:
* Storage/StorageThread.cpp:
(WebCore::StorageThread::dispatch):
(WebCore::StorageThread::terminate):
* Storage/StorageThread.h:

Source/WebKit2:

* NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
(WebKit::NetworkCache::runTaskInQueue):

Source/WTF:

* wtf/FunctionDispatcher.h:
* wtf/MainThread.cpp:
(WTF::functionQueue):
(WTF::dispatchFunctionsFromMainThread):
(WTF::callOnMainThread):
* wtf/MainThread.h:
* wtf/NoncopyableFunction.h:
* wtf/RunLoop.cpp:
(WTF::RunLoop::performWork):
(WTF::RunLoop::dispatch):
* wtf/RunLoop.h:
* wtf/WorkQueue.h:
* wtf/cocoa/WorkQueueCocoa.cpp:
(WTF::WorkQueue::dispatch):
(WTF::WorkQueue::dispatchAfter):
* wtf/efl/DispatchQueueWorkItemEfl.h:
(WorkItem::WorkItem):
(TimerWorkItem::create):
(TimerWorkItem::TimerWorkItem):
* wtf/efl/WorkQueueEfl.cpp:
(WTF::WorkQueue::dispatch):
(WTF::WorkQueue::dispatchAfter):
* wtf/generic/RunLoopGeneric.cpp:
(WTF::RunLoop::TimerBase::ScheduledTask::create):
(WTF::RunLoop::TimerBase::ScheduledTask::ScheduledTask):
(WTF::RunLoop::dispatchAfter):
* wtf/generic/WorkQueueGeneric.cpp:
(WorkQueue::dispatch):
(WorkQueue::dispatchAfter):
* wtf/glib/RunLoopGLib.cpp:
(WTF::DispatchAfterContext::DispatchAfterContext):
(WTF::RunLoop::dispatchAfter):
* wtf/win/WorkItemWin.cpp:
(WTF::WorkItemWin::WorkItemWin):
(WTF::WorkItemWin::create):
(WTF::HandleWorkItem::HandleWorkItem):
(WTF::HandleWorkItem::createByAdoptingHandle):
* wtf/win/WorkItemWin.h:
(WTF::WorkItemWin::function):
* wtf/win/WorkQueueWin.cpp:
(WTF::WorkQueue::dispatch):
(WTF::WorkQueue::dispatchAfter):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@201493 268f45cc-cd09-0410-ab3c-d52691b4dbfc

37 files changed:
Source/WTF/ChangeLog
Source/WTF/wtf/FunctionDispatcher.h
Source/WTF/wtf/MainThread.cpp
Source/WTF/wtf/MainThread.h
Source/WTF/wtf/NoncopyableFunction.h
Source/WTF/wtf/RunLoop.cpp
Source/WTF/wtf/RunLoop.h
Source/WTF/wtf/WorkQueue.h
Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp
Source/WTF/wtf/efl/DispatchQueueWorkItemEfl.h
Source/WTF/wtf/efl/WorkQueueEfl.cpp
Source/WTF/wtf/generic/RunLoopGeneric.cpp
Source/WTF/wtf/generic/WorkQueueGeneric.cpp
Source/WTF/wtf/glib/RunLoopGLib.cpp
Source/WTF/wtf/win/WorkItemWin.cpp
Source/WTF/wtf/win/WorkItemWin.h
Source/WTF/wtf/win/WorkQueueWin.cpp
Source/WebCore/ChangeLog
Source/WebCore/Modules/mediastream/MediaEndpointPeerConnection.cpp
Source/WebCore/Modules/mediastream/MediaEndpointPeerConnection.h
Source/WebCore/fileapi/AsyncFileStream.cpp
Source/WebCore/fileapi/AsyncFileStream.h
Source/WebCore/page/scrolling/ScrollingThread.cpp
Source/WebCore/page/scrolling/ScrollingThread.h
Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.h
Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.mm
Source/WebCore/platform/mediastream/MediaStreamPrivate.cpp
Source/WebCore/platform/mediastream/MediaStreamPrivate.h
Source/WebCore/platform/mediastream/mac/AVMediaCaptureSource.h
Source/WebCore/platform/mediastream/mac/AVMediaCaptureSource.mm
Source/WebKit/ChangeLog
Source/WebKit/Storage/StorageSyncManager.cpp
Source/WebKit/Storage/StorageSyncManager.h
Source/WebKit/Storage/StorageThread.cpp
Source/WebKit/Storage/StorageThread.h
Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp

index 697cb5d..a88bed0 100644 (file)
@@ -1,3 +1,58 @@
+2016-05-28  Chris Dumez  <cdumez@apple.com>
+
+        Templatize NoncopyableFunction class similarly to std::function
+        https://bugs.webkit.org/show_bug.cgi?id=158185
+
+        Reviewed by Darin Adler.
+
+        Templatize NoncopyableFunction class similarly to std::function, so
+        that it can be used as a std::function replacement in more places.
+
+        Previously, NoncopyableFunction could only support "void()" lambdas.
+
+        * wtf/FunctionDispatcher.h:
+        * wtf/MainThread.cpp:
+        (WTF::functionQueue):
+        (WTF::dispatchFunctionsFromMainThread):
+        (WTF::callOnMainThread):
+        * wtf/MainThread.h:
+        * wtf/NoncopyableFunction.h:
+        * wtf/RunLoop.cpp:
+        (WTF::RunLoop::performWork):
+        (WTF::RunLoop::dispatch):
+        * wtf/RunLoop.h:
+        * wtf/WorkQueue.h:
+        * wtf/cocoa/WorkQueueCocoa.cpp:
+        (WTF::WorkQueue::dispatch):
+        (WTF::WorkQueue::dispatchAfter):
+        * wtf/efl/DispatchQueueWorkItemEfl.h:
+        (WorkItem::WorkItem):
+        (TimerWorkItem::create):
+        (TimerWorkItem::TimerWorkItem):
+        * wtf/efl/WorkQueueEfl.cpp:
+        (WTF::WorkQueue::dispatch):
+        (WTF::WorkQueue::dispatchAfter):
+        * wtf/generic/RunLoopGeneric.cpp:
+        (WTF::RunLoop::TimerBase::ScheduledTask::create):
+        (WTF::RunLoop::TimerBase::ScheduledTask::ScheduledTask):
+        (WTF::RunLoop::dispatchAfter):
+        * wtf/generic/WorkQueueGeneric.cpp:
+        (WorkQueue::dispatch):
+        (WorkQueue::dispatchAfter):
+        * wtf/glib/RunLoopGLib.cpp:
+        (WTF::DispatchAfterContext::DispatchAfterContext):
+        (WTF::RunLoop::dispatchAfter):
+        * wtf/win/WorkItemWin.cpp:
+        (WTF::WorkItemWin::WorkItemWin):
+        (WTF::WorkItemWin::create):
+        (WTF::HandleWorkItem::HandleWorkItem):
+        (WTF::HandleWorkItem::createByAdoptingHandle):
+        * wtf/win/WorkItemWin.h:
+        (WTF::WorkItemWin::function):
+        * wtf/win/WorkQueueWin.cpp:
+        (WTF::WorkQueue::dispatch):
+        (WTF::WorkQueue::dispatchAfter):
+
 2016-05-28  Dan Bernstein  <mitz@apple.com>
 
         Build fix for projects that include MainThread.h without including FastMalloc.h.
index c3a60f0..b52eecf 100644 (file)
@@ -39,7 +39,7 @@ class FunctionDispatcher : public ThreadSafeRefCounted<FunctionDispatcher> {
 public:
     WTF_EXPORT_PRIVATE virtual ~FunctionDispatcher();
 
-    virtual void dispatch(NoncopyableFunction&&) = 0;
+    virtual void dispatch(NoncopyableFunction<void ()>&&) = 0;
 
 protected:
     WTF_EXPORT_PRIVATE FunctionDispatcher();
index d3f025e..bd11e10 100644 (file)
@@ -47,9 +47,9 @@ static ThreadIdentifier mainThreadIdentifier;
 
 static StaticLock mainThreadFunctionQueueMutex;
 
-static Deque<NoncopyableFunction>& functionQueue()
+static Deque<NoncopyableFunction<void ()>>& functionQueue()
 {
-    static NeverDestroyed<Deque<NoncopyableFunction>> functionQueue;
+    static NeverDestroyed<Deque<NoncopyableFunction<void ()>>> functionQueue;
     return functionQueue;
 }
 
@@ -120,7 +120,7 @@ void dispatchFunctionsFromMainThread()
 
     auto startTime = std::chrono::steady_clock::now();
 
-    NoncopyableFunction function;
+    NoncopyableFunction<void ()> function;
 
     while (true) {
         {
@@ -144,7 +144,7 @@ void dispatchFunctionsFromMainThread()
     }
 }
 
-void callOnMainThread(NoncopyableFunction&& function)
+void callOnMainThread(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(function);
 
index 9f5856d..f19f559 100644 (file)
@@ -41,7 +41,7 @@ typedef uint32_t ThreadIdentifier;
 // Must be called from the main thread.
 WTF_EXPORT_PRIVATE void initializeMainThread();
 
-WTF_EXPORT_PRIVATE void callOnMainThread(NoncopyableFunction&&);
+WTF_EXPORT_PRIVATE void callOnMainThread(NoncopyableFunction<void ()>&&);
 
 #if PLATFORM(COCOA)
 WTF_EXPORT_PRIVATE void callOnWebThreadOrDispatchAsyncOnMainThread(void (^block)());
index 1689de2..ab9f4dc 100644 (file)
 
 namespace WTF {
 
-// FIXME: We could make this templated to support other lambdas than void() and make this more reusable.
-class NoncopyableFunction {
+template<typename> class NoncopyableFunction;
+
+template <typename Out, typename... In>
+class NoncopyableFunction<Out(In...)> {
 public:
     NoncopyableFunction() = default;
 
@@ -41,10 +43,11 @@ public:
     {
     }
 
-    void operator()() const
+    Out operator()(In... in) const
     {
         if (m_callableWrapper)
-            m_callableWrapper->call();
+            return m_callableWrapper->call(std::forward<In>(in)...);
+        return Out();
     }
 
     explicit operator bool() const { return !!m_callableWrapper; }
@@ -68,11 +71,11 @@ private:
     public:
         virtual ~CallableWrapperBase() { }
 
-        virtual void call() = 0;
+        virtual Out call(In...) = 0;
     };
 
     template<typename CallableType>
-    class CallableWrapper final : public CallableWrapperBase {
+    class CallableWrapper : public CallableWrapperBase {
     public:
         explicit CallableWrapper(CallableType&& callable)
             : m_callable(WTFMove(callable))
@@ -82,7 +85,7 @@ private:
         CallableWrapper(const CallableWrapper&) = delete;
         CallableWrapper& operator=(const CallableWrapper&) = delete;
 
-        void call() final { m_callable(); }
+        Out call(In... in) final { return m_callable(std::forward<In>(in)...); }
 
     private:
         CallableType m_callable;
index 4ed06eb..9660f15 100644 (file)
@@ -91,7 +91,7 @@ void RunLoop::performWork()
 
     size_t functionsToHandle = 0;
     {
-        NoncopyableFunction function;
+        NoncopyableFunction<void ()> function;
         {
             MutexLocker locker(m_functionQueueLock);
             functionsToHandle = m_functionQueue.size();
@@ -106,7 +106,7 @@ void RunLoop::performWork()
     }
 
     for (size_t functionsHandled = 1; functionsHandled < functionsToHandle; ++functionsHandled) {
-        NoncopyableFunction function;
+        NoncopyableFunction<void ()> function;
         {
             MutexLocker locker(m_functionQueueLock);
 
@@ -123,7 +123,7 @@ void RunLoop::performWork()
     }
 }
 
-void RunLoop::dispatch(NoncopyableFunction&& function)
+void RunLoop::dispatch(NoncopyableFunction<void ()>&& function)
 {
     {
         MutexLocker locker(m_functionQueueLock);
index 3e0a1b4..e1558b6 100644 (file)
@@ -59,7 +59,7 @@ public:
     WTF_EXPORT_PRIVATE static bool isMain();
     ~RunLoop();
 
-    void dispatch(NoncopyableFunction&&) override;
+    void dispatch(NoncopyableFunction<void ()>&&) override;
 
     WTF_EXPORT_PRIVATE static void run();
     WTF_EXPORT_PRIVATE void stop();
@@ -79,7 +79,7 @@ public:
 #endif
 
 #if USE(GLIB_EVENT_LOOP) || USE(GENERIC_EVENT_LOOP)
-    WTF_EXPORT_PRIVATE void dispatchAfter(std::chrono::nanoseconds, NoncopyableFunction&&);
+    WTF_EXPORT_PRIVATE void dispatchAfter(std::chrono::nanoseconds, NoncopyableFunction<void ()>&&);
 #endif
 
     class TimerBase {
@@ -155,7 +155,7 @@ private:
     void performWork();
 
     Mutex m_functionQueueLock;
-    Deque<NoncopyableFunction> m_functionQueue;
+    Deque<NoncopyableFunction<void ()>> m_functionQueue;
 
 #if USE(WINDOWS_EVENT_LOOP)
     static bool registerRunLoopMessageWindowClass();
index 330e5f7..e553259 100644 (file)
@@ -72,8 +72,8 @@ public:
     WTF_EXPORT_PRIVATE static Ref<WorkQueue> create(const char* name, Type = Type::Serial, QOS = QOS::Default);
     virtual ~WorkQueue();
 
-    WTF_EXPORT_PRIVATE void dispatch(NoncopyableFunction&&) override;
-    WTF_EXPORT_PRIVATE void dispatchAfter(std::chrono::nanoseconds, NoncopyableFunction&&);
+    WTF_EXPORT_PRIVATE void dispatch(NoncopyableFunction<void ()>&&) override;
+    WTF_EXPORT_PRIVATE void dispatchAfter(std::chrono::nanoseconds, NoncopyableFunction<void ()>&&);
 
     WTF_EXPORT_PRIVATE static void concurrentApply(size_t iterations, const std::function<void (size_t index)>&);
 
index 449a40f..995a954 100644 (file)
 
 namespace WTF {
 
-void WorkQueue::dispatch(NoncopyableFunction&& function)
+void WorkQueue::dispatch(NoncopyableFunction<void ()>&& function)
 {
     ref();
-    auto* functionPtr = new NoncopyableFunction(WTFMove(function));
+    auto* functionPtr = new NoncopyableFunction<void ()>(WTFMove(function));
     dispatch_async(m_dispatchQueue, ^{
         (*functionPtr)();
         delete functionPtr;
@@ -39,10 +39,10 @@ void WorkQueue::dispatch(NoncopyableFunction&& function)
     });
 }
 
-void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction&& function)
+void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction<void ()>&& function)
 {
     ref();
-    auto* functionPtr = new NoncopyableFunction(WTFMove(function));
+    auto* functionPtr = new NoncopyableFunction<void ()>(WTFMove(function));
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration.count()), m_dispatchQueue, ^{
         (*functionPtr)();
         delete functionPtr;
index d0e9c4f..3ddfe57 100644 (file)
@@ -34,7 +34,7 @@
 
 class WorkItem {
 public:
-    WorkItem(Ref<WorkQueue>&& workQueue, NoncopyableFunction&& function)
+    WorkItem(Ref<WorkQueue>&& workQueue, NoncopyableFunction<void ()>&& function)
         : m_workQueue(WTFMove(workQueue))
         , m_function(WTFMove(function))
     {
@@ -44,12 +44,12 @@ public:
 
 private:
     Ref<WorkQueue> m_workQueue;
-    NoncopyableFunction m_function;
+    NoncopyableFunction<void ()> m_function;
 };
 
 class TimerWorkItem : public WorkItem {
 public:
-    static std::unique_ptr<TimerWorkItem> create(Ref<WorkQueue>&& workQueue, NoncopyableFunction&& function, std::chrono::nanoseconds delayNanoSeconds)
+    static std::unique_ptr<TimerWorkItem> create(Ref<WorkQueue>&& workQueue, NoncopyableFunction<void ()>&& function, std::chrono::nanoseconds delayNanoSeconds)
     {
         ASSERT(delayNanoSeconds.count() >= 0);
         return std::unique_ptr<TimerWorkItem>(new TimerWorkItem(WTFMove(workQueue), WTFMove(function), monotonicallyIncreasingTime() * 1000000000.0 + delayNanoSeconds.count()));
@@ -58,7 +58,7 @@ public:
     bool hasExpired(double currentTimeNanoSeconds) const { return currentTimeNanoSeconds >= m_expirationTimeNanoSeconds; }
 
 protected:
-    TimerWorkItem(Ref<WorkQueue>&& workQueue, NoncopyableFunction&& function, double expirationTimeNanoSeconds)
+    TimerWorkItem(Ref<WorkQueue>&& workQueue, NoncopyableFunction<void ()>&& function, double expirationTimeNanoSeconds)
         : WorkItem(WTFMove(workQueue), WTFMove(function))
         , m_expirationTimeNanoSeconds(expirationTimeNanoSeconds)
     {
index 95dbf6c..40d1939 100644 (file)
@@ -54,7 +54,7 @@ void WorkQueue::unregisterSocketEventHandler(int fileDescriptor)
     m_dispatchQueue->clearSocketEventHandler();
 }
 
-void WorkQueue::dispatch(NoncopyableFunction&& function)
+void WorkQueue::dispatch(NoncopyableFunction<void ()>&& function)
 {
     if (!m_dispatchQueue)
         return;
@@ -62,7 +62,7 @@ void WorkQueue::dispatch(NoncopyableFunction&& function)
     m_dispatchQueue->dispatch(std::make_unique<WorkItem>(*this, WTFMove(function)));
 }
 
-void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction&& function)
+void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction<void ()>&& function)
 {
     if (!m_dispatchQueue)
         return;
index 5713581..171ad30 100644 (file)
@@ -32,12 +32,12 @@ namespace WTF {
 class RunLoop::TimerBase::ScheduledTask : public ThreadSafeRefCounted<ScheduledTask> {
 WTF_MAKE_NONCOPYABLE(ScheduledTask);
 public:
-    static RefPtr<ScheduledTask> create(NoncopyableFunction&& function, double interval, bool repeating)
+    static RefPtr<ScheduledTask> create(NoncopyableFunction<void ()>&& function, double interval, bool repeating)
     {
         return adoptRef(new ScheduledTask(WTFMove(function), interval, repeating));
     }
 
-    ScheduledTask(NoncopyableFunction&& function, double interval, bool repeating)
+    ScheduledTask(NoncopyableFunction<void ()>&& function, double interval, bool repeating)
         : m_function(WTFMove(function))
         , m_fireInterval(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::duration<double>(interval)))
         , m_isRepeating(repeating)
@@ -90,7 +90,7 @@ public:
     }
 
 private:
-    NoncopyableFunction m_function;
+    NoncopyableFunction<void ()> m_function;
     Condition::Clock::time_point m_scheduledTimePoint;
     std::chrono::microseconds m_fireInterval;
     std::atomic<bool> m_isActive { true };
@@ -237,7 +237,7 @@ void RunLoop::scheduleAndWakeUp(RefPtr<TimerBase::ScheduledTask> task)
     wakeUp(locker);
 }
 
-void RunLoop::dispatchAfter(std::chrono::nanoseconds delay, NoncopyableFunction&& function)
+void RunLoop::dispatchAfter(std::chrono::nanoseconds delay, NoncopyableFunction<void ()>&& function)
 {
     LockHolder locker(m_loopLock);
     bool repeating = false;
index a618d4d..7f80e69 100644 (file)
@@ -81,7 +81,7 @@ void WorkQueue::platformInvalidate()
     }
 }
 
-void WorkQueue::dispatch(NoncopyableFunction&& function)
+void WorkQueue::dispatch(NoncopyableFunction<void ()>&& function)
 {
     RefPtr<WorkQueue> protect(this);
     m_runLoop->dispatch([protect, function = WTFMove(function)] {
@@ -89,7 +89,7 @@ void WorkQueue::dispatch(NoncopyableFunction&& function)
     });
 }
 
-void WorkQueue::dispatchAfter(std::chrono::nanoseconds delay, NoncopyableFunction&& function)
+void WorkQueue::dispatchAfter(std::chrono::nanoseconds delay, NoncopyableFunction<void ()>&& function)
 {
     RefPtr<WorkQueue> protect(this);
     m_runLoop->dispatchAfter(delay, [protect, function = WTFMove(function)] {
index 0c50ff2..89b7f70 100644 (file)
@@ -124,7 +124,7 @@ void RunLoop::wakeUp()
 class DispatchAfterContext {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    DispatchAfterContext(NoncopyableFunction&& function)
+    DispatchAfterContext(NoncopyableFunction<void ()>&& function)
         : m_function(WTFMove(function))
     {
     }
@@ -135,10 +135,10 @@ public:
     }
 
 private:
-    NoncopyableFunction m_function;
+    NoncopyableFunction<void ()> m_function;
 };
 
-void RunLoop::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction&& function)
+void RunLoop::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction<void ()>&& function)
 {
     GRefPtr<GSource> source = adoptGRef(g_timeout_source_new(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()));
     g_source_set_name(source.get(), "[WebKit] RunLoop dispatchAfter");
index 605f77c..8642322 100644 (file)
 
 namespace WTF {
 
-WorkItemWin::WorkItemWin(NoncopyableFunction&& function, WorkQueue* queue)
+WorkItemWin::WorkItemWin(NoncopyableFunction<void ()>&& function, WorkQueue* queue)
     : m_function(WTFMove(function))
     , m_queue(queue)
 {
 }
 
-RefPtr<WorkItemWin> WorkItemWin::create(NoncopyableFunction&& function, WorkQueue* queue)
+RefPtr<WorkItemWin> WorkItemWin::create(NoncopyableFunction<void ()>&& function, WorkQueue* queue)
 {
     return adoptRef(new WorkItemWin(WTFMove(function), queue));
 }
@@ -47,7 +47,7 @@ WorkItemWin::~WorkItemWin()
 {
 }
 
-HandleWorkItem::HandleWorkItem(HANDLE handle, NoncopyableFunction&& function, WorkQueue* queue)
+HandleWorkItem::HandleWorkItem(HANDLE handle, NoncopyableFunction<void ()>&& function, WorkQueue* queue)
     : WorkItemWin(WTFMove(function), queue)
     , m_handle(handle)
     , m_waitHandle(0)
@@ -55,7 +55,7 @@ HandleWorkItem::HandleWorkItem(HANDLE handle, NoncopyableFunction&& function, Wo
     ASSERT_ARG(handle, handle);
 }
 
-RefPtr<HandleWorkItem> HandleWorkItem::createByAdoptingHandle(HANDLE handle, NoncopyableFunction&& function, WorkQueue* queue)
+RefPtr<HandleWorkItem> HandleWorkItem::createByAdoptingHandle(HANDLE handle, NoncopyableFunction<void ()>&& function, WorkQueue* queue)
 {
     return adoptRef(new HandleWorkItem(handle, WTFMove(function), queue));
 }
index a609547..baa77d9 100644 (file)
@@ -39,30 +39,30 @@ class WorkQueue;
 
 class WorkItemWin : public ThreadSafeRefCounted<WorkItemWin> {
 public:
-    static RefPtr<WorkItemWin> create(NoncopyableFunction&&, WorkQueue*);
+    static RefPtr<WorkItemWin> create(NoncopyableFunction<void ()>&&, WorkQueue*);
     virtual ~WorkItemWin();
 
-    NoncopyableFunction& function() { return m_function; }
+    NoncopyableFunction<void ()>& function() { return m_function; }
     WorkQueue* queue() const { return m_queue.get(); }
 
 protected:
-    WorkItemWin(NoncopyableFunction&&, WorkQueue*);
+    WorkItemWin(NoncopyableFunction<void ()>&&, WorkQueue*);
 
 private:
-    NoncopyableFunction m_function;
+    NoncopyableFunction<void ()> m_function;
     RefPtr<WorkQueue> m_queue;
 };
 
 class HandleWorkItem : public WorkItemWin {
 public:
-    static RefPtr<HandleWorkItem> createByAdoptingHandle(HANDLE, NoncopyableFunction&&, WorkQueue*);
+    static RefPtr<HandleWorkItem> createByAdoptingHandle(HANDLE, NoncopyableFunction<void ()>&&, WorkQueue*);
     virtual ~HandleWorkItem();
 
     void setWaitHandle(HANDLE waitHandle) { m_waitHandle = waitHandle; }
     HANDLE waitHandle() const { return m_waitHandle; }
 
 private:
-    HandleWorkItem(HANDLE, NoncopyableFunction&&, WorkQueue*);
+    HandleWorkItem(HANDLE, NoncopyableFunction<void ()>&&, WorkQueue*);
 
     HANDLE m_handle;
     HANDLE m_waitHandle;
index e78f934..e452981 100644 (file)
@@ -129,7 +129,7 @@ void WorkQueue::platformInvalidate()
     ::DeleteTimerQueueEx(m_timerQueue, 0);
 }
 
-void WorkQueue::dispatch(NoncopyableFunction&& function)
+void WorkQueue::dispatch(NoncopyableFunction<void ()>&& function)
 {
     MutexLocker locker(m_workItemQueueLock);
     ref();
@@ -149,7 +149,7 @@ struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
     static RefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
 
     WorkQueue* queue;
-    NoncopyableFunction function;
+    NoncopyableFunction<void ()> function;
     Mutex timerMutex;
     HANDLE timer;
 
@@ -180,7 +180,7 @@ void WorkQueue::timerCallback(void* context, BOOLEAN timerOrWaitFired)
     }
 }
 
-void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction&& function)
+void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, NoncopyableFunction<void ()>&& function)
 {
     ASSERT(m_timerQueue);
     ref();
index 0bb12d3..750f918 100644 (file)
@@ -1,3 +1,41 @@
+2016-05-28  Chris Dumez  <cdumez@apple.com>
+
+        Templatize NoncopyableFunction class similarly to std::function
+        https://bugs.webkit.org/show_bug.cgi?id=158185
+
+        Reviewed by Darin Adler.
+
+        Templatize NoncopyableFunction class similarly to std::function, so
+        that it can be used as a std::function replacement in more places.
+
+        Previously, NoncopyableFunction could only support "void()" lambdas.
+
+        * Modules/mediastream/MediaEndpointPeerConnection.cpp:
+        (WebCore::MediaEndpointPeerConnection::runTask):
+        * Modules/mediastream/MediaEndpointPeerConnection.h:
+        * fileapi/AsyncFileStream.cpp:
+        (WebCore::callOnFileThread):
+        (WebCore::AsyncFileStream::perform):
+        (WebCore::AsyncFileStream::getSize):
+        (WebCore::AsyncFileStream::openForRead):
+        (WebCore::AsyncFileStream::openForWrite):
+        (WebCore::AsyncFileStream::write):
+        * fileapi/AsyncFileStream.h:
+        * page/scrolling/ScrollingThread.cpp:
+        (WebCore::ScrollingThread::dispatch):
+        (WebCore::ScrollingThread::dispatchBarrier):
+        (WebCore::ScrollingThread::dispatchFunctionsFromScrollingThread):
+        * page/scrolling/ScrollingThread.h:
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.h:
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.mm:
+        (WebCore::MediaPlayerPrivateMediaStreamAVFObjC::scheduleDeferredTask):
+        * platform/mediastream/MediaStreamPrivate.cpp:
+        (WebCore::MediaStreamPrivate::scheduleDeferredTask):
+        * platform/mediastream/MediaStreamPrivate.h:
+        * platform/mediastream/mac/AVMediaCaptureSource.h:
+        * platform/mediastream/mac/AVMediaCaptureSource.mm:
+        (WebCore::AVMediaCaptureSource::scheduleDeferredTask):
+
 2016-05-28  Alexey Proskuryakov  <ap@apple.com>
 
         Fix the build with newer clang and other custom configuration options
index 385c14f..f1eb64e 100644 (file)
@@ -95,7 +95,7 @@ MediaEndpointPeerConnection::MediaEndpointPeerConnection(PeerConnectionBackendCl
     m_mediaEndpoint->generateDtlsInfo();
 }
 
-void MediaEndpointPeerConnection::runTask(NoncopyableFunction&& task)
+void MediaEndpointPeerConnection::runTask(NoncopyableFunction<void ()>&& task)
 {
     if (m_dtlsFingerprint.isNull()) {
         // Only one task needs to be deferred since it will hold off any others until completed.
index 86b2477..967d0c9 100644 (file)
@@ -78,7 +78,7 @@ public:
     void clearNegotiationNeededState() override { notImplemented(); };
 
 private:
-    void runTask(NoncopyableFunction&&);
+    void runTask(NoncopyableFunction<void ()>&&);
     void startRunningTasks();
 
     void createOfferTask(RTCOfferOptions&, PeerConnection::SessionDescriptionPromise&);
@@ -92,7 +92,7 @@ private:
     PeerConnectionBackendClient* m_client;
     std::unique_ptr<MediaEndpoint> m_mediaEndpoint;
 
-    NoncopyableFunction m_initialDeferredTask;
+    NoncopyableFunction<void ()> m_initialDeferredTask;
 
     std::unique_ptr<SDPProcessor> m_sdpProcessor;
 
index f4b70cf..4ae6c80 100644 (file)
@@ -64,12 +64,12 @@ inline AsyncFileStream::Internals::Internals(FileStreamClient& client)
 #endif
 }
 
-static void callOnFileThread(NoncopyableFunction&& function)
+static void callOnFileThread(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(isMainThread());
     ASSERT(function);
 
-    static NeverDestroyed<MessageQueue<NoncopyableFunction>> queue;
+    static NeverDestroyed<MessageQueue<NoncopyableFunction<void ()>>> queue;
 
     static std::once_flag createFileThreadOnce;
     std::call_once(createFileThreadOnce, [] {
@@ -90,7 +90,7 @@ static void callOnFileThread(NoncopyableFunction&& function)
         });
     });
 
-    queue.get().append(std::make_unique<NoncopyableFunction>(WTFMove(function)));
+    queue.get().append(std::make_unique<NoncopyableFunction<void ()>>(WTFMove(function)));
 }
 
 AsyncFileStream::AsyncFileStream(FileStreamClient& client)
@@ -114,7 +114,7 @@ AsyncFileStream::~AsyncFileStream()
     });
 }
 
-void AsyncFileStream::perform(std::function<std::function<void(FileStreamClient&)>(FileStream&)>&& operation)
+void AsyncFileStream::perform(NoncopyableFunction<std::function<void(FileStreamClient&)>(FileStream&)>&& operation)
 {
     auto& internals = *m_internals;
     callOnFileThread([&internals, operation = WTFMove(operation)] {
@@ -134,11 +134,10 @@ void AsyncFileStream::perform(std::function<std::function<void(FileStreamClient&
 
 void AsyncFileStream::getSize(const String& path, double expectedModificationTime)
 {
-    StringCapture capturedPath(path);
     // FIXME: Explicit return type here and in all the other cases like this below is a workaround for a deficiency
     // in the Windows compiler at the time of this writing. Could remove it if that is resolved.
-    perform([capturedPath, expectedModificationTime](FileStream& stream) -> std::function<void(FileStreamClient&)> {
-        long long size = stream.getSize(capturedPath.string(), expectedModificationTime);
+    perform([path = path.isolatedCopy(), expectedModificationTime](FileStream& stream) -> std::function<void(FileStreamClient&)> {
+        long long size = stream.getSize(path, expectedModificationTime);
         return [size](FileStreamClient& client) {
             client.didGetSize(size);
         };
@@ -147,10 +146,9 @@ void AsyncFileStream::getSize(const String& path, double expectedModificationTim
 
 void AsyncFileStream::openForRead(const String& path, long long offset, long long length)
 {
-    StringCapture capturedPath(path);
     // FIXME: Explicit return type here is a workaround for a deficiency in the Windows compiler at the time of this writing.
-    perform([capturedPath, offset, length](FileStream& stream) -> std::function<void(FileStreamClient&)> {
-        bool success = stream.openForRead(capturedPath.string(), offset, length);
+    perform([path = path.isolatedCopy(), offset, length](FileStream& stream) -> std::function<void(FileStreamClient&)> {
+        bool success = stream.openForRead(path, offset, length);
         return [success](FileStreamClient& client) {
             client.didOpen(success);
         };
@@ -159,9 +157,8 @@ void AsyncFileStream::openForRead(const String& path, long long offset, long lon
 
 void AsyncFileStream::openForWrite(const String& path)
 {
-    StringCapture capturedPath(path);
-    perform([capturedPath](FileStream& stream) -> std::function<void(FileStreamClient&)> {
-        bool success = stream.openForWrite(capturedPath.string());
+    perform([path = path.isolatedCopy()](FileStream& stream) -> std::function<void(FileStreamClient&)> {
+        bool success = stream.openForWrite(path);
         return [success](FileStreamClient& client) {
             client.didOpen(success);
         };
@@ -188,9 +185,8 @@ void AsyncFileStream::read(char* buffer, int length)
 
 void AsyncFileStream::write(const URL& blobURL, long long position, int length)
 {
-    URLCapture capturedURL(blobURL);
-    perform([capturedURL, position, length](FileStream& stream) -> std::function<void(FileStreamClient&)> {
-        int bytesWritten = stream.write(capturedURL.url(), position, length);
+    perform([blobURL = blobURL.isolatedCopy(), position, length](FileStream& stream) -> std::function<void(FileStreamClient&)> {
+        int bytesWritten = stream.write(blobURL, position, length);
         return [bytesWritten](FileStreamClient& client) {
             client.didWrite(bytesWritten);
         };
index ff481a4..088bb37 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <functional>
 #include <wtf/Forward.h>
+#include <wtf/NoncopyableFunction.h>
 
 namespace WebCore {
 
@@ -56,7 +57,7 @@ public:
 
 private:
     void start();
-    void perform(std::function<std::function<void(FileStreamClient&)>(FileStream&)>&&);
+    void perform(NoncopyableFunction<std::function<void(FileStreamClient&)>(FileStream&)>&&);
 
     struct Internals;
     std::unique_ptr<Internals> m_internals;
index 35a17b8..5760783 100644 (file)
@@ -45,7 +45,7 @@ bool ScrollingThread::isCurrentThread()
     return threadIdentifier && currentThread() == threadIdentifier;
 }
 
-void ScrollingThread::dispatch(NoncopyableFunction&& function)
+void ScrollingThread::dispatch(NoncopyableFunction<void ()>&& function)
 {
     auto& scrollingThread = ScrollingThread::singleton();
     scrollingThread.createThreadIfNeeded();
@@ -58,7 +58,7 @@ void ScrollingThread::dispatch(NoncopyableFunction&& function)
     scrollingThread.wakeUpRunLoop();
 }
 
-void ScrollingThread::dispatchBarrier(NoncopyableFunction&& function)
+void ScrollingThread::dispatchBarrier(NoncopyableFunction<void ()>&& function)
 {
     dispatch([function = WTFMove(function)]() mutable {
         callOnMainThread(WTFMove(function));
@@ -104,7 +104,7 @@ void ScrollingThread::dispatchFunctionsFromScrollingThread()
 {
     ASSERT(isCurrentThread());
 
-    Vector<NoncopyableFunction> functions;
+    Vector<NoncopyableFunction<void ()>> functions;
     
     {
         std::lock_guard<Lock> lock(m_functionsMutex);
index aaf577b..037f95b 100644 (file)
@@ -48,11 +48,11 @@ class ScrollingThread {
 
 public:
     static bool isCurrentThread();
-    WEBCORE_EXPORT static void dispatch(NoncopyableFunction&&);
+    WEBCORE_EXPORT static void dispatch(NoncopyableFunction<void ()>&&);
 
     // Will dispatch the given function on the main thread once all pending functions
     // on the scrolling thread have finished executing. Used for synchronization purposes.
-    WEBCORE_EXPORT static void dispatchBarrier(NoncopyableFunction&&);
+    WEBCORE_EXPORT static void dispatchBarrier(NoncopyableFunction<void ()>&&);
 
 private:
     friend NeverDestroyed<ScrollingThread>;
@@ -80,7 +80,7 @@ private:
     Lock m_initializeRunLoopMutex;
 
     Lock m_functionsMutex;
-    Vector<NoncopyableFunction> m_functions;
+    Vector<NoncopyableFunction<void ()>> m_functions;
 
 #if PLATFORM(COCOA)
     // FIXME: We should use WebCore::RunLoop here.
index daee2ef..869a68e 100644 (file)
@@ -143,7 +143,7 @@ private:
     void updateTracks();
     void renderingModeChanged();
 
-    void scheduleDeferredTask(NoncopyableFunction&&);
+    void scheduleDeferredTask(NoncopyableFunction<void ()>&&);
 
     enum DisplayMode {
         None,
index 7a79642..84c7d62 100644 (file)
@@ -624,7 +624,7 @@ void MediaPlayerPrivateMediaStreamAVFObjC::setNetworkState(MediaPlayer::NetworkS
     m_player->networkStateChanged();
 }
 
-void MediaPlayerPrivateMediaStreamAVFObjC::scheduleDeferredTask(NoncopyableFunction&& function)
+void MediaPlayerPrivateMediaStreamAVFObjC::scheduleDeferredTask(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(function);
     callOnMainThread([weakThis = createWeakPtr(), function = WTFMove(function)] {
index 42d2466..d49d151 100644 (file)
@@ -299,7 +299,7 @@ void MediaStreamPrivate::trackEnded(MediaStreamTrackPrivate&)
     });
 }
 
-void MediaStreamPrivate::scheduleDeferredTask(NoncopyableFunction&& function)
+void MediaStreamPrivate::scheduleDeferredTask(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(function);
     callOnMainThread([weakThis = createWeakPtr(), function = WTFMove(function)] {
index 8cf604c..92304ef 100644 (file)
@@ -112,7 +112,7 @@ private:
     void characteristicsChanged();
     void updateActiveVideoTrack();
 
-    void scheduleDeferredTask(NoncopyableFunction&&);
+    void scheduleDeferredTask(NoncopyableFunction<void ()>&&);
 
     WeakPtrFactory<MediaStreamPrivate> m_weakPtrFactory;
     Vector<Observer*> m_observers;
index 21acaf0..f474aeb 100644 (file)
@@ -86,7 +86,7 @@ protected:
     void setVideoSampleBufferDelegate(AVCaptureVideoDataOutput*);
     void setAudioSampleBufferDelegate(AVCaptureAudioDataOutput*);
 
-    void scheduleDeferredTask(NoncopyableFunction&&);
+    void scheduleDeferredTask(NoncopyableFunction<void ()>&&);
 
 private:
     void setupSession();
index 7915a84..76afd9b 100644 (file)
@@ -240,7 +240,7 @@ void AVMediaCaptureSource::setAudioSampleBufferDelegate(AVCaptureAudioDataOutput
     [audioOutput setSampleBufferDelegate:m_objcObserver.get() queue:globaAudioCaptureSerialQueue()];
 }
 
-void AVMediaCaptureSource::scheduleDeferredTask(NoncopyableFunction&& function)
+void AVMediaCaptureSource::scheduleDeferredTask(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(function);
     callOnMainThread([weakThis = createWeakPtr(), function = WTFMove(function)] {
index 830df7d..7389af7 100644 (file)
@@ -1,3 +1,23 @@
+2016-05-28  Chris Dumez  <cdumez@apple.com>
+
+        Templatize NoncopyableFunction class similarly to std::function
+        https://bugs.webkit.org/show_bug.cgi?id=158185
+
+        Reviewed by Darin Adler.
+
+        Templatize NoncopyableFunction class similarly to std::function, so
+        that it can be used as a std::function replacement in more places.
+
+        Previously, NoncopyableFunction could only support "void()" lambdas.
+
+        * Storage/StorageSyncManager.cpp:
+        (WebCore::StorageSyncManager::dispatch):
+        * Storage/StorageSyncManager.h:
+        * Storage/StorageThread.cpp:
+        (WebCore::StorageThread::dispatch):
+        (WebCore::StorageThread::terminate):
+        * Storage/StorageThread.h:
+
 2016-05-27  Chris Dumez  <cdumez@apple.com>
 
         callOnMainThread() should not copy captured lambda variables
index 3e1f1c4..e1a4a15 100644 (file)
@@ -63,7 +63,7 @@ String StorageSyncManager::fullDatabaseFilename(const String& databaseIdentifier
     return pathByAppendingComponent(m_path, databaseIdentifier + ".localstorage");
 }
 
-void StorageSyncManager::dispatch(NoncopyableFunction&& function)
+void StorageSyncManager::dispatch(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(isMainThread());
     ASSERT(m_thread);
index edd717d..75a486b 100644 (file)
@@ -43,7 +43,7 @@ public:
     static Ref<StorageSyncManager> create(const String& path);
     ~StorageSyncManager();
 
-    void dispatch(NoncopyableFunction&&);
+    void dispatch(NoncopyableFunction<void ()>&&);
     void close();
 
 private:
index 62840d2..2966e04 100644 (file)
@@ -74,11 +74,11 @@ void StorageThread::threadEntryPoint()
     }
 }
 
-void StorageThread::dispatch(NoncopyableFunction&& function)
+void StorageThread::dispatch(NoncopyableFunction<void ()>&& function)
 {
     ASSERT(isMainThread());
     ASSERT(!m_queue.killed() && m_threadID);
-    m_queue.append(std::make_unique<NoncopyableFunction>(WTFMove(function)));
+    m_queue.append(std::make_unique<NoncopyableFunction<void ()>>(WTFMove(function)));
 }
 
 void StorageThread::terminate()
@@ -90,7 +90,7 @@ void StorageThread::terminate()
     if (!m_threadID)
         return;
 
-    m_queue.append(std::make_unique<NoncopyableFunction>([this] {
+    m_queue.append(std::make_unique<NoncopyableFunction<void ()>>([this] {
         performTerminate();
     }));
     waitForThreadCompletion(m_threadID);
index dce5ccf..4752d5b 100644 (file)
@@ -46,7 +46,7 @@ public:
     bool start();
     void terminate();
 
-    void dispatch(NoncopyableFunction&&);
+    void dispatch(NoncopyableFunction<void ()>&&);
 
     static void releaseFastMallocFreeMemoryInAllThreads();
 
@@ -59,7 +59,7 @@ private:
     void performTerminate();
 
     ThreadIdentifier m_threadID;
-    MessageQueue<NoncopyableFunction> m_queue;
+    MessageQueue<NoncopyableFunction<void ()>> m_queue;
 };
 
 } // namespace WebCore
index f60bb9f..a3cf947 100644 (file)
@@ -1,3 +1,18 @@
+2016-05-28  Chris Dumez  <cdumez@apple.com>
+
+        Templatize NoncopyableFunction class similarly to std::function
+        https://bugs.webkit.org/show_bug.cgi?id=158185
+
+        Reviewed by Darin Adler.
+
+        Templatize NoncopyableFunction class similarly to std::function, so
+        that it can be used as a std::function replacement in more places.
+
+        Previously, NoncopyableFunction could only support "void()" lambdas.
+
+        * NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
+        (WebKit::NetworkCache::runTaskInQueue):
+
 2016-05-28  Alexey Proskuryakov  <ap@apple.com>
 
         Fix the build with newer clang and other custom configuration options
index 2c3d034..8c91c9e 100644 (file)
@@ -69,7 +69,7 @@ Ref<IOChannel> IOChannel::open(const String& filePath, IOChannel::Type type)
     return adoptRef(*new IOChannel(filePath, type));
 }
 
-static inline void runTaskInQueue(NoncopyableFunction&& task, WorkQueue* queue)
+static inline void runTaskInQueue(NoncopyableFunction<void ()>&& task, WorkQueue* queue)
 {
     if (queue) {
         queue->dispatch(WTFMove(task));