Title: [247723] trunk
Revision
247723
Author
[email protected]
Date
2019-07-23 07:51:29 -0700 (Tue, 23 Jul 2019)

Log Message

[WTF] Add DataMutex and MainThreadData wrappers
https://bugs.webkit.org/show_bug.cgi?id=199831

Reviewed by Alex Christensen.

Source/WTF:

This patch adds two new wrapper classes:

DataMutex<T> stores an instance of T in a private member along with a
mutex. In order to use its fields, users need to instantiate a
DataMutex<T>::LockedWrapper instance in the stack. This class uses
RAII to lock and unlock the mutex in construction and destruction
respectively, and by using the arrow operator lets the user access T's
members.

This way, DataMutex<T> prevents most instances of accidental access to
data fields that should only be read and modified in an atomic matter.

Still, both the Lock and the LockHolder are exposed once the user has
taken the lock so that special cases such as waiting for a condition
variable or performing an early unlock are doable.

MainThreadData<T> is another wrapper class, in this case for data
fields that should only be accessed from the main thread. In this
case, it works similar to a smart pointer, except that (1) there is no
actual memory indirection, T is stored directly inside
MainThreadData<T> and (2) attempts to use the -> or * operator have an
isMainThread() assertion.

Together, these two wrapper classes make it easier to write
multi-threaded code in a safer, more self-documented way by letting
the author group data into structures that have certain access safety
requirements.

These structures were originally part of the new GStreamer
WebKitMediaSrc rework patch: https://bugs.webkit.org/show_bug.cgi?id=199719

* wtf/CMakeLists.txt:
* wtf/DataMutex.h: Added.
(WTF::DataMutex::DataMutex):
(WTF::DataMutex::LockedWrapper::LockedWrapper):
(WTF::DataMutex::LockedWrapper::operator->):
(WTF::DataMutex::LockedWrapper::operator*):
(WTF::DataMutex::LockedWrapper::mutex):
(WTF::DataMutex::LockedWrapper::lockHolder):
* wtf/MainThreadData.h: Added.
(WTF::MainThreadData::MainThreadData):
(WTF::MainThreadData::operator->):
(WTF::MainThreadData::operator*):

Tools:

Added a very simple test that checks that DataMutex<T> indeed takes
the lock and indeed holds data.

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/Tests/WTF/DataMutex.cpp: Added.
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (247722 => 247723)


--- trunk/Source/WTF/ChangeLog	2019-07-23 14:47:37 UTC (rev 247722)
+++ trunk/Source/WTF/ChangeLog	2019-07-23 14:51:29 UTC (rev 247723)
@@ -1,3 +1,54 @@
+2019-07-23  Alicia Boya García  <[email protected]>
+
+        [WTF] Add DataMutex and MainThreadData wrappers
+        https://bugs.webkit.org/show_bug.cgi?id=199831
+
+        Reviewed by Alex Christensen.
+
+        This patch adds two new wrapper classes:
+
+        DataMutex<T> stores an instance of T in a private member along with a
+        mutex. In order to use its fields, users need to instantiate a
+        DataMutex<T>::LockedWrapper instance in the stack. This class uses
+        RAII to lock and unlock the mutex in construction and destruction
+        respectively, and by using the arrow operator lets the user access T's
+        members.
+
+        This way, DataMutex<T> prevents most instances of accidental access to
+        data fields that should only be read and modified in an atomic matter.
+
+        Still, both the Lock and the LockHolder are exposed once the user has
+        taken the lock so that special cases such as waiting for a condition
+        variable or performing an early unlock are doable.
+
+        MainThreadData<T> is another wrapper class, in this case for data
+        fields that should only be accessed from the main thread. In this
+        case, it works similar to a smart pointer, except that (1) there is no
+        actual memory indirection, T is stored directly inside
+        MainThreadData<T> and (2) attempts to use the -> or * operator have an
+        isMainThread() assertion.
+
+        Together, these two wrapper classes make it easier to write
+        multi-threaded code in a safer, more self-documented way by letting
+        the author group data into structures that have certain access safety
+        requirements.
+
+        These structures were originally part of the new GStreamer
+        WebKitMediaSrc rework patch: https://bugs.webkit.org/show_bug.cgi?id=199719
+
+        * wtf/CMakeLists.txt:
+        * wtf/DataMutex.h: Added.
+        (WTF::DataMutex::DataMutex):
+        (WTF::DataMutex::LockedWrapper::LockedWrapper):
+        (WTF::DataMutex::LockedWrapper::operator->):
+        (WTF::DataMutex::LockedWrapper::operator*):
+        (WTF::DataMutex::LockedWrapper::mutex):
+        (WTF::DataMutex::LockedWrapper::lockHolder):
+        * wtf/MainThreadData.h: Added.
+        (WTF::MainThreadData::MainThreadData):
+        (WTF::MainThreadData::operator->):
+        (WTF::MainThreadData::operator*):
+
 2019-07-22  Yusuke Suzuki  <[email protected]>
 
         [bmalloc] Each IsoPage gets 1MB VA because VMHeap::tryAllocateLargeChunk rounds up

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (247722 => 247723)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2019-07-23 14:47:37 UTC (rev 247722)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2019-07-23 14:51:29 UTC (rev 247723)
@@ -44,6 +44,7 @@
     CryptographicUtilities.h
     CryptographicallyRandomNumber.h
     DataLog.h
+    DataMutex.h
     DateMath.h
     DebugUtilities.h
     DeferrableRefCounted.h
