]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeDenseMap.h
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ThreadSafeDenseMap.h
1 //===-- ThreadSafeDenseMap.h ------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef liblldb_ThreadSafeDenseMap_h_
12 #define liblldb_ThreadSafeDenseMap_h_
13
14 // C Includes
15 // C++ Includes
16 #include <mutex>
17
18 // Other libraries and framework includes
19 #include "llvm/ADT/DenseMap.h"
20
21 // Project includes
22
23 namespace lldb_private {
24
25 template <typename _KeyType, typename _ValueType,
26           typename _MutexType = std::mutex>
27 class ThreadSafeDenseMap {
28 public:
29   typedef llvm::DenseMap<_KeyType, _ValueType> LLVMMapType;
30
31   ThreadSafeDenseMap(unsigned map_initial_capacity = 0)
32       : m_map(map_initial_capacity), m_mutex() {}
33
34   void Insert(_KeyType k, _ValueType v) {
35     std::lock_guard<_MutexType> guard(m_mutex);
36     m_map.insert(std::make_pair(k, v));
37   }
38
39   void Erase(_KeyType k) {
40     std::lock_guard<_MutexType> guard(m_mutex);
41     m_map.erase(k);
42   }
43
44   _ValueType Lookup(_KeyType k) {
45     std::lock_guard<_MutexType> guard(m_mutex);
46     return m_map.lookup(k);
47   }
48
49   bool Lookup(_KeyType k, _ValueType &v) {
50     std::lock_guard<_MutexType> guard(m_mutex);
51     auto iter = m_map.find(k), end = m_map.end();
52     if (iter == end)
53       return false;
54     v = iter->second;
55     return true;
56   }
57
58   void Clear() {
59     std::lock_guard<_MutexType> guard(m_mutex);
60     m_map.clear();
61   }
62
63 protected:
64   LLVMMapType m_map;
65   _MutexType m_mutex;
66 };
67
68 } // namespace lldb_private
69
70 #endif // liblldb_ThreadSafeSTLMap_h_