Make gBn/sConstructorMap concurrent.

Test: builds
Test: hidl_test

Bug: 35041785
Change-Id: I60099ddd56fee1faec7e54245d0fa3ccae6e777a
diff --git a/transport/HidlPassthroughSupport.cpp b/transport/HidlPassthroughSupport.cpp
index 9d4e2b4..2e1671d 100644
--- a/transport/HidlPassthroughSupport.cpp
+++ b/transport/HidlPassthroughSupport.cpp
@@ -33,11 +33,11 @@
         // interfaceChain fails
         return nullptr;
     }
-    auto iter = gBsConstructorMap.find(myDescriptor);
-    if (iter == gBsConstructorMap.end()) {
+    auto func = gBsConstructorMap.get(myDescriptor, nullptr);
+    if (!func) {
         return nullptr;
     }
-    return (iter->second)(reinterpret_cast<void *>(iface.get()));
+    return func(reinterpret_cast<void *>(iface.get()));
 }
 
 
diff --git a/transport/Static.cpp b/transport/Static.cpp
index 4a140e5..d5b335e 100644
--- a/transport/Static.cpp
+++ b/transport/Static.cpp
@@ -25,10 +25,10 @@
 Mutex gDefaultServiceManagerLock;
 sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
 
-std::map<std::string, std::function<sp<IBinder>(void *)>>
+ConcurrentMap<std::string, std::function<sp<IBinder>(void *)>>
         gBnConstructorMap{};
 
-std::map<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
+ConcurrentMap<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
         gBsConstructorMap;
 
 }   // namespace hardware
diff --git a/transport/include/hidl/ConcurrentMap.h b/transport/include/hidl/ConcurrentMap.h
new file mode 100644
index 0000000..18881f1
--- /dev/null
+++ b/transport/include/hidl/ConcurrentMap.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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 ANDROID_HIDL_CONCURRENT_MAP_H
+#define ANDROID_HIDL_CONCURRENT_MAP_H
+
+#include <mutex>
+#include <map>
+
+namespace android {
+namespace hardware {
+
+template<typename K, typename V>
+class ConcurrentMap {
+private:
+    using size_type = typename std::map<K, V>::size_type;
+    using iterator = typename std::map<K, V>::iterator;
+    using const_iterator = typename std::map<K, V>::const_iterator;
+
+public:
+    void set(K &&k, V &&v) {
+        std::unique_lock<std::mutex> _lock(mMutex);
+        mMap[std::forward<K>(k)] = std::forward<V>(v);
+    }
+
+    // get with the given default value.
+    const V &get(const K &k, const V &def) const {
+        std::unique_lock<std::mutex> _lock(mMutex);
+        const_iterator iter = mMap.find(k);
+        if (iter == mMap.end()) {
+            return def;
+        }
+        return iter->second;
+    }
+
+    size_type erase(const K &k) {
+        std::unique_lock<std::mutex> _lock(mMutex);
+        return mMap.erase(k);
+    }
+
+private:
+    mutable std::mutex mMutex;
+    std::map<K, V> mMap;
+};
+
+}  // namespace hardware
+}  // namespace android
+
+
+#endif  // ANDROID_HIDL_CONCURRENT_MAP_H
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index fb0b1f0..2fff6cb 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -328,11 +328,11 @@
             // interfaceChain fails
             return nullptr;
         }
-        auto iter = gBnConstructorMap.find(myDescriptor);
-        if (iter == gBnConstructorMap.end()) {
+        auto func = gBnConstructorMap.get(myDescriptor, nullptr);
+        if (!func) {
             return nullptr;
         }
-        return sp<IBinder>((iter->second)(reinterpret_cast<void *>(ifacePtr)));
+        return sp<IBinder>(func(reinterpret_cast<void *>(ifacePtr)));
     }
 }
 
diff --git a/transport/include/hidl/Static.h b/transport/include/hidl/Static.h
index 1f6fa9f..d62d675 100644
--- a/transport/include/hidl/Static.h
+++ b/transport/include/hidl/Static.h
@@ -19,6 +19,7 @@
 
 #include <android/hidl/base/1.0/IBase.h>
 #include <android/hidl/manager/1.0/IServiceManager.h>
+#include <hidl/ConcurrentMap.h>
 #include <hwbinder/IBinder.h>
 #include <utils/threads.h>
 
@@ -32,14 +33,14 @@
 // For HidlBinderSupport
 // value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
 // returns sp<IBinder>
-extern std::map<std::string, std::function<sp<IBinder>(void *)>>
-        gBnConstructorMap;
+extern ConcurrentMap<std::string,
+        std::function<sp<IBinder>(void *)>> gBnConstructorMap;
 
 // For HidlPassthroughSupport
 // value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
 // returns sp<IBase>
-extern std::map<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
-        gBsConstructorMap;
+extern ConcurrentMap<std::string,
+        std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>> gBsConstructorMap;
 
 }   // namespace hardware
 }   // namespace android