@@ -122,6 +123,7 @@
     MD5.h
     MachSendRight.h
     MainThread.h
+    MainThreadData.h
     MallocPtr.h
     Markable.h
     MathExtras.h

Added: trunk/Source/WTF/wtf/DataMutex.h (0 => 247723)


--- trunk/Source/WTF/wtf/DataMutex.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/DataMutex.h	2019-07-23 14:51:29 UTC (rev 247723)
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2019 Igalia, S.L.
+ * Copyright (C) 2019 Metrological Group B.V.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * aint with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <wtf/Lock.h>
+
+namespace WTF {
+
+template<typename T>
+class DataMutex {
+    WTF_MAKE_NONCOPYABLE(DataMutex);
+public:
+    template<typename ...Args>
+    explicit DataMutex(Args&&... args)
+        : m_data(std::forward<Args>(args)...)
+    { }
+
+    class LockedWrapper {
+    public:
+        explicit LockedWrapper(DataMutex& dataMutex)
+            : m_mutex(dataMutex.m_mutex)
+            , m_lockHolder(dataMutex.m_mutex)
+            , m_data(dataMutex.m_data)
+        { }
+
+        T* operator->()
+        {
+            return &m_data;
+        }
+
+        T& operator*()
+        {
+            return m_data;
+        }
+
+        Lock& mutex()
+        {
+            return m_mutex;
+        }
+
+        LockHolder& lockHolder()
+        {
+            return m_lockHolder;
+        }
+
+    private:
+        Lock& m_mutex;
+        LockHolder m_lockHolder;
+        T& m_data;
+    };
+
+private:
+    Lock m_mutex;
+    T m_data;
+};
+
+} // namespace WTF

Added: trunk/Source/WTF/wtf/MainThreadData.h (0 => 247723)


--- trunk/Source/WTF/wtf/MainThreadData.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/MainThreadData.h	2019-07-23 14:51:29 UTC (rev 247723)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2019 Igalia, S.L.
+ * Copyright (C) 2019 Metrological Group B.V.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * aint with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <wtf/RunLoop.h>
+#include <wtf/StdLibExtras.h>
+
+namespace WTF {
+
+template<typename T>
+class MainThreadData {
+public:
+    MainThreadData() { }
+    explicit MainThreadData(T&& data)
+        : m_data(WTFMove(data))
+    { }
+
+    T* operator->()
+    {
+        ASSERT(isMainThread());
+        return &m_data;
+    }
+
+    T& operator*()
+    {
+        ASSERT(isMainThread());
+        return m_data;
+    }
+
+private:
+    T m_data;
+};
+
+} // namespace WTF

Modified: trunk/Tools/ChangeLog (247722 => 247723)


--- trunk/Tools/ChangeLog	2019-07-23 14:47:37 UTC (rev 247722)
+++ trunk/Tools/ChangeLog	2019-07-23 14:51:29 UTC (rev 247723)
@@ -1,3 +1,17 @@
+2019-07-23  Alicia Boya García  <[email protected]>
+
+        [WTF] Add DataMutex and MainThreadData wrappers
+        https://bugs.webkit.org/show_bug.cgi?id=199831
+
+        Reviewed by Alex Christensen.
+
+        Added a very simple test that checks that DataMutex<T> indeed takes
+        the lock and indeed holds data.
+
+        * TestWebKitAPI/CMakeLists.txt:
+        * TestWebKitAPI/Tests/WTF/DataMutex.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2019-07-23  Wenson Hsieh  <[email protected]>
 
         [macOS 10.15] Web process crashes when attempting to show the font panel via Font > Show Fonts

Modified: trunk/Tools/TestWebKitAPI/CMakeLists.txt (247722 => 247723)


--- trunk/Tools/TestWebKitAPI/CMakeLists.txt	2019-07-23 14:47:37 UTC (rev 247722)
+++ trunk/Tools/TestWebKitAPI/CMakeLists.txt	2019-07-23 14:51:29 UTC (rev 247723)
@@ -33,6 +33,7 @@
     Tests/WTF/Condition.cpp
     Tests/WTF/CrossThreadCopier.cpp
     Tests/WTF/CrossThreadTask.cpp
+    Tests/WTF/DataMutex.cpp
     Tests/WTF/DateMath.cpp
     Tests/WTF/Deque.cpp
     Tests/WTF/EnumTraits.cpp

Added: trunk/Tools/TestWebKitAPI/Tests/WTF/DataMutex.cpp (0 => 247723)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/DataMutex.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/DataMutex.cpp	2019-07-23 14:51:29 UTC (rev 247723)
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2019 Igalia, S.L.
+ * Copyright (C) 2019 Metrological Group B.V.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/DataMutex.h>
+
+using namespace WTF;
+
+namespace TestWebKitAPI {
+
+struct MyStructure {
+    int number;
+};
+DataMutex<MyStructure> myDataMutex;
+
+TEST(WTF_DataMutex, TakingTheMutex)
+{
+    Lock* mutex;
+    {
+        DataMutex<MyStructure>::LockedWrapper wrapper(myDataMutex);
+        mutex = &wrapper.mutex();
+        ASSERT_TRUE(mutex->isHeld());
+        wrapper->number = 5;
+    }
+    ASSERT_FALSE(mutex->isHeld());
+
+    {
+        DataMutex<MyStructure>::LockedWrapper wrapper(myDataMutex);
+        EXPECT_EQ(wrapper->number, 5);
+    }
+}
+
+} // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to