1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ANDROID_HIDL_CONCURRENT_MAP_H
17 #define ANDROID_HIDL_CONCURRENT_MAP_H
18 
19 #include <mutex>
20 #include <map>
21 
22 namespace android {
23 namespace hardware {
24 
25 template<typename K, typename V>
26 class ConcurrentMap {
27 private:
28     using size_type = typename std::map<K, V>::size_type;
29     using iterator = typename std::map<K, V>::iterator;
30     using const_iterator = typename std::map<K, V>::const_iterator;
31 
32 public:
set(K && k,V && v)33     void set(K &&k, V &&v) {
34         std::unique_lock<std::mutex> _lock(mMutex);
35         mMap[std::forward<K>(k)] = std::forward<V>(v);
36     }
37 
38     // get with the given default value.
get(const K & k,const V & def)39     const V &get(const K &k, const V &def) const {
40         std::unique_lock<std::mutex> _lock(mMutex);
41         const_iterator iter = mMap.find(k);
42         if (iter == mMap.end()) {
43             return def;
44         }
45         return iter->second;
46     }
47 
erase(const K & k)48     size_type erase(const K &k) {
49         std::unique_lock<std::mutex> _lock(mMutex);
50         return mMap.erase(k);
51     }
52 
eraseIfEqual(const K & k,const V & v)53     size_type eraseIfEqual(const K& k, const V& v) {
54         std::unique_lock<std::mutex> _lock(mMutex);
55         const_iterator iter = mMap.find(k);
56         if (iter == mMap.end()) {
57             return 0;
58         }
59         if (iter->second == v) {
60             mMap.erase(iter);
61             return 1;
62         } else {
63             return 0;
64         }
65     }
66 
lock()67     std::unique_lock<std::mutex> lock() { return std::unique_lock<std::mutex>(mMutex); }
68 
setLocked(const K & k,V && v)69     void setLocked(const K& k, V&& v) { mMap[k] = std::forward<V>(v); }
setLocked(const K & k,const V & v)70     void setLocked(const K& k, const V& v) { mMap[k] = v; }
71 
getLocked(const K & k,const V & def)72     const V& getLocked(const K& k, const V& def) const {
73         const_iterator iter = mMap.find(k);
74         if (iter == mMap.end()) {
75             return def;
76         }
77         return iter->second;
78     }
79 
eraseLocked(const K & k)80     size_type eraseLocked(const K& k) { return mMap.erase(k); }
81 
82     // the concurrent map must be locked in order to iterate over it
begin()83     iterator begin() { return mMap.begin(); }
end()84     iterator end() { return mMap.end(); }
begin()85     const_iterator begin() const { return mMap.begin(); }
end()86     const_iterator end() const { return mMap.end(); }
87 
88    private:
89     mutable std::mutex mMutex;
90     std::map<K, V> mMap;
91 };
92 
93 namespace details {
94 
95 // TODO(b/69122224): remove this type and usages of it
96 // DO NOT ADD USAGES
97 template <typename T>
98 class DoNotDestruct {
99   public:
DoNotDestruct()100     DoNotDestruct() { new (buffer) T(); }
get()101     T& get() { return *reinterpret_cast<T*>(buffer); }
102     T* operator->() { return reinterpret_cast<T*>(buffer); }
103 
104   private:
105     alignas(T) char buffer[sizeof(T)];
106 };
107 
108 }  // namespace details
109 
110 }  // namespace hardware
111 }  // namespace android
112 
113 
114 #endif  // ANDROID_HIDL_CONCURRENT_MAP_H
115