Implement ConcurrentCache

This patch implements the template class ConcurrentCache which will
be used to implement all the thread-safe object caches in Dawn (e.g.
PipelineLayout, ComputePipeline and RenderPipeline).

BUG=dawn:529
TEST=dawn_unittests

Change-Id: Ia6f75191110a93285bdb23fdc1d275444d1ee866
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/67760
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn
index b4ae077..92da725 100644
--- a/src/common/BUILD.gn
+++ b/src/common/BUILD.gn
@@ -171,6 +171,7 @@
       "Assert.h",
       "BitSetIterator.h",
       "Compiler.h",
+      "ConcurrentCache.h",
       "Constants.h",
       "CoreFoundationRef.h",
       "DynamicLib.cpp",
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index fe90b29..d839d84 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -19,6 +19,7 @@
     "Assert.h"
     "BitSetIterator.h"
     "Compiler.h"
+    "ConcurrentCache.h"
     "Constants.h"
     "CoreFoundationRef.h"
     "DynamicLib.cpp"
diff --git a/src/common/ConcurrentCache.h b/src/common/ConcurrentCache.h
new file mode 100644
index 0000000..0e93ddd
--- /dev/null
+++ b/src/common/ConcurrentCache.h
@@ -0,0 +1,54 @@
+// Copyright 2021 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef COMMON_CONCURRENT_CACHE_H_
+#define COMMON_CONCURRENT_CACHE_H_
+
+#include "common/NonCopyable.h"
+
+#include <mutex>
+#include <unordered_set>
+#include <utility>
+
+template <typename T>
+class ConcurrentCache : public NonMovable {
+  public:
+    ConcurrentCache() = default;
+
+    T* Find(T* object) {
+        std::lock_guard<std::mutex> lock(mMutex);
+        auto iter = mCache.find(object);
+        if (iter == mCache.end()) {
+            return nullptr;
+        }
+        return *iter;
+    }
+
+    std::pair<T*, bool> Insert(T* object) {
+        std::lock_guard<std::mutex> lock(mMutex);
+        auto insertion = mCache.insert(object);
+        return std::make_pair(*(insertion.first), insertion.second);
+    }
+
+    size_t Erase(T* object) {
+        std::lock_guard<std::mutex> lock(mMutex);
+        return mCache.erase(object);
+    }
+
+  private:
+    std::mutex mMutex;
+    std::unordered_set<T*, typename T::HashFunc, typename T::EqualityFunc> mCache;
+};
+
+#endif
diff --git a/src/tests/BUILD.gn b/src/tests/BUILD.gn
index 77b4099..c7f4b7a 100644
--- a/src/tests/BUILD.gn
+++ b/src/tests/BUILD.gn
@@ -191,6 +191,7 @@
     "unittests/BuddyMemoryAllocatorTests.cpp",
     "unittests/ChainUtilsTests.cpp",
     "unittests/CommandAllocatorTests.cpp",
+    "unittests/ConcurrentCacheTests.cpp",
     "unittests/EnumClassBitmasksTests.cpp",
     "unittests/EnumMaskIteratorTests.cpp",
     "unittests/ErrorTests.cpp",
diff --git a/src/tests/unittests/ConcurrentCacheTests.cpp b/src/tests/unittests/ConcurrentCacheTests.cpp
new file mode 100644
index 0000000..079355a
--- /dev/null
+++ b/src/tests/unittests/ConcurrentCacheTests.cpp
@@ -0,0 +1,114 @@
+// Copyright 2021 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <gtest/gtest.h>
+
+#include "common/ConcurrentCache.h"
+#include "dawn_native/AsyncTask.h"
+#include "dawn_platform/DawnPlatform.h"
+#include "utils/SystemUtils.h"
+
+namespace {
+    class SimpleCachedObject {
+      public:
+        explicit SimpleCachedObject(size_t value) : mValue(value) {
+        }
+
+        size_t GetValue() const {
+            return mValue;
+        }
+
+        struct EqualityFunc {
+            bool operator()(const SimpleCachedObject* a, const SimpleCachedObject* b) const {
+                return a->mValue == b->mValue;
+            }
+        };
+
+        struct HashFunc {
+            size_t operator()(const SimpleCachedObject* obj) const {
+                return obj->mValue;
+            }
+        };
+
+      private:
+        size_t mValue;
+    };
+
+}  // anonymous namespace
+
+class ConcurrentCacheTest : public testing::Test {
+  public:
+    ConcurrentCacheTest() : mPool(mPlatform.CreateWorkerTaskPool()), mTaskManager(mPool.get()) {
+    }
+
+  protected:
+    dawn_platform::Platform mPlatform;
+    std::unique_ptr<dawn_platform::WorkerTaskPool> mPool;
+    dawn_native::AsyncTaskManager mTaskManager;
+    ConcurrentCache<SimpleCachedObject> mCache;
+};
+
+// Test inserting two objects that are equal to each other into the concurrent cache works as
+// expected.
+TEST_F(ConcurrentCacheTest, InsertAtSameTime) {
+    SimpleCachedObject cachedObject(1);
+    SimpleCachedObject anotherCachedObject(1);
+
+    std::pair<SimpleCachedObject*, bool> insertOutput = {};
+    std::pair<SimpleCachedObject*, bool> anotherInsertOutput = {};
+
+    ConcurrentCache<SimpleCachedObject>* cachePtr = &mCache;
+    dawn_native::AsyncTask asyncTask1([&insertOutput, cachePtr, &cachedObject] {
+        insertOutput = cachePtr->Insert(&cachedObject);
+    });
+    dawn_native::AsyncTask asyncTask2([&anotherInsertOutput, cachePtr, &anotherCachedObject] {
+        anotherInsertOutput = cachePtr->Insert(&anotherCachedObject);
+    });
+    mTaskManager.PostTask(std::move(asyncTask1));
+    mTaskManager.PostTask(std::move(asyncTask2));
+
+    mTaskManager.WaitAllPendingTasks();
+
+    ASSERT_TRUE(insertOutput.first == &cachedObject || insertOutput.first == &anotherCachedObject);
+    ASSERT_EQ(insertOutput.first, anotherInsertOutput.first);
+    ASSERT_EQ(insertOutput.second, !anotherInsertOutput.second);
+}
+
+// Testing erasing an object after inserting into the cache works as expected.
+TEST_F(ConcurrentCacheTest, EraseAfterInsertion) {
+    SimpleCachedObject cachedObject(1);
+
+    std::pair<SimpleCachedObject*, bool> insertOutput = {};
+    ConcurrentCache<SimpleCachedObject>* cachePtr = &mCache;
+    dawn_native::AsyncTask insertTask([&insertOutput, cachePtr, &cachedObject] {
+        insertOutput = cachePtr->Insert(&cachedObject);
+    });
+
+    size_t erasedObjectCount = 0;
+    dawn_native::AsyncTask eraseTask([&erasedObjectCount, cachePtr, &cachedObject] {
+        while (cachePtr->Find(&cachedObject) == nullptr) {
+            utils::USleep(100);
+        }
+        erasedObjectCount = cachePtr->Erase(&cachedObject);
+    });
+
+    mTaskManager.PostTask(std::move(insertTask));
+    mTaskManager.PostTask(std::move(eraseTask));
+
+    mTaskManager.WaitAllPendingTasks();
+
+    ASSERT_EQ(&cachedObject, insertOutput.first);
+    ASSERT_TRUE(insertOutput.second);
+    ASSERT_EQ(1u, erasedObjectCount);
+}