]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/include/lldb/Core/ThreadSafeSTLMap.h
Unbreak DRM KMS build by adding the needed compatibility field in the LinuxKPI.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / include / lldb / Core / ThreadSafeSTLMap.h
1 //===-- ThreadSafeSTLMap.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 liblldb_ThreadSafeSTLMap_h_
10 #define liblldb_ThreadSafeSTLMap_h_
11
12 #include <map>
13 #include <mutex>
14
15 #include "lldb/lldb-defines.h"
16
17 namespace lldb_private {
18
19 template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
20 public:
21   typedef std::map<_Key, _Tp> collection;
22   typedef typename collection::iterator iterator;
23   typedef typename collection::const_iterator const_iterator;
24   // Constructors and Destructors
25   ThreadSafeSTLMap() : m_collection(), m_mutex() {}
26
27   ~ThreadSafeSTLMap() {}
28
29   bool IsEmpty() const {
30     std::lock_guard<std::recursive_mutex> guard(m_mutex);
31     return m_collection.empty();
32   }
33
34   void Clear() {
35     std::lock_guard<std::recursive_mutex> guard(m_mutex);
36     return m_collection.clear();
37   }
38
39   size_t Erase(const _Key &key) {
40     std::lock_guard<std::recursive_mutex> guard(m_mutex);
41     return EraseNoLock(key);
42   }
43
44   size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }
45
46   bool GetValueForKey(const _Key &key, _Tp &value) const {
47     std::lock_guard<std::recursive_mutex> guard(m_mutex);
48     return GetValueForKeyNoLock(key, value);
49   }
50
51   // Call this if you have already manually locked the mutex using the
52   // GetMutex() accessor
53   bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
54     const_iterator pos = m_collection.find(key);
55     if (pos != m_collection.end()) {
56       value = pos->second;
57       return true;
58     }
59     return false;
60   }
61
62   bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
63     std::lock_guard<std::recursive_mutex> guard(m_mutex);
64     return GetFirstKeyForValueNoLock(value, key);
65   }
66
67   bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
68     const_iterator pos, end = m_collection.end();
69     for (pos = m_collection.begin(); pos != end; ++pos) {
70       if (pos->second == value) {
71         key = pos->first;
72         return true;
73       }
74     }
75     return false;
76   }
77
78   bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
79                   bool decrement_if_not_equal) const {
80     std::lock_guard<std::recursive_mutex> guard(m_mutex);
81     return LowerBoundNoLock(key, match_key, match_value,
82                             decrement_if_not_equal);
83   }
84
85   bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
86                         bool decrement_if_not_equal) const {
87     const_iterator pos = m_collection.lower_bound(key);
88     if (pos != m_collection.end()) {
89       match_key = pos->first;
90       if (decrement_if_not_equal && key != match_key &&
91           pos != m_collection.begin()) {
92         --pos;
93         match_key = pos->first;
94       }
95       match_value = pos->second;
96       return true;
97     }
98     return false;
99   }
100
101   iterator lower_bound_unsafe(const _Key &key) {
102     return m_collection.lower_bound(key);
103   }
104
105   void SetValueForKey(const _Key &key, const _Tp &value) {
106     std::lock_guard<std::recursive_mutex> guard(m_mutex);
107     SetValueForKeyNoLock(key, value);
108   }
109
110   // Call this if you have already manually locked the mutex using the
111   // GetMutex() accessor
112   void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
113     m_collection[key] = value;
114   }
115
116   std::recursive_mutex &GetMutex() { return m_mutex; }
117
118 private:
119   collection m_collection;
120   mutable std::recursive_mutex m_mutex;
121
122   // For ThreadSafeSTLMap only
123   DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
124 };
125
126 } // namespace lldb_private
127
128 #endif // liblldb_ThreadSafeSTLMap_h_