Title: [225681] trunk/Source
Revision
225681
Author
utatane....@gmail.com
Date
2017-12-08 09:48:31 -0800 (Fri, 08 Dec 2017)

Log Message

[WTF] Remove remaining use of Mutex
https://bugs.webkit.org/show_bug.cgi?id=180579

Reviewed by Alex Christensen.

Source/WebKit:

Remove unused "BinarySemaphore.h".

* Platform/IPC/win/ConnectionWin.cpp:

Source/WTF:

Mutex should be only used in low-level Locking and Threading.
Other ones should use WTF::Lock and WTF::Condition instead.
This patch replaces WTF::Mutex with WTF::Lock in WTF if it
is not related to threading or locking implementation.

And we also use WTF::Lock and WTF::Condition in WTF::BinarySemaphore.

* wtf/RunLoop.cpp:
(WTF::RunLoop::performWork):
(WTF::RunLoop::dispatch):
* wtf/RunLoop.h:
* wtf/WorkQueue.cpp:
* wtf/WorkQueue.h:
* wtf/threads/BinarySemaphore.cpp:
(WTF::BinarySemaphore::signal):
(WTF::BinarySemaphore::wait):
(WTF::BinarySemaphore::BinarySemaphore): Deleted.
(WTF::BinarySemaphore::~BinarySemaphore): Deleted.
* wtf/threads/BinarySemaphore.h:
* wtf/win/WorkQueueWin.cpp:
(WTF::WorkQueue::registerHandle):
(WTF::WorkQueue::unregisterAndCloseHandle):
(WTF::WorkQueue::dispatch):
(WTF::WorkQueue::timerCallback):
(WTF::WorkQueue::dispatchAfter):
(WTF::TimerContext::TimerContext): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (225680 => 225681)


--- trunk/Source/WTF/ChangeLog	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/ChangeLog	2017-12-08 17:48:31 UTC (rev 225681)
@@ -1,5 +1,39 @@
 2017-12-08  Yusuke Suzuki  <utatane....@gmail.com>
 
+        [WTF] Remove remaining use of Mutex
+        https://bugs.webkit.org/show_bug.cgi?id=180579
+
+        Reviewed by Alex Christensen.
+
+        Mutex should be only used in low-level Locking and Threading.
+        Other ones should use WTF::Lock and WTF::Condition instead.
+        This patch replaces WTF::Mutex with WTF::Lock in WTF if it
+        is not related to threading or locking implementation.
+
+        And we also use WTF::Lock and WTF::Condition in WTF::BinarySemaphore.
+
+        * wtf/RunLoop.cpp:
+        (WTF::RunLoop::performWork):
+        (WTF::RunLoop::dispatch):
+        * wtf/RunLoop.h:
+        * wtf/WorkQueue.cpp:
+        * wtf/WorkQueue.h:
+        * wtf/threads/BinarySemaphore.cpp:
+        (WTF::BinarySemaphore::signal):
+        (WTF::BinarySemaphore::wait):
+        (WTF::BinarySemaphore::BinarySemaphore): Deleted.
+        (WTF::BinarySemaphore::~BinarySemaphore): Deleted.
+        * wtf/threads/BinarySemaphore.h:
+        * wtf/win/WorkQueueWin.cpp:
+        (WTF::WorkQueue::registerHandle):
+        (WTF::WorkQueue::unregisterAndCloseHandle):
+        (WTF::WorkQueue::dispatch):
+        (WTF::WorkQueue::timerCallback):
+        (WTF::WorkQueue::dispatchAfter):
+        (WTF::TimerContext::TimerContext): Deleted.
+
+2017-12-08  Yusuke Suzuki  <utatane....@gmail.com>
+
         Use StaticLock and Lock instead of Mutex in Windows WebKitLegacy
         https://bugs.webkit.org/show_bug.cgi?id=180572
 

Modified: trunk/Source/WTF/wtf/RunLoop.cpp (225680 => 225681)


