]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeSTLMap.h
Merge libc++ trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ThreadSafeSTLMap.h
1 //===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_ThreadSafeSTLMap_h_
11 #define liblldb_ThreadSafeSTLMap_h_
12
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <mutex>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-defines.h"
21
22 namespace lldb_private {
23
24 template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
25 public:
26   typedef std::map<_Key, _Tp> collection;
27   typedef typename collection::iterator iterator;
28   typedef typename collection::const_iterator const_iterator;
29   //------------------------------------------------------------------
30   // Constructors and Destructors
31   //------------------------------------------------------------------
32   ThreadSafeSTLMap() : m_collection(), m_mutex() {}
33
34   ~ThreadSafeSTLMap() {}
35
36   bool IsEmpty() const {
37     std::lock_guard<std::recursive_mutex> guard(m_mutex);
38     return m_collection.empty();
39   }
40
41   void Clear() {
42     std::lock_guard<std::recursive_mutex> guard(m_mutex);
43     return m_collection.clear();
44   }
45
46   size_t Erase(const _Key &key) {
47     std::lock_guard<std::recursive_mutex> guard(m_mutex);
48     return EraseNoLock(key);
49   }
50
51   size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }
52
53   bool GetValueForKey(const _Key &key, _Tp &value) const {
54     std::lock_guard<std::recursive_mutex> guard(m_mutex);
55     return GetValueForKeyNoLock(key, value);
56   }
57
58   // Call this if you have already manually locked the mutex using the
59   // GetMutex() accessor
60   bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
61     const_iterator pos = m_collection.find(key);
62     if (pos != m_collection.end()) {
63       value = pos->second;
64       return true;
65     }
66     return false;
67   }
68
69   bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
70     std::lock_guard<std::recursive_mutex> guard(m_mutex);
71     return GetFirstKeyForValueNoLock(value, key);
72   }
73
74   bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
75     const_iterator pos, end = m_collection.end();
76     for (pos = m_collection.begin(); pos != end; ++pos) {
77       if (pos->second == value) {
78         key = pos->first;
79         return true;
80       }
81     }
82     return false;
83   }
84
85   bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
86                   bool decrement_if_not_equal) const {
87     std::lock_guard<std::recursive_mutex> guard(m_mutex);
88     return LowerBoundNoLock(key, match_key, match_value,
89                             decrement_if_not_equal);
90   }
91
92   bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
93                         bool decrement_if_not_equal) const {
94     const_iterator pos = m_collection.lower_bound(key);
95     if (pos != m_collection.end()) {
96       match_key = pos->first;
97       if (decrement_if_not_equal && key != match_key &&
98           pos != m_collection.begin()) {
99         --pos;
100         match_key = pos->first;
101       }
102       match_value = pos->second;
103       return true;
104     }
105     return false;
106   }
107
108   iterator lower_bound_unsafe(const _Key &key) {
109     return m_collection.lower_bound(key);
110   }
111
112   void SetValueForKey(const _Key &key, const _Tp &value) {
113     std::lock_guard<std::recursive_mutex> guard(m_mutex);
114     SetValueForKeyNoLock(key, value);
115   }
116
117   // Call this if you have already manually locked the mutex using the
118   // GetMutex() accessor
119   void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
120     m_collection[key] = value;
121   }
122
123   std::recursive_mutex &GetMutex() { return m_mutex; }
124
125 private:
126   collection m_collection;
127   mutable std::recursive_mutex m_mutex;
128
129   //------------------------------------------------------------------
130   // For ThreadSafeSTLMap only
131   //------------------------------------------------------------------
132   DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
133 };
134
135 } // namespace lldb_private
136
137 #endif // liblldb_ThreadSafeSTLMap_h_