SkDiscardableMemoryPool: always have a mutex for testing
authorHal Canary <halcanary@google.com>
Tue, 25 Apr 2017 12:58:57 +0000 (08:58 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Fri, 5 May 2017 15:10:13 +0000 (15:10 +0000)
Change-Id: I898058f1125f62832f2b2b661c661efb0f84da6d
Reviewed-on: https://skia-review.googlesource.com/14274
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Herb Derby <herb@google.com>
src/lazy/SkDiscardableMemoryPool.cpp
src/lazy/SkDiscardableMemoryPool.h
tests/DiscardableMemoryPoolTest.cpp
tests/DiscardableMemoryTest.cpp
tests/ImageCacheTest.cpp
tests/SkResourceCacheTest.cpp

index aeba34bb0482fbf57f7b61b87ba952da3c5cec32..6e5582a494b1c5c76a7a8f7da89d219ceac35b12 100644 (file)
@@ -27,10 +27,7 @@ class PoolDiscardableMemory;
  */
 class DiscardableMemoryPool : public SkDiscardableMemoryPool {
 public:
-    /**
-     *  Without mutex, will be not be thread safe.
-     */
-    DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = nullptr);
+    DiscardableMemoryPool(size_t budget);
     ~DiscardableMemoryPool() override;
 
     std::unique_ptr<SkDiscardableMemory> make(size_t bytes);
@@ -56,7 +53,7 @@ public:
     #endif  // SK_LAZY_CACHE_STATS
 
 private:
-    SkBaseMutex* fMutex;
+    SkMutex      fMutex;
     size_t       fBudget;
     size_t       fUsed;
     SkTInternalLList<PoolDiscardableMemory> fList;
@@ -126,10 +123,8 @@ void PoolDiscardableMemory::unlock() {
 
 ////////////////////////////////////////////////////////////////////////////////
 
-DiscardableMemoryPool::DiscardableMemoryPool(size_t budget,
-                                             SkBaseMutex* mutex)
-    : fMutex(mutex)
-    , fBudget(budget)
+DiscardableMemoryPool::DiscardableMemoryPool(size_t budget)
+    : fBudget(budget)
     , fUsed(0) {
     #if SK_LAZY_CACHE_STATS
     fCacheHits = 0;
@@ -144,9 +139,7 @@ DiscardableMemoryPool::~DiscardableMemoryPool() {
 }
 
 void DiscardableMemoryPool::dumpDownTo(size_t budget) {
-    if (fMutex != nullptr) {
-        fMutex->assertHeld();
-    }
+    fMutex.assertHeld();
     if (fUsed <= budget) {
         return;
     }
@@ -236,14 +229,13 @@ void DiscardableMemoryPool::dumpPool() {
 
 }  // namespace
 
-sk_sp<SkDiscardableMemoryPool> SkDiscardableMemoryPool::Make(size_t size, SkBaseMutex* mutex) {
-    return sk_make_sp<DiscardableMemoryPool>(size, mutex);
+sk_sp<SkDiscardableMemoryPool> SkDiscardableMemoryPool::Make(size_t size) {
+    return sk_make_sp<DiscardableMemoryPool>(size);
 }
 
 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
-    static SkBaseMutex gMutex;
     // Intentionally leak this global pool.
     static SkDiscardableMemoryPool* global =
-            new DiscardableMemoryPool(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, &gMutex);
+            new DiscardableMemoryPool(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE);
     return global;
 }
index f7beaad5812579290677c72fe05b36bb3b97cf63..facdf3d27c98d0ace32442157b9a99fa28773fba 100644 (file)
@@ -50,9 +50,8 @@ public:
     /**
      *  This non-global pool can be used for unit tests to verify that
      *  the pool works.
-     *  Without mutex, will be not be thread safe.
      */
-    static sk_sp<SkDiscardableMemoryPool> Make(size_t size, SkBaseMutex* mutex = nullptr);
+    static sk_sp<SkDiscardableMemoryPool> Make(size_t size);
 };
 
 /**
index b8fd6d712a5305a204ff20ceea3a4ecfe4390cdf..c91e827eb3018eb554a07cbf316ed7c018635708 100644 (file)
@@ -9,8 +9,7 @@
 #include "Test.h"
 
 DEF_TEST(DiscardableMemoryPool, reporter) {
-    sk_sp<SkDiscardableMemoryPool> pool(
-        SkDiscardableMemoryPool::Make(1, nullptr));
+    sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(1));
     pool->setRAMBudget(3);
     REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
 
index e0125ec21bb3a9c800113bcd95baa8cad47c3cbc..818fbea2e30e6772763164e6b31e22ffd1c93b28 100644 (file)
@@ -53,7 +53,7 @@ DEF_TEST(DiscardableMemory_global, reporter) {
 
 DEF_TEST(DiscardableMemory_nonglobal, reporter) {
     sk_sp<SkDiscardableMemoryPool> pool(
-        SkDiscardableMemoryPool::Make(1024, /* mutex = */ nullptr));
+        SkDiscardableMemoryPool::Make(1024));
     std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
     test_dm(reporter, dm.get(), true);
 }
index 9472388225685ab3ec20e0ac965197ade79ecebc..457df911c1b9d32bb46c62dac25a6116573fe5a3 100644 (file)
@@ -121,7 +121,7 @@ DEF_TEST(ImageCache, reporter) {
         test_cache(reporter, cache, true);
     }
     {
-        sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit, nullptr));
+        sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit));
         gPool = pool.get();
         SkResourceCache cache(pool_factory);
         test_cache(reporter, cache, true);
index 7a9c8760e56167b892530056b3fa73750f5e5cfc..5f8770dda6aabe44a1a3d033ef60c2b482eab8e5 100644 (file)
@@ -127,7 +127,7 @@ DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
         testBitmapCache_discarded_bitmap(reporter, &cache, nullptr);
     }
     {
-        sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(byteLimit, nullptr));
+        sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(byteLimit));
         gPool = pool.get();
         SkResourceCache::DiscardableFactory factory = pool_factory;
         SkResourceCache cache(factory);