Bug 1194912 (Part 1) - Add CopyOnWrite<T> to support automatic copy-on-write for recursive writes to data structures. r=tn
authorSeth Fowler <mark.seth.fowler@gmail.com>
Tue, 25 Aug 2015 16:26:39 -0700
changeset 259342 c61c1035879cad1758b2e005bbdf646bccf5b58c
parent 259341 91e5acc46643ee1b32df12bdcd18ccca7c916087
child 259343 4797d5683bf366faca78d415e7c7d1af83e3c0cc
push id29277
push userryanvm@gmail.com
push dateWed, 26 Aug 2015 18:32:23 +0000
treeherdermozilla-central@fea87cbeaa6b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstn
bugs1194912
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1194912 (Part 1) - Add CopyOnWrite<T> to support automatic copy-on-write for recursive writes to data structures. r=tn
image/CopyOnWrite.h
image/test/gtest/TestCopyOnWrite.cpp
image/test/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/image/CopyOnWrite.h
@@ -0,0 +1,250 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * CopyOnWrite<T> allows code to safely read from a data structure without
+ * worrying that reentrant code will modify it.
+ */
+
+#ifndef mozilla_image_CopyOnWrite_h
+#define mozilla_image_CopyOnWrite_h
+
+#include "mozilla/nsRefPtr.h"
+#include "MainThreadUtils.h"
+#include "nsISupportsImpl.h"
+
+namespace mozilla {
+namespace image {
+
+///////////////////////////////////////////////////////////////////////////////
+// Implementation Details
+///////////////////////////////////////////////////////////////////////////////
+
+namespace detail {
+
+template <typename T>
+class CopyOnWriteValue final
+{
+public:
+  NS_INLINE_DECL_REFCOUNTING(CopyOnWriteValue)
+
+  explicit CopyOnWriteValue(T* aValue) : mValue(aValue) { }
+  explicit CopyOnWriteValue(already_AddRefed<T>& aValue) : mValue(aValue) { }
+  explicit CopyOnWriteValue(already_AddRefed<T>&& aValue) : mValue(aValue) { }
+  explicit CopyOnWriteValue(const nsRefPtr<T>& aValue) : mValue(aValue) { }
+  explicit CopyOnWriteValue(nsRefPtr<T>&& aValue) : mValue(aValue) { }
+
+  T* get() { return mValue.get(); }
+  const T* get() const { return mValue.get(); }
+
+  bool HasReaders() const { return mReaders > 0; }
+  bool HasWriter() const { return mWriter; }
+  bool HasUsers() const { return HasReaders() || HasWriter(); }
+
+  void LockForReading() { MOZ_ASSERT(!HasWriter()); mReaders++; }
+  void UnlockForReading() { MOZ_ASSERT(HasReaders()); mReaders--; }
+
+  struct MOZ_STACK_CLASS AutoReadLock
+  {
+    explicit AutoReadLock(CopyOnWriteValue* aValue)
+      : mValue(aValue)
+    {
+      mValue->LockForReading();
+    }
+    ~AutoReadLock() { mValue->UnlockForReading(); }
+    CopyOnWriteValue<T>* mValue;
+  };
+
+  void LockForWriting() { MOZ_ASSERT(!HasUsers()); mWriter = true; }
+  void UnlockForWriting() { MOZ_ASSERT(HasWriter()); mWriter = false; }
+
+  struct MOZ_STACK_CLASS AutoWriteLock
+  {
+    explicit AutoWriteLock(CopyOnWriteValue* aValue)
+      : mValue(aValue)
+    {
+      mValue->LockForWriting();
+    }
+    ~AutoWriteLock() { mValue->UnlockForWriting(); }
+    CopyOnWriteValue<T>* mValue;
+  };
+
+private:
+  CopyOnWriteValue(const CopyOnWriteValue&) = delete;
+  CopyOnWriteValue(CopyOnWriteValue&&) = delete;
+
+  ~CopyOnWriteValue() { }
+
+  nsRefPtr<T> mValue;
+  uint64_t mReaders = 0;
+  bool mWriter = false;
+};
+
+} // namespace detail
+
+
+///////////////////////////////////////////////////////////////////////////////
+// Public API
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+ * CopyOnWrite<T> allows code to safely read from a data structure without
+ * worrying that reentrant code will modify it. If reentrant code would modify
+ * the data structure while other code is reading from it, a copy is made so
+ * that readers can continue to use the old version.
+ *
+ * Note that it's legal to nest a writer inside any number of readers, but
+ * nothing can be nested inside a writer. This is because it's assumed that the
+ * state of the contained data structure may not be consistent during the write.
+ *
+ * This is a main-thread-only data structure.
+ *
+ * To work with CopyOnWrite<T>, a type T needs to be reference counted and to
+ * support copy construction.
+ */
+template <typename T>
+class CopyOnWrite final
+{
+  typedef detail::CopyOnWriteValue<T> CopyOnWriteValue;
+
+public:
+  explicit CopyOnWrite(T* aValue)
+  : mValue(new CopyOnWriteValue(aValue))
+  { }
+
+  explicit CopyOnWrite(already_AddRefed<T>& aValue)
+    : mValue(new CopyOnWriteValue(aValue))
+  { }
+
+  explicit CopyOnWrite(already_AddRefed<T>&& aValue)
+    : mValue(new CopyOnWriteValue(aValue))
+  { }
+
+  explicit CopyOnWrite(const nsRefPtr<T>& aValue)
+    : mValue(new CopyOnWriteValue(aValue))
+  { }
+
+  explicit CopyOnWrite(nsRefPtr<T>&& aValue)
+    : mValue(new CopyOnWriteValue(aValue))
+  { }
+
+  /// @return true if it's safe to read at this time.
+  bool CanRead() const { return !mValue->HasWriter(); }
+
+  /**
+   * Read from the contained data structure using the function @aReader.
+   * @aReader will be passed a pointer of type |const T*|. It's not legal to
+   * call this while a writer is active.
+   *
+   * @return whatever value @aReader returns, or nothing if @aReader is a void
+   *         function.
+   */
+  template <typename ReadFunc>
+  auto Read(ReadFunc aReader) const
+    -> decltype(aReader(static_cast<const T*>(nullptr)))
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+    MOZ_ASSERT(CanRead());
+
+    // Run the provided function while holding a read lock.
+    nsRefPtr<CopyOnWriteValue> cowValue = mValue;
+    typename CopyOnWriteValue::AutoReadLock lock(cowValue);
+    return aReader(cowValue->get());
+  }
+
+  /**
+   * Read from the contained data structure using the function @aReader.
+   * @aReader will be passed a pointer of type |const T*|. If it's currently not
+   * possible to read because a writer is currently active, @aOnError will be
+   * called instead.
+   *
+   * @return whatever value @aReader or @aOnError returns (their return types
+   *         must be consistent), or nothing if the provided functions are void.
+   */
+  template <typename ReadFunc, typename ErrorFunc>
+  auto Read(ReadFunc aReader, ErrorFunc aOnError) const
+    -> decltype(aReader(static_cast<const T*>(nullptr)))
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (!CanRead()) {
+      return aOnError();
+    }
+
+    return Read(aReader);
+  }
+
+  /// @return true if it's safe to write at this time.
+  bool CanWrite() const { return !mValue->HasWriter(); }
+
+  /**
+   * Write to the contained data structure using the function @aWriter.
+   * @aWriter will be passed a pointer of type |T*|. It's not legal to call this
+   * while another writer is active.
+   *
+   * If readers are currently active, they will be able to continue reading from
+   * a copy of the old version of the data structure. The copy will be destroyed
+   * when all its readers finish.  Later readers and writers will see the
+   * version of the data structure produced by the most recent call to Write().
+   *
+   * @return whatever value @aWriter returns, or nothing if @aWriter is a void
+   *         function.
+   */
+  template <typename WriteFunc>
+  auto Write(WriteFunc aWriter)
+    -> decltype(aWriter(static_cast<T*>(nullptr)))
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+    MOZ_ASSERT(CanWrite());
+
+    // If there are readers, we need to copy first.
+    if (mValue->HasReaders()) {
+      mValue = new CopyOnWriteValue(new T(*mValue->get()));
+    }
+
+    // Run the provided function while holding a write lock.
+    nsRefPtr<CopyOnWriteValue> cowValue = mValue;
+    typename CopyOnWriteValue::AutoWriteLock lock(cowValue);
+    return aWriter(cowValue->get());
+  }
+
+  /**
+   * Write to the contained data structure using the function @aWriter.
+   * @aWriter will be passed a pointer of type |T*|. If it's currently not
+   * possible to write because a writer is currently active, @aOnError will be
+   * called instead.
+   *
+   * If readers are currently active, they will be able to continue reading from
+   * a copy of the old version of the data structure. The copy will be destroyed
+   * when all its readers finish.  Later readers and writers will see the
+   * version of the data structure produced by the most recent call to Write().
+   *
+   * @return whatever value @aWriter or @aOnError returns (their return types
+   *         must be consistent), or nothing if the provided functions are void.
+   */
+  template <typename WriteFunc, typename ErrorFunc>
+  auto Write(WriteFunc aWriter, ErrorFunc aOnError)
+    -> decltype(aWriter(static_cast<T*>(nullptr)))
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (!CanWrite()) {
+      return aOnError();
+    }
+
+    return Write(aWriter);
+  }
+
+private:
+  CopyOnWrite(const CopyOnWrite&) = delete;
+  CopyOnWrite(CopyOnWrite&&) = delete;
+
+  nsRefPtr<CopyOnWriteValue> mValue;
+};
+
+} // namespace image
+} // namespace mozilla
+
+#endif // mozilla_image_CopyOnWrite_h
new file mode 100644
--- /dev/null
+++ b/image/test/gtest/TestCopyOnWrite.cpp
@@ -0,0 +1,235 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "CopyOnWrite.h"
+
+using namespace mozilla;
+using namespace mozilla::image;
+
+struct ValueStats
+{
+  int32_t mCopies = 0;
+  int32_t mFrees = 0;
+  int32_t mCalls = 0;
+  int32_t mConstCalls = 0;
+  int32_t mSerial = 0;
+};
+
+struct Value
+{
+  NS_INLINE_DECL_REFCOUNTING(Value)
+
+  explicit Value(ValueStats& aStats)
+    : mStats(aStats)
+    , mSerial(mStats.mSerial++)
+  { }
+
+  Value(const Value& aOther)
+    : mStats(aOther.mStats)
+    , mSerial(mStats.mSerial++)
+  {
+    mStats.mCopies++;
+  }
+
+  void Go() { mStats.mCalls++; }
+  void Go() const { mStats.mConstCalls++; }
+
+  int32_t Serial() const { return mSerial; }
+
+protected:
+  ~Value() { mStats.mFrees++; }
+
+private:
+  ValueStats& mStats;
+  int32_t mSerial;
+};
+
+TEST(ImageCopyOnWrite, Read)
+{
+  ValueStats stats;
+
+  {
+    CopyOnWrite<Value> cow(new Value(stats));
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_TRUE(cow.CanRead());
+
+    cow.Read([&](const Value* aValue) {
+      EXPECT_EQ(0, stats.mCopies);
+      EXPECT_EQ(0, stats.mFrees);
+      EXPECT_EQ(0, aValue->Serial());
+      EXPECT_TRUE(cow.CanRead());
+      EXPECT_TRUE(cow.CanWrite());
+
+      aValue->Go();
+
+      EXPECT_EQ(0, stats.mCalls);
+      EXPECT_EQ(1, stats.mConstCalls);
+    });
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_EQ(0, stats.mCalls);
+    EXPECT_EQ(1, stats.mConstCalls);
+  }
+
+  EXPECT_EQ(0, stats.mCopies);
+  EXPECT_EQ(1, stats.mFrees);
+}
+
+TEST(ImageCopyOnWrite, RecursiveRead)
+{
+  ValueStats stats;
+
+  {
+    CopyOnWrite<Value> cow(new Value(stats));
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_TRUE(cow.CanRead());
+
+    cow.Read([&](const Value* aValue) {
+      EXPECT_EQ(0, stats.mCopies);
+      EXPECT_EQ(0, stats.mFrees);
+      EXPECT_EQ(0, aValue->Serial());
+      EXPECT_TRUE(cow.CanRead());
+      EXPECT_TRUE(cow.CanWrite());
+
+      // Make sure that Read() inside a Read() succeeds.
+      cow.Read([&](const Value* aValue) {
+        EXPECT_EQ(0, stats.mCopies);
+        EXPECT_EQ(0, stats.mFrees);
+        EXPECT_EQ(0, aValue->Serial());
+        EXPECT_TRUE(cow.CanRead());
+        EXPECT_TRUE(cow.CanWrite());
+
+        aValue->Go();
+
+        EXPECT_EQ(0, stats.mCalls);
+        EXPECT_EQ(1, stats.mConstCalls);
+      }, []() {
+        // This gets called if we can't read. We shouldn't get here.
+        EXPECT_TRUE(false);
+      });
+    });
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_EQ(0, stats.mCalls);
+    EXPECT_EQ(1, stats.mConstCalls);
+  }
+
+  EXPECT_EQ(0, stats.mCopies);
+  EXPECT_EQ(1, stats.mFrees);
+}
+
+TEST(ImageCopyOnWrite, Write)
+{
+  ValueStats stats;
+
+  {
+    CopyOnWrite<Value> cow(new Value(stats));
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_TRUE(cow.CanRead());
+    EXPECT_TRUE(cow.CanWrite());
+
+    cow.Write([&](Value* aValue) {
+      EXPECT_EQ(0, stats.mCopies);
+      EXPECT_EQ(0, stats.mFrees);
+      EXPECT_EQ(0, aValue->Serial());
+      EXPECT_TRUE(!cow.CanRead());
+      EXPECT_TRUE(!cow.CanWrite());
+
+      aValue->Go();
+
+      EXPECT_EQ(1, stats.mCalls);
+      EXPECT_EQ(0, stats.mConstCalls);
+    });
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_EQ(1, stats.mCalls);
+    EXPECT_EQ(0, stats.mConstCalls);
+  }
+
+  EXPECT_EQ(0, stats.mCopies);
+  EXPECT_EQ(1, stats.mFrees);
+}
+
+TEST(ImageCopyOnWrite, WriteRecursive)
+{
+  ValueStats stats;
+
+  {
+    CopyOnWrite<Value> cow(new Value(stats));
+
+    EXPECT_EQ(0, stats.mCopies);
+    EXPECT_EQ(0, stats.mFrees);
+    EXPECT_TRUE(cow.CanRead());
+    EXPECT_TRUE(cow.CanWrite());
+
+    cow.Read([&](const Value* aValue) {
+      EXPECT_EQ(0, stats.mCopies);
+      EXPECT_EQ(0, stats.mFrees);
+      EXPECT_EQ(0, aValue->Serial());
+      EXPECT_TRUE(cow.CanRead());
+      EXPECT_TRUE(cow.CanWrite());
+
+      // Make sure Write() inside a Read() succeeds.
+      cow.Write([&](Value* aValue) {
+        EXPECT_EQ(1, stats.mCopies);
+        EXPECT_EQ(0, stats.mFrees);
+        EXPECT_EQ(1, aValue->Serial());
+        EXPECT_TRUE(!cow.CanRead());
+        EXPECT_TRUE(!cow.CanWrite());
+
+        aValue->Go();
+
+        EXPECT_EQ(1, stats.mCalls);
+        EXPECT_EQ(0, stats.mConstCalls);
+
+        // Make sure Read() inside a Write() fails.
+        cow.Read([](const Value* aValue) {
+          // This gets called if we can read. We shouldn't get here.
+          EXPECT_TRUE(false);
+        }, []() {
+          // This gets called if we can't read. We *should* get here.
+          EXPECT_TRUE(true);
+        });
+
+        // Make sure Write() inside a Write() fails.
+        cow.Write([](Value* aValue) {
+          // This gets called if we can write. We shouldn't get here.
+          EXPECT_TRUE(false);
+        }, []() {
+          // This gets called if we can't write. We *should* get here.
+          EXPECT_TRUE(true);
+        });
+      }, []() {
+        // This gets called if we can't write. We shouldn't get here.
+        EXPECT_TRUE(false);
+      });
+
+      aValue->Go();
+
+      EXPECT_EQ(1, stats.mCopies);
+      EXPECT_EQ(0, stats.mFrees);
+      EXPECT_EQ(1, stats.mCalls);
+      EXPECT_EQ(1, stats.mConstCalls);
+    });
+
+    EXPECT_EQ(1, stats.mCopies);
+    EXPECT_EQ(1, stats.mFrees);
+    EXPECT_EQ(1, stats.mCalls);
+    EXPECT_EQ(1, stats.mConstCalls);
+  }
+
+  EXPECT_EQ(1, stats.mCopies);
+  EXPECT_EQ(2, stats.mFrees);
+}
--- a/image/test/gtest/moz.build
+++ b/image/test/gtest/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 Library('imagetest')
 
 FAIL_ON_WARNINGS = True
 
 UNIFIED_SOURCES = [
     'Common.cpp',
+    'TestCopyOnWrite.cpp',
     'TestDecoders.cpp',
     'TestDecodeToSurface.cpp',
     'TestMetadata.cpp',
 ]
 
 TEST_HARNESS_FILES.gtest += [
     'corrupt.jpg',
     'first-frame-green.gif',