1 //===-- ThreadSafeDenseMap.h ------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ThreadSafeDenseMap_h_
11 #define liblldb_ThreadSafeDenseMap_h_
15 #include "llvm/ADT/DenseMap.h"
18 namespace lldb_private {
20 template <typename _KeyType, typename _ValueType,
21 typename _MutexType = std::mutex>
22 class ThreadSafeDenseMap {
24 typedef llvm::DenseMap<_KeyType, _ValueType> LLVMMapType;
26 ThreadSafeDenseMap(unsigned map_initial_capacity = 0)
27 : m_map(map_initial_capacity), m_mutex() {}
29 void Insert(_KeyType k, _ValueType v) {
30 std::lock_guard<_MutexType> guard(m_mutex);
31 m_map.insert(std::make_pair(k, v));
34 void Erase(_KeyType k) {
35 std::lock_guard<_MutexType> guard(m_mutex);
39 _ValueType Lookup(_KeyType k) {
40 std::lock_guard<_MutexType> guard(m_mutex);
41 return m_map.lookup(k);
44 bool Lookup(_KeyType k, _ValueType &v) {
45 std::lock_guard<_MutexType> guard(m_mutex);
46 auto iter = m_map.find(k), end = m_map.end();
54 std::lock_guard<_MutexType> guard(m_mutex);
63 } // namespace lldb_private
65 #endif // liblldb_ThreadSafeSTLMap_h_