--- trunk/Source/WTF/wtf/RunLoop.cpp	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/RunLoop.cpp	2017-12-08 17:48:31 UTC (rev 225681)
@@ -94,7 +94,7 @@
     {
         Function<void ()> function;
         {
-            MutexLocker locker(m_functionQueueLock);
+            auto locker = holdLock(m_functionQueueLock);
             functionsToHandle = m_functionQueue.size();
 
             if (m_functionQueue.isEmpty())
@@ -109,7 +109,7 @@
     for (size_t functionsHandled = 1; functionsHandled < functionsToHandle; ++functionsHandled) {
         Function<void ()> function;
         {
-            MutexLocker locker(m_functionQueueLock);
+            auto locker = holdLock(m_functionQueueLock);
 
             // Even if we start off with N functions to handle and we've only handled less than N functions, the queue
             // still might be empty because those functions might have been handled in an inner RunLoop::performWork().
@@ -127,7 +127,7 @@
 void RunLoop::dispatch(Function<void ()>&& function)
 {
     {
-        MutexLocker locker(m_functionQueueLock);
+        auto locker = holdLock(m_functionQueueLock);
         m_functionQueue.append(WTFMove(function));
     }
 

Modified: trunk/Source/WTF/wtf/RunLoop.h (225680 => 225681)


--- trunk/Source/WTF/wtf/RunLoop.h	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/RunLoop.h	2017-12-08 17:48:31 UTC (rev 225681)
@@ -162,7 +162,7 @@
 
     void performWork();
 
-    Mutex m_functionQueueLock;
+    Lock m_functionQueueLock;
     Deque<Function<void()>> m_functionQueue;
 
 #if USE(WINDOWS_EVENT_LOOP)

Modified: trunk/Source/WTF/wtf/WorkQueue.cpp (225680 => 225681)


--- trunk/Source/WTF/wtf/WorkQueue.cpp	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/WorkQueue.cpp	2017-12-08 17:48:31 UTC (rev 225681)
@@ -37,7 +37,6 @@
 #include <wtf/Ref.h>
 #include <wtf/Threading.h>
 #include <wtf/text/WTFString.h>
-#include <wtf/threads/BinarySemaphore.h>
 
 #if USE(WINDOWS_EVENT_LOOP)
 #include <wtf/win/WorkItemContext.h>

Modified: trunk/Source/WTF/wtf/WorkQueue.h (225680 => 225681)


--- trunk/Source/WTF/wtf/WorkQueue.h	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/WorkQueue.h	2017-12-08 17:48:31 UTC (rev 225681)
@@ -111,10 +111,9 @@
 #elif USE(WINDOWS_EVENT_LOOP)
     volatile LONG m_isWorkThreadRegistered;
 
-    Mutex m_functionQueueLock;
+    Lock m_functionQueueLock;
+    Lock m_itemsMapLock;
     Vector<Function<void()>> m_functionQueue;
-
-    Mutex m_itemsMapLock;
     HashMap<HANDLE, Ref<WorkItemContext>> m_itemsMap;
 
     HANDLE m_timerQueue;

Modified: trunk/Source/WTF/wtf/threads/BinarySemaphore.cpp (225680 => 225681)


--- trunk/Source/WTF/wtf/threads/BinarySemaphore.cpp	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/threads/BinarySemaphore.cpp	2017-12-08 17:48:31 UTC (rev 225681)
@@ -28,38 +28,20 @@
 
 namespace WTF {
 
-BinarySemaphore::BinarySemaphore()
-    : m_isSet(false)
-{
-}
-    
-BinarySemaphore::~BinarySemaphore()
-{
-}
-
 void BinarySemaphore::signal()
 {
-    MutexLocker locker(m_mutex);
-
+    auto locker = holdLock(m_lock);
     m_isSet = true;
-    m_condition.signal();
+    m_condition.notifyOne();
 }
 
 bool BinarySemaphore::wait(TimeWithDynamicClockType absoluteTime)
 {
-    MutexLocker locker(m_mutex);
-
-    bool timedOut = false;
-    while (!m_isSet) {
-        timedOut = !m_condition.timedWait(
-            m_mutex, absoluteTime.approximateWallTime().secondsSinceEpoch().value());
-        if (timedOut)
-            return false;
-    }
-
-    // Reset the semaphore.
-    m_isSet = false;
-    return true;
+    auto locker = holdLock(m_lock);
+    bool satisfied = m_condition.waitUntil(m_lock, absoluteTime, [&] { return m_isSet; });
+    if (satisfied)
+        m_isSet = false;
+    return satisfied;
 }
 
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/threads/BinarySemaphore.h (225680 => 225681)


--- trunk/Source/WTF/wtf/threads/BinarySemaphore.h	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/threads/BinarySemaphore.h	2017-12-08 17:48:31 UTC (rev 225681)
@@ -26,8 +26,9 @@
 #ifndef BinarySemaphore_h
 #define BinarySemaphore_h
 
+#include <wtf/Condition.h>
+#include <wtf/Lock.h>
 #include <wtf/Noncopyable.h>
-#include <wtf/ThreadingPrimitives.h>
 #include <wtf/TimeWithDynamicClockType.h>
 
 namespace WTF {
@@ -34,19 +35,16 @@
 
 class BinarySemaphore {
     WTF_MAKE_NONCOPYABLE(BinarySemaphore);
-
 public:
-    WTF_EXPORT_PRIVATE BinarySemaphore();
-    WTF_EXPORT_PRIVATE ~BinarySemaphore();
+    BinarySemaphore() = default;
 
     WTF_EXPORT_PRIVATE void signal();
     WTF_EXPORT_PRIVATE bool wait(TimeWithDynamicClockType);
 
 private:
-    bool m_isSet;
-
-    Mutex m_mutex;
-    ThreadCondition m_condition;
+    bool m_isSet { false };
+    Lock m_lock;
+    Condition m_condition;
 };
 
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/win/WorkQueueWin.cpp (225680 => 225681)


--- trunk/Source/WTF/wtf/win/WorkQueueWin.cpp	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WTF/wtf/win/WorkQueueWin.cpp	2017-12-08 17:48:31 UTC (rev 225681)
@@ -54,7 +54,7 @@
     if (!::RegisterWaitForSingleObject(&context->waitHandle().m_handle, handle, handleCallback, context.ptr(), INFINITE, WT_EXECUTEDEFAULT))
         ASSERT_WITH_MESSAGE(m_timerQueue, "::RegisterWaitForSingleObject %lu", ::GetLastError());
 
-    MutexLocker lock(m_itemsMapLock);
+    auto locker = holdLock(m_itemsMapLock);
     ASSERT_ARG(handle, !m_itemsMap.contains(handle));
     m_itemsMap.set(handle, WTFMove(context));
 }
@@ -61,7 +61,7 @@
 
 void WorkQueue::unregisterAndCloseHandle(HANDLE handle)
 {
-    MutexLocker locker(m_itemsMapLock);
+    auto locker = holdLock(m_itemsMapLock);
     ASSERT_ARG(handle, m_itemsMap.contains(handle));
 
     unregisterWaitAndDestroyItemSoon(WTFMove(m_itemsMap.take(handle).value()));
@@ -135,7 +135,7 @@
 
 void WorkQueue::dispatch(Function<void()>&& function)
 {
-    MutexLocker locker(m_functionQueueLock);
+    auto locker = holdLock(m_functionQueueLock);
     m_functionQueue.append(WTFMove(function));
 
     // Spawn a work thread to perform the work we just added. As an optimization, we avoid
@@ -153,17 +153,13 @@
 struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
     static RefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
 
-    WorkQueue* queue;
+    Lock timerLock;
+    WorkQueue* queue { nullptr };
+    HANDLE timer { nullptr };
     Function<void()> function;
-    Mutex timerMutex;
-    HANDLE timer;
 
 private:
-    TimerContext()
-        : queue(nullptr)
-        , timer(0)
-    {
-    }
+    TimerContext() = default;
 };
 
 void WorkQueue::timerCallback(void* context, BOOLEAN timerOrWaitFired)
@@ -176,7 +172,7 @@
 
     timerContext->queue->dispatch(WTFMove(timerContext->function));
 
-    MutexLocker lock(timerContext->timerMutex);
+    auto locker = holdLock(timerContext->timerLock);
     ASSERT(timerContext->timer);
     ASSERT(timerContext->queue->m_timerQueue);
     if (!::DeleteTimerQueueTimer(timerContext->queue->m_timerQueue, timerContext->timer, 0)) {
@@ -198,7 +194,7 @@
         // The timer callback could fire before ::CreateTimerQueueTimer even returns, so we protect
         // context->timer with a mutex to ensure the timer callback doesn't access it before the
         // timer handle has been stored in it.
-        MutexLocker lock(context->timerMutex);
+        auto locker = holdLock(context->timerLock);
 
         int64_t milliseconds = duration.milliseconds();
 

Modified: trunk/Source/WebKit/ChangeLog (225680 => 225681)


--- trunk/Source/WebKit/ChangeLog	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WebKit/ChangeLog	2017-12-08 17:48:31 UTC (rev 225681)
@@ -1,3 +1,14 @@
+2017-12-08  Yusuke Suzuki  <utatane....@gmail.com>
+
+        [WTF] Remove remaining use of Mutex
+        https://bugs.webkit.org/show_bug.cgi?id=180579
+
+        Reviewed by Alex Christensen.
+
+        Remove unused "BinarySemaphore.h".
+
+        * Platform/IPC/win/ConnectionWin.cpp:
+
 2017-12-08  Michael Catanzaro  <mcatanz...@igalia.com>
 
         [GTK] WebInspectorProxyClient needs a virtual destructor

Modified: trunk/Source/WebKit/Platform/IPC/win/ConnectionWin.cpp (225680 => 225681)


--- trunk/Source/WebKit/Platform/IPC/win/ConnectionWin.cpp	2017-12-08 17:26:58 UTC (rev 225680)
+++ trunk/Source/WebKit/Platform/IPC/win/ConnectionWin.cpp	2017-12-08 17:48:31 UTC (rev 225681)
@@ -29,7 +29,6 @@
 #include "DataReference.h"
 #include <wtf/RandomNumber.h>
 #include <wtf/text/WTFString.h>
-#include <wtf/threads/BinarySemaphore.h>
 
 using namespace std;
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to