1 | //===-- ThreadSafeDenseMap.h ------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLDB_UTILITY_THREADSAFEDENSEMAP_H |
10 | #define LLDB_UTILITY_THREADSAFEDENSEMAP_H |
11 | |
12 | #include <mutex> |
13 | |
14 | #include "llvm/ADT/DenseMap.h" |
15 | |
16 | namespace lldb_private { |
17 | |
18 | template <typename _KeyType, typename _ValueType> class ThreadSafeDenseMap { |
19 | public: |
20 | typedef llvm::DenseMap<_KeyType, _ValueType> LLVMMapType; |
21 | |
22 | ThreadSafeDenseMap(unsigned map_initial_capacity = 0) |
23 | : m_map(map_initial_capacity), m_mutex() {} |
24 | |
25 | void Insert(_KeyType k, _ValueType v) { |
26 | std::lock_guard<std::mutex> guard(m_mutex); |
27 | m_map.insert(std::make_pair(k, v)); |
28 | } |
29 | |
30 | void Erase(_KeyType k) { |
31 | std::lock_guard<std::mutex> guard(m_mutex); |
32 | m_map.erase(k); |
33 | } |
34 | |
35 | _ValueType Lookup(_KeyType k) { |
36 | std::lock_guard<std::mutex> guard(m_mutex); |
37 | return m_map.lookup(k); |
38 | } |
39 | |
40 | bool Lookup(_KeyType k, _ValueType &v) { |
41 | std::lock_guard<std::mutex> guard(m_mutex); |
42 | auto iter = m_map.find(k), end = m_map.end(); |
43 | if (iter == end) |
44 | return false; |
45 | v = iter->second; |
46 | return true; |
47 | } |
48 | |
49 | void Clear() { |
50 | std::lock_guard<std::mutex> guard(m_mutex); |
51 | m_map.clear(); |
52 | } |
53 | |
54 | protected: |
55 | LLVMMapType m_map; |
56 | std::mutex m_mutex; |
57 | }; |
58 | |
59 | } // namespace lldb_private |
60 | |
61 | #endif // LLDB_UTILITY_THREADSAFEDENSEMAP_H |
62 | |