1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SAMGR_CONCURRENT_MAP_H 17 #define OHOS_SAMGR_CONCURRENT_MAP_H 18 19 #include <map> 20 #include <functional> 21 22 #include "samgr_ffrt_api.h" 23 24 namespace OHOS { 25 template <typename K, typename V> class ConcurrentMap { 26 public: ConcurrentMap()27 ConcurrentMap() {} 28 ~ConcurrentMap()29 ~ConcurrentMap() {} 30 ConcurrentMap(const ConcurrentMap & rhs)31 ConcurrentMap(const ConcurrentMap &rhs) 32 { 33 map_ = rhs.map_; 34 } 35 36 ConcurrentMap &operator = (const ConcurrentMap &rhs) 37 { 38 if (&rhs != this) { 39 map_ = rhs.map_; 40 } 41 42 return *this; 43 } 44 45 // when multithread calling size() return a tmp status, some threads may insert just after size() call Size()46 int Size() 47 { 48 std::lock_guard<samgr::mutex> lock(mutex_); 49 return map_.size(); 50 } 51 52 // when multithread calling Empty() return a tmp status, some threads may insert just after Empty() call IsEmpty()53 bool IsEmpty() 54 { 55 std::lock_guard<samgr::mutex> lock(mutex_); 56 return map_.empty(); 57 } 58 Insert(const K & key,const V & value)59 bool Insert(const K &key, const V &value) 60 { 61 std::lock_guard<samgr::mutex> lock(mutex_); 62 auto ret = map_.insert(std::pair<K, V>(key, value)); 63 return ret.second; 64 } 65 FirstInsert(const K & key,const V & value)66 bool FirstInsert(const K &key, const V &value) 67 { 68 std::lock_guard<samgr::mutex> lock(mutex_); 69 auto isFirst = map_.empty(); 70 auto ret = map_.insert(std::pair<K, V>(key, value)); 71 // find key and cannot insert 72 if (!ret.second) { 73 map_.erase(ret.first); 74 map_.insert(std::pair<K, V>(key, value)); 75 } 76 return isFirst; 77 } 78 EnsureInsert(const K & key,const V & value)79 void EnsureInsert(const K &key, const V &value) 80 { 81 std::lock_guard<samgr::mutex> lock(mutex_); 82 auto ret = map_.insert(std::pair<K, V>(key, value)); 83 // find key and cannot insert 84 if (!ret.second) { 85 map_.erase(ret.first); 86 map_.insert(std::pair<K, V>(key, value)); 87 return; 88 } 89 return; 90 } 91 Find(const K & key,V & value)92 bool Find(const K &key, V &value) 93 { 94 bool ret = false; 95 std::lock_guard<samgr::mutex> lock(mutex_); 96 97 auto iter = map_.find(key); 98 if (iter != map_.end()) { 99 value = iter->second; 100 ret = true; 101 } 102 103 return ret; 104 } 105 Erase(const K & key)106 void Erase(const K &key) 107 { 108 std::lock_guard<samgr::mutex> lock(mutex_); 109 map_.erase(key); 110 } 111 Clear()112 void Clear() 113 { 114 std::lock_guard<samgr::mutex> lock(mutex_); 115 map_.clear(); 116 return; 117 } 118 Clear(std::function<void (K)> func)119 void Clear(std::function<void(K)> func) 120 { 121 std::lock_guard<samgr::mutex> lock(mutex_); 122 for (auto iter = map_.begin(); iter != map_.end(); iter++) { 123 func(iter->first); 124 } 125 map_.clear(); 126 return; 127 } 128 129 private: 130 samgr::mutex mutex_; 131 std::map<K, V> map_; 132 }; 133 } // namespace OHOS 134 #endif